📄 bitstream.h
字号:
#ifndef _BITSTREAM_H_
#define _BITSTREAM_H_
//static unsigned short mask[16]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
// //用于读取bytenew的特定位
typedef struct
{
unsigned char* h;
unsigned char* t;
unsigned int bytenew;
unsigned int bytepos;
}bitstream_t;
bitstream_t stream;
bitstream_init(unsigned char* s)
{
stream.h = s;
stream.t = s;
stream.bytenew = 0;
stream.bytepos = 7;
}
int bitstream_getlen()
{
return stream.t - stream.h;
}
//写1字节到文件
void WriteByte(unsigned char val)
{
*stream.t++ = val;
}
bitstream_pad2byte()
{
if(stream.bytepos != 7)
{
stream.bytenew |= (1 << (stream.bytepos+1)) - 1;
WriteByte(stream.bytenew);
stream.bytepos=7;
stream.bytenew=0;
}
}
//********************************************************************
// 方法名称:WriteBitsStream
//
// 方法说明:写入二进制流
//
// 参数说明:
// value:需要写入的值
// codeLen:二进制长度
//********************************************************************
/*
void WriteBitsStream(unsigned short value,unsigned char codeLen)
{
char posval;//bit position in the bitstring we read, should be<=15 and >=0
posval=codeLen-1;
while (posval>=0)
{
if (value & mask[posval])
{
stream.bytenew |= mask[stream.bytepos];
}
posval--;stream.bytepos--;
if (stream.bytepos<0)
{
if (stream.bytenew==0xFF)
{
//WriteByte(0xFF); //一般码字如出现0xFF,后面则填充0,以区别标记码
//WriteByte(0);
*stream.t++ = 0xFF;
*stream.t++ = 0;
}
else
{
//WriteByte(stream.bytenew);
*stream.t++ = stream.bytenew;
}
stream.bytepos=7;stream.bytenew=0;
}
}
}
void WriteBitsStream(unsigned short value,unsigned char codeLen)
{
int i;
unsigned int v = value & (1<<codeLen)-1;
int total_len = 7-stream.bytepos+codeLen;
v <<= stream.bytepos + 1;
for(i = 0; i < (7-stream.bytepos+codeLen)/8; i++)
{
}
posval=codeLen-1;
while (posval>=0)
{
if (value & mask[posval])
{
stream.bytenew |= mask[stream.bytepos];
}
posval--;stream.bytepos--;
if (stream.bytepos<0)
{
if (stream.bytenew==0xFF)
{
//WriteByte(0xFF); //一般码字如出现0xFF,后面则填充0,以区别标记码
//WriteByte(0);
*stream.t++ = 0xFF;
*stream.t++ = 0;
}
else
{
//WriteByte(stream.bytenew);
*stream.t++ = stream.bytenew;
}
stream.bytepos=7;stream.bytenew=0;
}
}
}
*/
//**********************************************************************
//写入码流时调用的函数
void WriteSOI(void)
{
memcpy(stream.t, &SOITAG, sizeof(SOITAG));
stream.t += sizeof(SOITAG);
}
void WriteAPP0(void)
{
JPEGAPP0 APP0;
char* p = (char*)&APP0;
*p++ = 0xFF;
*p++ = 0xE0;
*p++ = 0x00;
*p++ = 0x10;
*p++ = 'J';
*p++ = 'F';
*p++ = 'I';
*p++ = 'F';
*p++ = 0x0;
*p++ = 0x01;
*p++ = 0x01;
*p++ = 0x00;
*p++ = 0x00;
*p++ = 0x01;
*p++ = 0x00;
*p++ = 0x01;
*p++ = 0x00;
*p++ = 0x00;
/* APP0.segmentTag = 0xE0FF;
APP0.length = 0x1000;
APP0.id[0] = 'J';
APP0.id[1] = 'F';
APP0.id[2] = 'I';
APP0.id[3] = 'F';
APP0.id[4] = 0;
APP0.ver = 0x0101;
APP0.densityUnit = 0x00;
APP0.densityX = 0x0100;
APP0.densityY = 0x0100;
APP0.thp = 0x00;
APP0.tvp = 0x00;
*/
memcpy(stream.t, &APP0, sizeof(APP0));
stream.t += sizeof(APP0);
}
void WriteDQT(void)
{
unsigned int i = 0;
JPEGDQT_8BITS DQT_Y;
DQT_Y.segmentTag = 0xDBFF;
DQT_Y.length = 0x4300;
DQT_Y.tableInfo = 0x00;
for (i = 0; i < DCTBLOCKSIZE; i++)
{
DQT_Y.table[FZBT[i]] = YQT[i];
}
memcpy(stream.t, &DQT_Y, sizeof(DQT_Y));
stream.t += sizeof(DQT_Y);
DQT_Y.tableInfo = 0x01;
for (i = 0; i < DCTBLOCKSIZE; i++)
{
DQT_Y.table[FZBT[i]] = UVQT[i];
}
memcpy(stream.t, &DQT_Y, sizeof(DQT_Y));
stream.t += sizeof(DQT_Y);
}
void WriteSOF(int w, int h)
{
JPEGSOF0_24BITS SOF;
char* p = (char*)&SOF;
*p++ = 0xFF;
*p++ = 0xC0;
*p++ = 0x00;
*p++ = 0x11;
*p++ = 0x08;
*p++ = h >> 8;
*p++ = h;
*p++ = w >> 8;
*p++ = w;
*p++ = 0x03;
*p++ = 0x01;
*p++ = 0x22;
*p++ = 0x00;
*p++ = 0x02;
*p++ = 0x11;
*p++ = 0x01;
*p++ = 0x03;
*p++ = 0x11;
*p++ = 0x01;
/*
unsigned short segmentTag; //SOF标记码长度,必须为0xFFC0
unsigned short length; //SOF0长度,24bits为17
unsigned char precision; //精度,每个信号分量所用的位数,基本系统为0x08
unsigned short height; //图像高度
unsigned short width; //图像宽度
unsigned char sigNum; //信号数量,24 bits JPEG应该为3,8 bits为1
unsigned char YID; //信号编号,亮度Y
unsigned char HVY; //采样方式,0-3位是垂直采样,4-7位是水平采样
unsigned char QTY; //亮度Y对应的量化表号
unsigned char UID; //信号编号,色差U
unsigned char HVU; //采样方式,0-3位是垂直采样,4-7位是水平采样
unsigned char QTU; //色差U对应的量化表号
unsigned char VID; //信号编号,色差V
unsigned char HVV; //采样方式,0-3位是垂直采样,4-7位是水平采样
unsigned char QTV; //色差V对应的量化表号
SOF.segmentTag = 0xC0FF;
SOF.length = 0x1100;
SOF.precision = 0x08;
SOF.height = Intel2Moto((unsigned short)(h));
SOF.width = Intel2Moto((unsigned short)(w));
SOF.sigNum = 0x03;
SOF.YID = 0x01;
// SOF.HVY = 0x11;
SOF.HVY = 0x22;
SOF.QTY = 0x00;
SOF.UID = 0x02;
SOF.HVU = 0x11;
SOF.QTU = 0x01;
SOF.VID = 0x03;
SOF.HVV = 0x11;
SOF.QTV = 0x01;
*/
memcpy(stream.t, &SOF, sizeof(SOF));
stream.t += sizeof(SOF);
}
void WriteDHT(void)
{
unsigned int i = 0;
JPEGDHT DHT;
DHT.segmentTag = 0xC4FF;
DHT.length = (19 + 12) << 8;
//------------------------------------------------
DHT.tableInfo = 0x00;
for (i = 0; i < 16; i++)
{
DHT.huffCode[i] = STD_DC_Y_NRCODES[i + 1];
}
memcpy(stream.t, &DHT, sizeof(DHT));
stream.t += sizeof(DHT);
for (i = 0; i <= 11; i++)
{
WriteByte(STD_DC_Y_VALUES[i]);
}
//------------------------------------------------
DHT.tableInfo = 0x01;
for (i = 0; i < 16; i++)
{
DHT.huffCode[i] = STD_DC_UV_NRCODES[i + 1];
}
memcpy(stream.t, &DHT, sizeof(DHT));
stream.t += sizeof(DHT);
for (i = 0; i <= 11; i++)
{
WriteByte(STD_DC_UV_VALUES[i]);
}
//-------------------------------------------------
DHT.length = (19 + 162) << 8;
DHT.tableInfo = 0x10;
for (i = 0; i < 16; i++)
{
DHT.huffCode[i] = STD_AC_Y_NRCODES[i + 1];
}
memcpy(stream.t, &DHT, sizeof(DHT));
stream.t += sizeof(DHT);
for (i = 0; i <= 161; i++)
{
WriteByte(STD_AC_Y_VALUES[i]);
}
//--------------------------------------------------
DHT.tableInfo = 0x11;
for (i = 0; i < 16; i++)
{
DHT.huffCode[i] = STD_AC_UV_NRCODES[i + 1];
}
memcpy(stream.t, &DHT, sizeof(DHT));
stream.t += sizeof(DHT);
for (i = 0; i <= 161; i++)
{
WriteByte(STD_AC_UV_VALUES[i]);
}
}
void WriteSOS(void)
{
JPEGSOS_24BITS SOS;
SOS.segmentTag = 0xDAFF;
SOS.length = 0x0C00;
SOS.sigNum = 0x03;
SOS.YID = 0x01;
SOS.HTY = 0x00;
SOS.UID = 0x02;
SOS.HTU = 0x11;
SOS.VID = 0x03;
SOS.HTV = 0x11;
SOS.Ss = 0x00;
SOS.Se = 0x3F;
SOS.Bf = 0x00;
memcpy(stream.t, &SOS, sizeof(SOS));
stream.t += sizeof(SOS);
}
void WriteEOI(void)
{
memcpy(stream.t, &EOITAG, sizeof(EOITAG));
stream.t += sizeof(EOITAG);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -