#include "DataType.h"
#include "tables.h"
#include "dct.h"
#include "flash.h"
#define DEBUG
#ifdef DEBUG
#include "serial.h"
#endif
#define CIFW 352
#define CIFH 288
LINT8 cha_y[CIFW][CIFH];
LINT8 cha_u[CIFW/2][CIFH/2];
LINT8 cha_v[CIFW/2][CIFH/2];
LINT8 ix=0;
LINT8 iy=0;
LINT32 ydc=0,udc=0,vdc=0;
LINT32 accoder[16][11],acbit[16][11];
LINT32 accoder2[16][11],acbit2[16][11];
///////////文件部分/////////
#define MAXSIZE 262114 //256K
typedef struct{
LINT8 buffer[MAXSIZE];
LINT32 fp,lp,CurrentPosition,length;
LINT8 CurrentData;
LINT8 CurrentBits;
}FILEBUF;
FILEBUF infile;
//输入数据,长度为bits
void PutDatatoJpegFile(LINT32 data,LINT8 bits,FILEBUF *buf)
{
LINT8 CurrentFreeBits=0;
while (bits!=0)
{
if (buf->CurrentBits+bits<8)
{
buf->CurrentData=(buf->CurrentData<<bits)|data;
buf->CurrentBits+=bits;
bits=0;
}
else
{
CurrentFreeBits=8-buf->CurrentBits;
bits=bits-CurrentFreeBits;
buf->CurrentData=(buf->CurrentData<<CurrentFreeBits)|data>>bits;
buf->buffer[buf->CurrentPosition]=buf->CurrentData;
buf->CurrentPosition++;
buf->length++;
//如果data=0xFF, 加0
if (buf->CurrentData==0xff)
{
buf->buffer[buf->CurrentPosition]=0;
buf->CurrentPosition++;
buf->length++;
}
buf->CurrentData=0;
buf->CurrentBits=0;
data=data&mask[bits];
}
}
}
///////////Huffman编码 /////////
typedef struct
{
LINT32 maxcode[16];
LINT32 mincode[16];
LINT8 ml;
unsigned valptr[16];
}DHUFF;
typedef struct
{
LINT8 huffval[256];
}XHUFF;
DHUFF dhuff,dhuffdc,dhuff2,dhuffdc2;
XHUFF xhuff,xhuffdc,xhuff2,xhuffdc2;
void len2huff(LINT8 *lengthtab,DHUFF *dhuff,XHUFF *xhuff,LINT8 *val)
{
int i,l,k,base=0;
dhuff->ml=16;
for(i=0;i<dhuff->ml;i++)
{
dhuff->mincode[i]=0;
dhuff->maxcode[i]=0;
dhuff->valptr[i]=-1;
}
for(i=0;i<256;i++)
xhuff->huffval[i]=0;
i=0;
base=0;
for(l=0;l<dhuff->ml;l++)
{
if(lengthtab[l]!=0)
{
dhuff->mincode[l]=base;
dhuff->valptr[l]=i;
}
for(k=0;k<lengthtab[l];k++)
{
xhuff->huffval[i]=val[i];
i++;
base++;
}
if(lengthtab[l]!=0)dhuff->maxcode[l]=base-1;
base=base*2;
}
}
void iztransfer(LINT32 *array)
{
LINT32 temp[64],n,x,y;
for(n=0;n<64;n++)
{
x=iztab[2*n];
y=iztab[2*n+1];
temp[n]=array[x*8+y];
}
for(n=0;n<64;n++)
array[n]=temp[n];
}
//量化
void qdata(LINT32 * in,LINT32 * out,LINT8 level)
{
int i;
for(i=0;i<64;i++)
{
if(in[i]>0)
out[i]=in[i]/agraytab[level][i];
else
out[i]=in[i]/agraytab[level][i];
}
}
void quvdata(LINT32 * in,LINT32 * out,LINT8 level)
{
int i;
for(i=0;i<64;i++)
{
if(in[i]>0)
out[i]=in[i]/acolortab[level][i];
else
out[i]=in[i]/acolortab[level][i];
}
}
//从SRAM 0xC0100000读数据到 cha_y[CIFW][CIFH];
//cha_u[CIFW/2][CIFH/2]; cha_v[CIFW/2][CIFH/2];
#ifdef DEBUG
void put_num8(unsigned char i)
{
put_char(((((i>>4) & 0x0f) + '0')> '9' )? ((i>>4) & 0x0f) +'0'+7 : ((i>>4) & 0x0f) +'0' );
put_char((((i & 0x0f) + '0')> '9' )? (i & 0x0f) +'0'+7 : (i & 0x0f) +'0' );
}
static void put_num32(int i)
{
put_num8((unsigned char)(( i>>24) & 0xff));
put_num8( (unsigned char) (i>>16 & 0xff));
put_num8((unsigned char)(( i>>8) & 0xff));
put_num8( (unsigned char) (i & 0xff));
}
#endif
void readfile(void)
{
int i,count=0;
for (i=0;i<CIFW*CIFH*6/4;i+=6)
{
//取决于图像的帧格式
cha_y[(i*2/6%CIFW)][(i*2/6/CIFW)*2]=RAM_BYTE(i);
cha_y[(i*2/6%CIFW)+1][(i*2/6/CIFW)*2]=RAM_BYTE(i+1);
cha_y[(i*2/6%CIFW)][(i*2/6/CIFW)*2+1]=RAM_BYTE(i+2);
cha_y[(i*2/6%CIFW)+1][(i*2/6/CIFW)*2+1]=RAM_BYTE(i+3);
cha_u[i/6%(CIFW/2)][i/6/(CIFW/2)]=RAM_BYTE(i+4);
cha_v[i/6%(CIFW/2)][i/6/(CIFW/2)]=RAM_BYTE(i+5);
count++;
}
}
//写编码文件到串行口
void writefile(FILEBUF *buf)
{
int i;
for (i=0;i<=buf->length;i++)
{
put_num8(buf->buffer[i]);
}
}
LINT8 GetLength(LINT32 in)
{
int val;
if (in<0)
val=-in;
else
val = in;
if (val>511) return 10;
else if (val>255) return 9;
else if (val>127) return 8;
else if (val>63) return 7;
else if (val>31) return 6;
else if (val>15) return 5;
else if (val>7) return 4;
else if (val>3) return 3;
else if (val>1) return 2;
else if (val>0) return 1;
else return 0;
}
//从22*18矩阵压缩到8*8,6(4Y+U+V)
void mcugen(LINT8 x,LINT8 y)
{
LINT8 p_array[8][8];
LINT32 dct_array[8][8];
LINT32 q_array[8][8],diff;
LINT8 length,flag=0;
LINT8 AcCount=1,ZeroCount=0;
unsigned int i,j;
///////////////Y1亮度/////////////////////////////////////
for(i=0;i<8;i++)
for(j=0;j<8;j++)
p_array[i][j]=cha_y[x*16+j][y*16+i];
ffdct(&p_array[0][0],&dct_array[0][0]);
iztransfer(&dct_array[0][0]);
qdata(&dct_array[0][0],&q_array[0][0],5);
//DC编码
diff=q_array[0][0]-ydc;
ydc=q_array[0][0];
if(diff<0)
{
diff=-diff;
flag=1;
}
length=GetLength(diff);
if(flag==1)
{
diff=two[length]-1-diff;
}
PutDatatoJpegFile(dccoder[length],dcbit[length],&infile);
PutDatatoJpegFile(diff,length,&infile);
//AC编码
while(AcCount<64)
{
if (q_array[AcCount/8][AcCount%8]==0)
{
AcCount++;
ZeroCount++;
if (AcCount==64)
{
PutDatatoJpegFile(accoder[0][0],acbit[0][0],&infile);
}
}
else
{
while (ZeroCount>=16)
{
PutDatatoJpegFile(accoder[15][0],acbit[15][0],&infile);
ZeroCount-=16;
}
flag=0;
diff=q_array[AcCount/8][AcCount%8];
if (diff<0)
{
diff=-diff;
flag=1;
}
length=GetLength(diff);
if (flag==1)
{
diff=two[length]-1-diff;
}
PutDatatoJpegFile(accoder[ZeroCount][length],acbit[ZeroCount][length],&infile);
PutDatatoJpegFile(diff,length,&infile);
AcCount++;
ZeroCount=0;
}
}
//////////////////////////////Y2/////////////////////////////////////////
flag=0;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
p_array[i][j]=cha_y[x*16+8+j][y*16+i];
ffdct(&p_array[0][0],&dct_array[0][0]);
iztransfer(&dct_array[0][0]);
qdata(&dct_array[0][0],&q_array[0][0],5);
//DC编码
diff=q_array[0][0]-ydc;
ydc=q_array[0][0];
if(diff<0)
{
diff=-diff;
flag=1;
}
length=GetLength(diff);
if(flag==1)
{
diff=two[length]-1-diff;
}
PutDatatoJpegFile(dccoder[length],dcbit[length],&infile);
PutDatatoJpegFile(diff,length,&infile);
//AC编码
AcCount=1;ZeroCount=0;
while(AcCount<64)
{
if (q_array[AcCount/8][AcCount%8]==0)
{
AcCount++;
ZeroCount++;
if (AcCount==64)
{
PutDatatoJpegFile(accoder[0][0],acbit[0][0],&infile);
}
}
else
{
while (ZeroCount>=16)
{
PutDatatoJpegFile(accoder[15][0],acbit[15][0],&infile);
ZeroCount-=16;
}
flag=0;
diff=q_array[AcCount/8][AcCount%8];
if (diff<0)
{
diff=-diff;
flag=1;
}
length=GetLength(diff);
if (flag==1)
{
diff=two[length]-1-diff;
}
PutDatatoJpegFile(accoder[ZeroCount][length],acbit[ZeroCount][length],&infile);
PutDatatoJpegFile(diff,length,&infile);
AcCount++;
ZeroCount=0;
}
}
//////////////////////////////Y3/////////////////////////////////////////
flag=0;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
p_array[i][j]=cha_y[x*16+j][y*16+8+i];
ffdct(&p_array[0][0],&dct_array[0][0]);
iztransfer(&dct_array[0][0]);
qdata(&dct_array[0][0],&q_array[0][0],5);
//DC编码
diff=q_array[0][0]-ydc;
ydc=q_array[0][0];
if(diff<0)
{
diff=-diff;
flag=1;
}
length=GetLength(diff);
if(flag==1)
{
diff=two[length]-1-diff;
}
PutDatatoJpegFile(dccoder[length],dcbit[length],&infile);
PutDatatoJpegFile(diff,length,&infile);
//AC编码
AcCount=1;ZeroCount=0;
while(AcCount<64)
{
if (q_array[AcCount/8][AcCount%8]==0)
{
AcCount++;
ZeroCount++;
if (AcCount==64)
{
PutDatatoJpegFile(accoder[0][0],acbit[0][0],&infile);
}
}
else
{
while