📄 image.c
字号:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
void get_block(int ref_frame,int x_pos, int y_pos, struct img_par *img, int block[8][8], unsigned char **ref_pic)
{
int dx, dy;
int x, y;
int i, j;
int maxold_x,maxold_y;
int result;
int tmp_res[26][26];
int tmp_res_2[26][26];
static const int COEF_HALF[4] = {
-1, 5, 5, -1
};
static const int COEF_QUART[4] = {
1, 7, 7, 1
};
// A a 1 b B
// c d e f
// 2 h 3 i
// j k l m
// C D
dx = x_pos&3;
dy = y_pos&3;
x_pos = (x_pos-dx)/4;
y_pos = (y_pos-dy)/4;
maxold_x = img->width-1;
maxold_y = img->height-1;
if (dx == 0 && dy == 0) { //fullpel position: A
for (j = 0; j < B8_SIZE; j++)
for (i = 0; i < B8_SIZE; i++)
block[i][j] = ref_pic[max(0,min(maxold_y,y_pos+j))][max(0,min(maxold_x,x_pos+i))];
}
else
{ /* other positions */
if((dx==2) && (dy==0)){//horizonal 1/2 position: 1
for (j = 0; j < B8_SIZE; j++){
for (i = 0; i < B8_SIZE; i++) {
for (result = 0, x = -1; x < 3; x++)
result += ref_pic[max(0,min(maxold_y,y_pos+j))][max(0,min(maxold_x,x_pos+i+x))]*COEF_HALF[x+1];
block[i][j] = max(0, min(255, (result+4)/8));
}
}
}
else if(((dx==1) || (dx==3)) && (dy==0)){//horizonal 1/4 position: a and b
for (j = 0; j < B8_SIZE; j++) {
for (i = -1; i < B8_SIZE+1; i++) {
for (result = 0, x = -1; x < 3; x++)
result += ref_pic[max(0,min(maxold_y,y_pos+j))][max(0,min(maxold_x,x_pos+i+x))]*COEF_HALF[x+1];
tmp_res[j][2*(i+1)] = result;
tmp_res[j][2*(i+1)+1] = ref_pic[max(0,min(maxold_y,y_pos+j))][max(0,min(maxold_x,x_pos+i+1))]*8;
}
}
for (j = 0; j < B8_SIZE; j++) {
for (i = 0; i < B8_SIZE; i++) {
for (result = 0, x = -1; x < 3; x++)
if(dx==1)//a
result += tmp_res[j][2*i+x+1]*COEF_QUART[x+1];
else//b
result += tmp_res[j][2*i+x+2]*COEF_QUART[x+1];
block[i][j] = max(0, min(255, (result+64)/128));
}
}
}
else if((dy==2) && (dx==0)){//vertical 1/2 position: 2
for (j = 0; j < B8_SIZE; j++) {
for (i = 0; i < B8_SIZE; i++) {
for (result = 0, y = -1; y < 3; y++)
result += ref_pic[max(0,min(maxold_y,y_pos+j+y))][max(0,min(maxold_x,x_pos+i))]*COEF_HALF[y+1];
block[i][j] = max(0, min(255, (result+4)/8));
}
}
}
else if(((dy==1) || (dy==3)) && (dx==0)){//vertical 1/4 position: c and j
for (j = -1; j < B8_SIZE+1; j++) {
for (i = 0; i < B8_SIZE; i++) {
for (result = 0, y = -1; y < 3; y++)
result += ref_pic[max(0,min(maxold_y,y_pos+j+y))][max(0,min(maxold_x,x_pos+i))]*COEF_HALF[y+1];
tmp_res[2*(j+1)][i] = result;
tmp_res[2*(j+1)+1][i] = ref_pic[max(0,min(maxold_y,y_pos+j+1))][max(0,min(maxold_x,x_pos+i))]*8;
}
}
for (j = 0; j < B8_SIZE; j++) {
for (i = 0; i < B8_SIZE; i++) {
for (result = 0, y = -1; y < 3; y++)
if(dy==1)//c
result += tmp_res[2*j+y+1][i]*COEF_QUART[y+1];
else//j
result += tmp_res[2*j+y+2][i]*COEF_QUART[y+1];
block[i][j] = max(0, min(255, (result+64)/128));
}
}
}
else if((dx==2) && (dy==2)){//horizonal and vertical 1/2 position: 3
for (j = -1; j < B8_SIZE+2; j++) {
for (i = 0; i < B8_SIZE; i++) {
for (result = 0, x = -1; x < 3; x++)
result += ref_pic[max(0,min(maxold_y,y_pos+j))][max(0,min(maxold_x,x_pos+i+x))]*COEF_HALF[x+1];
tmp_res[j+1][i] = result;
}
}
for (j = 0; j < B8_SIZE; j++) {
for (i = 0; i < B8_SIZE; i++) {
for (result = 0, y = -1; y < 3; y++)
result += tmp_res[j+y+1][i]*COEF_HALF[y+1];
block[i][j] = max(0, min(255, (result+32)/64));
}
}
}
else if(((dx==1) || (dx==3)) && dy==2){//horizonal and vertical 1/4 position: h and i
for (j = 0; j < B8_SIZE; j++) {
for (i = -2; i < B8_SIZE+3; i++) {
for (result = 0, y = -1; y < 3; y++)
result += ref_pic[max(0,min(maxold_y,y_pos+j+y))][max(0,min(maxold_x,x_pos+i))]*COEF_HALF[y+1];
tmp_res[j][i+2] = result;
}
}
for (j = 0; j < B8_SIZE; j++) {
for (i = 0; i < B8_SIZE+2; i++) {
for (result = 0, x = -1; x < 3; x++)
result += tmp_res[j][i+1+x]*COEF_HALF[x+1];
tmp_res_2[j][2*i] = result;
tmp_res_2[j][2*i+1] = tmp_res[j][i+2]*8;
}
}
for (j = 0; j < B8_SIZE; j++) {
for (i = 0; i < B8_SIZE; i++) {
for (result = 0, x = -1; x < 3; x++)
if(dx==1)//h
result += tmp_res_2[j][2*i+x+1]*COEF_QUART[x+1];
else
result += tmp_res_2[j][2*i+x+2]*COEF_QUART[x+1];
block[i][j] = max(0, min(255, (result+512)/1024));
}
}
}
else if(((dy==1) || (dy==3)) && (dx==2)){//vertical and horizonal 1/4 position: e and l
for (j = -2; j < B8_SIZE+3; j++) {
for (i = 0; i < B8_SIZE; i++) {
for (result = 0, x = -1; x < 3; x++)
result += ref_pic[max(0,min(maxold_y,y_pos+j))][max(0,min(maxold_x,x_pos+i+x))]*COEF_HALF[x+1];
tmp_res[j+2][i] =result;
}
}
for (j = 0; j < B8_SIZE+2; j++) {
for (i = 0; i < B8_SIZE; i++) {
for (result = 0, y = -1; y < 3; y++)
result += tmp_res[j+y+1][i]*COEF_HALF[y+1];
tmp_res_2[2*j][i] = result;
tmp_res_2[2*j+1][i] = tmp_res[j+2][i]*8;
}
}
for (j = 0; j < B8_SIZE; j++) {
for (i = 0; i < B8_SIZE; i++) {
for (result = 0, y = -1; y < 3; y++)
if(dy==1)//e
result += tmp_res_2[2*j+y+1][i]*COEF_QUART[y+1];
else//l
result += tmp_res_2[2*j+y+2][i]*COEF_QUART[y+1];
block[i][j] = max(0, min(255, (result+512)/1024));
}
}
}
else{//Diagonal 1/4 position : d, f, k and m
for (j = -1; j < B8_SIZE+2; j++) {
for (i = 0; i < B8_SIZE; i++) {
for (result = 0, x = -1; x < 3; x++)
result += ref_pic[max(0,min(maxold_y,y_pos+j))][max(0,min(maxold_x,x_pos+i+x))]*COEF_HALF[x+1];
tmp_res[j+1][i] = result;
}
}
for (j = 0; j < B8_SIZE; j++) {
for (i = 0; i < B8_SIZE; i++) {
for (result = 0, y = -1; y < 3; y++)
result += tmp_res[j+y+1][i]*COEF_HALF[y+1];
tmp_res_2[j][i] = result;
}
}
for (j = 0; j < B8_SIZE; j++) {
for (i = 0; i < B8_SIZE; i++) {
if((dx==1) && (dy==1))//d
result = tmp_res_2[j][i]+ref_pic[max(0,min(maxold_y,y_pos+j))][max(0,min(maxold_x,x_pos+i))]*64;
else if((dx==3) && (dy==1))//f
result = tmp_res_2[j][i]+ref_pic[max(0,min(maxold_y,y_pos+j))][max(0,min(maxold_x,x_pos+i+1))]*64;
else if((dx==1) && (dy==3))//k
result = tmp_res_2[j][i]+ref_pic[max(0,min(maxold_y,y_pos+j+1))][max(0,min(maxold_x,x_pos+i))]*64;
else if((dx==3) && (dy==3))//m
result = tmp_res_2[j][i]+ref_pic[max(0,min(maxold_y,y_pos+j+1))][max(0,min(maxold_x,x_pos+i+1))]*64;
block[i][j] = max(0, min(255, (result+64)/128));
}
}
}
}
}
/*
*************************************************************************
* Function:Reads new slice from bit_stream
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
int Header()
{
unsigned char *Buf;
int startcodepos,length;
if ((Buf = (char*)calloc (MAX_CODED_FRAME_SIZE , sizeof(char))) == NULL)
no_mem_exit("GetAnnexbNALU: Buf");
while (1)
{
StartCodePosition = GetOneUnit(Buf,&startcodepos,&length); //jlzheng 7.5
switch(Buf[startcodepos])
{
case SEQUENCE_HEADER_CODE:
SequenceHeader(Buf,startcodepos,length);
break;
case EXTENSION_START_CODE:
extension_data(Buf,startcodepos,length);
break;
case USER_DATA_START_CODE:
user_data(Buf,startcodepos,length);
break;
case VIDEO_EDIT_CODE:
video_edit_code_data(Buf,startcodepos,length);
break;
case I_PICTURE_START_CODE:
I_Picture_Header(Buf,startcodepos,length);
calc_picture_distance(img);
free(Buf);
return SOP;
case PB_PICTURE_START_CODE:
PB_Picture_Header(Buf,startcodepos,length);
calc_picture_distance(img);
free(Buf);
return SOP;
case SEQUENCE_END_CODE:
free(Buf);
return EOS;
break;
default:
if (Buf[startcodepos]>=SLICE_START_CODE_MIN && Buf[startcodepos]<=SLICE_START_CODE_MAX) {
break;
} else {
printf("Can't find start code");
free(Buf);
return EOS;
}
}
}
}
/*
*************************************************************************
* Function:Initializes the parameters for a new frame
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
void init_frame(struct img_par *img, struct inp_par *inp)
{
static int first_P = TRUE;
int i,j;
img->top_bot = -1; // Yulj 2004.07.20
last_P_no = last_P_no_frm;
nextP_tr = nextP_tr_frm;
if (img->number == 0) // first picture
{
nextP_tr=prevP_tr=img->tr;
}
else if(img->type == I_IMG || img->type == P_IMG )
{
nextP_tr=img->tr;
if(first_P) // first P picture
{
first_P = FALSE;
P_interval=nextP_tr-prevP_tr; //! TO 4.11.2001 we get problems here in case the first P-Frame was lost
}
//added by Zheng Xiaozhen, HiSilicon, 2007.03.21 Begin
if (p_ref)
find_snr(snr,img,p_ref); // if ref sequence exist
if(pre_img_type == I_IMG) // I picture
fprintf(stdout,"%3d(I) %3d %5d %7.4f %7.4f %7.4f %5d\n",
FrameNum, pre_img_tr, pre_img_qp,snr->snr_y,snr->snr_u,snr->snr_v,pre_tmp_time);
else if(pre_img_type == P_IMG) // P pictures
{
if (pre_img_types == I_IMG)
fprintf(stdout,"%3d(I) %3d %5d %7.4f %7.4f %7.4f %5d\n",
FrameNum, pre_img_tr, pre_img_qp,snr->snr_y,snr->snr_u,snr->snr_v,pre_tmp_time); //jlzheng 7.3
else
fprintf(stdout,"%3d(P) %3d %5d %7.4f %7.4f %7.4f %5d\n",
FrameNum, pre_img_tr, pre_img_qp,snr->snr_y,snr->snr_u,snr->snr_v,pre_tmp_time);
}
report_frame(snr,pre_tmp_time);
//added by Zheng Xiaozhen, HiSilicon, 2007.03.21 End
write_prev_Pframe(img, p_out); // imgY_prev, imgUV_prev -> file
}
// allocate memory for frame buffers
if (img->number == 0)
{
init_global_buffers(inp, img);
}
for(i=0;i<img->width/(2*BLOCK_SIZE)+1;i++) // set edge to -1, indicate nothing to predict from
{
img->ipredmode[i+1][0]=-1;
img->ipredmode[i+1][img->height/(2*BLOCK_SIZE)+1]=-1;
}
for(j=0;j<img->height/(2*BLOCK_SIZE)+1;j++)
{
img->ipredmode[0][j+1]=-1;
img->ipredmode[img->width/(2*BLOCK_SIZE)+1][j+1]=-1;
}
img->ipredmode [0][0]=-1;
for(i=0; i<img->max_mb_nr; i++)
{
mb_data[i].slice_nr = -1;
}
//luma
mref[0] = mref_frm[0]; //mref[ref_index][yuv]height(height/2)][width] ref_index=0,1 for P frame, ref_index = 0,1,2,3 for P field
mref[1] = mref_frm[1];
mref_fref[0] = mref_fref_frm[0]; //mref_fref[ref_index][yuv]height(height/2)][width] ref_index=0 for B frame, ref_index = 0,1 for B field
mref_bref[0] = mref_bref_frm[0];
//chroma
mcef[0][0] = mcef_frm[0][0]; //mcef[ref_index][uv][height][width]
mcef[0][1] = mcef_frm[0][1];
mcef[1][0] = mcef_frm[1][0];
mcef[1][1] = mcef_frm[1][1];
mcef_fref[0][0] = mcef_fref_frm[0][0]; //mcef_fref[ref_index][uv][height/2][width]
mcef_fref[0][1] = mcef_fref_frm[0][1];
mcef_bref[0][0] = mcef_bref_frm[0][0];
mcef_bref[0][1] = mcef_bref_frm[0][1];
imgY = imgY_frm = current_frame[0];
imgUV = imgUV_frm = ¤t_frame[1];
img->mv = img->mv_frm;
refFrArr = refFrArr_frm;
img->fw_refFrArr = img->fw_refFrArr_frm;
img->bw_refFrArr = img->bw_refFrArr_frm;
}
void init_frame_buffer()
{
//luma
mref[0] = mref_frm[0]; //mref[ref_index][yuv]height(height/2)][width] ref_index=0,1 for P frame, ref_index = 0,1,2,3 for P field
mref[1] = mref_frm[1];
mref_fref[0] = mref_fref_frm[0]; //mref_fref[ref_index][yuv]height(height/2)][width] ref_index=0 for B frame, ref_index = 0,1 for B field
mref_bref[0] = mref_bref_frm[0];
//chroma
mcef[0][0] = mcef_frm[0][0]; //mcef[ref_index][uv][height][width]
mcef[0][1] = mcef_frm[0][1];
mcef[1][0] = mcef_frm[1][0];
mcef[1][1] = mcef_frm[1][1];
mcef_fref[0][0] = mcef_fref_frm[0][0]; //mcef_fref[ref_index][uv][height/2][width]
mcef_fref[0][1] = mcef_fref_frm[0][1];
mcef_bref[0][0] = mcef_bref_frm[0][0];
mcef_bref[0][1] = mcef_bref_frm[0][1];
imgY_frm = current_frame[0];
imgUV_frm = ¤t_frame[1];
}
void init_top(struct img_par *img, struct inp_par *inp)
{
int i,j;
img->top_bot = 0; // Yulj 2004.07.20
for(i=0;i<img->width/(2*BLOCK_SIZE)+1;i++) // set edge to -1, indicate nothing to predict from
{
img->ipredmode[i+1][0]=-1;
img->ipredmode[i+1][img->height/(2*BLOCK_SIZE)+1]=-1;
}
for(j=0;j<img->height/(2*BLOCK_SIZE)+1;j++)
{
img->ipredmode[0][j+1]=-1;
img->ipredmode[img->width/(2*BLOCK_SIZE)+1][j+1]=-1;
}
img->ipredmode [0][0]=-1;
for(i=0; i<img->max_mb_nr; i++)
{
mb_data[i].slice_nr = -1;
}
//luma
for (i=0;i<4;i++)
{
mref[i] = mref_fld[i]; //mref[ref_index][yuv]height(height/2)][width] ref_index=0,1 for P frame, ref_index = 0,1,2,3 for P field
}
//chroma
for(j=0;j<4;j++)
for(i=0;i<2;i++)
{
mcef[j][i] = mcef_fld[j][i]; //mcef[ref_index][uv][height][width]
}
//forward/backward
for (i=0;i<2;i++)
{
mref_fref[i] = mref_fref_fld[i]; //mref_fref[ref_index][yuv]height(height/2)][width] ref_index=0 for B frame, ref_index = 0,1 for B field
mref_bref[i] = mref_bref_fld[i];
}
for(j=0;j<2;j++)
for(i=0;i<2;i++)
{
mcef_fref[j][i] = mcef_fref_fld[j][i]; //mcef_fref[ref_index][uv][height/2][width]
mcef_bref[j][i] = mcef_bref_fld[j][i];
}
imgY = imgY_top = current_field[0];
imgUV = imgUV_top = ¤t_field[1];
img->mv = img->mv_top;
refFrArr = refFrArr_top;
img->fw_refFrArr = img->fw_refFrArr_top;
img->bw_refFrArr = img->bw_refFrArr_top;
}
void init_top_buffer()
{
int i,j;
//luma
for (i=0;i<4;i++)
{
mref[i] = mref_fld[i]; //mref[ref_index][yuv]height(height/2)][width] ref_index=0,1 for P frame, ref_index = 0,1,2,3 for P field
}
//chroma
for(j=0;j<4;j++)
for(i=0;i<2;i++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -