Newer
Older

Karsten Suehring
committed
// init coeff coding context
CoeffCodingContext cctx ( tu, compID, signHiding );
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 != MTS_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;
}
#endif

Karsten Suehring
committed
// code last coeff position

Karsten Suehring
committed
// code subblocks
const int stateTab = ( tu.cs->slice->getDepQuantEnabledFlag() ? 32040 : 0 );
int state = 0;
#if JVET_O0052_TU_LEVEL_CTX_CODED_BIN_CONSTRAINT
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;
#endif

Karsten Suehring
committed
for( int subSetId = ( cctx.scanPosLast() >> cctx.log2CGSize() ); subSetId >= 0; subSetId--)
{
cctx.initSubblock ( subSetId, sigGroupFlags[subSetId] );
if( ( tu.mtsIdx > MTS_SKIP || ( tu.cu->sbtInfo != 0 && tu.blocks[ compID ].height <= 32 && tu.blocks[ compID ].width <= 32 ) ) && !tu.cu->transQuantBypass && 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 );

Karsten Suehring
committed
}
}
void CABACWriter::mts_coding( const TransformUnit& tu, ComponentID compID )
{
const bool tsAllowed = TU::isTSAllowed ( tu, compID );
const bool mtsAllowed = TU::isMTSAllowed( tu, compID );
if( !mtsAllowed && !tsAllowed ) return;
int symbol = 0;
int ctxIdx = 0;
if( tsAllowed )
{
#if JVET_O0294_TRANSFORM_CLEANUP
symbol = (tu.mtsIdx == MTS_SKIP) ? 1 : 0;
#else
symbol = (tu.mtsIdx == MTS_SKIP) ? 0 : 1;
ctxIdx = 6;
m_BinEncoder.encodeBin( symbol, Ctx::MTSIndex( ctxIdx ) );
}
symbol = tu.mtsIdx != MTS_DCT2_DCT2 ? 1 : 0;
#if JVET_O0294_TRANSFORM_CLEANUP
ctxIdx = 0;
#else

Karsten Suehring
committed
if( symbol )
{
ctxIdx = 7;
for( int i = 0; i < 3; i++, ctxIdx++ )
{
symbol = tu.mtsIdx > i + MTS_DST7_DST7 ? 1 : 0;
m_BinEncoder.encodeBin( symbol, Ctx::MTSIndex( ctxIdx ) );
if( !symbol )
{
break;
}
}
}
}
}
#if JVET_O0294_TRANSFORM_CLEANUP
DTRACE( g_trace_ctx, D_SYNTAX, "mts_coding() etype=%d pos=(%d,%d) mtsIdx=%d\n", COMPONENT_Y, tu.cu->lx(), tu.cu->ly(), tu.mtsIdx);
#else
DTRACE( g_trace_ctx, D_SYNTAX, "mts_coding() etype=%d pos=(%d,%d) mtsIdx=%d\n", COMPONENT_Y, cu.lx(), cu.ly(), tu.mtsIdx );

Karsten Suehring
committed
void CABACWriter::isp_mode( const CodingUnit& cu )
{
if( !CU::isIntra( cu ) || !isLuma( cu.chType ) || cu.firstPU->multiRefIdx || cu.ipcm || !cu.cs->sps->getUseISP() || cu.bdpcmMode )
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
{
CHECK( cu.ispMode != NOT_INTRA_SUBPARTITIONS, "error: cu.intraSubPartitions != 0" );
return;
}
const ISPType allowedSplits = CU::canUseISPSplit( cu, getFirstComponentOfChannel( cu.chType ) );
if( allowedSplits == NOT_INTRA_SUBPARTITIONS ) return;
if( cu.ispMode == NOT_INTRA_SUBPARTITIONS )
{
m_BinEncoder.encodeBin( 0, Ctx::ISPMode( 0 ) );
}
else
{
m_BinEncoder.encodeBin( 1, Ctx::ISPMode( 0 ) );
if( allowedSplits == CAN_USE_VER_AND_HORL_SPLITS )
{
m_BinEncoder.encodeBin( cu.ispMode - 1, Ctx::ISPMode( 1 ) );
}
}
DTRACE( g_trace_ctx, D_SYNTAX, "intra_subPartitions() etype=%d pos=(%d,%d) ispIdx=%d\n", cu.chType, cu.blocks[cu.chType].x, cu.blocks[cu.chType].y, (int)cu.ispMode );
}

Karsten Suehring
committed
void CABACWriter::explicit_rdpcm_mode( const TransformUnit& tu, ComponentID compID )
{
const CodingUnit& cu = *tu.cu;
if( !CU::isIntra(cu) && CU::isRDPCMEnabled(cu) && ( tu.mtsIdx==MTS_SKIP || cu.transQuantBypass ) )

Karsten Suehring
committed
{
ChannelType chType = toChannelType( compID );
switch( tu.rdpcm[compID] )
{
case RDPCM_VER:
m_BinEncoder.encodeBin( 1, Ctx::RdpcmFlag(chType) );
m_BinEncoder.encodeBin( 1, Ctx::RdpcmDir (chType) );
break;
case RDPCM_HOR:
m_BinEncoder.encodeBin( 1, Ctx::RdpcmFlag(chType) );
m_BinEncoder.encodeBin( 0, Ctx::RdpcmDir (chType) );
break;
default: // RDPCM_OFF
m_BinEncoder.encodeBin( 0, Ctx::RdpcmFlag(chType) );
}
}
}
void CABACWriter::residual_lfnst_mode( const CodingUnit& cu, CUCtx& cuCtx )
{
if( cu.ispMode != NOT_INTRA_SUBPARTITIONS || cu.mipFlag == true ||
( CS::isDualITree( *cu.cs ) && cu.chType == CHANNEL_TYPE_CHROMA && std::min( cu.blocks[ 1 ].width, cu.blocks[ 1 ].height ) < 4 ) )
{
return;
}
if( cu.cs->sps->getUseLFNST() && CU::isIntra( cu ) && !CU::isLosslessCoded( cu ) )
{
const bool lumaFlag = CS::isDualITree( *cu.cs ) ? ( isLuma( cu.chType ) ? true : false ) : true;
const bool chromaFlag = CS::isDualITree( *cu.cs ) ? ( isChroma( cu.chType ) ? true : false ) : true;
bool nonZeroCoeffNonTs;
bool nonZeroCoeffNonTsCorner8x8 = ( lumaFlag && cuCtx.violatesLfnstConstrained[CHANNEL_TYPE_LUMA] ) || (chromaFlag && cuCtx.violatesLfnstConstrained[CHANNEL_TYPE_CHROMA] );
#else
bool nonZeroCoeffNonTsCorner8x8 = CU::getNumNonZeroCoeffNonTsCorner8x8( cu, lumaFlag, chromaFlag ) > 0;
const int nonZeroCoeffThr = CS::isDualITree( *cu.cs ) ? ( isLuma( cu.chType ) ? LFNST_SIG_NZ_LUMA : LFNST_SIG_NZ_CHROMA ) : LFNST_SIG_NZ_LUMA + LFNST_SIG_NZ_CHROMA;
cuCtx.numNonZeroCoeffNonTs = CU::getNumNonZeroCoeffNonTs( cu, lumaFlag, chromaFlag );
nonZeroCoeffNonTs = cuCtx.numNonZeroCoeffNonTs > nonZeroCoeffThr;
Hilmi Egilmez
committed
#if JVET_O0368_LFNST_WITH_DCT2_ONLY
const bool isNonDCT2 = (TU::getCbf(*cu.firstTU, ComponentID(COMPONENT_Y)) && cu.firstTU->mtsIdx != MTS_DCT2_DCT2);
if (!nonZeroCoeffNonTs || nonZeroCoeffNonTsCorner8x8 || isNonDCT2 )
#else
if( !nonZeroCoeffNonTs || nonZeroCoeffNonTsCorner8x8 )
Hilmi Egilmez
committed
#endif
{
return;
}
}
else
{
return;
}
unsigned cctx = 0;
Hilmi Egilmez
committed
#if JVET_O0368_LFNST_WITH_DCT2_ONLY
if ( CS::isDualITree(*cu.cs) ) cctx++;
#else
if( cu.firstTU->mtsIdx < MTS_DST7_DST7 && CS::isDualITree( *cu.cs ) ) cctx++;
Hilmi Egilmez
committed
#endif
const uint32_t idxLFNST = cu.lfnstIdx;
assert( idxLFNST < 3 );
m_BinEncoder.encodeBin( idxLFNST ? 1 : 0, Ctx::LFNSTIdx( cctx ) );
if( idxLFNST )
{
m_BinEncoder.encodeBinEP( ( idxLFNST - 1 ) ? 1 : 0 );
}
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_uiGroupIdx[ posX ];
unsigned GroupIdxY = g_uiGroupIdx[ posY ];
unsigned maxLastPosX = cctx.maxLastPosX();
unsigned maxLastPosY = cctx.maxLastPosY();
if( ( tu.mtsIdx > MTS_SKIP || ( tu.cu->sbtInfo != 0 && tu.blocks[ compID ].width <= 32 && tu.blocks[ compID ].height <= 32 ) ) && !tu.cu->transQuantBypass && compID == COMPONENT_Y )
maxLastPosX = ( tu.blocks[compID].width == 32 ) ? g_uiGroupIdx[ 15 ] : maxLastPosX;
maxLastPosY = ( tu.blocks[compID].height == 32 ) ? g_uiGroupIdx[ 15 ] : maxLastPosY;

Karsten Suehring
committed
for( CtxLast = 0; CtxLast < GroupIdxX; CtxLast++ )
{
m_BinEncoder.encodeBin( 1, cctx.lastXCtxId( CtxLast ) );
}

Karsten Suehring
committed
{
m_BinEncoder.encodeBin( 0, cctx.lastXCtxId( CtxLast ) );
}
for( CtxLast = 0; CtxLast < GroupIdxY; CtxLast++ )
{
m_BinEncoder.encodeBin( 1, cctx.lastYCtxId( CtxLast ) );
}

Karsten Suehring
committed
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
{
m_BinEncoder.encodeBin( 0, cctx.lastYCtxId( CtxLast ) );
}
if( GroupIdxX > 3 )
{
posX -= g_uiMinInGroup[ GroupIdxX ];
for (int i = ( ( GroupIdxX - 2 ) >> 1 ) - 1 ; i >= 0; i-- )
{
m_BinEncoder.encodeBinEP( ( posX >> i ) & 1 );
}
}
if( GroupIdxY > 3 )
{
posY -= g_uiMinInGroup[ GroupIdxY ];
for ( int i = ( ( GroupIdxY - 2 ) >> 1 ) - 1 ; i >= 0; i-- )
{
m_BinEncoder.encodeBinEP( ( posY >> i ) & 1 );
}
}
}
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;
//===== encode significant_coeffgroup_flag =====
if( !isLast && cctx.isNotFirst() )
{
if( cctx.isSigGroup() )
{
m_BinEncoder.encodeBin( 1, cctx.sigGroupCtxId() );
}
else
{
m_BinEncoder.encodeBin( 0, cctx.sigGroupCtxId() );
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 remAbsLevel = -1;
int numNonZero = 0;
unsigned signPattern = 0;
#if JVET_O0052_TU_LEVEL_CTX_CODED_BIN_CONSTRAINT
int remRegBins = cctx.regBinLimit;
#else
bool is2x2subblock = ( cctx.log2CGSize() == 2 );
int remRegBins = ( is2x2subblock ? MAX_NUM_REG_BINS_2x2SUBBLOCK : MAX_NUM_REG_BINS_4x4SUBBLOCK );
int firstPosMode2 = minSubPos - 1;

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

Karsten Suehring
committed
{
TCoeff Coeff = coeff[ cctx.blockPos( nextSigPos ) ];
unsigned sigFlag = ( Coeff != 0 );
if( numNonZero || nextSigPos != inferSigPos )
{
const unsigned sigCtxId = cctx.sigCtxIdAbs( nextSigPos, coeff, state );
m_BinEncoder.encodeBin( sigFlag, sigCtxId );
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 );
remAbsLevel = abs( Coeff ) - 1;
if( nextSigPos != cctx.scanPosLast() ) signPattern <<= 1;
if( Coeff < 0 ) signPattern++;
unsigned gt1 = !!remAbsLevel;
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 )
{
remAbsLevel -= 1;
m_BinEncoder.encodeBin( remAbsLevel&1, cctx.parityCtxIdAbs( ctxOff ) );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "par_flag() bin=%d ctx=%d\n", remAbsLevel&1, cctx.parityCtxIdAbs( ctxOff ) );
remAbsLevel >>= 1;
remRegBins--;
unsigned gt2 = !!remAbsLevel;
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--;

Karsten Suehring
committed
}
state = ( stateTransTable >> ((state<<2)+((Coeff&1)<<1)) ) & 3;
}
firstPosMode2 = nextSigPos;
#if JVET_O0052_TU_LEVEL_CTX_CODED_BIN_CONSTRAINT
cctx.regBinLimit = remRegBins;
#endif

