parent
a3d460c617
commit
e46aef1141
@ -0,0 +1,516 @@
|
||||
#include <string.h>
|
||||
#include "codecAO40.h"
|
||||
|
||||
/* ---------------------- */
|
||||
/* AO40 Encoder - Decoder */
|
||||
/* ---------------------- */
|
||||
|
||||
/* Scramble and RS encode 256 byte blocks of data into 5200 bits
|
||||
* or Descramble and RS decode 5200 bits into the 256 bytes of data
|
||||
*
|
||||
* --------------------------------------------------------------------------
|
||||
* This decoder has evolved extensively through the work of Phil Karn. It draws
|
||||
* on his own ideas and optimisations, and on the work of others. The lineage
|
||||
* is as below, and parts of the authors' notices are included here. (JRM)
|
||||
|
||||
* AO40 encoder / decoder
|
||||
* Copyright 2002 Phil Karn, KA9Q
|
||||
* May be used under the terms of the GNU General Public License (GPL)
|
||||
*
|
||||
* Reed-Solomon coding and decoding
|
||||
* Phil Karn (karn@ka9q.ampr.org) September 1996
|
||||
*
|
||||
* This file is derived from the program "new_rs_erasures.c" by Robert
|
||||
* Morelos-Zaragoza (robert@spectra.eng.hawaii.edu) and Hari Thirumoorthy
|
||||
* (harit@spectra.eng.hawaii.edu), Aug 1995
|
||||
* --------------------------------------------------------------------------
|
||||
*
|
||||
* From the RM-Z & HT program:
|
||||
* The encoding and decoding methods are based on the
|
||||
* book "Error Control Coding: Fundamentals and Applications",
|
||||
* by Lin and Costello, Prentice Hall, 1983, ISBN 0-13-283796-X
|
||||
* Portions of this program are from a Reed-Solomon encoder/decoder
|
||||
* in C, written by Simon Rockliff (simon@augean.ua.oz.au) on 21/9/89.
|
||||
* --------------------------------------------------------------------------
|
||||
*
|
||||
* From the 1989/1991 SR program (also based on Lin and Costello):
|
||||
* This program may be freely modified and/or given to whoever wants it.
|
||||
* A condition of such distribution is that the author's contribution be
|
||||
* acknowledged by his name being left in the comments heading the program,
|
||||
* Simon Rockliff, 26th June 1991
|
||||
*
|
||||
*/
|
||||
|
||||
/* Defines for RS Decoder(s) */
|
||||
#ifndef min
|
||||
#define min(a,b) ((a) < (b) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
CCodecAO40::CCodecAO40(void)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
CCodecAO40::~CCodecAO40(void)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int CCodecAO40::mod255(int x) {
|
||||
while (x >= 255) {
|
||||
x -= 255;
|
||||
x = (x >> 8) + (x & 255);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int CCodecAO40::decode_rs_8(char *data, int *eras_pos, int no_eras){
|
||||
|
||||
int deg_lambda, el, deg_omega;
|
||||
int i, j, r,k;
|
||||
unsigned char u,q,tmp,num1,num2,den,discr_r;
|
||||
unsigned char lambda[NROOTS+1], s[NROOTS]; /* Err+Eras Locator poly and syndrome poly */
|
||||
unsigned char b[NROOTS+1], t[NROOTS+1], omega[NROOTS+1];
|
||||
unsigned char root[NROOTS], reg[NROOTS+1], loc[NROOTS];
|
||||
int syn_error, count;
|
||||
|
||||
/* form the syndromes; i.e., evaluate data(x) at roots of g(x) */
|
||||
for(i=0;i<NROOTS;i++)
|
||||
s[i] = data[0];
|
||||
|
||||
for(j=1;j<NN;j++){
|
||||
for(i=0;i<NROOTS;i++){
|
||||
if(s[i] == 0){
|
||||
s[i] = data[j];
|
||||
} else {
|
||||
s[i] = data[j] ^ ALPHA_TO[mod255(INDEX_OF[s[i]] + (FCR+i)*PRIM)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Convert syndromes to index form, checking for nonzero condition */
|
||||
syn_error = 0;
|
||||
for(i=0;i<NROOTS;i++){
|
||||
syn_error |= s[i];
|
||||
s[i] = INDEX_OF[s[i]];
|
||||
}
|
||||
|
||||
if (!syn_error) {
|
||||
/* if syndrome is zero, data[] is a codeword and there are no
|
||||
* errors to correct. So return data[] unmodified
|
||||
*/
|
||||
count = 0;
|
||||
goto finish;
|
||||
}
|
||||
memset(&lambda[1],0,NROOTS*sizeof(lambda[0]));
|
||||
lambda[0] = 1;
|
||||
|
||||
if (no_eras > 0) {
|
||||
/* Init lambda to be the erasure locator polynomial */
|
||||
lambda[1] = ALPHA_TO[mod255(PRIM*(NN-1-eras_pos[0]))];
|
||||
for (i = 1; i < no_eras; i++) {
|
||||
u = mod255(PRIM*(NN-1-eras_pos[i]));
|
||||
for (j = i+1; j > 0; j--) {
|
||||
tmp = INDEX_OF[lambda[j - 1]];
|
||||
if(tmp != A0)
|
||||
lambda[j] ^= ALPHA_TO[mod255(u + tmp)];
|
||||
}
|
||||
}
|
||||
}
|
||||
for(i=0;i<NROOTS+1;i++)
|
||||
b[i] = INDEX_OF[lambda[i]];
|
||||
|
||||
/*
|
||||
* Begin Berlekamp-Massey algorithm to determine error+erasure
|
||||
* locator polynomial
|
||||
*/
|
||||
r = no_eras;
|
||||
el = no_eras;
|
||||
while (++r <= NROOTS) { /* r is the step number */
|
||||
/* Compute discrepancy at the r-th step in poly-form */
|
||||
discr_r = 0;
|
||||
for (i = 0; i < r; i++){
|
||||
if ((lambda[i] != 0) && (s[r-i-1] != A0)) {
|
||||
discr_r ^= ALPHA_TO[mod255(INDEX_OF[lambda[i]] + s[r-i-1])];
|
||||
}
|
||||
}
|
||||
discr_r = INDEX_OF[discr_r]; /* Index form */
|
||||
if (discr_r == A0) {
|
||||
/* 2 lines below: B(x) <-- x*B(x) */
|
||||
memmove(&b[1],b,NROOTS*sizeof(b[0]));
|
||||
b[0] = A0;
|
||||
} else {
|
||||
/* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
|
||||
t[0] = lambda[0];
|
||||
for (i = 0 ; i < NROOTS; i++) {
|
||||
if(b[i] != A0)
|
||||
t[i+1] = lambda[i+1] ^ ALPHA_TO[mod255(discr_r + b[i])];
|
||||
else
|
||||
t[i+1] = lambda[i+1];
|
||||
}
|
||||
if (2 * el <= r + no_eras - 1) {
|
||||
el = r + no_eras - el;
|
||||
/*
|
||||
* 2 lines below: B(x) <-- inv(discr_r) *
|
||||
* lambda(x)
|
||||
*/
|
||||
for (i = 0; i <= NROOTS; i++)
|
||||
b[i] = (lambda[i] == 0) ? A0 : mod255(INDEX_OF[lambda[i]] - discr_r + NN);
|
||||
} else {
|
||||
/* 2 lines below: B(x) <-- x*B(x) */
|
||||
memmove(&b[1],b,NROOTS*sizeof(b[0]));
|
||||
b[0] = A0;
|
||||
}
|
||||
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] = INDEX_OF[lambda[i]];
|
||||
if(lambda[i] != A0)
|
||||
deg_lambda = i;
|
||||
}
|
||||
/* Find roots of the error+erasure locator polynomial by Chien search */
|
||||
memcpy(®[1],&lambda[1],NROOTS*sizeof(reg[0]));
|
||||
count = 0; /* Number of roots of lambda(x) */
|
||||
for (i = 1,k=IPRIM-1; i <= NN; i++,k = mod255(k+IPRIM)) {
|
||||
q = 1; /* lambda[0] is always 0 */
|
||||
for (j = deg_lambda; j > 0; j--){
|
||||
if (reg[j] != A0) {
|
||||
reg[j] = mod255(reg[j] + j);
|
||||
q ^= ALPHA_TO[reg[j]];
|
||||
}
|
||||
}
|
||||
if (q != 0)
|
||||
continue; /* Not a root */
|
||||
/* store root (index-form) and error location number */
|
||||
root[count] = i;
|
||||
loc[count] = k;
|
||||
/* If we've already found max possible roots,
|
||||
* abort the search to save time
|
||||
*/
|
||||
if(++count == deg_lambda)
|
||||
break;
|
||||
}
|
||||
if (deg_lambda != count) {
|
||||
/*
|
||||
* deg(lambda) unequal to number of roots => uncorrectable
|
||||
* error detected
|
||||
*/
|
||||
count = -1;
|
||||
goto finish;
|
||||
}
|
||||
/*
|
||||
* Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
|
||||
* x**NROOTS). in index form. Also find deg(omega).
|
||||
*/
|
||||
deg_omega = 0;
|
||||
for (i = 0; i < NROOTS;i++){
|
||||
tmp = 0;
|
||||
j = (deg_lambda < i) ? deg_lambda : i;
|
||||
for(;j >= 0; j--){
|
||||
if ((s[i - j] != A0) && (lambda[j] != A0))
|
||||
tmp ^= ALPHA_TO[mod255(s[i - j] + lambda[j])];
|
||||
}
|
||||
if(tmp != 0)
|
||||
deg_omega = i;
|
||||
omega[i] = INDEX_OF[tmp];
|
||||
}
|
||||
omega[NROOTS] = A0;
|
||||
|
||||
/*
|
||||
* Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
|
||||
* inv(X(l))**(FCR-1) and den = lambda_pr(inv(X(l))) all in poly-form
|
||||
*/
|
||||
for (j = count-1; j >=0; j--) {
|
||||
num1 = 0;
|
||||
for (i = deg_omega; i >= 0; i--) {
|
||||
if (omega[i] != A0)
|
||||
num1 ^= ALPHA_TO[mod255(omega[i] + i * root[j])];
|
||||
}
|
||||
num2 = ALPHA_TO[mod255(root[j] * (FCR - 1) + NN)];
|
||||
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] != A0)
|
||||
den ^= ALPHA_TO[mod255(lambda[i+1] + i * root[j])];
|
||||
}
|
||||
if (den == 0) {
|
||||
count = -1;
|
||||
goto finish;
|
||||
}
|
||||
/* Apply error to data */
|
||||
if (num1 != 0) {
|
||||
data[loc[j]] ^= ALPHA_TO[mod255(INDEX_OF[num1] + INDEX_OF[num2] + NN - INDEX_OF[den])];
|
||||
}
|
||||
}
|
||||
finish:
|
||||
if(eras_pos != NULL){
|
||||
for(i=0;i<count;i++)
|
||||
eras_pos[i] = loc[i];
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* ---------- */
|
||||
/* Re-encoder */
|
||||
/* ---------- */
|
||||
|
||||
/* Reference encoder for proposed coded AO-40 telemetry format - v1.0 7 Jan 2002
|
||||
* Copyright 2002, Phil Karn, KA9Q
|
||||
* This software may be used under the terms of the GNU Public License (GPL)
|
||||
*/
|
||||
|
||||
/* Adapted from the above enc_ref.c as used by the spacecraft (JRM) */
|
||||
|
||||
|
||||
/* Write one binary channel symbol into the interleaver frame and update the pointers */
|
||||
void CCodecAO40::interleave_symbol(int c)
|
||||
{
|
||||
int row,col;
|
||||
col=m_ileaver_index/COLUMNS;
|
||||
row=m_ileaver_index%COLUMNS;
|
||||
if(c)
|
||||
{
|
||||
m_encoded[row*ROWS+col] = 1;
|
||||
}
|
||||
m_ileaver_index++;
|
||||
}
|
||||
|
||||
/* Convolutionally encode and interleave one byte */
|
||||
void CCodecAO40::encode_and_interleave(unsigned char c,int cnt){
|
||||
while(cnt-- != 0)
|
||||
{
|
||||
m_conv_sr = (m_conv_sr << 1) | (c >> 7);
|
||||
c <<= 1;
|
||||
interleave_symbol( Partab[m_conv_sr & CPOLYA]);
|
||||
interleave_symbol(!Partab[m_conv_sr & CPOLYB]); /* Second encoder symbol is inverted */
|
||||
}
|
||||
}
|
||||
|
||||
/* Scramble a byte, convolutionally encode and interleave into frame */
|
||||
void CCodecAO40::scramble_and_encode(unsigned char c){
|
||||
c ^= Scrambler[m_encoded_bytes]; /* Scramble byte */
|
||||
encode_and_interleave(c,8); /* RS encode and place into reencode buffer */
|
||||
}
|
||||
|
||||
/* Encodes the 256 byte source block RSdecdata[] into 5200 byte block of symbols
|
||||
* results stored in m_encoded.
|
||||
* On success encoded buffer is returned, an zeroed buffer on failure
|
||||
*/
|
||||
const unsigned char *CCodecAO40::encode(unsigned char *source_bytes, int byte_count)
|
||||
{
|
||||
if(BLOCKSIZE != byte_count || NULL == source_bytes )
|
||||
{
|
||||
memset(m_encoded, 0, BLOCKSIZE);
|
||||
return m_encoded;
|
||||
}
|
||||
|
||||
init_encoder();
|
||||
|
||||
for(int i=0;i<BLOCKSIZE;i++)
|
||||
{
|
||||
encode_byte(source_bytes[i]) ;
|
||||
}
|
||||
|
||||
for(int i=0;i<64;i++)
|
||||
{
|
||||
encode_parity();
|
||||
}
|
||||
|
||||
return m_encoded;
|
||||
}
|
||||
|
||||
/* The original three C user's entry points now follow. They are:
|
||||
|
||||
init_encoder() Called once before using system.
|
||||
encode_byte(unsigned char c) Called with each user byte (i.e. 256 calls)
|
||||
encode_parity() Called 64 times to finish off
|
||||
|
||||
*/
|
||||
|
||||
/* This function initializes the encoder. */
|
||||
void CCodecAO40::init_encoder(void){
|
||||
int i,j,sr;
|
||||
|
||||
m_encoded_bytes = 0;
|
||||
m_conv_sr = 0;
|
||||
m_ileaver_index = COLUMNS; /* Sync vector is in first column; data starts here */
|
||||
|
||||
for(j=0;j<RSBLOCKS;j++) /* Flush parity array */
|
||||
{
|
||||
for(i=0;i<NROOTS;i++)
|
||||
{
|
||||
m_RS_block[j][i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Clear re-encoded array */
|
||||
for(i=0;i<SYMPBLOCK;i++)
|
||||
{
|
||||
m_encoded[i] = 0;
|
||||
}
|
||||
|
||||
/* Generate sync vector, interleave into re-encode array, 1st column */
|
||||
sr = 0x7f;
|
||||
for(i=0;i<65;i++)
|
||||
{
|
||||
if(sr & 64)
|
||||
{
|
||||
m_encoded[ROWS*i] = 1; /* Every 80th symbol is a sync bit */
|
||||
}
|
||||
sr = (sr << 1) | Partab[sr & SYNC_POLY];
|
||||
}
|
||||
}
|
||||
|
||||
/* This function is called with each user data byte to be encoded into the
|
||||
* current frame. It should be called in sequence 256 times per frame, followed
|
||||
* by 64 calls to encode_parity().
|
||||
*/
|
||||
void CCodecAO40::encode_byte(unsigned char c){
|
||||
unsigned char *rp; /* RS block pointer */
|
||||
int i;
|
||||
unsigned char feedback;
|
||||
|
||||
/* Update the appropriate Reed-Solomon codeword */
|
||||
rp = m_RS_block[m_encoded_bytes & 1];
|
||||
|
||||
/* Compute feedback term */
|
||||
feedback = INDEX_OF[c ^ rp[0]];
|
||||
|
||||
/* If feedback is non-zero, multiply by each generator polynomial coefficient and
|
||||
* add to corresponding shift register elements
|
||||
*/
|
||||
if(feedback != A0){
|
||||
int j;
|
||||
|
||||
/* This loop exploits the palindromic nature of the generator polynomial
|
||||
* to halve the number of discrete multiplications
|
||||
*/
|
||||
for(j=0;j<15;j++){
|
||||
unsigned char t;
|
||||
|
||||
t = ALPHA_TO[mod255(feedback + RS_poly[j])];
|
||||
rp[j+1] ^= t; rp[31-j] ^= t;
|
||||
}
|
||||
rp[16] ^= ALPHA_TO[mod255(feedback + RS_poly[15])];
|
||||
}
|
||||
|
||||
/* Shift 32 byte RS register one position down */
|
||||
for(i=0;i<31;i++)
|
||||
rp[i] = rp[i+1];
|
||||
|
||||
/* Handle highest order coefficient, which is unity */
|
||||
if(feedback != A0){
|
||||
rp[31] = ALPHA_TO[feedback];
|
||||
} else {
|
||||
rp[31] = 0;
|
||||
}
|
||||
scramble_and_encode(c);
|
||||
m_encoded_bytes++;
|
||||
}
|
||||
|
||||
/* This function should be called 64 times after the 256 data bytes
|
||||
* have been passed to update_encoder. Each call scrambles, encodes and
|
||||
* interleaves one byte of Reed-Solomon parity.
|
||||
*/
|
||||
void CCodecAO40::encode_parity(void) {
|
||||
unsigned char c;
|
||||
|
||||
c = m_RS_block[m_encoded_bytes & 1][(m_encoded_bytes-256)>>1];
|
||||
scramble_and_encode(c);
|
||||
if(++m_encoded_bytes == 320){
|
||||
/* Tail off the convolutional encoder (flush) */
|
||||
encode_and_interleave(0,6);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void CCodecAO40::descramble_to_rsblocks(unsigned char viterbi_decoded[NBITS_OUT], char rsblocks[RSBLOCKS][NN])
|
||||
{
|
||||
/* interleave into Reed Solomon codeblocks */
|
||||
memset(rsblocks,0,RSBLOCKS*NN); /* Zero rsblocks array */
|
||||
int di = 0;
|
||||
int si = 0;
|
||||
for(int col=RSPAD;col<NN;col++)
|
||||
{
|
||||
for(int row=0;row<RSBLOCKS;row++)
|
||||
{
|
||||
rsblocks[row][col] = viterbi_decoded[di++] ^ Scrambler[si++]; /* Remove scrambling */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* -------- */
|
||||
/* Decoder */
|
||||
/* -------- */
|
||||
|
||||
/* ------------------- */
|
||||
|
||||
/* There are two RS decoders, processing 128 bytes each.
|
||||
*
|
||||
* If both RS decoders are SUCCESSFUL
|
||||
* On exit:
|
||||
* rs_failures = 0
|
||||
* rserrs[x] = number of errors corrected; range 0 to 16 (x= 0 or 1)
|
||||
* Data output is in array RSdecdata[256].
|
||||
*
|
||||
* If an RS decoder FAILS
|
||||
* On exit:
|
||||
* rs_failures = 1 or 2 (i.e. != 0)
|
||||
* rserrs[x] contains -1
|
||||
* Data output should not be used.
|
||||
*/
|
||||
int CCodecAO40::decode(unsigned char vitdecdata[NBITS_OUT], unsigned char *decoded_data)
|
||||
{
|
||||
int row, col, row_errors, total_errors;
|
||||
char rsblocks[RSBLOCKS][NN];
|
||||
|
||||
descramble_to_rsblocks(vitdecdata, rsblocks);
|
||||
|
||||
/* Run RS-decoder(s) */
|
||||
row_errors = total_errors = 0;
|
||||
for(row=0; row<RSBLOCKS && row_errors!= -1; row++)
|
||||
{
|
||||
// decode row, returns -1 on failure or number of corrected errors
|
||||
row_errors = decode_rs_8(rsblocks[row],NULL,0);
|
||||
total_errors += row_errors;
|
||||
}
|
||||
|
||||
if(row_errors != -1)
|
||||
{
|
||||
/* if frame decoded OK, deinterleave data from RS codeword(s) */
|
||||
int j = 0;
|
||||
for(col=RSPAD;col<KK;col++)
|
||||
{
|
||||
for(row=0;row<RSBLOCKS;row++)
|
||||
{
|
||||
decoded_data[j++] = rsblocks[row][col];
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
total_errors = -1;
|
||||
}
|
||||
|
||||
return total_errors;
|
||||
}
|
||||
|
||||
/* Compairs raw input symbols to current buffer of encoded symbols and counts the errors */
|
||||
int CCodecAO40::count_errors(unsigned char *raw_symbols)
|
||||
{
|
||||
int error_count = 0;
|
||||
for(int i=0;i<SYMPBLOCK;i++)
|
||||
{
|
||||
if ( m_encoded[i] != (raw_symbols[i]>>7) )
|
||||
{
|
||||
error_count++ ;
|
||||
}
|
||||
}
|
||||
return error_count;
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
/* AO40 encoder / decoder
|
||||
* Copyright 2002 Phil Karn, KA9Q
|
||||
* May be used under the terms of the GNU General Public License (GPL)
|
||||
* See CodecAO40.cpp for lineage
|
||||
*
|
||||
* This file is part of FUNcubeLib.
|
||||
*
|
||||
* FUNcubeLib is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* FUNcubeLib is distributed in the hope that it will be useful,
|
||||
* 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
|
||||
* along with FUNcubeLib If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "fecConstants.h"
|
||||
|
||||
class CCodecAO40
|
||||
{
|
||||
public:
|
||||
CCodecAO40(void);
|
||||
virtual ~CCodecAO40(void);
|
||||
|
||||
int decode(unsigned char viterbi_decoded[NBITS_OUT], unsigned char *decoded_data);
|
||||
|
||||
/* Encodes the 256 byte source block into 5200 byte block of symbols into m_encoded buffer */
|
||||
const unsigned char *encode(
|
||||
unsigned char *source_bytes, /* input to encode */
|
||||
int byte_count); /* input length in bytes */
|
||||
|
||||
/* Compares raw input symbols to current buffer of encoded symbols and counts the errors */
|
||||
int count_errors( unsigned char *raw_symbols);
|
||||
|
||||
private:
|
||||
int mod255(int x);
|
||||
int decode_rs_8(char *data, int *eras_pos, int no_eras);
|
||||
void scramble_and_encode(unsigned char c);
|
||||
void encode_and_interleave(unsigned char c,int cnt);
|
||||
|
||||
void descramble_to_rsblocks(
|
||||
unsigned char viterbi_decoded[NBITS_OUT],
|
||||
char rsblocks[RSBLOCKS][NN]);
|
||||
|
||||
void init_encoder(void);
|
||||
void encode_byte(unsigned char c);
|
||||
void encode_parity(void);
|
||||
|
||||
void interleave_symbol(int c);
|
||||
|
||||
int m_encoded_bytes; /* Byte counter for encode_data() */
|
||||
int m_ileaver_index; /* Byte counter for interleaver */
|
||||
unsigned char m_conv_sr; /* Convolutional encoder shift register state */
|
||||
|
||||
unsigned char m_RS_block[RSBLOCKS][NROOTS]; /* RS parity blocks */
|
||||
unsigned char m_encoded[SYMPBLOCK] ; /* encoded symbols */
|
||||
};
|
||||
@ -0,0 +1,128 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
Amsat P3 FEC Encoder/decoder system. Look-up tables
|
||||
Created by Phil Karn KA9Q and James Miller G3RUH
|
||||
Last modified 2003 Jun 20
|
||||
*/
|
||||
|
||||
/* Defines for Viterbi Decoder for r=1/2 k=7 (to CCSDS convention) */
|
||||
#define K 7 /* Constraint length */
|
||||
#define N 2 /* Number of symbols per data bit */
|
||||
#define CPOLYA 0x4f /* First convolutional encoder polynomial */
|
||||
#define CPOLYB 0x6d /* Second convolutional encoder polynomial */
|
||||
|
||||
#define SYNC_POLY 0x48 /* Sync vector PN polynomial */
|
||||
|
||||
#define NN 255
|
||||
#define KK 223
|
||||
#define NROOTS 32 /* NN-KK */
|
||||
#define A0 (NN)
|
||||
#define FCR 112
|
||||
#define PRIM 11
|
||||
#define IPRIM 116
|
||||
#define BLOCKSIZE 256 /* Data bytes per frame */
|
||||
#define RSBLOCKS 2 /* Number of RS decoder blocks */
|
||||
#define RSPAD 95 /* Unused bytes in block (KK-BLOCKSIZE/RSBLOCKS) */
|
||||
|
||||
/* Defines for Interleaver */
|
||||
#define ROWS 80 /* Block interleaver rows */
|
||||
#define COLUMNS 65 /* Block interleaver columns */
|
||||
#define SYMPBLOCK (ROWS*COLUMNS) /* Encoded symbols per block */
|
||||
|
||||
/* Number of symbols in an FEC block that are */
|
||||
/* passed to the Viterbi decoder (320*8 + 6) */
|
||||
#define NBITS ((BLOCKSIZE+NROOTS*RSBLOCKS)*8+K-1)
|
||||
/* Number of bits obtained from Viterbi decoder */
|
||||
#define NBITS_OUT (BLOCKSIZE+NROOTS*RSBLOCKS)
|
||||
|
||||
const unsigned char RS_poly[] = {
|
||||
249, 59, 66, 4, 43,126,251, 97, 30, 3,213, 50, 66,170, 5, 24
|
||||
};
|
||||
|
||||
/* Tables for RS decoder */
|
||||
/* Galois field log/antilog tables */
|
||||
const unsigned char ALPHA_TO[] =
|
||||
{
|
||||
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x87, 0x89, 0x95, 0xad, 0xdd, 0x3d, 0x7a, 0xf4,
|
||||
0x6f, 0xde, 0x3b, 0x76, 0xec, 0x5f, 0xbe, 0xfb, 0x71, 0xe2, 0x43, 0x86, 0x8b, 0x91, 0xa5, 0xcd,
|
||||
0x1d, 0x3a, 0x74, 0xe8, 0x57, 0xae, 0xdb, 0x31, 0x62, 0xc4, 0x0f, 0x1e, 0x3c, 0x78, 0xf0, 0x67,
|
||||
0xce, 0x1b, 0x36, 0x6c, 0xd8, 0x37, 0x6e, 0xdc, 0x3f, 0x7e, 0xfc, 0x7f, 0xfe, 0x7b, 0xf6, 0x6b,
|
||||
0xd6, 0x2b, 0x56, 0xac, 0xdf, 0x39, 0x72, 0xe4, 0x4f, 0x9e, 0xbb, 0xf1, 0x65, 0xca, 0x13, 0x26,
|
||||
0x4c, 0x98, 0xb7, 0xe9, 0x55, 0xaa, 0xd3, 0x21, 0x42, 0x84, 0x8f, 0x99, 0xb5, 0xed, 0x5d, 0xba,
|
||||
0xf3, 0x61, 0xc2, 0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0x07, 0x0e, 0x1c, 0x38, 0x70, 0xe0,
|
||||
0x47, 0x8e, 0x9b, 0xb1, 0xe5, 0x4d, 0x9a, 0xb3, 0xe1, 0x45, 0x8a, 0x93, 0xa1, 0xc5, 0x0d, 0x1a,
|
||||
0x34, 0x68, 0xd0, 0x27, 0x4e, 0x9c, 0xbf, 0xf9, 0x75, 0xea, 0x53, 0xa6, 0xcb, 0x11, 0x22, 0x44,
|
||||
0x88, 0x97, 0xa9, 0xd5, 0x2d, 0x5a, 0xb4, 0xef, 0x59, 0xb2, 0xe3, 0x41, 0x82, 0x83, 0x81, 0x85,
|
||||
0x8d, 0x9d, 0xbd, 0xfd, 0x7d, 0xfa, 0x73, 0xe6, 0x4b, 0x96, 0xab, 0xd1, 0x25, 0x4a, 0x94, 0xaf,
|
||||
0xd9, 0x35, 0x6a, 0xd4, 0x2f, 0x5e, 0xbc, 0xff, 0x79, 0xf2, 0x63, 0xc6, 0x0b, 0x16, 0x2c, 0x58,
|
||||
0xb0, 0xe7, 0x49, 0x92, 0xa3, 0xc1, 0x05, 0x0a, 0x14, 0x28, 0x50, 0xa0, 0xc7, 0x09, 0x12, 0x24,
|
||||
0x48, 0x90, 0xa7, 0xc9, 0x15, 0x2a, 0x54, 0xa8, 0xd7, 0x29, 0x52, 0xa4, 0xcf, 0x19, 0x32, 0x64,
|
||||
0xc8, 0x17, 0x2e, 0x5c, 0xb8, 0xf7, 0x69, 0xd2, 0x23, 0x46, 0x8c, 0x9f, 0xb9, 0xf5, 0x6d, 0xda,
|
||||
0x33, 0x66, 0xcc, 0x1f, 0x3e, 0x7c, 0xf8, 0x77, 0xee, 0x5b, 0xb6, 0xeb, 0x51, 0xa2, 0xc3, 0x00,
|
||||
};
|
||||
|
||||
const unsigned char INDEX_OF[]=
|
||||
{
|
||||
0xff, 0x00, 0x01, 0x63, 0x02, 0xc6, 0x64, 0x6a, 0x03, 0xcd, 0xc7, 0xbc, 0x65, 0x7e, 0x6b, 0x2a,
|
||||
0x04, 0x8d, 0xce, 0x4e, 0xc8, 0xd4, 0xbd, 0xe1, 0x66, 0xdd, 0x7f, 0x31, 0x6c, 0x20, 0x2b, 0xf3,
|
||||
0x05, 0x57, 0x8e, 0xe8, 0xcf, 0xac, 0x4f, 0x83,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0, 1,
|
||||
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
||||
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
||||
};
|
||||
|
||||
/* Scramble byte table */
|
||||
const unsigned char Scrambler[]=
|
||||
{
|
||||
0xff, 0x48, 0x0e, 0xc0, 0x9a, 0x0d, 0x70, 0xbc, 0x8e, 0x2c, 0x93, 0xad, 0xa7, 0xb7, 0x46, 0xce,
|
||||
0x5a, 0x97, 0x7d, 0xcc, 0x32, 0xa2, 0xbf, 0x3e, 0x0a, 0x10, 0xf1, 0x88, 0x94, 0xcd, 0xea, 0xb1,
|
||||
0xfe, 0x90, 0x1d, 0x81, 0x34, 0x1a, 0xe1, 0x79, 0x1c, 0x59, 0x27, 0x5b, 0x4f, 0x6e, 0x8d, 0x9c,
|
||||
0xb5, 0x2e, 0xfb, 0x98, 0x65, 0x45, 0x7e, 0x7c, 0x14, 0x21, 0xe3, 0x11, 0x29, 0x9b, 0xd5, 0x63,
|
||||
0xfd, 0x20, 0x3b, 0x02, 0x68, 0x35, 0xc2, 0xf2, 0x38, 0xb2, 0x4e, 0xb6, 0x9e, 0xdd, 0x1b, 0x39,
|
||||
0x6a, 0x5d, 0xf7, 0x30, 0xca, 0x8a, 0xfc, 0xf8, 0x28, 0x43, 0xc6, 0x22, 0x53, 0x37, 0xaa, 0xc7,
|
||||
0xfa, 0x40, 0x76, 0x04, 0xd0, 0x6b, 0x85, 0xe4, 0x71, 0x64, 0x9d, 0x6d, 0x3d, 0xba, 0x36, 0x72,
|
||||
0xd4, 0xbb, 0xee, 0x61, 0x95, 0x15, 0xf9, 0xf0, 0x50, 0x87, 0x8c, 0x44, 0xa6, 0x6f, 0x55, 0x8f,
|
||||
0xf4, 0x80, 0xec, 0x09, 0xa0, 0xd7, 0x0b, 0xc8, 0xe2, 0xc9, 0x3a, 0xda, 0x7b, 0x74, 0x6c, 0xe5,
|
||||
0xa9,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||||
};
|
||||
Loading…
Reference in new issue