// computes the number of encoded bytes after packetizing // // _n : number of uncoded input bytes // _crc : error-detecting scheme // _fec0 : inner forward error-correction code // _fec1 : outer forward error-correction code unsigned int packetizer_compute_enc_msg_len(unsigned int _n, int _crc, int _fec0, int _fec1) { unsigned int k = _n + crc_get_length(_crc); unsigned int n0 = fec_get_enc_msg_length(_fec0, k); unsigned int n1 = fec_get_enc_msg_length(_fec1, n0); return n1; }
// decode a block of data using a fec scheme // _q : fec object // _dec_msg_len : decoded message length // _msg_enc : encoded message // _msg_dec : decoded message void fec_decode_soft(fec _q, unsigned int _dec_msg_len, unsigned char * _msg_enc, unsigned char * _msg_dec) { if (_q->decode_soft_func != NULL) { // call internal decoding method _q->decode_soft_func(_q, _dec_msg_len, _msg_enc, _msg_dec); } else { // pack bytes and use hard-decision decoding unsigned enc_msg_len = fec_get_enc_msg_length(_q->scheme, _dec_msg_len); unsigned char msg_enc_hard[enc_msg_len]; unsigned int i; for (i=0; i<enc_msg_len; i++) { // TODO : use pack bytes msg_enc_hard[i] = 0; msg_enc_hard[i] |= (_msg_enc[8*i+0] >> 0) & 0x80; msg_enc_hard[i] |= (_msg_enc[8*i+1] >> 1) & 0x40; msg_enc_hard[i] |= (_msg_enc[8*i+2] >> 2) & 0x20; msg_enc_hard[i] |= (_msg_enc[8*i+3] >> 3) & 0x10; msg_enc_hard[i] |= (_msg_enc[8*i+4] >> 4) & 0x08; msg_enc_hard[i] |= (_msg_enc[8*i+5] >> 5) & 0x04; msg_enc_hard[i] |= (_msg_enc[8*i+6] >> 6) & 0x02; msg_enc_hard[i] |= (_msg_enc[8*i+7] >> 7) & 0x01; } // use hard-decoding method fec_decode(_q, _dec_msg_len, msg_enc_hard, _msg_dec); } }
// encode block of data using SEC-DEC (39,32) encoder // // _q : encoder/decoder object // _dec_msg_len : decoded message length (number of bytes) // _msg_dec : decoded message [size: 1 x _dec_msg_len] // _msg_enc : encoded message [size: 1 x 2*_dec_msg_len] void fec_secded3932_encode(fec _q, unsigned int _dec_msg_len, unsigned char *_msg_dec, unsigned char *_msg_enc) { unsigned int i=0; // decoded byte counter unsigned int j=0; // encoded byte counter // determine remainder of input length / 4 unsigned int r = _dec_msg_len % 4; // for now simply encode as 4/5-rate codec (eat // last parity bit) // TODO : make more efficient for (i=0; i<_dec_msg_len-r; i+=4) { // compute parity (7 bits) on two input bytes (32 bits) _msg_enc[j+0] = fec_secded3932_compute_parity(&_msg_dec[i]); // copy remaining two input bytes (32 bits) _msg_enc[j+1] = _msg_dec[i+0]; _msg_enc[j+2] = _msg_dec[i+1]; _msg_enc[j+3] = _msg_dec[i+2]; _msg_enc[j+4] = _msg_dec[i+3]; // increment output counter j += 5; } // if input length isn't divisible by 4, encode last few bytes if (r) { // one 32-bit symbol (decoded) unsigned char m[4] = {0,0,0,0}; unsigned int n; for (n=0; n<r; n++) m[n] = _msg_dec[i+n]; // one 39-bit symbol (encoded) unsigned char v[5]; // encode fec_secded3932_encode_symbol(m, v); // there is no need to actually send all five bytes; // the last few bytes are zero and can be artificially // inserted at the decoder _msg_enc[j+0] = v[0]; for (n=0; n<r; n++) _msg_enc[j+n+1] = v[n+1]; i += r; j += r+1; } assert( j == fec_get_enc_msg_length(LIQUID_FEC_SECDED3932,_dec_msg_len) ); assert( i == _dec_msg_len); }
// Helper function to keep code base small void fec_test_codec(fec_scheme _fs, unsigned int _n, void * _opts) { #if !LIBFEC_ENABLED if ( _fs == LIQUID_FEC_CONV_V27 || _fs == LIQUID_FEC_CONV_V29 || _fs == LIQUID_FEC_CONV_V39 || _fs == LIQUID_FEC_CONV_V615 || _fs == LIQUID_FEC_CONV_V27P23 || _fs == LIQUID_FEC_CONV_V27P34 || _fs == LIQUID_FEC_CONV_V27P45 || _fs == LIQUID_FEC_CONV_V27P56 || _fs == LIQUID_FEC_CONV_V27P67 || _fs == LIQUID_FEC_CONV_V27P78 || _fs == LIQUID_FEC_CONV_V29P23 || _fs == LIQUID_FEC_CONV_V29P34 || _fs == LIQUID_FEC_CONV_V29P45 || _fs == LIQUID_FEC_CONV_V29P56 || _fs == LIQUID_FEC_CONV_V29P67 || _fs == LIQUID_FEC_CONV_V29P78 || _fs == LIQUID_FEC_RS_M8) { AUTOTEST_WARN("convolutional, Reed-Solomon codes unavailable (install libfec)\n"); return; } #endif // generate fec object fec q = fec_create(_fs,_opts); // create arrays unsigned int n_enc = fec_get_enc_msg_length(_fs,_n); unsigned char msg[_n]; // original message unsigned char msg_enc[n_enc]; // encoded message unsigned char msg_dec[_n]; // decoded message // initialze message unsigned int i; for (i=0; i<_n; i++) { msg[i] = rand() & 0xff; msg_dec[i] = 0; } // encode message fec_encode(q,_n,msg,msg_enc); // channel: add single error msg_enc[0] ^= 0x01; // decode message fec_decode(q,_n,msg_enc,msg_dec); // validate output CONTEND_SAME_DATA(msg,msg_dec,_n); // clean up objects fec_destroy(q); }
// // AUTOTEST: repeat/3 codec // void autotest_rep5_codec() { unsigned int n=4; unsigned char msg[] = {0x25, 0x62, 0x3F, 0x52}; fec_scheme fs = LIQUID_FEC_REP5; // create arrays unsigned int n_enc = fec_get_enc_msg_length(fs,n); unsigned char msg_dec[n]; unsigned char msg_enc[n_enc]; // create object fec q = fec_create(fs,NULL); if (liquid_autotest_verbose) fec_print(q); // encode message fec_encode(q, n, msg, msg_enc); // corrupt encoded message, but no so much that it // can't be decoded msg_enc[ 0] = ~msg_enc[ 0]; msg_enc[ 4] = ~msg_enc[ 4]; // msg_enc[ 8] = ~msg_enc[ 8]; // msg_enc[12] = ~msg_enc[12]; // msg_enc[16] = ~msg_enc[16]; msg_enc[ 1] = ~msg_enc[ 1]; // msg_enc[ 5] = ~msg_enc[ 5]; msg_enc[ 9] = ~msg_enc[ 9]; // msg_enc[13] = ~msg_enc[13]; // msg_enc[17] = ~msg_enc[17]; // msg_enc[ 2] = ~msg_enc[ 2]; // msg_enc[ 6] = ~msg_enc[ 6]; msg_enc[10] = ~msg_enc[10]; msg_enc[14] = ~msg_enc[14]; // msg_enc[18] = ~msg_enc[18]; msg_enc[ 3] = ~msg_enc[ 3]; // msg_enc[ 7] = ~msg_enc[ 7]; // msg_enc[11] = ~msg_enc[11]; // msg_enc[15] = ~msg_enc[15]; msg_enc[19] = ~msg_enc[19]; // decode message fec_decode(q, n, msg_enc, msg_dec); // validate data are the same CONTEND_SAME_DATA(msg, msg_dec, n); // clean up objects fec_destroy(q); }
// create packetizer object // // _n : number of uncoded intput bytes // _crc : error-detecting scheme // _fec0 : inner forward error-correction code // _fec1 : outer forward error-correction code packetizer packetizer_create(unsigned int _n, int _crc, int _fec0, int _fec1) { packetizer p = (packetizer) malloc(sizeof(struct packetizer_s)); p->msg_len = _n; p->packet_len = packetizer_compute_enc_msg_len(_n, _crc, _fec0, _fec1); p->check = _crc; p->crc_length = crc_get_length(p->check); // allocate memory for buffers (scale by 8 for soft decoding) p->buffer_len = p->packet_len; p->buffer_0 = (unsigned char*) malloc(8*p->buffer_len); p->buffer_1 = (unsigned char*) malloc(8*p->buffer_len); // create plan p->plan_len = 2; p->plan = (struct fecintlv_plan*) malloc((p->plan_len)*sizeof(struct fecintlv_plan)); // set schemes unsigned int i; unsigned int n0 = _n + p->crc_length; for (i=0; i<p->plan_len; i++) { // set schemes p->plan[i].fs = (i==0) ? _fec0 : _fec1; // compute lengths p->plan[i].dec_msg_len = n0; p->plan[i].enc_msg_len = fec_get_enc_msg_length(p->plan[i].fs, p->plan[i].dec_msg_len); // create objects p->plan[i].f = fec_create(p->plan[i].fs, NULL); p->plan[i].q = interleaver_create(p->plan[i].enc_msg_len); // set interleaver depth to zero if no error correction scheme // is applied to this plan if (p->plan[i].fs == LIQUID_FEC_NONE) interleaver_set_depth(p->plan[i].q, 0); // update length n0 = p->plan[i].enc_msg_len; } return p; }
// decode block of data using SEC-DEC (39,32) decoder // // _q : encoder/decoder object // _dec_msg_len : decoded message length (number of bytes) // _msg_enc : encoded message [size: 1 x 2*_dec_msg_len] // _msg_dec : decoded message [size: 1 x _dec_msg_len] // //unsigned int void fec_secded3932_decode(fec _q, unsigned int _dec_msg_len, unsigned char *_msg_enc, unsigned char *_msg_dec) { unsigned int i=0; // decoded byte counter unsigned int j=0; // encoded byte counter // determine remainder of input length / 4 unsigned int r = _dec_msg_len % 4; for (i=0; i<_dec_msg_len-r; i+=4) { // decode straight to output fec_secded3932_decode_symbol(&_msg_enc[j], &_msg_dec[i]); j += 5; } // if input length isn't divisible by 4, decode last several bytes if (r) { // one 39-bit symbol unsigned char v[5] = {_msg_enc[j+0], 0, 0, 0, 0}; unsigned int n; for (n=0; n<r; n++) v[n+1] = _msg_enc[j+n+1]; // one 32-bit symbol (decoded) unsigned char m_hat[4]; // decode symbol fec_secded3932_decode_symbol(v, m_hat); // copy non-zero bytes to output (ignore zeros artifically // inserted at receiver) for (n=0; n<r; n++) _msg_dec[i+n] = m_hat[n]; i += r; j += r+1; } assert( j == fec_get_enc_msg_length(LIQUID_FEC_SECDED3932,_dec_msg_len) ); assert( i == _dec_msg_len); //return num_errors; }
// decode block of data using SEC-DEC (72,64) decoder // // _q : encoder/decoder object // _dec_msg_len : decoded message length (number of bytes) // _msg_enc : encoded message [size: 1 x 2*_dec_msg_len] // _msg_dec : decoded message [size: 1 x _dec_msg_len] // //unsigned int void fec_secded7264_decode(fec _q, unsigned int _dec_msg_len, unsigned char *_msg_enc, unsigned char *_msg_dec) { unsigned int i=0; // decoded byte counter unsigned int j=0; // encoded byte counter // determine remainder of input length / 8 unsigned int r = _dec_msg_len % 8; for (i=0; i<_dec_msg_len-r; i+=8) { // decode nine input bytes fec_secded7264_decode_symbol(&_msg_enc[j], &_msg_dec[i]); j += 9; } // if input length isn't divisible by 8, decode last several bytes if (r) { unsigned char v[9] = {0,0,0,0,0,0,0,0,0}; // received message unsigned char c[8] = {0,0,0,0,0,0,0,0}; // decoded message unsigned int n; // output length is input + 1 (parity byte) for (n=0; n<r+1; n++) v[n] = _msg_enc[j+n]; // decode symbol fec_secded7264_decode_symbol(v,c); // store only relevant bytes for (n=0; n<r; n++) _msg_dec[i+n] = c[n]; i += r; j += r+1; } assert( j == fec_get_enc_msg_length(LIQUID_FEC_SECDED7264,_dec_msg_len) ); assert( i == _dec_msg_len); //return num_errors; }
// encode block of data using SEC-DEC (72,64) encoder // // _q : encoder/decoder object // _dec_msg_len : decoded message length (number of bytes) // _msg_dec : decoded message [size: 1 x _dec_msg_len] // _msg_enc : encoded message [size: 1 x 2*_dec_msg_len] void fec_secded7264_encode(fec _q, unsigned int _dec_msg_len, unsigned char *_msg_dec, unsigned char *_msg_enc) { unsigned int i=0; // decoded byte counter unsigned int j=0; // encoded byte counter unsigned char parity; // parity byte // determine remainder of input length / 8 unsigned int r = _dec_msg_len % 8; // TODO : devise more efficient way of doing this for (i=0; i<_dec_msg_len-r; i+=8) { // encode directly to output fec_secded7264_encode_symbol(&_msg_dec[i], &_msg_enc[j]); j += 9; } // if input length isn't divisible by 8, encode last few bytes if (r) { unsigned char v[8] = {0,0,0,0,0,0,0,0}; unsigned int n; for (n=0; n<r; n++) v[n] = _msg_dec[i+n]; // compute parity parity = fec_secded7264_compute_parity(v); // there is no need to actually send all the bytes; the // last 8-r bytes are zeros and can be added at the // decoder _msg_enc[j+0] = parity; for (n=0; n<r; n++) _msg_enc[j+n+1] = _msg_dec[i+n]; i += r; j += r+1; } assert( j == fec_get_enc_msg_length(LIQUID_FEC_SECDED7264,_dec_msg_len) ); assert( i == _dec_msg_len); }
// // AUTOTEST: Hamming (7,4) codec // void autotest_hamming74_codec() { unsigned int n=4; unsigned char msg[] = {0x25, 0x62, 0x3F, 0x52}; fec_scheme fs = LIQUID_FEC_HAMMING74; // create arrays unsigned int n_enc = fec_get_enc_msg_length(fs,n); unsigned char msg_dec[n]; unsigned char msg_enc[n_enc]; // create object fec q = fec_create(fs,NULL); if (liquid_autotest_verbose) fec_print(q); // encode message fec_encode(q, n, msg, msg_enc); // corrupt encoded message msg_enc[0] ^= 0x04; // position 5 #if 0 msg_enc[1] ^= 0x04; // msg_enc[2] ^= 0x02; // msg_enc[3] ^= 0x01; // msg_enc[4] ^= 0x80; // msg_enc[5] ^= 0x40; // msg_enc[6] ^= 0x20; // #endif // decode message fec_decode(q, n, msg_enc, msg_dec); // validate data are the same CONTEND_SAME_DATA(msg, msg_dec, n); // clean up objects fec_destroy(q); }
// Helper function to keep code base small void fec_encode_bench( struct rusage *_start, struct rusage *_finish, unsigned long int *_num_iterations, fec_scheme _fs, unsigned int _n, void * _opts) { #if !LIBFEC_ENABLED if ( _fs == LIQUID_FEC_CONV_V27 || _fs == LIQUID_FEC_CONV_V29 || _fs == LIQUID_FEC_CONV_V39 || _fs == LIQUID_FEC_CONV_V615 || _fs == LIQUID_FEC_CONV_V27P23 || _fs == LIQUID_FEC_CONV_V27P34 || _fs == LIQUID_FEC_CONV_V27P45 || _fs == LIQUID_FEC_CONV_V27P56 || _fs == LIQUID_FEC_CONV_V27P67 || _fs == LIQUID_FEC_CONV_V27P78 || _fs == LIQUID_FEC_CONV_V29P23 || _fs == LIQUID_FEC_CONV_V29P34 || _fs == LIQUID_FEC_CONV_V29P45 || _fs == LIQUID_FEC_CONV_V29P56 || _fs == LIQUID_FEC_CONV_V29P67 || _fs == LIQUID_FEC_CONV_V29P78 || _fs == LIQUID_FEC_RS_M8) { fprintf(stderr,"warning: convolutional, Reed-Solomon codes unavailable (install libfec)\n"); getrusage(RUSAGE_SELF, _start); memmove((void*)_finish,(void*)_start,sizeof(struct rusage)); return; } #endif // normalize number of iterations *_num_iterations /= _n; switch (_fs) { case LIQUID_FEC_NONE: *_num_iterations *= 500; break; case LIQUID_FEC_REP3: *_num_iterations *= 200; break; case LIQUID_FEC_REP5: *_num_iterations *= 100; break; case LIQUID_FEC_HAMMING74: *_num_iterations *= 30; break; case LIQUID_FEC_HAMMING84: *_num_iterations *= 100; break; case LIQUID_FEC_HAMMING128: *_num_iterations *= 100; break; case LIQUID_FEC_SECDED2216: *_num_iterations *= 10; break; case LIQUID_FEC_SECDED3932: *_num_iterations *= 10; break; case LIQUID_FEC_SECDED7264: *_num_iterations *= 10; break; case LIQUID_FEC_GOLAY2412: *_num_iterations *= 2; break; case LIQUID_FEC_CONV_V27: case LIQUID_FEC_CONV_V29: case LIQUID_FEC_CONV_V39: case LIQUID_FEC_CONV_V615: case LIQUID_FEC_CONV_V27P23: case LIQUID_FEC_CONV_V27P34: case LIQUID_FEC_CONV_V27P45: case LIQUID_FEC_CONV_V27P56: case LIQUID_FEC_CONV_V27P67: case LIQUID_FEC_CONV_V27P78: case LIQUID_FEC_CONV_V29P23: case LIQUID_FEC_CONV_V29P34: case LIQUID_FEC_CONV_V29P45: case LIQUID_FEC_CONV_V29P56: case LIQUID_FEC_CONV_V29P67: case LIQUID_FEC_CONV_V29P78: case LIQUID_FEC_RS_M8: *_num_iterations *= 1; break; default:; } if (*_num_iterations < 1) *_num_iterations = 1; // generate fec object fec q = fec_create(_fs,_opts); // create arrays unsigned int n_enc = fec_get_enc_msg_length(_fs,_n); unsigned char msg[_n]; // original message unsigned char msg_enc[n_enc]; // encoded message // initialze message unsigned long int i; for (i=0; i<_n; i++) { msg[i] = rand() & 0xff; } // start trials getrusage(RUSAGE_SELF, _start); for (i=0; i<(*_num_iterations); i++) { fec_encode(q,_n,msg,msg_enc); fec_encode(q,_n,msg,msg_enc); fec_encode(q,_n,msg,msg_enc); fec_encode(q,_n,msg,msg_enc); } getrusage(RUSAGE_SELF, _finish); *_num_iterations *= 4; // clean up objects fec_destroy(q); }