📄 lencod.c
字号:
free(stat->bit_use_mode_Bframe);
}
/*!
************************************************************************
* \brief
* Some matrices are initilized here.
* \par Input:
* none
* \par Output:
* none
************************************************************************
*/
void init()
{
int i, ii, ind, j, i2;
InitMotionVectorSearchModule();
/* 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;
}
}
// quad(0:255) SNR quad array
for (i=0; i < 256; ++i) // fix from TML1 / TML2 sw, truncation removed
{
i2=i*i;
img->quad[i]=i2;
}
// B pictures : img->blk_bitsue[] is used when getting bidirection SAD
for (i=0; i < 7; i++)
{
if(i==0) img->blk_bituse[i]=1;
else if(i==1 || i==2) img->blk_bituse[i]=3;
else img->blk_bituse[i]=5;
}
}
/*!
************************************************************************
* \brief
* Prints the header of the protocol.
* \par Input:
* struct inp_par *inp
* \par Output:
* none
************************************************************************
*/
void information_init()
{
printf("--------------------------------------------------------------------------\n");
printf(" Input YUV file : %s \n",input->infile);
printf(" Output H.26L bitstream : %s \n",input->outfile);
if (p_dec != NULL)
printf(" Output YUV file(debug) : %s \n",input->ReconFile);
printf(" Output log file : log.dat \n");
printf(" Output statistics file : stat.dat \n");
printf("--------------------------------------------------------------------------\n");
printf(" Frame Bit/pic QP SnrY SnrU SnrV Time(ms) IntraMBs\n");
}
/*!
************************************************************************
* \brief
* Dynamic memory allocation of frame size related global buffers
* buffers are defined in global.h, allocated memory must be freed in
* void free_global_buffers()
* \par Input:
* Input Parameters struct inp_par *inp, \n
* Image Parameters struct img_par *img
* \return Number of allocated bytes
************************************************************************
*/
int init_global_buffers()
{
int j,memory_size=0;
#ifdef _ADAPT_LAST_GROUP_
extern int *last_P_no;
if ((last_P_no = (int*)malloc(img->buf_cycle*sizeof(int))) == NULL)
no_mem_exit("init_global_buffers: last_P_no");
#endif
// allocate memory for encoding frame buffers: imgY, imgUV
// byte imgY[288][352];
// byte imgUV[2][144][176];
memory_size += get_mem2D(&imgY, img->height, img->width);
memory_size += get_mem3D(&imgUV, 2, img->height_cr, img->width_cr);
// allocate memory for reference frame buffers: imgY_org, imgUV_org
// byte imgY_org[288][352];
// byte imgUV_org[2][144][176];
memory_size += get_mem2D(&imgY_org, img->height, img->width);
memory_size += get_mem3D(&imgUV_org, 2, img->height_cr, img->width_cr);
// allocate memory for post filter frame buffers: imgY_pf, imgUV_pf
// byte imgY_pf[288][352];
// byte imgUV_pf[2][144][176];
memory_size += get_mem2D(&imgY_pf, img->height, img->width);
memory_size += get_mem3D(&imgUV_pf, 2, img->height_cr, img->width_cr);
// allocate memory for B frame coding: nextP_imgY, nextP_imgUV
// byte nextP_imgY[288][352];
// byte nextP_imgUV[2][144][176];
memory_size += get_mem2D(&nextP_imgY, img->height, img->width);
memory_size += get_mem3D(&nextP_imgUV, 2, img->height_cr, img->width_cr);
// allocate memory for multiple ref. frame buffers: mref, mcref
//byte mref[MAX_MULT_PRED][1152][1408]; */ /* 1/4 pix luma
//byte mcef[MAX_MULT_PRED][2][352][288]; */ /* pix chroma
// rows and cols for croma component mcef[ref][croma][4x][4y] are switched
// compared to luma mref[ref][4y][4x] for whatever reason
// number of reference frames increased by one for next P-frame
alloc_mref(img);
alloc_Refbuf (img);
if (NULL == (Refbuf11_P = malloc ((img->width * img->height) * sizeof (pel_t))))
no_mem_exit ("init_global_buffers: Refbuf11_P");
// allocate memory for B-frame coding (last upsampled P-frame): mref_P, mcref_P
// is currently also used in oneforthpix_1() and _2() for coding without B-frames
//byte mref[1152][1408]; */ /* 1/4 pix luma
//byte mcef[2][352][288]; */ /* pix chroma
memory_size += get_mem2D(&mref_P, (img->height+2*IMG_PAD_SIZE)*4, (img->width+2*IMG_PAD_SIZE)*4);
memory_size += get_mem3D(&mcef_P, 2, img->height_cr, img->width_cr);
if(input->successive_Bframe!=0)
{
// allocate memory for temp B-frame motion vector buffer: tmp_fwMV, tmp_bwMV, dfMV, dbMV
// int ...MV[2][72][92]; ([2][72][88] should be enough)
memory_size += get_mem3Dint(&tmp_fwMV, 2, img->height/BLOCK_SIZE, img->width/BLOCK_SIZE+4);
memory_size += get_mem3Dint(&tmp_bwMV, 2, img->height/BLOCK_SIZE, img->width/BLOCK_SIZE+4);
memory_size += get_mem3Dint(&dfMV, 2, img->height/BLOCK_SIZE, img->width/BLOCK_SIZE+4);
memory_size += get_mem3Dint(&dbMV, 2, img->height/BLOCK_SIZE, img->width/BLOCK_SIZE+4);
// allocate memory for temp B-frame motion vector buffer: fw_refFrArr, bw_refFrArr
// int ...refFrArr[72][88];
memory_size += get_mem2Dint(&fw_refFrArr, img->height/BLOCK_SIZE, img->width/BLOCK_SIZE);
memory_size += get_mem2Dint(&bw_refFrArr, img->height/BLOCK_SIZE, img->width/BLOCK_SIZE);
}
// allocate memory for temp P and B-frame motion vector buffer: tmp_mv, temp_mv_block
// int tmp_mv[2][72][92]; ([2][72][88] should be enough)
memory_size += get_mem3Dint(&tmp_mv, 2, img->height/BLOCK_SIZE, img->width/BLOCK_SIZE+4);
// allocate memory for temp quarter pel luma frame buffer: img4Y_tmp
// int img4Y_tmp[576][704]; (previously int imgY_tmp in global.h)
memory_size += get_mem2Dint(&img4Y_tmp, img->height+2*IMG_PAD_SIZE, (img->width+2*IMG_PAD_SIZE)*4);
// allocate memory for tmp loop filter frame buffers: imgY_tmp, imgUV_tmp
// byte imgY_tmp[288][352];
memory_size += get_mem2D(&imgY_tmp, img->height, img->width);
memory_size += get_mem3D(&imgUV_tmp, 2, img->height_cr, img->width_cr);
// allocate memory for reference frames of each block: refFrArr
// int refFrArr[72][88];
memory_size += get_mem2Dint(&refFrArr, img->height/BLOCK_SIZE, img->width/BLOCK_SIZE);
if (input->rdopt==2)
{
memory_size += get_mem2Dint(&resY, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
if ((decref = (byte****) calloc(input->NoOfDecoders,sizeof(byte***))) == NULL)
no_mem_exit("init_global_buffers: decref");
for (j=0 ; j<input->NoOfDecoders; j++)
{
memory_size += get_mem3D(&decref[j], img->buf_cycle+1, img->height, img->width);
}
memory_size += get_mem2D(&RefBlock, BLOCK_SIZE,BLOCK_SIZE);
memory_size += get_mem3D(&decY, input->NoOfDecoders, img->height, img->width);
memory_size += get_mem3D(&decY_best, input->NoOfDecoders, img->height, img->width);
memory_size += get_mem2D(&status_map, img->height/MB_BLOCK_SIZE,img->width/MB_BLOCK_SIZE);
memory_size += get_mem2D(&dec_mb_mode, img->width/MB_BLOCK_SIZE,img->height/MB_BLOCK_SIZE);
memory_size += get_mem2D(&dec_mb_ref, img->width/MB_BLOCK_SIZE,img->height/MB_BLOCK_SIZE);
}
return (memory_size);
}
/*!
************************************************************************
* \brief
* Free allocated memory of frame size related global buffers
* buffers are defined in global.h, allocated memory is allocated in
* int get_mem4global_buffers()
* \par Input:
* Input Parameters struct inp_par *inp, \n
* Image Parameters struct img_par *img
* \par Output:
* none
************************************************************************
*/
void free_global_buffers()
{
int j;
#ifdef _ADAPT_LAST_GROUP_
extern int *last_P_no;
free (last_P_no);
#endif
free_mem2D(imgY);
free_mem3D(imgUV,2);
free_mem2D(imgY_org); // free ref frame buffers
free_mem3D(imgUV_org,2);
free_mem2D(imgY_pf); // free post filtering frame buffers
free_mem3D(imgUV_pf,2);
free_mem2D(nextP_imgY); // free next frame buffers (for B frames)
free_mem3D(nextP_imgUV,2);
free_mem2D(mref_P);
free_mem3D(mcef_P,2);
free (Refbuf11_P);
free (Refbuf11);
// free multiple ref frame buffers
// number of reference frames increased by one for next P-frame
free(mref);
free(mcef);
if(input->successive_Bframe!=0)
{
// free last P-frame buffers for B-frame coding
free_mem3Dint(tmp_fwMV,2);
free_mem3Dint(tmp_bwMV,2);
free_mem3Dint(dfMV,2);
free_mem3Dint(dbMV,2);
free_mem2Dint(fw_refFrArr);
free_mem2Dint(bw_refFrArr);
} // end if B frame
free_mem3Dint(tmp_mv,2);
free_mem2Dint(img4Y_tmp); // free temp quarter pel frame buffer
free_mem2D(imgY_tmp); // free temp loop filter frame buffer
free_mem3D(imgUV_tmp,2);
free_mem2Dint(refFrArr);
// free mem, allocated in init_img()
// free intra pred mode buffer for blocks
free_mem2Dint(img->ipredmode);
free(img->mb_data);
if(input->UseConstrainedIntraPred)
{
free(img->intra_mb);
}
if (input->rdopt==2)
{
free_mem2Dint(resY);
free_mem2D(RefBlock);
free_mem3D(decY,input->NoOfDecoders);
free_mem3D(decY_best,input->NoOfDecoders);
for (j=0; j<input->NoOfDecoders; j++)
{
free_mem3D(decref[j],img->buf_cycle+1);
}
free(decref);
free_mem2D(status_map);
free_mem2D(dec_mb_mode);
free_mem2D(dec_mb_ref);
}
}
/*!
************************************************************************
* \brief
* Allocate memory for mv
* \par Input:
* Image Parameters struct img_par *img \n
* int****** mv
* \return memory size in bytes
************************************************************************
*/
int get_mem_mv (int****** mv)
{
int i, j, k, l;
if ((*mv = (int*****)calloc(4,sizeof(int****))) == NULL)
no_mem_exit ("get_mem_mv: mv");
for (i=0; i<4; i++)
{
if (((*mv)[i] = (int****)calloc(4,sizeof(int***))) == NULL)
no_mem_exit ("get_mem_mv: mv");
for (j=0; j<4; j++)
{
if (((*mv)[i][j] = (int***)calloc(img->buf_cycle,sizeof(int**))) == NULL)
no_mem_exit ("get_mem_mv: mv");
for (k=0; k<img->buf_cycle; k++)
{
if (((*mv)[i][j][k] = (int**)calloc(9,sizeof(int*))) == NULL)
no_mem_exit ("get_mem_mv: mv");
for (l=0; l<9; l++)
if (((*mv)[i][j][k][l] = (int*)calloc(2,sizeof(int))) == NULL)
no_mem_exit ("get_mem_mv: mv");
}
}
}
return 4*4*img->buf_cycle*9*2*sizeof(int);
}
/*!
************************************************************************
* \brief
* Free memory from mv
* \par Input:
* int****** mv
************************************************************************
*/
void free_mem_mv (int***** mv)
{
int i, j, k, l;
for (i=0; i<4; i++)
{
for (j=0; j<4; j++)
{
for (k=0; k<img->buf_cycle; k++)
{
for (l=0; l<9; l++)
free (mv[i][j][k][l]);
free (mv[i][j][k]);
}
free (mv[i][j]);
}
free (mv[i]);
}
free (mv);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -