📄 lencod.cpp
字号:
img->height = IMG_HEIGHT * scale_factor;
img->height_cr = IMG_HEIGHT_CR * scale_factor;
EimgY=(unsigned char *)malloc((img->width+IMG_PAD_SIZE)*(img->height+IMG_PAD_SIZE));
EimgY_org=(unsigned char *)malloc((img->width+IMG_PAD_SIZE)*(img->height+IMG_PAD_SIZE));
EimgUV[0]=(unsigned char *)malloc((img->width_cr+IMG_PAD_SIZE)*(img->height_cr+IMG_PAD_SIZE)+2048);
EimgUV[1]=(unsigned char *)malloc((img->width_cr+IMG_PAD_SIZE)*(img->height_cr+IMG_PAD_SIZE)+2048);
EimgUV_org[0]=(unsigned char *)malloc((img->width_cr+IMG_PAD_SIZE)*(img->height_cr+IMG_PAD_SIZE)+2048);
EimgUV_org[1]=(unsigned char *)malloc((img->width_cr+IMG_PAD_SIZE)*(img->height_cr+IMG_PAD_SIZE)+2048);
Emcef[0]=(unsigned char *)malloc((img->width_cr+IMG_PAD_SIZE)*(img->height_cr+IMG_PAD_SIZE)+2048);
Emcef[1]=(unsigned char *)malloc((img->width_cr+IMG_PAD_SIZE)*(img->height_cr+IMG_PAD_SIZE)+2048);
for(i=0;i<16;i++)
{
Eipol[i]=(unsigned char *)malloc((img->width+IMG_PAD_SIZE)*(img->height+IMG_PAD_SIZE)+2048);
}
offset =(img->width+IMG_PAD_SIZE)*(IMG_PAD_SIZE>>1)+(IMG_PAD_SIZE>>1);
coffset=(img->width_cr+IMG_PAD_SIZE)*(IMG_PAD_SIZE>>1)+(IMG_PAD_SIZE>>1);
imgY=EimgY+offset;
imgY_org=EimgY_org+offset;
imgUV[0]=EimgUV[0]+coffset;
imgUV[1]=EimgUV[1]+coffset;
imgUV_org[0]=EimgUV_org[0]+coffset;
imgUV_org[1]=EimgUV_org[1]+coffset;
mcef[0]=Emcef[0]+coffset;
mcef[1]=Emcef[1]+coffset;
for(i=0;i<16;i++)
{
ipol[i]=Eipol[i]+offset;
}
sch[3].offset=-img->width-IMG_PAD_SIZE;
sch[6].offset=-img->width-IMG_PAD_SIZE;
sch[8].offset=-img->width-1-IMG_PAD_SIZE;
qsch[0][3].offset=-img->width-IMG_PAD_SIZE;
qsch[0][6].offset=-img->width-IMG_PAD_SIZE;
qsch[0][8].offset=-img->width-1-IMG_PAD_SIZE;
qsch[2][3].offset=-img->width-IMG_PAD_SIZE;
qsch[2][6].offset=-img->width-IMG_PAD_SIZE;
qsch[2][8].offset=-img->width-IMG_PAD_SIZE;
for(i=0;i<SAD_TABLE_SIZE;i++)
{
sad->offset = sad->dx + sad->dy*(img->width+IMG_PAD_SIZE);
sad++;
}
memcpy(sadTable,SAD_TABLES,SAD_TABLE_SIZE*sizeof(SAD_TABLE));
memcpy(search,sch,sizeof(Point)*9);
for(i=0;i<4;i++)
{
memcpy(qsearch[i],qsch[i],sizeof(Point)*9);
}
img->total_number_mb = (img->width/MB_BLOCK_SIZE)*(img->height/MB_BLOCK_SIZE);
/* Prediction mode is set to -1 outside the frame, indicating that no prediction can be made from this part*/
for (i=0; i < img->width/BLOCK_SIZE+1; i++)
{
img->ipredmode[i+1][0]=-1;
}
for (j=0; j < img->height/BLOCK_SIZE+1; j++)
{
img->ipredmode[0][j+1]=-1;
}
k=1;
for (l=1; l < 41; l++)
{
l2=l+l;
for (i=-l+1; i < l; i++)
{
for (j=-l; j < l+1; j += l2)
{
/* img->spiral_search contains the vector positions organized to perform a spiral search */
img->spiral_search[0][k]= i;
img->spiral_search[1][k]= j;
k++;
}
}
for (j=-l; j <= l; j++)
{
for (i=-l;i <= l; i += l2)
{
img->spiral_search[0][k]= i;
img->spiral_search[1][k]= j;
k++;
}
}
}
/* img->mv_bituse[] is the number of bits used for motion vectors.
It is used to add a portion to the SAD depending on bit usage in the motion search
*/
img->mv_bituse[0]=1;
ind=0;
for (i=0; i < 9; i++)
{
ii= 2*i + 3;
for (j=1; j <= (int)pow(2,i); j++)
{
ind++;
img->mv_bituse[ind]=ii;
}
}
for(j=0;j<72;j++)
{
intra_mb_pos[j][0]=1; intra_mb_pos[j][1]=1;
intra_mb_pos[j][2]=1; intra_mb_pos[j][3]=1;
intra_mb_pos1[j][0]=1; intra_mb_pos1[j][1]=1;
intra_mb_pos1[j][2]=1; intra_mb_pos1[j][3]=1;
}
img->i16offset = 0;/*yummy*/
}
void ReadImage(struct img_par *img)
{
int i;
int width=img->width+IMG_PAD_SIZE;
if(feof(p_in))
fseek( p_in, 0, SEEK_SET);
for(i=0;i<img->height;i++)
{
fread(imgY_org+i*width,1,img->width,p_in);
}
width=img->width_cr+IMG_PAD_SIZE;
for(i=0;i<img->height_cr;i++)
{
fread(imgUV_org[0]+i*width,1,img->width_cr,p_in);
}
for(i=0;i<img->height_cr;i++)
{
fread(imgUV_org[1]+i*width,1,img->width_cr,p_in);
}
}
void InitLookupTable()
{
int i;
for (i = 0; i < 256; i++) RGBYUV02990[i] = (float)0.2990 * i;
for (i = 0; i < 256; i++) RGBYUV05870[i] = (float)0.5870 * i;
for (i = 0; i < 256; i++) RGBYUV01140[i] = (float)0.1140 * i;
for (i = 0; i < 256; i++) RGBYUV01684[i] = (float)0.1684 * i;
for (i = 0; i < 256; i++) RGBYUV03316[i] = (float)0.3316 * i;
for (i = 0; i < 256; i++) RGBYUV04187[i] = (float)0.4187 * i;
for (i = 0; i < 256; i++) RGBYUV00813[i] = (float)0.0813 * i;
}
/*int RGB2YUV (int x_dim, int y_dim, void *bmp, void *y_out, void *u_out, void *v_out)
{
long i, j, size;
unsigned char *r, *g, *b;
unsigned char *y, *u, *v;
unsigned char *pu1, *pu2, *pv1, *pv2, *psu, *psv;
unsigned char *y_buffer, *u_buffer, *v_buffer;
unsigned char *sub_u_buf, *sub_v_buf;
size = x_dim * y_dim;
// allocate memory
y_buffer = (unsigned char *)y_out;
sub_u_buf =(unsigned char *)u_out;
sub_v_buf =(unsigned char *)v_out;
u_buffer = (unsigned char *)malloc(size * sizeof(unsigned char));
v_buffer = (unsigned char *)malloc(size * sizeof(unsigned char));
b = (unsigned char *)bmp;
y = y_buffer;
u = u_buffer;
v = v_buffer;
// convert RGB to YUV
for (j = 0; j < y_dim; j ++)
{
y = y_buffer + (y_dim - j - 1) * (x_dim+IMG_PAD_SIZE);
u = u_buffer + (y_dim - j - 1) * x_dim;
v = v_buffer + (y_dim - j - 1) * x_dim;
for (i = 0; i < x_dim; i ++) {
g = b + 1;
r = b + 2;
*y = (unsigned char)( RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]);
*u = (unsigned char)( (*b)/2 - RGBYUV01684[*r] - RGBYUV03316[*g] + 128);
*v = (unsigned char)( (*r)/2 - RGBYUV04187[*g] - RGBYUV00813[*b] + 128);
b += 3;
y ++;
u ++;
v ++;
}
}
// subsample UV
for (j = 0; j < (y_dim>>1); j ++)
{
psu = sub_u_buf + j * ((x_dim >>1) +IMG_PAD_SIZE);
psv = sub_v_buf + j * ((x_dim >>1) +IMG_PAD_SIZE);
pu1 = u_buffer + 2 * j * x_dim;
pu2 = u_buffer + (2 * j + 1) * x_dim;
pv1 = v_buffer + 2 * j * x_dim;
pv2 = v_buffer + (2 * j + 1) * x_dim;
for (i = 0; i < (x_dim>>1); i ++)
{
*psu = (*pu1 + *(pu1+1) + *pu2 + *(pu2+1)) >>2;
*psv = (*pv1 + *(pv1+1) + *pv2 + *(pv2+1)) >>2;
psu ++;
psv ++;
pu1 += 2;
pu2 += 2;
pv1 += 2;
pv2 += 2;
}
}
free(u_buffer);
free(v_buffer);
return 0;
}*/
RGB2YUV (int x_dim, int y_dim, void *bmp, void *y_out, void *u_out, void *v_out)
{
long i, j, sub_x_dim, sub_y_dim;//, size;
unsigned char *r, *g, *b;
unsigned char *y, *u, *v;
unsigned char temp_u, temp_v;
unsigned char *y_buffer, *u_buffer, *v_buffer;
sub_x_dim = x_dim>>1;
sub_y_dim = y_dim>>1;
// allocate memory
y_buffer = (unsigned char *)y_out;
u_buffer =(unsigned char *)u_out;
v_buffer =(unsigned char *)v_out;
b = (unsigned char *)bmp;
y = y_buffer;
u = u_buffer;
v = v_buffer;
// convert RGB to YUV
for (j = 0; j < sub_y_dim; j ++)
{
//奇数行
y = y_buffer + (y_dim - 2*j - 1) * (x_dim+IMG_PAD_SIZE);
u = u_buffer + (sub_y_dim - j - 1) * (sub_x_dim+IMG_PAD_SIZE);
v = v_buffer + (sub_y_dim - j - 1) * (sub_x_dim+IMG_PAD_SIZE);
for (i = 0; i < x_dim; i ++) {
g = b + 1;
r = b + 2;
*y = (unsigned char)( RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]);
temp_u = (unsigned char)( (*b)/2 - RGBYUV01684[*r] - RGBYUV03316[*g] + 128);
temp_v = (unsigned char)( (*r)/2 - RGBYUV04187[*g] - RGBYUV00813[*b] + 128);
b += 3;
y ++;
i++;
g = b + 1;
r = b + 2;
*y = (unsigned char)( RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]);
*u = (temp_u +(unsigned char)( (*b)/2 - RGBYUV01684[*r] - RGBYUV03316[*g] + 128))>>2;
*v = (temp_v +(unsigned char)( (*r)/2 - RGBYUV04187[*g] - RGBYUV00813[*b] + 128))>>2;
b += 3;
y ++; u++; v++;
}
//偶数行
y = y_buffer + (y_dim - 2*j - 2) * (x_dim+IMG_PAD_SIZE);
u = u_buffer + (sub_y_dim - j - 1) * (sub_x_dim+IMG_PAD_SIZE);
v = v_buffer + (sub_y_dim - j - 1) * (sub_x_dim+IMG_PAD_SIZE);
for (i = 0; i < x_dim; i ++) {
g = b + 1;
r = b + 2;
*y = (unsigned char)( RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]);
temp_u = (unsigned char)( (*b)/2 - RGBYUV01684[*r] - RGBYUV03316[*g] + 128);
temp_v = (unsigned char)( (*r)/2 - RGBYUV04187[*g] - RGBYUV00813[*b] + 128);
b += 3;
y ++;
i++;
g = b + 1;
r = b + 2;
*y = (unsigned char)( RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]);
*u = *u + ((temp_u + (unsigned char)( (*b)/2 - RGBYUV01684[*r] - RGBYUV03316[*g] + 128))>>2);
*v = *v + ((temp_v + (unsigned char)( (*r)/2 - RGBYUV04187[*g] - RGBYUV00813[*b] + 128))>>2);
b += 3;
y ++; u++; v++;
}
}
return 0;
}
unsigned char dispY[352*288],dispU[176*144],dispV[176*144];
void WriteImage(struct img_par *img)
{
int i;
int width=img->width+IMG_PAD_SIZE;
for(i=0;i<img->height;i++)
{
fwrite(imgY+i*width,1,img->width,p_dec);//ipol[2]
memcpy(dispY+i*img->width,imgY+i*width,img->width);
}
width=img->width_cr+IMG_PAD_SIZE;
for(i=0;i<img->height_cr;i++)
{
fwrite(imgUV[0]+i*width,1,img->width_cr,p_dec);
memcpy(dispU+i*img->width_cr,imgUV[0]+i*width,img->width_cr);
}
for(i=0;i<img->height_cr;i++)
{
fwrite(imgUV[1]+i*width,1,img->width_cr,p_dec);
memcpy(dispV+i*img->width_cr,imgUV[1]+i*width,img->width_cr);
}
displayImage ((unsigned char*)dispY,(unsigned char*) dispV,(unsigned char*) dispU);
}
void ShowImage(struct img_par *img)
{
int i;
int width=img->width+IMG_PAD_SIZE;
for(i=0;i<img->height;i++)
{
memcpy(dispY+i*img->width,imgY+i*width,img->width);
}
width=img->width_cr+IMG_PAD_SIZE;
for(i=0;i<img->height_cr;i++)
{
memcpy(dispU+i*img->width_cr,imgUV[0]+i*width,img->width_cr);
}
for(i=0;i<img->height_cr;i++)
{
memcpy(dispV+i*img->width_cr,imgUV[1]+i*width,img->width_cr);
}
displayImage ((unsigned char*)dispY,(unsigned char*) dispV,(unsigned char*) dispU);
}
void CAVLC_init(struct img_par *img)
{
int i, j, k, l;
for (i=0;i < img->width>>4; i++)
for (j=0; j < img->height>>4; j++)
for (k=0;k<4;k++)
for (l=0;l<6;l++)
img->nz_coeff[i][j][k][l]=-1;
}
#define ZEROBYTES_SHORTSTARTCODE 2
unsigned char NAL_Payload_buffer[28000];
int RBSPtoEBSP(byte *streamBuffer, int begin_bytepos, int end_bytepos)
{
int i, j, count;
for(i = begin_bytepos; i < end_bytepos; i++)
NAL_Payload_buffer[i] = streamBuffer[i];
count = 0;
j = begin_bytepos;
for(i = begin_bytepos; i < end_bytepos; i++)
{
if(count == ZEROBYTES_SHORTSTARTCODE && !(NAL_Payload_buffer[i] & 0xFC))
{
streamBuffer[j] = 0x03;
j++;
count = 0;
}
streamBuffer[j] = NAL_Payload_buffer[i];
if(NAL_Payload_buffer[i] == 0x00)
count++;
else
count = 0;
j++;
}
return j;
}
void writeOnePacket(unsigned char *pBuff, int nBytes)
{
int nBytesCanWrite;
int nTailLen;
if(m_pRead == m_pWrite)
{
nBytesCanWrite = 10240;
}
else if(m_pRead > m_pWrite)
{
nBytesCanWrite = m_pRead - m_pWrite;
}
else
{
nBytesCanWrite = 10240 - (m_pWrite - m_pRead);
}
if(nBytesCanWrite < nBytes)
return ;
if(m_pWrite < m_pRead)
{
CopyMemory(m_pWrite,pBuff,nBytes);
m_pWrite += nBytes;
}
else
{
nTailLen = m_pEnd - m_pWrite;
if(nBytes <= nTailLen)
{
CopyMemory(m_pWrite,pBuff,nBytes);
m_pWrite += nBytes;
}
else
{
CopyMemory(m_pWrite,pBuff,nTailLen);
m_pWrite = m_pHead;
CopyMemory(m_pWrite,pBuff+nTailLen,nBytes-nTailLen);
m_pWrite += (nBytes-nTailLen);
}
}
if(m_pWrite >= m_pEnd)
m_pWrite = m_pHead;
m_nTotalBytes += nBytes;
}
bool getOnePacket(char *buffer)
{
int nBlock=0;
int nBytesCanRead;
int nBytes = 512;
if(m_nTotalBytes<512 )
return FALSE;
if(m_pRead == m_pWrite)
nBytesCanRead = 0;
else if(m_pRead > m_pWrite)
nBytesCanRead = 10240 - (m_pRead-m_pWrite);
else
nBytesCanRead =m_pWrite - m_pRead;
if(m_pWrite > m_pRead)
{
CopyMemory(buffer,m_pRead,512);
}
else
{
int nTailLen = m_pEnd - m_pRead;
if(nBytes <= nTailLen)
CopyMemory(buffer,m_pRead,512);
else
{
CopyMemory(buffer,m_pRead,nTailLen);
m_pRead = m_pHead;
nBytes -= nTailLen;
CopyMemory(buffer+nTailLen,m_pRead,nBytes);
}
}
m_pRead += nBytes;
if(m_pRead >= m_pEnd)
m_pRead = m_pHead;
m_nTotalBytes -= 512;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -