📄 rc_quadratic.c
字号:
return prc->m_Qc;
}
}
/*bottom field*/
else
{
if( generic_RC->NoGranularFieldRC == 0 )
updateBottomField( prc );
return prc->m_Qc;
}
}
/*basic unit layer rate control*/
else
{
/*top field of I frame*/
if (img->number == 0)
{
prc->m_Qc = prc->MyInitialQp;
return prc->m_Qc;
}
else
{
if((generic_RC->NumberofGOP==1)&&(prc->NumberofPPicture==0))
{
if((generic_RC->FieldControl==0)||((generic_RC->FieldControl==1) && (generic_RC->NoGranularFieldRC==0)))
return updateFirstP( prc, topfield );
}
else
{
prc->m_X1=prc->Pm_X1;
prc->m_X2=prc->Pm_X2;
prc->MADPictureC1=prc->PMADPictureC1;
prc->MADPictureC2=prc->PMADPictureC2;
m_Qp=prc->Pm_Qp;
if(generic_RC->FieldControl==0)
SumofBasicUnit=prc->TotalNumberofBasicUnit;
else
SumofBasicUnit=prc->TotalNumberofBasicUnit>>1;
/*the average QP of the previous frame is used to coded the first basic unit of the current frame or field*/
if(prc->NumberofBasicUnit==SumofBasicUnit)
return updateFirstBU( prc, topfield );
else
{
/*compute the number of remaining bits*/
prc->Target -= (generic_RC->NumberofBasicUnitHeaderBits + generic_RC->NumberofBasicUnitTextureBits);
generic_RC->NumberofBasicUnitHeaderBits = 0;
generic_RC->NumberofBasicUnitTextureBits = 0;
if(prc->Target<0)
return updateNegativeTarget( prc, topfield, m_Qp );
else
{
/*predict the MAD of current picture*/
predictCurrPicMAD( prc );
/*compute the total number of bits for the current basic unit*/
updateModelQPBU( prc, topfield, m_Qp );
prc->TotalFrameQP +=prc->m_Qc;
prc->Pm_Qp=prc->m_Qc;
prc->NumberofBasicUnit--;
if((prc->NumberofBasicUnit==0) && (img->number != 0))
updateLastBU( prc, topfield );
return prc->m_Qc;
}
}
}
}
}
return prc->m_Qc;
}
/*!
*************************************************************************************
* \brief
* compute a quantization parameter for each frame
*
*************************************************************************************
*/
int updateQPRC2(rc_quadratic *prc, int topfield)
{
int m_Bits;
int SumofBasicUnit;
int MaxQpChange, m_Qp, m_Hp;
/* frame layer rate control */
if( img->BasicUnit == img->FrameSizeInMbs )
{
/* fixed quantization parameter is used to coded I frame, the first P frame and the first B frame
the quantization parameter is adjusted according the available channel bandwidth and
the type of vide */
/*top field*/
if((topfield) || (generic_RC->FieldControl==0))
{
if (img->number == 0)
{
prc->m_Qc = prc->MyInitialQp;
return prc->m_Qc;
}
else if (img->type==I_SLICE)
{
if((input->PicInterlace==ADAPTIVE_CODING)||(input->MbInterlace))
updateQPInterlace( prc );
prc->m_Qc = prc->CurrLastQP; // Set QP to average qp of last P frame
return prc->m_Qc;
}
else if(img->type == B_SLICE)
{
int prevQP = imax(prc->PrevLastQP, prc->CurrLastQP);
// for more than one consecutive B frames the below call will overwrite the old anchor frame QP with the current value
// it should be called once in the B-frame sequence....this should be modified for the BU < frame as well
if((input->PicInterlace==ADAPTIVE_CODING)||(input->MbInterlace))
updateQPInterlace( prc );
if (input->HierarchicalCoding)
{
if (img->b_frame_to_code == 0)
prc->m_Qc = prevQP;
else
prc->m_Qc = prevQP + img->GopLevels - gop_structure[img->b_frame_to_code-1].hierarchy_layer;
}
else
prc->m_Qc = prevQP + 2 - img->nal_reference_idc;
prc->m_Qc = iClip3(prc->RC_MIN_QUANT, prc->RC_MAX_QUANT, prc->m_Qc); // Clipping
return prc->m_Qc;
}
else if( img->type == P_SLICE && prc->NumberofPPicture == 0 )
{
prc->m_Qc=prc->MyInitialQp;
if(generic_RC->FieldControl==0)
updateQPNonPicAFF( prc );
return prc->m_Qc;
}
else
{
/*adaptive field/frame coding*/
if( ( input->PicInterlace == ADAPTIVE_CODING || input->MbInterlace ) && generic_RC->FieldControl == 0 )
updateQPInterlaceBU( prc );
prc->m_X1 = prc->Pm_X1;
prc->m_X2 = prc->Pm_X2;
prc->MADPictureC1 = prc->PMADPictureC1;
prc->MADPictureC2 = prc->PMADPictureC2;
prc->PreviousPictureMAD = prc->PPictureMAD[0];
MaxQpChange = prc->PMaxQpChange;
m_Qp = prc->Pm_Qp;
m_Hp = prc->PPreHeader;
/* predict the MAD of current picture*/
prc->CurrentFrameMAD=prc->MADPictureC1*prc->PreviousPictureMAD + prc->MADPictureC2;
/*compute the number of bits for the texture*/
if(prc->Target < 0)
{
prc->m_Qc=m_Qp+MaxQpChange;
prc->m_Qc = iClip3(prc->RC_MIN_QUANT, prc->RC_MAX_QUANT, prc->m_Qc); // Clipping
}
else
{
m_Bits = prc->Target-m_Hp;
m_Bits = imax(m_Bits, (int)(prc->bit_rate/(MINVALUE*prc->frame_rate)));
updateModelQPFrame( prc, m_Bits );
prc->m_Qc = iClip3(prc->RC_MIN_QUANT, prc->RC_MAX_QUANT, prc->m_Qc); // clipping
prc->m_Qc = iClip3(m_Qp-MaxQpChange, m_Qp+MaxQpChange, prc->m_Qc); // control variation
}
if( generic_RC->FieldControl == 0 )
updateQPNonPicAFF( prc );
return prc->m_Qc;
}
}
/*bottom field*/
else
{
if( img->type==P_SLICE && generic_RC->NoGranularFieldRC == 0 )
updateBottomField( prc );
return prc->m_Qc;
}
}
/*basic unit layer rate control*/
else
{
/*top field of I frame*/
if (img->number == 0)
{
prc->m_Qc = prc->MyInitialQp;
return prc->m_Qc;
}
else if (img->type==I_SLICE)
{
/*adaptive field/frame coding*/
if((input->PicInterlace==ADAPTIVE_CODING)||(input->MbInterlace))
updateQPInterlace( prc );
prc->m_Qc = prc->PrevLastQP; // Set QP to average qp of last P frame
prc->PrevLastQP = prc->CurrLastQP;
prc->CurrLastQP = prc->PrevLastQP;
prc->PAveFrameQP = prc->CurrLastQP;
return prc->m_Qc;
}
else if(img->type == B_SLICE)
{
int prevQP = imax(prc->PrevLastQP, prc->CurrLastQP);
if((input->PicInterlace==ADAPTIVE_CODING)||(input->MbInterlace))
updateQPInterlace( prc );
if (input->HierarchicalCoding)
{
if (img->b_frame_to_code == 0)
prc->m_Qc = prevQP;
else
prc->m_Qc = prevQP + img->GopLevels - gop_structure[img->b_frame_to_code-1].hierarchy_layer;
}
else
prc->m_Qc = prevQP + 2 - img->nal_reference_idc;
prc->m_Qc = iClip3(prc->RC_MIN_QUANT, prc->RC_MAX_QUANT, prc->m_Qc); // Clipping
return prc->m_Qc;
}
else if( img->type == P_SLICE )
{
if((generic_RC->NumberofGOP==1)&&(prc->NumberofPPicture==0))
{
if((generic_RC->FieldControl==0)||((generic_RC->FieldControl==1) && (generic_RC->NoGranularFieldRC==0)))
return updateFirstP( prc, topfield );
}
else
{
prc->m_X1=prc->Pm_X1;
prc->m_X2=prc->Pm_X2;
prc->MADPictureC1=prc->PMADPictureC1;
prc->MADPictureC2=prc->PMADPictureC2;
m_Qp=prc->Pm_Qp;
if(generic_RC->FieldControl==0)
SumofBasicUnit=prc->TotalNumberofBasicUnit;
else
SumofBasicUnit=prc->TotalNumberofBasicUnit>>1;
/*the average QP of the previous frame is used to coded the first basic unit of the current frame or field*/
if(prc->NumberofBasicUnit==SumofBasicUnit)
return updateFirstBU( prc, topfield );
else
{
/*compute the number of remaining bits*/
prc->Target -= (generic_RC->NumberofBasicUnitHeaderBits + generic_RC->NumberofBasicUnitTextureBits);
generic_RC->NumberofBasicUnitHeaderBits = 0;
generic_RC->NumberofBasicUnitTextureBits = 0;
if(prc->Target<0)
return updateNegativeTarget( prc, topfield, m_Qp );
else
{
/*predict the MAD of current picture*/
predictCurrPicMAD( prc );
/*compute the total number of bits for the current basic unit*/
updateModelQPBU( prc, topfield, m_Qp );
prc->TotalFrameQP +=prc->m_Qc;
prc->Pm_Qp=prc->m_Qc;
prc->NumberofBasicUnit--;
if((prc->NumberofBasicUnit==0) && img->type == P_SLICE )
updateLastBU( prc, topfield );
return prc->m_Qc;
}
}
}
}
}
return prc->m_Qc;
}
/*!
*************************************************************************************
* \brief
* compute a quantization parameter for each frame
*
*************************************************************************************
*/
int updateQPRC3(rc_quadratic *prc, int topfield)
{
int m_Bits;
int SumofBasicUnit;
int MaxQpChange, m_Qp, m_Hp;
/* frame layer rate control */
if( img->BasicUnit == img->FrameSizeInMbs || img->type != P_SLICE )
{
/* fixed quantization parameter is used to coded I frame, the first P frame and the first B frame
the quantization parameter is adjusted according the available channel bandwidth and
the type of video */
/*top field*/
if((topfield) || (generic_RC->FieldControl==0))
{
if (img->number == 0)
{
if((input->PicInterlace == ADAPTIVE_CODING) || (input->MbInterlace))
updateQPInterlace( prc );
prc->m_Qc = prc->MyInitialQp;
return prc->m_Qc;
}
else if( img->type == P_SLICE && prc->NumberofPPicture == 0 )
{
prc->m_Qc=prc->MyInitialQp;
if(generic_RC->FieldControl==0)
updateQPNonPicAFF( prc );
return prc->m_Qc;
}
else
{
if( ( (img->type == B_SLICE && img->b_frame_to_code == 1) || img->type == I_SLICE) && ((input->PicInterlace == ADAPTIVE_CODING) || (input->MbInterlace)) )
updateQPInterlace( prc );
/*adaptive field/frame coding*/
if( img->type == P_SLICE && ( input->PicInterlace == ADAPTIVE_CODING || input->MbInterlace ) && generic_RC->FieldControl == 0 )
updateQPInterlaceBU( prc );
prc->m_X1 = prc->Pm_X1;
prc->m_X2 = prc->Pm_X2;
prc->MADPictureC1 = prc->PMADPictureC1;
prc->MADPictureC2 = prc->PMADPictureC2;
prc->PreviousPictureMAD = prc->PPictureMAD[0];
MaxQpChange = prc->PMaxQpChange;
m_Qp = prc->Pm_Qp;
m_Hp = prc->PPreHeader;
if ( img->BasicUnit < img->FrameSizeInMbs && img->type != P_SLICE )
{
// when RC_MODE_3 is set and basic unit is smaller than a frame, note that:
// the linear MAD model and the quadratic QP model operate on small units and not on a whole frame;
// we therefore have to account for this
prc->PreviousPictureMAD = prc->PreviousWholeFrameMAD;
}
if ( img->type == I_SLICE )
m_Hp = 0; // it is usually a very small portion of the total I_SLICE bit budget
/* predict the MAD of current picture*/
prc->CurrentFrameMAD=prc->MADPictureC1*prc->PreviousPictureMAD + prc->MADPictureC2;
/*compute the number of bits for the texture*/
if(prc->Target < 0)
{
prc->m_Qc=m_Qp+MaxQpChange;
prc->m_Qc = iClip3(prc->RC_MIN_QUANT, prc->RC_MAX_QUANT, prc->m_Qc); // Clipping
}
else
{
if ( img->type != P_SLICE )
{
if ( img->BasicUnit < img->FrameSizeInMbs )
m_Bits =(prc->Target-m_Hp)/prc->TotalNumberofBasicUnit;
else
m_Bits =prc->Target-m_Hp;
}
else {
m_Bits = prc->Target-m_Hp;
m_Bits = imax(m_Bits, (int)(prc->bit_rate/(MINVALUE*prc->frame_rate)));
}
updateModelQPFrame( prc, m_Bits );
prc->m_Qc = iClip3(prc->RC_MIN_QUANT, prc->RC_MAX_QUANT, prc->m_Qc); // clipping
if ( img->type == P_SLICE )
prc->m_Qc = iClip3(m_Qp-MaxQpChange, m_Qp+MaxQpChange, prc->m_Qc); // control variation
}
i
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -