📄 rs.h
字号:
// RSDecDll.cpp : Defines the entry point for the DLL application.
//
//#include "stdafx.h"
//#include <time.h>
//#include <stdio.h>
//#include <stdlib.h>
//#include <memory.h>
//#include "RSDecDll.h"
//#include "..\China7_Dec\struct.h"
/*************************************************************/
/* PROGRAMM FOR THE SIMULATION OF (N,KK) RS CODE */
/* RS code is a systematic code */
/*************************************************************/
/* 域生成多项式 f1(x)=x8+x4+x3+x2+1 */
/* 码生成多项式 g1(x)=(x-arfa)(x-arfa2)(x-arfa3)...(x-arfa(N-KK)) */
/* 域生成多项式 f2(x)=x8+x7+x2+x+1 */
/* 码生成多项式 g2(x)=x16+(arfa75)x15+(arfa235)x14+(arfa213)x13+(arfa239)x12
+(arfa76)x11+(arfa113)x10+x9+(arfa244)x8+x7
+(arfa113)x6+(arfa76)x5+(arfa239)x4+(arfa213)x3+(arfa235)x2
+(arfa75)x+1 */
// Modify By Mabel begin [2005.04.15 pm.14:54]
//
#define N 255
#define U 243
#define T (N-U)/2
#define P 8
#define GROUPNUM 4 //一包中的码组数
#define RESBYTES 4 //一包中剩余码子数
#define PACKET_SIZE_WITH_RS 1024
#define PACKET_SIZE_DE_RS 976
//
static unsigned char lookup[N]={0};
static unsigned char nlookup[N+1]={0};
unsigned char sigma[2*T+1][2*T+120],omiga[2*T+1][2*T+120];
unsigned char d[2*T+1],L[2*T+1],sigmak[2*T],error[N];
//
unsigned char *RSbit; //RS coder stream
int RSbitlen; //RS stream length
static unsigned char bmulti_result;
/***********************/
/* multiply of GF(255) */
/***********************/
//unsigned char bmulti(unsigned char ba, unsigned char bb)
//{
// if(ba==0||bb==0)
// return 0;
// else
// return(lookup[(nlookup[ba]+nlookup[bb])%N]);
//}
#define Macro_bmulti(ba, bb)\
{\
if(ba==0||bb==0)\
bmulti_result=0;\
else\
bmulti_result=lookup[(nlookup[ba]+nlookup[bb])%N];\
}
/****************************************
function description:
Initialize the too lookup and inverse loopup table for RS decoder
****************************************/
void RS_init()
{
int i;
lookup[0]=1;nlookup[0]=0;
for(i=1;i<8;i++){
lookup[i]=1<<i;
nlookup[lookup[i]]=i;
}
for(i=8;i<N;i++){
lookup[i]=lookup[i-8]^lookup[i-7]^lookup[i-6]^lookup[i-1];
nlookup[lookup[i]]=i;
}
}
/************************************************************************
function description:
Decode the RS stream.the suffix Ex means extention of original functionality.
If the packet is not the type of coded packets,discard it and continue to decode the
rest of stream,then return the actual decode packet number,If encounter the lead packet,
then break the decoding process.
Return value:
Whether the decoding process encounter Lead packet,if that happened the current
subframe should be flushed.And the actually decoded packets are returned as a pointer parameter
************************************************************************/
void fnRSDecDllEx(FILE *fErrorReport,
unsigned char *streamIn, //解码输入码流(含RS编码)
int nPakNum, //输入的包数
int *pnPakDeced, //the actual RS decoded packets excluding lead packet and test Packs
int *pnPakProed, //the actual packet been processed including lead packet
unsigned char *streamOut //输出码流
)
{
int nLoop, i,j,nPakLoop,m;
unsigned char a0,va,vb,val,mark;
unsigned char *rec,*rm;
unsigned char s[2*T+120];
FILE *fRs;
unsigned char *pRs;
CString szRS;
szRS.Format(_T("e:\\Decompressed stream\\rstest.dat"));
fRs = fopen(szRS,"wb+");
pRs = streamIn;
fwrite(pRs,1,2680832,fRs);
fclose(fRs);
for (nPakLoop=0,*pnPakDeced=0; nPakLoop<nPakNum;nPakLoop++,streamIn+=PACKET_SIZE_WITH_RS)
{
//discard packets with the frame type of lead packet or pad packet
if((streamIn[PACKET_TYPE_OFFSET]==LEAD_PACKET))
{
continue;//结束本次循环
}
else
{
for(nLoop=0; nLoop<GROUPNUM; nLoop++)//将码流分成码组,依次进行RS解码
{
rec=streamIn+nLoop*N;
rm=streamOut+nLoop*U;
mark=0;
s[0]=1;
/* compute Syndroms S(x) begin */
memset(s+1,0,2*T+119);
for(i=1;i<=2*T;i++)
{
// #pragma vector always
for(j=0;j<N-1;j++)
{
if(rec[j]!=0)
s[i]=s[i]^(lookup[(nlookup[rec[j]]+(i+119)*(N-1-j))%N]);
}
s[i]=s[i]^rec[N-1];
mark=mark+s[i];
}
/* compute Syndroms S(x) end */
// /* output s[i] begin */
if(mark==0)
{
memcpy(rm,rec,U);
}
else /* when mark!=0,compute L[2*T] begin */
{
TRACE("RS Err Found.\n");
// TRACE("%d packet is wrong in RS decoding\n",nPakLoop);
// TRACE("%d subpacket is wrong in RS decoding\n",nLoop);
TRACE("The RS error is in %d subframe %d packet\n",(nPakLoop-10)/125,(nPakLoop - 11 -(nPakLoop-10)/125*125 ));
fprintf(fErrorReport,"RS Err Found.\n");
fprintf(fErrorReport,"%d packet is wrong in RS decoding\n",nPakLoop);
fprintf(fErrorReport,"%d subpacket is wrong in RS decoding\n",nLoop);
fprintf(fErrorReport,"The RS error is in %d subframe %d packet\n",(nPakLoop-10)/125,(nPakLoop - 11 -(nPakLoop-10)/125*125 ));
for(i=0;i<U;i++)
error[i]=0;
sigma[0][0]=1;
omiga[0][0]=1;
for(i=1;i<2*T+1;i++)
{
sigma[0][i]=0; /* set sigma[0][]=1 */
omiga[0][i]=0; /* set omiga[0][]=1 */
}
L[0]=0; /* degree of sigma[0][]=0 */
m=-1;
for(j=0;j<2*T;j++) /* Berlekamp's recursive algorithm begin*/
{
for(i=0;i<2*T+1;i++)
{
sigma[j+1][i]=sigma[j][i];
omiga[j+1][i]=omiga[j][i];
}
d[j]=0;
for(i=0;i<=L[j];i++){ /* compute d[j] */
Macro_bmulti(s[j+1-i],sigma[j][i])
d[j]=d[j]^(bmulti_result);
}
if(d[j]==0) L[j+1]=L[j];
else
{
if(m==-1)
{
sigma[j+1][j-m]=d[j]^sigma[j+1][j-m];
L[j+1]=L[j]>(j-m)?L[j]:(j-m);
m=(j-L[j])>m?j:m;
}
else
{
a0=nlookup[d[m]];
a0=lookup[(N-a0)%N];
Macro_bmulti(a0,d[j])
a0=bmulti_result;
for(i=0;i<=L[m];i++)
{
Macro_bmulti(a0,sigma[m][i]);
sigma[j+1][j-m+i]=sigma[j+1][j-m+i]^bmulti_result;
Macro_bmulti(a0,omiga[m][i]);
omiga[j+1][j-m+i]=omiga[j+1][j-m+i]^bmulti_result;
}
L[j+1]=L[j]>(j-m+L[m])?L[j]:(j-m+L[m]);
m=(j-L[j])>(m-L[m])?j:m;
}
}
}/* Berlekamp's recursive algorithm end */
if(L[2*T]>T) /* the number of errors greater than T */
{
TRACE("The number of RS errors is greater than 6,no correcting\n");
fprintf(fErrorReport,"The number of RS errors is greater than 6,no correcting\n");
for(i=0;i<U;i++)
//rm[i]=rec[i+N-KK];
rm[i]=rec[U-1-i];
}
else/*error correct begin*/
{
TRACE("Correcting RS error\n");
fprintf(fErrorReport,"RS error corrected \n");
for(i=L[2*T];i>0;i--)
{
if((i%2)==0) sigmak[i-1]=0;
else sigmak[i-1]=sigma[2*T][i];
}
/* compute error location and value begin*/
for(i=1;i<=U;i++)
{
a0=lookup[i];
val=0;
for(j=0;j<=L[2*T];j++){ /* Chien search */
Macro_bmulti(val,a0)
val=sigma[2*T][L[2*T]-j]^(bmulti_result);
}
if(val==0)
{
bmulti_result=0;
for(j=0;j<=L[2*T];j++){
Macro_bmulti(bmulti_result,a0)
bmulti_result=omiga[2*T][L[2*T]-j]^(bmulti_result);
}
va=bmulti_result;
bmulti_result=0;
for(j=0;j<L[2*T];j++){
Macro_bmulti(bmulti_result,a0)
bmulti_result=sigmak[L[2*T]-1-j]^(bmulti_result);
}
Macro_bmulti(bmulti_result,a0);
Macro_bmulti(bmulti_result,lookup[(i*N-i*119)%N]);
vb=lookup[(N-nlookup[bmulti_result])%N];
//error[N-i]=bmulti(va,vb); /* Forney formula */
Macro_bmulti(va,vb);
error[i-1]=bmulti_result;
}
}
/* compute error location and value end*/
/* compute trnasmitted code */
//?stopped here
for(i=0;i<U;i++)
rm[i]=rec[i]^error[i];
}
/*error correct end*/
}
}
// 取了下整,只到125包
// //对最后一个不完整的码组直接输出
// //for(nLoop=0; nLoop<RESBYTES; nLoop++)
// //streamOut[GROUPNUM*KK+nLoop] = streamIn[GROUPNUM*N+nLoop];
// what's for?
// if((streamIn[1021]==0x26)&&(streamIn[1022]==0x0b)&&(streamIn[1023]==0x18))
// {
// i++;
// }
//
//if decode the packet increase the counter
(*pnPakDeced)++;
streamOut+=PACKET_AFTER_RSDEC_LENGTH;
}
}
*pnPakProed=nPakLoop;
fwrite(pRs,1,2680832,fRs);
fclose(fRs);
// return FALSE;
}
//-----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -