Newer
Older
if ( pu.mergeIdx == idx )
{
break;
}
}
}
}
DTRACE( g_trace_ctx, D_SYNTAX, "aff_merge_idx() aff_merge_idx=%d\n", pu.mergeIdx );
}
else
{
if( pu.cu->geoFlag )
{
const uint8_t splitDir = pu.geoSplitDir;
const uint8_t candIdx0 = pu.geoMergeIdx[0];
uint8_t candIdx1 = pu.geoMergeIdx[1];
DTRACE( g_trace_ctx, D_SYNTAX, "merge_idx() geo_split_dir=%d\n", splitDir );
DTRACE( g_trace_ctx, D_SYNTAX, "merge_idx() geo_idx0=%d\n", candIdx0 );
DTRACE( g_trace_ctx, D_SYNTAX, "merge_idx() geo_idx1=%d\n", candIdx1 );
xWriteTruncBinCode(splitDir, GEO_NUM_PARTITION_MODE);
candIdx1 -= candIdx1 < candIdx0 ? 0 : 1;
const int maxNumGeoCand = pu.cs->sps->getMaxNumGeoCand();
CHECK(maxNumGeoCand < 2, "Incorrect max number of geo candidates");
CHECK(candIdx0 >= maxNumGeoCand, "Incorrect candIdx0");
CHECK(candIdx1 >= maxNumGeoCand, "Incorrect candIdx1");
const int numCandminus2 = maxNumGeoCand - 2;
m_binEncoder.encodeBin(candIdx0 == 0 ? 0 : 1, Ctx::MergeIdx());
if( candIdx0 > 0 )
{
unary_max_eqprob(candIdx0 - 1, numCandminus2);
}
if (numCandminus2 > 0)
{
m_binEncoder.encodeBin(candIdx1 == 0 ? 0 : 1, Ctx::MergeIdx());
if (candIdx1 > 0)
{
unary_max_eqprob(candIdx1 - 1, numCandminus2 - 1);
}
}
return;
}
if (CU::isIBC(*pu.cu))
numCandminus1 = int(pu.cs->sps->getMaxNumIBCMergeCand()) - 1;

Karsten Suehring
committed
{
numCandminus1 = int(pu.cs->sps->getMaxNumMergeCand()) - 1;

Karsten Suehring
committed
}
CHECK(pu.mergeIdx > numCandminus1, "mergeIdx out of range");

Karsten Suehring
committed
{

Karsten Suehring
committed
{
DTRACE(g_trace_ctx, D_SYNTAX, "merge_idx() merge_idx=%d\n", pu.mergeIdx);
return;
}
else
{
for (unsigned idx = 1; idx < numCandminus1; idx++)

Karsten Suehring
committed
{
m_binEncoder.encodeBinEP(pu.mergeIdx == idx ? 0 : 1);
if (pu.mergeIdx == idx)
{
break;
}

Karsten Suehring
committed
}
}
}
DTRACE(g_trace_ctx, D_SYNTAX, "merge_idx() merge_idx=%d\n", pu.mergeIdx);

Karsten Suehring
committed
}
void CABACWriter::mmvd_merge_idx(const PredictionUnit& pu)
{
const int mvdBaseIdx = pu.mmvdMergeIdx.pos.baseIdx;
const int mvdStep = pu.mmvdMergeIdx.pos.step;
const int mvdPosition = pu.mmvdMergeIdx.pos.position;
CHECK(mvdBaseIdx >= std::min<int>(pu.cs->sps->getMaxNumMergeCand(), MmvdIdx::BASE_MV_NUM), "MMVD base index out of range");
static_assert(MmvdIdx::BASE_MV_NUM == 2, "");
m_binEncoder.encodeBin(mvdBaseIdx, Ctx::MmvdMergeIdx());
DTRACE(g_trace_ctx, D_SYNTAX, "base_mvp_idx() base_mvp_idx=%d\n", mvdBaseIdx);
int numStepCandMinus1 = MmvdIdx::REFINE_STEP - 1;
if (numStepCandMinus1 > 0)
}
else
{
for (unsigned idx = 1; idx < numStepCandMinus1; idx++)
m_binEncoder.encodeBinEP(mvdStep == idx ? 0 : 1);
if (mvdStep == idx)
{
break;
}
}
}
}
DTRACE(g_trace_ctx, D_SYNTAX, "MmvdStepMvpIdx() MmvdStepMvpIdx=%d\n", mvdStep);
DTRACE(g_trace_ctx, D_SYNTAX, "pos() pos=%d\n", mvdPosition);
DTRACE(g_trace_ctx, D_SYNTAX, "mmvd_merge_idx() mmvd_merge_idx=%d\n", pu.mmvdMergeIdx.val);

Karsten Suehring
committed
void CABACWriter::inter_pred_idc( const PredictionUnit& pu )
{
if( !pu.cs->slice->isInterB() )
{
return;
}

Karsten Suehring
committed
{
unsigned ctxId = DeriveCtx::CtxInterDir(pu);
if( pu.interDir == 3 )
{

Karsten Suehring
committed
DTRACE( g_trace_ctx, D_SYNTAX, "inter_pred_idc() ctx=%d value=%d pos=(%d,%d)\n", ctxId, pu.interDir, pu.lumaPos().x, pu.lumaPos().y );
return;
}
else
{

Karsten Suehring
committed
}
}
m_binEncoder.encodeBin((pu.interDir == 2), Ctx::InterDir(5));
DTRACE( g_trace_ctx, D_SYNTAX, "inter_pred_idc() ctx=5 value=%d pos=(%d,%d)\n", pu.interDir, pu.lumaPos().x, pu.lumaPos().y );

Karsten Suehring
committed
}
void CABACWriter::ref_idx( const PredictionUnit& pu, RefPicList eRefList )
{
if ( pu.cu->smvdMode )
{
CHECK( pu.refIdx[eRefList] != pu.cs->slice->getSymRefIdx( eRefList ), "Invalid reference index!\n" );
return;
}

Karsten Suehring
committed
int numRef = pu.cs->slice->getNumRefIdx(eRefList);
if (eRefList == REF_PIC_LIST_0 && pu.cs->sps->getIBCFlag())

Karsten Suehring
committed
if( numRef <= 1 )
{
return;
}
int refIdx = pu.refIdx[eRefList];

Karsten Suehring
committed
if( numRef <= 2 || refIdx == 0 )
{
DTRACE( g_trace_ctx, D_SYNTAX, "ref_idx() value=%d pos=(%d,%d)\n", refIdx, pu.lumaPos().x, pu.lumaPos().y );
return;
}
m_binEncoder.encodeBin((refIdx > 1), Ctx::RefPic(1));

Karsten Suehring
committed
if( numRef <= 3 || refIdx == 1 )
{
DTRACE( g_trace_ctx, D_SYNTAX, "ref_idx() value=%d pos=(%d,%d)\n", refIdx, pu.lumaPos().x, pu.lumaPos().y );
return;
}
for( int idx = 3; idx < numRef; idx++ )
{
if( refIdx > idx - 1 )
{

Karsten Suehring
committed
}
else
{

Karsten Suehring
committed
break;
}
}
DTRACE( g_trace_ctx, D_SYNTAX, "ref_idx() value=%d pos=(%d,%d)\n", refIdx, pu.lumaPos().x, pu.lumaPos().y );
}
void CABACWriter::mvp_flag( const PredictionUnit& pu, RefPicList eRefList )
{
m_binEncoder.encodeBin(pu.mvpIdx[eRefList], Ctx::MVPIdx());

Karsten Suehring
committed
DTRACE( g_trace_ctx, D_SYNTAX, "mvp_flag() value=%d pos=(%d,%d)\n", pu.mvpIdx[eRefList], pu.lumaPos().x, pu.lumaPos().y );
DTRACE( g_trace_ctx, D_SYNTAX, "mvpIdx(refList:%d)=%d\n", eRefList, pu.mvpIdx[eRefList] );
}
void CABACWriter::ciip_flag(const PredictionUnit &pu)
if (!pu.cs->sps->getUseCiip())
CHECK(pu.ciipFlag == true, "invalid Ciip SPS");
return;
}
if (pu.cu->skip)
{
CHECK(pu.ciipFlag == true, "invalid Ciip and skip");
return;
}
m_binEncoder.encodeBin(pu.ciipFlag, Ctx::CiipFlag());
DTRACE(g_trace_ctx, D_SYNTAX, "ciip_flag() Ciip=%d pos=(%d,%d) size=%dx%d\n", pu.ciipFlag ? 1 : 0, pu.lumaPos().x,
pu.lumaPos().y, pu.lumaSize().width, pu.lumaSize().height);

Karsten Suehring
committed
//================================================================================
// clause 7.3.8.8
//--------------------------------------------------------------------------------
// void transform_tree ( cs, area, cuCtx, chromaCbfs )
// bool split_transform_flag( split, depth )
// bool cbf_comp ( cbf, area, depth )
//================================================================================
void CABACWriter::transform_tree( const CodingStructure& cs, Partitioner& partitioner, CUCtx& cuCtx, const PartSplit ispType, const int subTuIdx )

Karsten Suehring
committed
{
const UnitArea& area = partitioner.currArea();
int subTuCounter = subTuIdx;
const TransformUnit &tu = *cs.getTU(area.block(partitioner.chType).pos(), partitioner.chType, subTuIdx);
const CodingUnit& cu = *tu.cu;
const unsigned trDepth = partitioner.currTrDepth;
const bool split = (tu.depth > trDepth);

Karsten Suehring
committed
// split_transform_flag

Karsten Suehring
committed
{

Karsten Suehring
committed
}
else if( cu.sbtInfo && partitioner.canSplit( PartSplit( cu.getSbtTuSplit() ), cs ) )
{
CHECK( !split, "transform split implied - sbt" );
}
CHECK(split && cu.ispMode == ISPType::NONE, "transform split not allowed with QTBT");

Karsten Suehring
committed
if( split )
{
if( partitioner.canSplit( TU_MAX_TR_SPLIT, cs ) )
{
#if ENABLE_TRACING
const CompArea &tuArea = partitioner.currArea().block(partitioner.chType);

Karsten Suehring
committed
DTRACE( g_trace_ctx, D_SYNTAX, "transform_tree() maxTrSplit chType=%d pos=(%d,%d) size=%dx%d\n", partitioner.chType, tuArea.x, tuArea.y, tuArea.width, tuArea.height );
#endif
partitioner.splitCurrArea( TU_MAX_TR_SPLIT, cs );
}
else if (cu.ispMode != ISPType::NONE)
{
partitioner.splitCurrArea( ispType, cs );
}
else if( cu.sbtInfo && partitioner.canSplit( PartSplit( cu.getSbtTuSplit() ), cs ) )
{
partitioner.splitCurrArea( PartSplit( cu.getSbtTuSplit() ), cs );
}

Karsten Suehring
committed
else

Karsten Suehring
committed
THROW( "Implicit TU split not available" );

Karsten Suehring
committed
do
{
transform_tree( cs, partitioner, cuCtx, ispType, subTuCounter );
subTuCounter += subTuCounter != -1 ? 1 : 0;

Karsten Suehring
committed
} while( partitioner.nextPart( cs ) );
partitioner.exitCurrSplit();
}
else
{
DTRACE(g_trace_ctx, D_SYNTAX, "transform_unit() pos=(%d,%d) size=%dx%d depth=%d trDepth=%d\n",
tu.block(tu.chType).x, tu.block(tu.chType).y, tu.block(tu.chType).width, tu.block(tu.chType).height,
cu.depth, partitioner.currTrDepth);

Karsten Suehring
committed
transform_unit( tu, cuCtx, partitioner, subTuCounter);

Karsten Suehring
committed
}
}
void CABACWriter::cbf_comp(bool cbf, const CompArea &area, unsigned depth, const bool prevCbf, const bool useISP,
const BdpcmMode bdpcmMode)
unsigned ctxId = DeriveCtx::CtxQtCbf(area.compID, prevCbf, useISP && isLuma(area.compID));

Karsten Suehring
committed
const CtxSet& ctxSet = Ctx::QtCbf[ area.compID ];
ctxId = area.compID == COMPONENT_Cr ? 2 : 1;
m_binEncoder.encodeBin(cbf ? 1 : 0, ctxSet(ctxId));
DTRACE(g_trace_ctx, D_SYNTAX, "cbf_comp() etype=%d pos=(%d,%d) ctx=%d cbf=%d\n", area.compID, area.x, area.y, ctxId,
cbf ? 1 : 0);

Karsten Suehring
committed
}
//================================================================================
// clause 7.3.8.9
//--------------------------------------------------------------------------------
//================================================================================
void CABACWriter::mvd_coding(const PredictionUnit& pu, Mv mvd, int amvr)

Karsten Suehring
committed
{
if (CU::isIBC(*pu.cu))
{
mvd.changeIbcPrecInternal2Amvr(amvr);
}
else if (pu.cu->affine)
{
mvd.changeAffinePrecInternal2Amvr(amvr);
}
else
{
mvd.changeTransPrecInternal2Amvr(amvr);

Karsten Suehring
committed
}
const int horMvd = mvd.getHor();
const int verMvd = mvd.getVer();
const unsigned int horAbs = std::abs(horMvd);
const unsigned int verAbs = std::abs(verMvd);

Karsten Suehring
committed
// abs_mvd_greater0_flag[ 0 | 1 ]
m_binEncoder.encodeBin((horAbs > 0), Ctx::Mvd());
m_binEncoder.encodeBin((verAbs > 0), Ctx::Mvd());

Karsten Suehring
committed
// abs_mvd_greater1_flag[ 0 | 1 ]
if( horAbs > 0 )
{

Karsten Suehring
committed
}
if( verAbs > 0 )
{

Karsten Suehring
committed
}
// abs_mvd_minus2[ 0 | 1 ] and mvd_sign_flag[ 0 | 1 ]
if( horAbs > 0 )
{
if( horAbs > 1 )
{
m_binEncoder.encodeRemAbsEP(horAbs - 2, 1, 0, MV_BITS - 1);

Karsten Suehring
committed
}

Karsten Suehring
committed
}
if( verAbs > 0 )
{
if( verAbs > 1 )
{
m_binEncoder.encodeRemAbsEP(verAbs - 2, 1, 0, MV_BITS - 1);

Karsten Suehring
committed
}

Karsten Suehring
committed
}
}
//================================================================================
// clause 7.3.8.10
//--------------------------------------------------------------------------------
// void transform_unit ( tu, cuCtx, chromaCbfs )
// void cu_qp_delta ( cu )
// void cu_chroma_qp_offset ( cu )
//================================================================================
void CABACWriter::transform_unit( const TransformUnit& tu, CUCtx& cuCtx, Partitioner& partitioner, const int subTuCounter)

Karsten Suehring
committed
{
const CodingUnit& cu = *tu.cu;
const UnitArea& area = partitioner.currArea();
const unsigned trDepth = partitioner.currTrDepth;
ChromaCbfs chromaCbfs;
CHECK(tu.depth != trDepth, " transform unit should be not be futher partitioned");
// cbf_cb & cbf_cr
if (isChromaEnabled(area.chromaFormat))
const bool chromaCbfISP = area.blocks[COMPONENT_Cb].valid() && cu.ispMode != ISPType::NONE;
if (area.blocks[COMPONENT_Cb].valid() && (!cu.isSepTree() || partitioner.chType == ChannelType::CHROMA)
&& (cu.ispMode == ISPType::NONE || chromaCbfISP))
{
unsigned cbfDepth = chromaCbfISP ? trDepth - 1 : trDepth;
chromaCbfs.Cb = TU::getCbfAtDepth(tu, COMPONENT_Cb, trDepth);
if (!(cu.sbtInfo && tu.noResidual))
{
cbf_comp(chromaCbfs.Cb, area.blocks[COMPONENT_Cb], cbfDepth, false, false, cu.getBdpcmMode(COMPONENT_Cb));
}
chromaCbfs.Cr = TU::getCbfAtDepth(tu, COMPONENT_Cr, trDepth);
if (!(cu.sbtInfo && tu.noResidual))
{
cbf_comp(chromaCbfs.Cr, area.blocks[COMPONENT_Cr], cbfDepth, chromaCbfs.Cb, false,
cu.getBdpcmMode(COMPONENT_Cr));
}
}
else if (cu.isSepTree())
{
chromaCbfs = ChromaCbfs(false);
}
}
else if (cu.isSepTree())
{
chromaCbfs = ChromaCbfs(false);
}
if (!isChroma(partitioner.chType))
{
if (!CU::isIntra(cu) && trDepth == 0 && !chromaCbfs.sigChroma(area.chromaFormat))
{
CHECK(!TU::getCbfAtDepth(tu, COMPONENT_Y, trDepth), "Luma cbf must be true for inter units with no chroma coeffs");
}
else if (cu.sbtInfo && tu.noResidual)
{
CHECK(TU::getCbfAtDepth(tu, COMPONENT_Y, trDepth), "Luma cbf must be false for inter sbt no-residual tu");
}
else if (cu.sbtInfo && !chromaCbfs.sigChroma(area.chromaFormat))
{
assert(!tu.noResidual);
CHECK(!TU::getCbfAtDepth(tu, COMPONENT_Y, trDepth), "Luma cbf must be true for inter sbt residual tu");
}
else
{
bool lumaCbfIsInferredACT =
(cu.colorTransform && CU::isIntra(cu) && trDepth == 0 && !chromaCbfs.sigChroma(area.chromaFormat));
CHECK(lumaCbfIsInferredACT && !TU::getCbfAtDepth(tu, COMPONENT_Y, trDepth), "adaptive color transform cannot have all zero coefficients");
bool lastCbfIsInferred = lumaCbfIsInferredACT; // ISP and ACT are mutually exclusive
bool previousCbf = false;
bool rootCbfSoFar = false;
{
uint32_t nTus =
cu.ispMode == ISPType::HOR ? cu.lheight() >> floorLog2(tu.lheight()) : cu.lwidth() >> floorLog2(tu.lwidth());
if (subTuCounter == nTus - 1)
{
TransformUnit* tuPointer = cu.firstTU;
for (int tuIdx = 0; tuIdx < subTuCounter; tuIdx++)
{
rootCbfSoFar |= TU::getCbfAtDepth(*tuPointer, COMPONENT_Y, trDepth);
tuPointer = tuPointer->next;
}
if (!rootCbfSoFar)
{
lastCbfIsInferred = true;
}
}
if (!lastCbfIsInferred)
{
previousCbf = TU::getPrevTuCbfAtDepth(tu, COMPONENT_Y, partitioner.currTrDepth);
}
}
if (!lastCbfIsInferred)
{
cbf_comp(TU::getCbfAtDepth(tu, COMPONENT_Y, trDepth), tu.Y(), trDepth, previousCbf, cu.ispMode != ISPType::NONE,
}
}
}
bool lumaOnly = !isChromaEnabled(cu.chromaFormat) || !tu.blocks[COMPONENT_Cb].valid();

Karsten Suehring
committed
bool cbf[3] = { TU::getCbf( tu, COMPONENT_Y ), chromaCbfs.Cb, chromaCbfs.Cr };
bool cbfLuma = ( cbf[ COMPONENT_Y ] != 0 );
bool cbfChroma = false;
Santiago de Luxán Hernández
committed
if( !lumaOnly )

Karsten Suehring
committed
{
if( tu.blocks[COMPONENT_Cb].valid() )
{
cbf [ COMPONENT_Cb ] = TU::getCbf( tu, COMPONENT_Cb );
cbf [ COMPONENT_Cr ] = TU::getCbf( tu, COMPONENT_Cr );
}
cbfChroma = ( cbf[ COMPONENT_Cb ] || cbf[ COMPONENT_Cr ] );
}
if( ( cu.lwidth() > 64 || cu.lheight() > 64 || cbfLuma || cbfChroma ) &&

Karsten Suehring
committed
{
if( cu.cs->pps->getUseDQP() && !cuCtx.isDQPCoded )
{
cu_qp_delta(cu, cuCtx.qp, cu.qp);
cuCtx.qp = cu.qp;
cuCtx.isDQPCoded = true;

Karsten Suehring
committed
}
if (!cu.isSepTree() || isChroma(tu.chType)) // !DUAL_TREE_LUMA
{
SizeType channelWidth = !cu.isSepTree() ? cu.lwidth() : cu.chromaSize().width;
SizeType channelHeight = !cu.isSepTree() ? cu.lheight() : cu.chromaSize().height;
if (cu.cs->slice->getUseChromaQpAdj() && (channelWidth > 64 || channelHeight > 64 || cbfChroma) && !cuCtx.isChromaQpAdjCoded)
{
cu_chroma_qp_offset(cu);
cuCtx.isChromaQpAdjCoded = true;
}
}
if( !lumaOnly )
{
joint_cb_cr( tu, ( cbf[COMPONENT_Cb] ? 2 : 0 ) + ( cbf[COMPONENT_Cr] ? 1 : 0 ) );
}
if (cbfLuma)
{
residual_coding(tu, COMPONENT_Y, &cuCtx);
}
if (!lumaOnly)
{
for (ComponentID compID = COMPONENT_Cb; compID <= COMPONENT_Cr; compID = ComponentID(compID + 1))

Karsten Suehring
committed
{

Karsten Suehring
committed
{

Karsten Suehring
committed
}
}
}
Philippe de Lagrange
committed
DTRACE_COND((isEncoding()), g_trace_ctx, D_DQP, "x=%d, y=%d, d=%d, qpAdj=%d\n", cu.block(cu.chType).lumaPos().x,
cu.block(cu.chType).lumaPos().y, cu.qtDepth, cu.chromaQpAdj);

Karsten Suehring
committed
}
void CABACWriter::cu_qp_delta( const CodingUnit& cu, int predQP, const int8_t qp )
{
CHECK(!( predQP != std::numeric_limits<int>::max()), "Unspecified error");
int DQp = qp - predQP;
int qpBdOffsetY = cu.cs->sps->getQpBDOffset(ChannelType::LUMA);

Karsten Suehring
committed
DQp = ( DQp + (MAX_QP + 1) + (MAX_QP + 1) / 2 + qpBdOffsetY + (qpBdOffsetY / 2)) % ((MAX_QP + 1) + qpBdOffsetY) - (MAX_QP + 1) / 2 - (qpBdOffsetY / 2);
unsigned absDQP = unsigned( DQp < 0 ? -DQp : DQp );
unsigned unaryDQP = std::min<unsigned>( absDQP, CU_DQP_TU_CMAX );
unary_max_symbol( unaryDQP, Ctx::DeltaQP(), Ctx::DeltaQP(1), CU_DQP_TU_CMAX );
if( absDQP >= CU_DQP_TU_CMAX )
{
exp_golomb_eqprob( absDQP - CU_DQP_TU_CMAX, CU_DQP_EG_k );
}
if( absDQP > 0 )
{

Karsten Suehring
committed
}
DTRACE_COND((isEncoding()), g_trace_ctx, D_DQP, "x=%d, y=%d, d=%d, pred_qp=%d, DQp=%d, qp=%d\n",
cu.block(cu.chType).lumaPos().x, cu.block(cu.chType).lumaPos().y, cu.qtDepth, predQP, DQp, qp);

Karsten Suehring
committed
}
void CABACWriter::cu_chroma_qp_offset( const CodingUnit& cu )
{
// cu_chroma_qp_offset_flag
unsigned qpAdj = cu.chromaQpAdj;
if( qpAdj == 0 )
{

Karsten Suehring
committed
}
else
{
int length = cu.cs->pps->getChromaQpOffsetListLen();

Karsten Suehring
committed
if( length > 1 )
{
unary_max_symbol( qpAdj-1, Ctx::ChromaQpAdjIdc(), Ctx::ChromaQpAdjIdc(), length-1 );
}
}
}
//================================================================================
// clause 7.3.8.11
//--------------------------------------------------------------------------------
// void residual_coding ( tu, compID )
// void transform_skip_flag ( tu, compID )
// void last_sig_coeff ( coeffCtx )
// void residual_coding_subblock( coeffCtx )
//================================================================================
void CABACWriter::joint_cb_cr( const TransformUnit& tu, const int cbfMask )
{
if ( !tu.cu->slice->getSPS()->getJointCbCrEnabledFlag() )
CHECK( tu.jointCbCr && tu.jointCbCr != cbfMask, "wrong value of jointCbCr (" << (int)tu.jointCbCr << " vs " << (int)cbfMask << ")" );
if ((CU::isIntra(*tu.cu) && cbfMask != 0) || cbfMask == CBF_MASK_CBCR)
m_binEncoder.encodeBin(tu.jointCbCr ? 1 : 0, Ctx::JointCbCrFlag(cbfMask - 1));
}
}
void CABACWriter::residual_coding( const TransformUnit& tu, ComponentID compID, CUCtx* cuCtx )

Karsten Suehring
committed
{
const CodingUnit& cu = *tu.cu;
DTRACE( g_trace_ctx, D_SYNTAX, "residual_coding() etype=%d pos=(%d,%d) size=%dx%d predMode=%d\n", tu.blocks[compID].compID, tu.blocks[compID].x, tu.blocks[compID].y, tu.blocks[compID].width, tu.blocks[compID].height, cu.predMode );
if( compID == COMPONENT_Cr && tu.jointCbCr == 3 )

Karsten Suehring
committed
if (tu.mtsIdx[compID] == MtsType::SKIP && !tu.cs->slice->getTSResidualCodingDisabledFlag())
{
residual_codingTS( tu, compID );
return;
}

Karsten Suehring
committed
// determine sign hiding
bool signHiding = cu.cs->slice->getSignDataHidingEnabledFlag();

Karsten Suehring
committed
// init coeff coding context
CoeffCodingContext cctx(tu, compID, signHiding, BdpcmMode::NONE);

Karsten Suehring
committed
const TCoeff* coeff = tu.getCoeffs( compID ).buf;
// determine and set last coeff position and sig group flags
int scanPosLast = -1;
std::bitset<MLS_GRP_NUM> sigGroupFlags;
for( int scanPos = 0; scanPos < cctx.maxNumCoeff(); scanPos++)
{
unsigned blkPos = cctx.blockPos( scanPos );
if( coeff[blkPos] )
{
scanPosLast = scanPos;
sigGroupFlags.set( scanPos >> cctx.log2CGSize() );
}
}
CHECK( scanPosLast < 0, "Coefficient coding called for empty TU" );
cctx.setScanPosLast(scanPosLast);
if (cuCtx && tu.mtsIdx[compID] != MtsType::SKIP && tu.blocks[compID].height >= 4 && tu.blocks[compID].width >= 4)
{
const int maxLfnstPos = ((tu.blocks[compID].height == 4 && tu.blocks[compID].width == 4) || (tu.blocks[compID].height == 8 && tu.blocks[compID].width == 8)) ? 7 : 15;
cuCtx->violatesLfnstConstrained[toChannelType(compID)] |= cctx.scanPosLast() > maxLfnstPos;
if (cuCtx && tu.mtsIdx[compID] != MtsType::SKIP && tu.blocks[compID].height >= 4 && tu.blocks[compID].width >= 4)
const int lfnstLastScanPosTh = isLuma( compID ) ? LFNST_LAST_SIG_LUMA : LFNST_LAST_SIG_CHROMA;
cuCtx->lfnstLastScanPos |= cctx.scanPosLast() >= lfnstLastScanPosTh;
if (cuCtx && isLuma(compID) && tu.mtsIdx[compID] != MtsType::SKIP)
{
cuCtx->mtsLastScanPos |= cctx.scanPosLast() >= 1;
}

Karsten Suehring
committed
// code last coeff position

Karsten Suehring
committed
// code subblocks
const int stateTab = ( tu.cs->slice->getDepQuantEnabledFlag() ? 32040 : 0 );

Karsten Suehring
committed
int state = 0;
int ctxBinSampleRatio = (compID == COMPONENT_Y) ? MAX_TU_LEVEL_CTX_CODED_BIN_CONSTRAINT_LUMA : MAX_TU_LEVEL_CTX_CODED_BIN_CONSTRAINT_CHROMA;
cctx.regBinLimit = (tu.getTbAreaAfterCoefZeroOut(compID) * ctxBinSampleRatio) >> 4;
int baseLevel = m_binEncoder.getCtx().getBaseLevel();
cctx.setBaseLevel(baseLevel);
if (tu.cs->slice->getSPS()->getSpsRangeExtension().getPersistentRiceAdaptationEnabledFlag())
{
cctx.setUpdateHist(1);
unsigned riceStats = m_binEncoder.getCtx().getGRAdaptStats((unsigned) compID);
TCoeff historyValue = (TCoeff)1 << riceStats;
cctx.setHistValue(historyValue);
}

Karsten Suehring
committed
for( int subSetId = ( cctx.scanPosLast() >> cctx.log2CGSize() ); subSetId >= 0; subSetId--)
{
cctx.initSubblock ( subSetId, sigGroupFlags[subSetId] );
if (tu.cs->sps->getMtsEnabled() && tu.cu->sbtInfo != 0 && tu.blocks[compID].height <= 32
&& tu.blocks[compID].width <= 32 && compID == COMPONENT_Y)
{
if( ( tu.blocks[ compID ].height == 32 && cctx.cgPosY() >= ( 16 >> cctx.log2CGHeight() ) )
|| ( tu.blocks[ compID ].width == 32 && cctx.cgPosX() >= ( 16 >> cctx.log2CGWidth() ) ) )
{
continue;
}
}
residual_coding_subblock( cctx, coeff, stateTab, state );
if ( cuCtx && isLuma(compID) && cctx.isSigGroup() && ( cctx.cgPosY() > 3 || cctx.cgPosX() > 3 ) )
{
cuCtx->violatesMtsCoeffConstraint = true;
}

Karsten Suehring
committed
}
}
void CABACWriter::ts_flag( const TransformUnit& tu, ComponentID compID )
{
const int tsFlag = tu.mtsIdx[compID] == MtsType::SKIP ? 1 : 0;
int ctxIdx = isLuma(compID) ? 0 : 1;
if( TU::isTSAllowed ( tu, compID ) )
{
m_binEncoder.encodeBin(tsFlag, Ctx::TransformSkipFlag(ctxIdx));
}
DTRACE( g_trace_ctx, D_SYNTAX, "ts_flag() etype=%d pos=(%d,%d) mtsIdx=%d\n", COMPONENT_Y, tu.cu->lx(), tu.cu->ly(), tsFlag );
}
void CABACWriter::mts_idx( const CodingUnit& cu, CUCtx* cuCtx )
{
TransformUnit &tu = *cu.firstTU;
MtsType mtsIdx = tu.mtsIdx[COMPONENT_Y];
if (CU::isMTSAllowed(cu, COMPONENT_Y) && cuCtx && !cuCtx->violatesMtsCoeffConstraint && cuCtx->mtsLastScanPos
&& cu.lfnstIdx == 0 && mtsIdx != MtsType::SKIP)
int symbol = mtsIdx != MtsType::DCT2_DCT2 ? 1 : 0;
ctxIdx = 1;
for( int i = 0; i < 3; i++, ctxIdx++ )
{
symbol = mtsIdx > MtsType::DST7_DST7 + i ? 1 : 0;
if( !symbol )
{
break;
}
}
}
}
DTRACE( g_trace_ctx, D_SYNTAX, "mts_idx() etype=%d pos=(%d,%d) mtsIdx=%d\n", COMPONENT_Y, tu.cu->lx(), tu.cu->ly(), mtsIdx);
}

Karsten Suehring
committed
void CABACWriter::isp_mode( const CodingUnit& cu )
{
if (!CU::isIntra(cu) || !isLuma(cu.chType) || cu.firstPU->multiRefIdx || !cu.cs->sps->getUseISP()
|| cu.bdpcmMode != BdpcmMode::NONE || !CU::canUseISP(cu, getFirstComponentOfChannel(cu.chType))
|| cu.colorTransform)
CHECK(cu.ispMode != ISPType::NONE, "cu.ispMode != 0");
return;
}
}
else
{
m_binEncoder.encodeBin( 1, Ctx::ISPMode( 0 ) );
m_binEncoder.encodeBin(cu.ispMode == ISPType::HOR ? 0 : 1, Ctx::ISPMode(1));
DTRACE(g_trace_ctx, D_SYNTAX, "intra_subPartitions() etype=%d pos=(%d,%d) ispIdx=%d\n", cu.chType,
cu.block(cu.chType).x, cu.block(cu.chType).y, (int) cu.ispMode);

Karsten Suehring
committed
void CABACWriter::residual_lfnst_mode( const CodingUnit& cu, CUCtx& cuCtx )
{
int chIdx = cu.isSepTree() && isChroma(cu.chType) ? 1 : 0;
if ((cu.ispMode != ISPType::NONE && !CU::canUseLfnstWithISP(cu, cu.chType))
|| (cu.cs->sps->getUseLFNST() && CU::isIntra(cu) && cu.mipFlag && !allowLfnstWithMip(cu.firstPU->lumaSize()))
|| (cu.isSepTree() && isChroma(cu.chType) && std::min(cu.blocks[1].width, cu.blocks[1].height) < 4)
|| (cu.blocks[chIdx].lumaSize().width > cu.cs->sps->getMaxTbSize()
|| cu.blocks[chIdx].lumaSize().height > cu.cs->sps->getMaxTbSize()))
{
return;
}
if( cu.cs->sps->getUseLFNST() && CU::isIntra( cu ) )
const bool lumaFlag = cu.isSepTree() ? ( isLuma( cu.chType ) ? true : false ) : true;
const bool chromaFlag = cu.isSepTree() ? ( isChroma( cu.chType ) ? true : false ) : true;
bool nonZeroCoeffNonTsCorner8x8 = (lumaFlag && cuCtx.violatesLfnstConstrained[ChannelType::LUMA])
|| (chromaFlag && cuCtx.violatesLfnstConstrained[ChannelType::CHROMA]);
Hilmi Egilmez
committed
bool isTrSkip = false;
for (auto &currTU : CU::traverseTUs(cu))
{
const uint32_t numValidComp = getNumberValidComponents(cu.chromaFormat);
for (uint32_t compID = COMPONENT_Y; compID < numValidComp; compID++)
{
if (currTU.blocks[compID].valid() && TU::getCbf(currTU, (ComponentID) compID)
&& currTU.mtsIdx[compID] == MtsType::SKIP)
Hilmi Egilmez
committed
{
isTrSkip = true;
break;
}
}
}
if ((!cuCtx.lfnstLastScanPos && cu.ispMode == ISPType::NONE) || nonZeroCoeffNonTsCorner8x8 || isTrSkip)
{
return;
}
}
else
{
return;
}
unsigned cctx = 0;
if (cu.isSepTree())
{
cctx++;
}
const uint32_t idxLFNST = cu.lfnstIdx;
assert( idxLFNST < 3 );
m_binEncoder.encodeBin(idxLFNST ? 1 : 0, Ctx::LFNSTIdx(cctx));
if( idxLFNST )
{
m_binEncoder.encodeBin((idxLFNST - 1) ? 1 : 0, Ctx::LFNSTIdx(2));
}
DTRACE( g_trace_ctx, D_SYNTAX, "residual_lfnst_mode() etype=%d pos=(%d,%d) mode=%d\n", COMPONENT_Y, cu.lx(), cu.ly(), ( int ) cu.lfnstIdx );
}

Karsten Suehring
committed
void CABACWriter::last_sig_coeff( CoeffCodingContext& cctx, const TransformUnit& tu, ComponentID compID )

Karsten Suehring
committed
{
unsigned blkPos = cctx.blockPos( cctx.scanPosLast() );
unsigned posX, posY;
{
posY = blkPos / cctx.width();
posX = blkPos - ( posY * cctx.width() );
}
unsigned CtxLast;
unsigned GroupIdxX = g_groupIdx[posX];
unsigned GroupIdxY = g_groupIdx[posY];

Karsten Suehring
committed
unsigned maxLastPosX = cctx.maxLastPosX();
unsigned maxLastPosY = cctx.maxLastPosY();
unsigned zoTbWdith = getNonzeroTuSize(cctx.width());
unsigned zoTbHeight = getNonzeroTuSize(cctx.height());
if (tu.cs->sps->getMtsEnabled() && tu.cu->sbtInfo != 0 && tu.blocks[compID].width <= 32
&& tu.blocks[compID].height <= 32 && compID == COMPONENT_Y)
maxLastPosX = (tu.blocks[compID].width == 32) ? g_groupIdx[15] : maxLastPosX;
maxLastPosY = (tu.blocks[compID].height == 32) ? g_groupIdx[15] : maxLastPosY;
zoTbWdith = (tu.blocks[compID].width == 32) ? 16 : zoTbWdith;
zoTbHeight = (tu.blocks[compID].height == 32) ? 16 : zoTbHeight;
}
if (isEncoding())
{
if ((posX + posY) > ((zoTbWdith + zoTbHeight + 2) / 2))
{
tu.cu->slice->updateCntRightBottom(1);
}
else
{
tu.cu->slice->updateCntRightBottom(-1);
}
if (tu.cu->slice->getReverseLastSigCoeffFlag())
{
posX = zoTbWdith - 1 - posX;
posY = zoTbHeight - 1 - posY;
GroupIdxX = g_groupIdx[posX];
GroupIdxY = g_groupIdx[posY];
}

Karsten Suehring
committed
for( CtxLast = 0; CtxLast < GroupIdxX; CtxLast++ )
{

Karsten Suehring
committed
}

Karsten Suehring
committed
{

Karsten Suehring
committed
}
for( CtxLast = 0; CtxLast < GroupIdxY; CtxLast++ )
{

Karsten Suehring
committed
}

Karsten Suehring
committed
{

Karsten Suehring
committed
}
if( GroupIdxX > 3 )
{

Karsten Suehring
committed
for (int i = ( ( GroupIdxX - 2 ) >> 1 ) - 1 ; i >= 0; i-- )
{

Karsten Suehring
committed
}
}
if( GroupIdxY > 3 )
{

Karsten Suehring
committed
for ( int i = ( ( GroupIdxY - 2 ) >> 1 ) - 1 ; i >= 0; i-- )
{

Karsten Suehring
committed
}
}
}
void CABACWriter::residual_coding_subblock( CoeffCodingContext& cctx, const TCoeff* coeff, const int stateTransTable, int& state )
{
//===== init =====
const int minSubPos = cctx.minSubPos();
const bool isLast = cctx.isLast();
int firstSigPos = ( isLast ? cctx.scanPosLast() : cctx.maxSubPos() );
int nextSigPos = firstSigPos;
int baseLevel = cctx.getBaseLevel();
bool updateHistory = cctx.getUpdateHist();

Karsten Suehring
committed
//===== encode significant_coeffgroup_flag =====
if( !isLast && cctx.isNotFirst() )
{
if( cctx.isSigGroup() )
{

Karsten Suehring
committed
}
else
{

Karsten Suehring
committed
return;
}
}
uint8_t ctxOffset[16];
//===== encode absolute values =====
const int inferSigPos = nextSigPos != cctx.scanPosLast() ? (cctx.isNotFirst() ? minSubPos : -1) : nextSigPos;
int firstNZPos = nextSigPos;
int lastNZPos = -1;
int numNonZero = 0;
unsigned signPattern = 0;
int remRegBins = cctx.regBinLimit;
int firstPos2ndPass = minSubPos - 1;

Karsten Suehring
committed
for( ; nextSigPos >= minSubPos && remRegBins >= 4; nextSigPos-- )

Karsten Suehring
committed
{
const TCoeff level = coeff[cctx.blockPos(nextSigPos)];
unsigned sigFlag = (level != 0);

Karsten Suehring
committed
if( numNonZero || nextSigPos != inferSigPos )
{
const unsigned sigCtxId = cctx.sigCtxIdAbs( nextSigPos, coeff, state );

Karsten Suehring
committed
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "sig_bin() bin=%d ctx=%d\n", sigFlag, sigCtxId );
remRegBins--;

Karsten Suehring
committed
}
else if( nextSigPos != cctx.scanPosLast() )
{
cctx.sigCtxIdAbs( nextSigPos, coeff, state ); // required for setting variables that are needed for gtx/par context selection
}

Karsten Suehring
committed
if( sigFlag )
{
uint8_t& ctxOff = ctxOffset[ nextSigPos - minSubPos ];
ctxOff = cctx.ctxOffsetAbs();
numNonZero++;
firstNZPos = nextSigPos;
lastNZPos = std::max<int>( lastNZPos, nextSigPos );
const TCoeff absLevel = abs(level);

Karsten Suehring
committed
if (nextSigPos != cctx.scanPosLast())
{
signPattern <<= 1;
}

Karsten Suehring
committed
const bool gt1 = absLevel > 1;
m_binEncoder.encodeBin( gt1, cctx.greater1CtxIdAbs(ctxOff) );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "gt1_flag() bin=%d ctx=%d\n", gt1, cctx.greater1CtxIdAbs(ctxOff) );
remRegBins--;
if( gt1 )
{
m_binEncoder.encodeBin(absLevel & 1, cctx.parityCtxIdAbs(ctxOff));
DTRACE(g_trace_ctx, D_SYNTAX_RESI, "par_flag() bin=%d ctx=%d\n", absLevel & 1, cctx.parityCtxIdAbs(ctxOff));
remRegBins--;
const bool gt2 = absLevel > 3;
m_binEncoder.encodeBin(gt2, cctx.greater2CtxIdAbs(ctxOff));
DTRACE(g_trace_ctx, D_SYNTAX_RESI, "gt2_flag() bin=%d ctx=%d\n", gt2, cctx.greater2CtxIdAbs(ctxOff));
remRegBins--;
if (gt2)
{
// Start 2nd pass with first coeff that has absLevel > 3
firstPos2ndPass = std::max(firstPos2ndPass, nextSigPos);
}

Karsten Suehring
committed
}
state = (stateTransTable >> ((state << 2) + ((level & 1) << 1))) & 3;

Karsten Suehring
committed
}
int minPos2ndPass = nextSigPos;
cctx.regBinLimit = remRegBins;

Karsten Suehring
committed
//===== 2nd PASS: Go-rice codes =====
for (int scanPos = firstPos2ndPass; scanPos > minPos2ndPass; scanPos--)
unsigned absLevel = (unsigned) abs( coeff[ cctx.blockPos( scanPos ) ] );
if( absLevel >= 4 )
{
const unsigned ricePar = (cctx.*(cctx.deriveRiceRRC))(scanPos, coeff, baseLevel);
unsigned rem = ( absLevel - 4 ) >> 1;
m_binEncoder.encodeRemAbsEP(rem, ricePar, COEF_REMAIN_BIN_REDUCTION, cctx.maxLog2TrDRange());
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "rem_val() bin=%d ctx=%d\n", rem, ricePar );