Karsten Suehring
committed
//===== 2nd PASS: Go-rice codes =====
unsigned ricePar = 0;
for( int scanPos = firstSigPos; scanPos > firstPosMode2; scanPos-- )
int sumAll = cctx.templateAbsSum(scanPos, coeff, 4);
ricePar = g_auiGoRiceParsCoeff[sumAll];
unsigned absLevel = abs( coeff[ cctx.blockPos( scanPos ) ] );
if( absLevel >= 4 )
{
unsigned rem = ( absLevel - 4 ) >> 1;
m_BinEncoder.encodeRemAbsEP( rem, ricePar, cctx.extPrec(), cctx.maxLog2TrDRange() );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "rem_val() bin=%d ctx=%d\n", rem, ricePar );
}
}
//===== coeff bypass ====
for( int scanPos = firstPosMode2; scanPos >= minSubPos; scanPos-- )
{
TCoeff Coeff = coeff[ cctx.blockPos( scanPos ) ];
unsigned absLevel = abs( Coeff );
int sumAll = cctx.templateAbsSum(scanPos, coeff, 0);
int rice = g_auiGoRiceParsCoeff [sumAll];
int pos0 = g_auiGoRicePosCoeff0[std::max(0, state - 1)][sumAll];
unsigned rem = ( absLevel == 0 ? pos0 : absLevel <= pos0 ? absLevel-1 : absLevel );
m_BinEncoder.encodeRemAbsEP( rem, rice, cctx.extPrec(), cctx.maxLog2TrDRange() );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "rem_val() bin=%d ctx=%d\n", rem, rice );
state = ( stateTransTable >> ((state<<2)+((absLevel&1)<<1)) ) & 3;
if( absLevel )
{
numNonZero++;
lastNZPos = std::max<int>( lastNZPos, scanPos );
signPattern <<= 1;
if( Coeff < 0 ) signPattern++;
}
}

Karsten Suehring
committed
//===== encode sign's =====
unsigned numSigns = numNonZero;
if( cctx.hideSign( firstNZPos, lastNZPos ) )
{
numSigns --;
signPattern >>= 1;
}
m_BinEncoder.encodeBinsEP( signPattern, numSigns );
}
void CABACWriter::residual_codingTS( const TransformUnit& tu, ComponentID compID )
{
DTRACE( g_trace_ctx, D_SYNTAX, "residual_codingTS() etype=%d pos=(%d,%d) size=%dx%d\n", tu.blocks[compID].compID, tu.blocks[compID].x, tu.blocks[compID].y, tu.blocks[compID].width, tu.blocks[compID].height );
// init coeff coding context
CoeffCodingContext cctx ( tu, compID, false, tu.cu->bdpcmMode );
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
const TCoeff* coeff = tu.getCoeffs( compID ).buf;
cctx.setNumCtxBins( 2 * tu.lwidth()*tu.lheight() );
// determine and set last coeff position and sig group flags
std::bitset<MLS_GRP_NUM> sigGroupFlags;
for( int scanPos = 0; scanPos < cctx.maxNumCoeff(); scanPos++)
{
unsigned blkPos = cctx.blockPos( scanPos );
if( coeff[blkPos] )
{
sigGroupFlags.set( scanPos >> cctx.log2CGSize() );
}
}
// code subblocks
for( int subSetId = 0; subSetId <= ( cctx.maxNumCoeff() - 1 ) >> cctx.log2CGSize(); subSetId++ )
{
cctx.initSubblock ( subSetId, sigGroupFlags[subSetId] );
residual_coding_subblockTS( cctx, coeff );
}
}
void CABACWriter::residual_coding_subblockTS( CoeffCodingContext& cctx, const TCoeff* coeff )
{
//===== init =====
const int minSubPos = cctx.maxSubPos();
int firstSigPos = cctx.minSubPos();
int nextSigPos = firstSigPos;
//===== encode significant_coeffgroup_flag =====
if( !cctx.isLastSubSet() || !cctx.only1stSigGroup() )
{
if( cctx.isSigGroup() )
{
if( cctx.isContextCoded() )
{
m_BinEncoder.encodeBin( 1, cctx.sigGroupCtxId( true ) );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_sigGroup() bin=%d ctx=%d\n", 1, cctx.sigGroupCtxId() );
}
else
{
m_BinEncoder.encodeBinEP( 1 );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_sigGroup() EPbin=%d\n", 1 );
}
}
else
{
if( cctx.isContextCoded() )
{
m_BinEncoder.encodeBin( 0, cctx.sigGroupCtxId( true ) );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_sigGroup() bin=%d ctx=%d\n", 0, cctx.sigGroupCtxId() );
}
else
{
m_BinEncoder.encodeBinEP( 0 );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_sigGroup() EPbin=%d\n", 0 );
}
return;
}
}
//===== encode absolute values =====
const int inferSigPos = minSubPos;
int remAbsLevel = -1;
int numNonZero = 0;
#if JVET_O0122_TS_SIGN_LEVEL
int rightPixel, belowPixel, modAbsCoeff;
#endif
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
for( ; nextSigPos <= minSubPos; nextSigPos++ )
{
TCoeff Coeff = coeff[ cctx.blockPos( nextSigPos ) ];
unsigned sigFlag = ( Coeff != 0 );
if( numNonZero || nextSigPos != inferSigPos )
{
if( cctx.isContextCoded() )
{
const unsigned sigCtxId = cctx.sigCtxIdAbsTS( nextSigPos, coeff );
m_BinEncoder.encodeBin( sigFlag, sigCtxId );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_sig_bin() bin=%d ctx=%d\n", sigFlag, sigCtxId );
}
else
{
m_BinEncoder.encodeBinEP( sigFlag );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_sig_bin() EPbin=%d\n", sigFlag );
}
}
if( sigFlag )
{
//===== encode sign's =====
int sign = Coeff < 0;
if( cctx.isContextCoded() )
{
#if JVET_O0122_TS_SIGN_LEVEL
const unsigned signCtxId = cctx.signCtxIdAbsTS(nextSigPos, coeff, cctx.bdpcm());
m_BinEncoder.encodeBin(sign, signCtxId);
#else
m_BinEncoder.encodeBin( sign, Ctx::TsResidualSign( cctx.bdpcm() ? 1 : 0 ) );
#endif
}
else
{
m_BinEncoder.encodeBinEP( sign );
}
numNonZero++;
#if JVET_O0122_TS_SIGN_LEVEL
cctx.neighTS(rightPixel, belowPixel, nextSigPos, coeff);
modAbsCoeff = cctx.deriveModCoeff(rightPixel, belowPixel, abs(Coeff), cctx.bdpcm());
remAbsLevel = modAbsCoeff - 1;
#else
remAbsLevel = abs( Coeff ) - 1;
#endif
unsigned gt1 = !!remAbsLevel;
#if JVET_O0122_TS_SIGN_LEVEL
const unsigned gt1CtxId = cctx.lrg1CtxIdAbsTS(nextSigPos, coeff, cctx.bdpcm());
if (cctx.isContextCoded())
{
m_BinEncoder.encodeBin(gt1, gt1CtxId);
DTRACE(g_trace_ctx, D_SYNTAX_RESI, "ts_gt1_flag() bin=%d ctx=%d\n", gt1, gt1CtxId);
}
else
{
m_BinEncoder.encodeBinEP(gt1);
DTRACE(g_trace_ctx, D_SYNTAX_RESI, "ts_gt1_flag() EPbin=%d\n", gt1);
}
#else
if( cctx.isContextCoded() )
{
m_BinEncoder.encodeBin( gt1, cctx.greaterXCtxIdAbsTS(0) );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_gt1_flag() bin=%d ctx=%d\n", gt1, cctx.greaterXCtxIdAbsTS(0) );
}
else
{
m_BinEncoder.encodeBinEP( gt1 );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_gt1_flag() EPbin=%d\n", gt1 );
}
#endif
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
if( gt1 )
{
remAbsLevel -= 1;
if( cctx.isContextCoded() )
{
m_BinEncoder.encodeBin( remAbsLevel&1, cctx.parityCtxIdAbsTS() );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_par_flag() bin=%d ctx=%d\n", remAbsLevel&1, cctx.parityCtxIdAbsTS() );
}
else
{
m_BinEncoder.encodeBinEP( remAbsLevel&1 );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_par_flag() EPbin=%d\n", remAbsLevel&1 );
}
}
}
}
int cutoffVal = 2;
int numGtBins = 4;
Mohammed Golam Sarwer
committed
#if JVET_O0619_GTX_SINGLE_PASS_TS_RESIDUAL_CODING
for (int scanPos = firstSigPos; scanPos <= minSubPos; scanPos++)
{
#if JVET_O0122_TS_SIGN_LEVEL
unsigned absLevel;
cctx.neighTS(rightPixel, belowPixel, scanPos, coeff);
absLevel = cctx.deriveModCoeff(rightPixel, belowPixel, abs(coeff[cctx.blockPos(scanPos)]), cctx.bdpcm());
#else
Mohammed Golam Sarwer
committed
unsigned absLevel = abs(coeff[cctx.blockPos(scanPos)]);
#endif
Mohammed Golam Sarwer
committed
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
cutoffVal = 2;
for (int i = 0; i < numGtBins; i++)
{
if (absLevel >= cutoffVal)
{
unsigned gt2 = (absLevel >= (cutoffVal + 2));
if (cctx.isContextCoded())
{
m_BinEncoder.encodeBin(gt2, cctx.greaterXCtxIdAbsTS(cutoffVal >> 1));
DTRACE(g_trace_ctx, D_SYNTAX_RESI, "ts_gt%d_flag() bin=%d ctx=%d sp=%d coeff=%d\n", i, gt2, cctx.greaterXCtxIdAbsTS(cutoffVal >> 1), scanPos, min<int>(absLevel, cutoffVal + 2));
}
else
{
m_BinEncoder.encodeBinEP(gt2);
DTRACE(g_trace_ctx, D_SYNTAX_RESI, "ts_gt%d_flag() EPbin=%d sp=%d coeff=%d\n", i, gt2, scanPos, min<int>(absLevel, cutoffVal + 2));
}
}
cutoffVal += 2;
}
}
#else
for( int i = 0; i < numGtBins; i++ )
{
for( int scanPos = firstSigPos; scanPos <= minSubPos; scanPos++ )
{
#if JVET_O0122_TS_SIGN_LEVEL
unsigned absLevel;
cctx.neighTS(rightPixel, belowPixel, scanPos, coeff);
absLevel = cctx.deriveModCoeff(rightPixel, belowPixel, abs(coeff[cctx.blockPos(scanPos)]), cctx.bdpcm());
#else
unsigned absLevel = abs( coeff[cctx.blockPos( scanPos )] );
#endif
if( absLevel >= cutoffVal )
{
unsigned gt2 = ( absLevel >= ( cutoffVal + 2 ) );
if( cctx.isContextCoded() )
{
m_BinEncoder.encodeBin( gt2, cctx.greaterXCtxIdAbsTS( cutoffVal>>1 ) );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_gt%d_flag() bin=%d ctx=%d sp=%d coeff=%d\n", i, gt2, cctx.greaterXCtxIdAbsTS( cutoffVal>>1 ), scanPos, min<int>( absLevel, cutoffVal+2 ) );
}
else
{
m_BinEncoder.encodeBinEP( gt2 );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_gt%d_flag() EPbin=%d sp=%d coeff=%d\n", i, gt2, scanPos, min<int>( absLevel, cutoffVal+2 ) );
}
}
}
cutoffVal += 2;
}
Mohammed Golam Sarwer
committed
#endif
//===== coeff bypass ====
for( int scanPos = firstSigPos; scanPos <= minSubPos; scanPos++ )
{
#if JVET_O0122_TS_SIGN_LEVEL
unsigned absLevel;
cctx.neighTS(rightPixel, belowPixel, scanPos, coeff);
absLevel = cctx.deriveModCoeff(rightPixel, belowPixel, abs(coeff[cctx.blockPos(scanPos)]), cctx.bdpcm());
#else
TCoeff Coeff = coeff[ cctx.blockPos( scanPos ) ];
unsigned absLevel = abs( Coeff );
#endif
if( absLevel >= cutoffVal )
{
int rice = cctx.templateAbsSumTS( scanPos, coeff );
unsigned rem = ( absLevel - cutoffVal ) >> 1;
m_BinEncoder.encodeRemAbsEP( rem, rice, cctx.extPrec(), cctx.maxLog2TrDRange() );
DTRACE( g_trace_ctx, D_SYNTAX_RESI, "ts_rem_val() bin=%d ctx=%d sp=%d\n", rem, rice, scanPos );
}
}
}

Karsten Suehring
committed
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
//================================================================================
// clause 7.3.8.12
//--------------------------------------------------------------------------------
// void cross_comp_pred( tu, compID )
//================================================================================
void CABACWriter::cross_comp_pred( const TransformUnit& tu, ComponentID compID )
{
CHECK(!( !isLuma( compID ) ), "Unspecified error");
signed char alpha = tu.compAlpha[compID];
unsigned ctxBase = ( compID == COMPONENT_Cr ? 5 : 0 );
if( alpha == 0 )
{
m_BinEncoder.encodeBin( 0, Ctx::CrossCompPred( ctxBase ) );
DTRACE( g_trace_ctx, D_SYNTAX, "cross_comp_pred() etype=%d pos=(%d,%d) alpha=%d\n", compID, tu.blocks[compID].x, tu.blocks[compID].y, tu.compAlpha[compID] );
return;
}
static const unsigned log2AbsAlphaMinus1Table[8] = { 0, 1, 1, 2, 2, 2, 3, 3 };
unsigned sign = ( alpha < 0 );
if( sign )
{
alpha = -alpha;
}
CHECK(!( alpha <= 8 ), "Unspecified error");
m_BinEncoder.encodeBin( 1, Ctx::CrossCompPred(ctxBase) );
if( alpha > 1)
{
m_BinEncoder.encodeBin( 1, Ctx::CrossCompPred(ctxBase+1) );
unary_max_symbol( log2AbsAlphaMinus1Table[alpha-1]-1, Ctx::CrossCompPred(ctxBase+2), Ctx::CrossCompPred(ctxBase+3), 2 );
}
else
{
m_BinEncoder.encodeBin( 0, Ctx::CrossCompPred(ctxBase+1) );
}
m_BinEncoder.encodeBin( sign, Ctx::CrossCompPred(ctxBase+4) );
DTRACE( g_trace_ctx, D_SYNTAX, "cross_comp_pred() etype=%d pos=(%d,%d) alpha=%d\n", compID, tu.blocks[compID].x, tu.blocks[compID].y, tu.compAlpha[compID] );
}
//================================================================================
// helper functions
//--------------------------------------------------------------------------------
// void unary_max_symbol ( symbol, ctxId0, ctxIdN, maxSymbol )
// void unary_max_eqprob ( symbol, maxSymbol )
// void exp_golomb_eqprob ( symbol, count )
//================================================================================
void CABACWriter::unary_max_symbol( unsigned symbol, unsigned ctxId0, unsigned ctxIdN, unsigned maxSymbol )
{
CHECK( symbol > maxSymbol, "symbol > maxSymbol" );
const unsigned totalBinsToWrite = std::min( symbol + 1, maxSymbol );
for( unsigned binsWritten = 0; binsWritten < totalBinsToWrite; ++binsWritten )
{
const unsigned nextBin = symbol > binsWritten;
m_BinEncoder.encodeBin( nextBin, binsWritten == 0 ? ctxId0 : ctxIdN );
}
}
void CABACWriter::unary_max_eqprob( unsigned symbol, unsigned maxSymbol )
{
if( maxSymbol == 0 )
{
return;
}
bool codeLast = ( maxSymbol > symbol );
unsigned bins = 0;
unsigned numBins = 0;
while( symbol-- )
{
bins <<= 1;
bins ++;
numBins++;
}
if( codeLast )
{
bins <<= 1;
numBins++;
}
CHECK(!( numBins <= 32 ), "Unspecified error");
m_BinEncoder.encodeBinsEP( bins, numBins );
}
void CABACWriter::exp_golomb_eqprob( unsigned symbol, unsigned count )
{
unsigned bins = 0;
unsigned numBins = 0;
while( symbol >= (unsigned)(1<<count) )
{
bins <<= 1;
bins++;
numBins++;
symbol -= 1 << count;
count++;
}
bins <<= 1;
numBins++;
bins = (bins << count) | symbol;
numBins += count;
CHECK(!( numBins <= 32 ), "Unspecified error");
m_BinEncoder.encodeBinsEP( bins, numBins );
}
void CABACWriter::codeAlfCtuEnableFlags( CodingStructure& cs, ChannelType channel, AlfSliceParam* alfParam)
{
if( isLuma( channel ) )
{
if (alfParam->enabledFlag[COMPONENT_Y])
codeAlfCtuEnableFlags( cs, COMPONENT_Y, alfParam );
}
else
{
if (alfParam->enabledFlag[COMPONENT_Cb])
codeAlfCtuEnableFlags( cs, COMPONENT_Cb, alfParam );
if (alfParam->enabledFlag[COMPONENT_Cr])
codeAlfCtuEnableFlags( cs, COMPONENT_Cr, alfParam );
}
}
void CABACWriter::codeAlfCtuEnableFlags( CodingStructure& cs, ComponentID compID, AlfSliceParam* alfParam)
{
uint32_t numCTUs = cs.pcv->sizeInCtus;
for( int ctuIdx = 0; ctuIdx < numCTUs; ctuIdx++ )
{
codeAlfCtuEnableFlag( cs, ctuIdx, compID, alfParam );
}
}
void CABACWriter::codeAlfCtuEnableFlag( CodingStructure& cs, uint32_t ctuRsAddr, const int compIdx, AlfSliceParam* alfParam)
{
const bool alfComponentEnabled = (alfParam != NULL) ? alfParam->enabledFlag[compIdx] : cs.slice->getTileGroupAlfEnabledFlag((ComponentID)compIdx);

Karsten Suehring
committed
if( cs.sps->getALFEnabledFlag() && alfComponentEnabled )

Karsten Suehring
committed
{
const PreCalcValues& pcv = *cs.pcv;
int frame_width_in_ctus = pcv.widthInCtus;
int ry = ctuRsAddr / frame_width_in_ctus;
int rx = ctuRsAddr - ry * frame_width_in_ctus;
const Position pos( rx * cs.pcv->maxCUWidth, ry * cs.pcv->maxCUHeight );
const uint32_t curSliceIdx = cs.slice->getIndependentSliceIdx();
const uint32_t curTileIdx = cs.picture->brickMap->getBrickIdxRsMap( pos );
bool leftAvail = cs.getCURestricted( pos.offset( -(int)pcv.maxCUWidth, 0 ), pos, curSliceIdx, curTileIdx, CH_L ) ? true : false;
bool aboveAvail = cs.getCURestricted( pos.offset( 0, -(int)pcv.maxCUHeight ), pos, curSliceIdx, curTileIdx, CH_L ) ? true : false;

Karsten Suehring
committed
int leftCTUAddr = leftAvail ? ctuRsAddr - 1 : -1;
int aboveCTUAddr = aboveAvail ? ctuRsAddr - frame_width_in_ctus : -1;
uint8_t* ctbAlfFlag = cs.slice->getPic()->getAlfCtuEnableFlag( compIdx );
int ctx = 0;
ctx += leftCTUAddr > -1 ? ( ctbAlfFlag[leftCTUAddr] ? 1 : 0 ) : 0;
ctx += aboveCTUAddr > -1 ? ( ctbAlfFlag[aboveCTUAddr] ? 1 : 0 ) : 0;
m_BinEncoder.encodeBin( ctbAlfFlag[ctuRsAddr], Ctx::ctbAlfFlag( compIdx * 3 + ctx ) );

Karsten Suehring
committed
}
}
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
void CABACWriter::code_unary_fixed( unsigned symbol, unsigned ctxId, unsigned unary_max, unsigned fixed )
{
bool unary = (symbol <= unary_max);
m_BinEncoder.encodeBin( unary, ctxId );
if( unary )
{
unary_max_eqprob( symbol, unary_max );
}
else
{
m_BinEncoder.encodeBinsEP( symbol - unary_max - 1, fixed );
}
}
void CABACWriter::mip_flag( const CodingUnit& cu )
{
if( !cu.Y().valid() )
{
return;
}
if( !cu.cs->sps->getUseMIP() )
{
return;
}
if( cu.lwidth() > MIP_MAX_WIDTH || cu.lheight() > MIP_MAX_HEIGHT )
{
return;
}
if( !mipModesAvailable( cu.Y() ) )
{
return;
}
unsigned ctxId = DeriveCtx::CtxMipFlag( cu );
m_BinEncoder.encodeBin( cu.mipFlag, Ctx::MipFlag( ctxId ) );
DTRACE( g_trace_ctx, D_SYNTAX, "mip_flag() pos=(%d,%d) mode=%d\n", cu.lumaPos().x, cu.lumaPos().y, cu.mipFlag ? 1 : 0 );
}
void CABACWriter::mip_pred_modes( const CodingUnit& cu )
{
if( !cu.Y().valid() )
{
return;
}
for( const auto &pu : CU::traversePUs( cu ) )
{
mip_pred_mode( pu );
}
}
void CABACWriter::mip_pred_mode( const PredictionUnit& pu )
{
const int numModes = getNumModesMip( pu.Y() ); CHECKD( numModes > MAX_NUM_MIP_MODE, "Error: too many MIP modes" );
// derive modeIdx from true MIP mode
unsigned mpm[NUM_MPM_MIP];
PU::getMipMPMs(pu, mpm);
unsigned mipMode = pu.intraDir[CHANNEL_TYPE_LUMA];
unsigned mpmIdx = NUM_MPM_MIP;
for( auto k = 0; k < NUM_MPM_MIP; k++ )
{
if( mipMode == mpm[k] )
{
mpmIdx = k;
break;
}
}
unsigned modeIdx;
if (mpmIdx < NUM_MPM_MIP)
{
modeIdx = mpmIdx;
}
else
{
std::sort( mpm, mpm + NUM_MPM_MIP);
modeIdx = mipMode;
for( auto k = (NUM_MPM_MIP - 1); k >= 0; k-- )
{
if( modeIdx > mpm[k] )
{
modeIdx--;
}
}
CHECK( modeIdx >= (1<<getNumEpBinsMip( pu.Y() )), "Incorrect mode" );
modeIdx += NUM_MPM_MIP;
}
CHECK( modeIdx >= numModes, "modeIdx out of range" );
int unaryMax = NUM_MPM_MIP - 1;
int fixedLength = getNumEpBinsMip( pu.Y() );
code_unary_fixed( modeIdx, Ctx::MipMode( 0 ), unaryMax, fixedLength );
DTRACE( g_trace_ctx, D_SYNTAX, "mip_pred_mode() pos=(%d,%d) mode=%d\n", pu.lumaPos().x, pu.lumaPos().y, pu.intraDir[CHANNEL_TYPE_LUMA] );
}
void CABACWriter::codeAlfCtuFilterIndex(CodingStructure& cs, uint32_t ctuRsAddr, bool alfEnableLuma)
if ( (!cs.sps->getALFEnabledFlag()) || (!alfEnableLuma))
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
{
return;
}
uint8_t* ctbAlfFlag = cs.slice->getPic()->getAlfCtuEnableFlag(COMPONENT_Y);
if (!ctbAlfFlag[ctuRsAddr])
{
return;
}
short* alfCtbFilterIndex = cs.slice->getPic()->getAlfCtbFilterIndex();
const unsigned filterSetIdx = alfCtbFilterIndex[ctuRsAddr];
unsigned numAps = cs.slice->getTileGroupNumAps();
unsigned numAvailableFiltSets = numAps + NUM_FIXED_FILTER_SETS;
if (numAvailableFiltSets > NUM_FIXED_FILTER_SETS)
{
int useLatestFilt = (filterSetIdx == NUM_FIXED_FILTER_SETS) ? 1 : 0;
m_BinEncoder.encodeBin(useLatestFilt, Ctx::AlfUseLatestFilt());
if (!useLatestFilt)
{
if (numAps == 1)
{
CHECK(filterSetIdx >= NUM_FIXED_FILTER_SETS, "fixed set numavail < num_fixed");
xWriteTruncBinCode(filterSetIdx, NUM_FIXED_FILTER_SETS);
}
else
{
int useTemporalFilt = (filterSetIdx > NUM_FIXED_FILTER_SETS) ? 1 : 0;
m_BinEncoder.encodeBin(useTemporalFilt, Ctx::AlfUseTemporalFilt());
if (useTemporalFilt)
{
CHECK((filterSetIdx - (NUM_FIXED_FILTER_SETS + 1)) >= (numAvailableFiltSets - (NUM_FIXED_FILTER_SETS + 1)), "temporal non-latest set");
#if JVET_O0247_ALF_CTB_CODING_REDUNDANCY_REMOVAL
if (numAps > 2)
{
#endif
xWriteTruncBinCode(filterSetIdx - (NUM_FIXED_FILTER_SETS + 1), numAvailableFiltSets - (NUM_FIXED_FILTER_SETS + 1));
#if JVET_O0247_ALF_CTB_CODING_REDUNDANCY_REMOVAL
}
#endif
}
else
{
CHECK(filterSetIdx >= NUM_FIXED_FILTER_SETS, "fixed set larger than temporal");
xWriteTruncBinCode(filterSetIdx, NUM_FIXED_FILTER_SETS);
}
}
}
}
else
{
CHECK(filterSetIdx >= NUM_FIXED_FILTER_SETS, "fixed set numavail < num_fixed");
xWriteTruncBinCode(filterSetIdx, NUM_FIXED_FILTER_SETS);
}
}