📄 image.c
字号:
{
last_P_no[2 * i] = last_P_no[2 * i - 2] - img->p_interval;
last_P_no[2 * i + 1] = last_P_no[2 * i - 1] - img->p_interval;
}
}
if (input->last_frame && img->number + 1 == input->no_frames)
{
nextP_no = input->last_frame;
img->p_interval = nextP_no - prevP_no;
}
#endif
img->b_interval =
(int) ((float) (input->jumpd + 1) / (input->successive_Bframe + 1.0) +
0.49999);
img->tr = prevP_no + (img->b_interval + 1) * img->b_frame_to_code; // from prev_P
if (img->tr >= nextP_no)
img->tr = nextP_no - 1; // ?????
//Rate control
if(!input->RCEnable) // without using rate control
{
#ifdef _CHANGE_QP_
if (input->qp2start > 0 && img->tr >= input->qp2start)
img->qp = input->qpB2;
else
#endif
img->qp = input->qpB;
}
// initialize arrays
for (k = 0; k < 2; k++)
for (i = 0; i < img->height / BLOCK_SIZE; i++)
for (j = 0; j < img->width / BLOCK_SIZE + 4; j++)
{
tmp_fwMV[k][i][j] = 0;
tmp_bwMV[k][i][j] = 0;
dfMV[k][i][j] = 0;
dbMV[k][i][j] = 0;
}
for (i = 0; i < img->height / BLOCK_SIZE; i++)
for (j = 0; j < img->width / BLOCK_SIZE; j++)
{
fw_refFrArr[i][j] = bw_refFrArr[i][j] = -1;
}
}
input->jumpd /= 2;
input->successive_Bframe /= 2;
img->buf_cycle *= 2;
img->number = 2 * img->number + img->fld_type;
img->total_number_mb = (img->width * img->height) / (MB_BLOCK_SIZE * MB_BLOCK_SIZE);
}
#define Clip(min,max,val) (((val)<(min))?(min):(((val)>(max))?(max):(val)))
/*!
************************************************************************
* \brief
* Estimates reference picture weighting factors
************************************************************************
*/
static void estimate_weighting_factor ()
{
int i, j, n;
int x,z;
int dc_org = 0;
int index;
int comp;
int p0, pt;
int fwd_ref[MAX_REFERENCE_PICTURES], bwd_ref[MAX_REFERENCE_PICTURES];
int bframe = (img->type == B_SLICE);
int num_ref = min (img->number-((enc_picture!=enc_frame_picture)&&img->fld_type&&bframe), img->buf_cycle);
int dc_ref[MAX_REFERENCE_PICTURES];
int log_weight_denom;
int num_bwd_ref, num_fwd_ref;
pel_t* ref_pic;
pel_t* ref_pic_w;
int default_weight;
int default_weight_chroma;
luma_log_weight_denom = 5;
chroma_log_weight_denom = 5;
wp_luma_round = 1 << (luma_log_weight_denom - 1);
wp_chroma_round = 1 << (chroma_log_weight_denom - 1);
default_weight = 1<<luma_log_weight_denom;
default_weight_chroma = 1<<chroma_log_weight_denom;
/* set all values to defaults */
for (i = 0; i < 2; i++)
for (j = 0; j < MAX_REFERENCE_PICTURES; j++)
for (n = 0; n < 3; n++)
{
wp_weight[i][j][n] = default_weight;
wp_offset[i][j][n] = 0;
}
for (i = 0; i < img->height; i++)
for (j = 0; j < img->width; j++)
{
dc_org += imgY_org[i][j];
}
for (n = 0; n < num_ref; n++)
{
dc_ref[n] = 0;
ref_pic = listX[LIST_0][n]->imgY_11;
ref_pic_w = img->type==B_SLICE? Refbuf11_w [n] : Refbuf11_w[n];
// Y
for (i = 0; i < img->height * img->width; i++)
{
dc_ref[n] += ref_pic[i];
}
if (dc_ref[n] != 0)
weight[n][0] = (int) (default_weight * (double) dc_org / (double) dc_ref[n] + 0.5);
else
weight[n][0] = 2*default_weight; // only used when reference picture is black
printf("dc_org = %d, dc_ref = %d, weight[%d] = %d\n",dc_org, dc_ref[n],n,weight[n][0]);
/* for now always use default weight for chroma weight */
weight[n][1] = default_weight_chroma;
weight[n][2] = default_weight_chroma;
/* store weighted reference pic for motion estimation */
for (i = 0; i < img->height * img->width; i++)
{
ref_pic_w[i] = Clip (0, 255, ((int) ref_pic[i] * weight[n][0] + wp_luma_round) / default_weight);
}
for (i = 0; i < 4*(img->height + 2*IMG_PAD_SIZE) ; i++)
{
for (j = 0; j< 4*(img->width + 2*IMG_PAD_SIZE); j++)
{
mref_w[n][i][j] = Clip (0, 255, ((int) mref[n][i][j] * weight[n][0] + wp_luma_round) / default_weight);
}
}
}
if ((img->type == P_SLICE)||(img->type == SP_SLICE))
{
num_bwd_ref = 0;
num_fwd_ref = num_ref;
}
else
{
num_bwd_ref = (img->type == B_SLICE && img->nal_reference_idc>0) ? num_ref : 1;
num_fwd_ref = (img->type == B_SLICE && img->nal_reference_idc>0) ? num_ref+1 : num_ref;
}
// printf("num_fwd_ref = %d num_bwd_ref = %d\n",num_fwd_ref,num_bwd_ref);
{ /* forward list */
if ((img->type == P_SLICE || img->type == SP_SLICE) && input->WeightedPrediction)
{
for (index = 0; index < num_ref; index++)
{
wp_weight[0][index][0] = weight[index][0];
wp_weight[0][index][1] = weight[index][1];
wp_weight[0][index][2] = weight[index][2];
// printf ("wp weight[%d] = %d \n", index, wp_weight[0][index][0]);
}
}
else if (img->type == B_SLICE && img->nal_reference_idc>0 && (input->WeightedBiprediction == 1))
{
for (index = 0; index < num_ref; index++)
{
wp_weight[0][index][0] = weight[index][0];
wp_weight[0][index][1] = weight[index][1];
wp_weight[0][index][2] = weight[index][2];
}
for (index = 0; index < num_ref; index++)
{ /* backward list */
if (index == 0)
n = 1;
else if (index == 1)
n = 0;
else
n = index;
}
}
else if (img->type == B_SLICE && (input->WeightedBiprediction == 1))
{
for (index = 0; index < num_ref - 1; index++)
{
wp_weight[0][index][0] = weight[index + 1][0];
wp_weight[0][index][1] = weight[index + 1][1];
wp_weight[0][index][2] = weight[index + 1][2];
}
wp_weight[1][0][0] = weight[0][0];
wp_weight[1][0][1] = weight[0][1];
wp_weight[1][0][2] = weight[0][2];
}
else
{
for (index = 0; index < num_ref; index++)
{
wp_weight[0][index][0] = 1<<luma_log_weight_denom;
wp_weight[0][index][1] = 1<<chroma_log_weight_denom;
wp_weight[0][index][2] = 1<<chroma_log_weight_denom;
wp_weight[1][index][0] = 1<<luma_log_weight_denom;
wp_weight[1][index][1] = 1<<chroma_log_weight_denom;
wp_weight[1][index][2] = 1<<chroma_log_weight_denom;
}
}
if (input->WeightedBiprediction > 0 && (img->type == B_SLICE))
{
if (img->nal_reference_idc>0)
{
for (index = 0; index < num_fwd_ref; index++)
{
fwd_ref[index] = index;
if (index == 0)
n = 1;
else if (index == 1)
n = 0;
else
n = index;
bwd_ref[index] = n;
}
}
else if (img->type == B_SLICE)
{
for (index = 0; index < num_fwd_ref; index++)
{
fwd_ref[index] = index+1;
}
bwd_ref[0] = 0; // only one possible backwards ref for traditional B picture in current software
}
}
if (img->type == B_SLICE) // need to fill in wbp_weight values
{
for (i = 0; i < num_fwd_ref; i++)
{
for (j = 0; j < num_bwd_ref; j++)
{
for (comp = 0; comp < 3; comp++)
{
log_weight_denom = (comp == 0) ? luma_log_weight_denom : chroma_log_weight_denom;
if (input->WeightedBiprediction == 1)
{
wbp_weight[0][i][j][comp] = wp_weight[0][i][comp];
wbp_weight[1][i][j][comp] = wp_weight[1][j][comp];
}
else if (input->WeightedBiprediction == 2)
{ // implicit mode
pt = poc_distance (fwd_ref[i], bwd_ref[j]);
p0 = poc_distance (fwd_ref[i], -1);
if (pt == 0)
{
wbp_weight[1][i][j][comp] = 32 ;
wbp_weight[0][i][j][comp] = 32;
}
else
{
x = (16384 + (pt>>1))/pt;
z = Clip(-1024, 1023, (x*p0 + 32 )>>6);
wbp_weight[1][i][j][comp] = z>>2;
if (wbp_weight[1][i][j][comp] < -64 || wbp_weight[1][i][j][comp] >128)
wbp_weight[1][i][j][comp] = 32;
wbp_weight[0][i][j][comp] = 64 - wbp_weight[1][i][j][comp];
}
// if (comp == 0 )
// printf ("bpw weight[%d][%d] = %d , %d \n", i, j, wbp_weight[0][i][j][0], wbp_weight[1][i][j][0]);
}
}
}
}
}
}
}
/*!
************************************************************************
* \brief
* Choose interpolation method depending on MV-resolution
************************************************************************
*/
static void interpolate_frame_to_fb ()
{ // write to mref[]
// UnifiedOneForthPix (imgY, imgUV[0], imgUV[1], mref[0], mcef[0][0], mcef[0][1], Refbuf11[0]);
}
/*!
************************************************************************
* \brief
* Choose interpolation method depending on MV-resolution
************************************************************************
*/
static void interpolate_frame ()
{ // write to mref[]
// UnifiedOneForthPix (imgY, imgUV[0], imgUV[1], mref[0], mcef[0][0], mcef[0][1], Refbuf11[0]);
}
static void GenerateFullPelRepresentation (pel_t ** Fourthpel,
pel_t * Fullpel, int xsize,
int ysize)
{
int x, y;
for (y = 0; y < ysize; y++)
for (x = 0; x < xsize; x++)
PutPel_11 (Fullpel, y, x, FastPelY_14 (Fourthpel, y * 4, x * 4));
}
/*!
************************************************************************
* \brief
* Upsample 4 times, store them in out4x. Color is simply copied
*
* \par Input:
* srcy, srcu, srcv, out4y, out4u, out4v
*
* \par Side Effects_
* Uses (writes) img4Y_tmp. This should be moved to a static variable
* in this module
************************************************************************/
void UnifiedOneForthPix (StorablePicture *s)
{
int is;
int i, j, j4;
int ie2, je2, jj, maxy;
byte **out4Y;
byte *ref11;
byte **imgY = s->imgY;
// don't upsample twice
if (s->imgY_ups || s->imgY_11)
return;
s->imgY_11 = malloc ((s->size_x * s->size_y) * sizeof (byte));
if (NULL == s->imgY_11)
no_mem_exit("alloc_storable_picture: s->imgY_11");
get_mem2D (&(s->imgY_ups), (2*IMG_PAD_SIZE + s->size_y)*4, (2*IMG_PAD_SIZE + s->size_x)*4);
out4Y = s->imgY_ups;
ref11 = s->imgY_11;
for (j = -IMG_PAD_SIZE; j < s->size_y + IMG_PAD_SIZE; j++)
{
for (i = -IMG_PAD_SIZE; i < s->size_x + IMG_PAD_SIZE; i++)
{
jj = max (0, min (s->size_y - 1, j));
is =
(ONE_FOURTH_TAP[0][0] *
(imgY[jj][max (0, min (s->size_x - 1, i))] +
imgY[jj][max (0, min (s->size_x - 1, i + 1))]) +
ONE_FOURTH_TAP[1][0] *
(imgY[jj][max (0, min (s->size_x - 1, i - 1))] +
imgY[jj][max (0, min (s->size_x - 1, i + 2))]) +
ONE_FOURTH_TAP[2][0] *
(imgY[jj][max (0, min (s->size_x - 1, i - 2))] +
imgY[jj][max (0, min (s->size_x - 1, i + 3))]));
img4Y_tmp[j + IMG_PAD_SIZE][(i + IMG_PAD_SIZE) * 2] = imgY[jj][max (0, min (s->size_x - 1, i))] * 1024; // 1/1 pix pos
img4Y_tmp[j + IMG_PAD_SIZE][(i + IMG_PAD_SIZE) * 2 + 1] = is * 32; // 1/2 pix pos
}
}
for (i = 0; i < (s->size_x + 2 * IMG_PAD_SIZE) * 2; i++)
{
for (j = 0; j < s->size_y + 2 * IMG_PAD_SIZE; j++)
{
j4 = j * 4;
maxy = s->size_y + 2 * IMG_PAD_SIZE - 1;
// change for TML4, use 6 TAP vertical filter
is =
(ONE_FOURTH_TAP[0][0] *
(img4Y_tmp[j][i] + img4Y_tmp[min (maxy, j + 1)][i]) +
ONE_FOURTH_TAP[1][0] * (img4Y_tmp[max (0, j - 1)][i] +
img4Y_tmp[min (maxy, j + 2)][i]) +
ONE_FOURTH_TAP[2][0] * (img4Y_tmp[max (0, j - 2)][i] +
img4Y_tmp[min (maxy, j + 3)][i])) / 32;
PutPel_14 (out4Y, (j - IMG_PAD_SIZE) * 4, (i - IMG_PAD_SIZE * 2) * 2, (pel_t) max (0, min (255, (int) ((img4Y_tmp[j][i] + 512) / 1024)))); // 1/2 pix
PutPel_14 (out4Y, (j - IMG_PAD_SIZE) * 4 + 2, (i - IMG_PAD_SIZE * 2) * 2, (pel_t) max (0, min (255, (int) ((is + 512) / 1024)))); // 1/2 pix
}
}
/* 1/4 pix */
/* luma */
ie2 = (s->size_x + 2 * IMG_PAD_SIZE - 1) * 4;
je2 = (s->size_y + 2 * IMG_PAD_SIZE - 1) * 4;
for (j = 0; j < je2 + 4; j += 2)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -