Newer
Older
pictureTimingSEI->m_subLayerDelaysPresentFlag[i] = true;
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
if( ((m_rapWithLeading == true) && (indexWithinGOP == 0)) || (m_totalCoded[maxNumSubLayers - 1] == 0) || m_bufferingPeriodSEIPresentInAU)
{
pictureTimingSEI->m_cpbRemovalDelayDeltaEnabledFlag[i] = false;
}
else
{
pictureTimingSEI->m_cpbRemovalDelayDeltaEnabledFlag[i] = m_HRD->getBufferingPeriodSEI()->m_cpbRemovalDelayDeltasPresentFlag;
}
if( pictureTimingSEI->m_cpbRemovalDelayDeltaEnabledFlag[i] )
{
if( m_rapWithLeading == false )
{
switch (m_pcCfg->getGOPSize())
{
case 8:
{
if((indexWithinGOP == 1 && i == 2))
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 0;
}
else if((indexWithinGOP == 2 && i == 2) || (indexWithinGOP == 6 && i == 2))
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 1;
}
else if((indexWithinGOP == 1 && i == 1) || (indexWithinGOP == 3 && i == 2))
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 2;
}
else if(indexWithinGOP == 2 && i == 1)
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 3;
}
else if(indexWithinGOP == 1 && i == 0)
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 4;
}
else
{
Yago Sanchez de la Fuente
committed
THROW("m_cpbRemovalDelayDeltaIdx not applicable for the sub-layer and GOP size");
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
}
}
break;
case 16:
{
if((indexWithinGOP == 1 && i == 3))
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 0;
}
else if((indexWithinGOP == 2 && i == 3) || (indexWithinGOP == 10 && i == 3) || (indexWithinGOP == 14 && i == 3))
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 1;
}
else if((indexWithinGOP == 1 && i == 2) || (indexWithinGOP == 3 && i == 3) || (indexWithinGOP == 7 && i == 3) || (indexWithinGOP == 11 && i == 3))
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 2;
}
else if(indexWithinGOP == 4 && i == 3)
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 3;
}
else if((indexWithinGOP == 2 && i == 2) || (indexWithinGOP == 10 && i == 2))
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 4;
}
else if(indexWithinGOP == 1 && i == 1)
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 5;
}
else if(indexWithinGOP == 3 && i == 2)
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 6;
}
else if(indexWithinGOP == 2 && i == 1)
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 7;
}
else if(indexWithinGOP == 1 && i == 0)
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 8;
}
else
{
Yago Sanchez de la Fuente
committed
THROW("m_cpbRemovalDelayDeltaIdx not applicable for the sub-layer and GOP size");
}
}
break;
default:
{
Yago Sanchez de la Fuente
committed
THROW("m_cpbRemovalDelayDeltaIdx not supported for the current GOP size");
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
}
break;
}
}
else
{
switch (m_pcCfg->getGOPSize())
{
case 8:
{
if((indexWithinGOP == 1 && i == 2) || (indexWithinGOP == 5 && i == 2))
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 0;
}
else if(indexWithinGOP == 2 && i == 2)
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 1;
}
else if(indexWithinGOP == 1 && i == 1)
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 2;
}
else
{
Yago Sanchez de la Fuente
committed
THROW("m_cpbRemovalDelayDeltaIdx not applicable for the sub-layer and GOP size");
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
}
}
break;
case 16:
{
if((indexWithinGOP == 1 && i == 3) || (indexWithinGOP == 9 && i == 3) || (indexWithinGOP == 13 && i == 3))
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 0;
}
else if((indexWithinGOP == 2 && i == 3) || (indexWithinGOP == 6 && i == 3) || (indexWithinGOP == 10 && i == 3))
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 1;
}
else if((indexWithinGOP == 1 && i == 2) || (indexWithinGOP == 9 && i == 2) || (indexWithinGOP == 3 && i == 3))
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 2;
}
else if(indexWithinGOP == 2 && i == 2)
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 3;
}
else if(indexWithinGOP == 1 && i == 1)
{
pictureTimingSEI->m_cpbRemovalDelayDeltaIdx[i] = 4;
}
else
{
Yago Sanchez de la Fuente
committed
THROW("m_cpbRemovalDelayDeltaIdx not applicable for the sub-layer and GOP size");
}
}
break;
default:
{
Yago Sanchez de la Fuente
committed
THROW("m_cpbRemovalDelayDeltaIdx not applicable for the sub-layer and GOP size");
}
break;
}
}
}
else
{
int scaledDistToBuffPeriod = (m_totalCoded[i] - m_lastBPSEI[i]) * static_cast<int>(pow(2, static_cast<double>(maxNumSubLayers - 1 - i)));
pictureTimingSEI->m_auCpbRemovalDelay[i] = std::min<int>(std::max<int>(1, scaledDistToBuffPeriod), static_cast<int>(pow(2, static_cast<double>(cpbRemovalDelayLegth)))); // Syntax element signalled as minus, hence the .
CHECK( (scaledDistToBuffPeriod) > pow(2, static_cast<double>(cpbRemovalDelayLegth)), " cpbRemovalDelayLegth too small for m_auCpbRemovalDelay[i] at picture timing SEI " );
}
}
pictureTimingSEI->m_picDpbOutputDelay = slice->getSPS()->getNumReorderPics(slice->getSPS()->getMaxTLayers()-1) + slice->getPOC() - m_totalCoded[maxNumSubLayers-1];

Karsten Suehring
committed
if(m_pcCfg->getEfficientFieldIRAPEnabled() && IRAPGOPid > 0 && IRAPGOPid < m_iGopSize)
{
// if pictures have been swapped there is likely one more picture delay on their tid. Very rough approximation
pictureTimingSEI->m_picDpbOutputDelay ++;
}
int factor = hrd->getTickDivisorMinus2() + 2;
pictureTimingSEI->m_picDpbOutputDuDelay = factor * pictureTimingSEI->m_picDpbOutputDelay;
if( m_pcCfg->getDecodingUnitInfoSEIEnabled() )
{
picSptDpbOutputDuDelay = factor * pictureTimingSEI->m_picDpbOutputDelay;
}
if (m_bufferingPeriodSEIPresentInAU)
{
for( int i = temporalId ; i < maxNumSubLayers ; i ++ )
{
m_lastBPSEI[i] = m_totalCoded[i];
}
if( (slice->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL)||(slice->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA) )
{
m_rapWithLeading = true;
}

Karsten Suehring
committed
}
if( m_pcCfg->getPictureTimingSEIEnabled() )
{
seiMessages.push_back(pictureTimingSEI);
#if HEVC_SEI

Karsten Suehring
committed
if ( m_pcCfg->getScalableNestingSEIEnabled() ) // put picture timing SEI into scalable nesting SEI
{
SEIPictureTiming *pictureTimingSEIcopy = new SEIPictureTiming();
pictureTimingSEI->copyTo(*pictureTimingSEIcopy);
nestedSeiMessages.push_back(pictureTimingSEIcopy);
}
#endif

Karsten Suehring
committed
}
#if JVET_P0202_P0203_FIX_HRD_RELATED_SEI
if( m_pcCfg->getDecodingUnitInfoSEIEnabled() && hrd->getGeneralDecodingUnitHrdParamsPresentFlag() )
#else
if( m_pcCfg->getDecodingUnitInfoSEIEnabled() && hrd->getDecodingUnitHrdParamsPresentFlag() )

Karsten Suehring
committed
{
for( int i = 0; i < ( pictureTimingSEI->m_numDecodingUnitsMinus1 + 1 ); i ++ )
{
SEIDecodingUnitInfo *duInfoSEI = new SEIDecodingUnitInfo();
duInfoSEI->m_decodingUnitIdx = i;
#if JVET_P0202_P0203_FIX_HRD_RELATED_SEI
for( int j = temporalId; j <= maxNumSubLayers; j++ )
duInfoSEI->m_duSptCpbRemovalDelayIncrement[j] = pictureTimingSEI->m_duCpbRemovalDelayMinus1[i*maxNumSubLayers+j] + 1;
#else

Karsten Suehring
committed
duInfoSEI->m_duSptCpbRemovalDelay = pictureTimingSEI->m_duCpbRemovalDelayMinus1[i] + 1;

Karsten Suehring
committed
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
duInfoSEI->m_dpbOutputDuDelayPresentFlag = false;
duInfoSEI->m_picSptDpbOutputDuDelay = picSptDpbOutputDuDelay;
duInfoSeiMessages.push_back(duInfoSEI);
}
}
if( !m_pcCfg->getPictureTimingSEIEnabled() && pictureTimingSEI )
{
delete pictureTimingSEI;
}
}
}
void EncGOP::xUpdateDuData(AccessUnit &testAU, std::deque<DUData> &duData)
{
if (duData.empty())
{
return;
}
// fix first
uint32_t numNalUnits = (uint32_t)testAU.size();
uint32_t numRBSPBytes = 0;
for (AccessUnit::const_iterator it = testAU.begin(); it != testAU.end(); it++)
{
numRBSPBytes += uint32_t((*it)->m_nalUnitData.str().size());
}
duData[0].accumBitsDU += ( numRBSPBytes << 3 );
duData[0].accumNalsDU += numNalUnits;
// adapt cumulative sums for all following DUs
// and add one DU info SEI, if enabled
for (int i=1; i<duData.size(); i++)
{
if (m_pcCfg->getDecodingUnitInfoSEIEnabled())
{
numNalUnits += 1;
numRBSPBytes += ( 5 << 3 );
}
duData[i].accumBitsDU += numRBSPBytes; // probably around 5 bytes
duData[i].accumNalsDU += numNalUnits;
}
// The last DU may have a trailing SEI
if (m_pcCfg->getDecodedPictureHashSEIType()!=HASHTYPE_NONE)
{
duData.back().accumBitsDU += ( 20 << 3 ); // probably around 20 bytes - should be further adjusted, e.g. by type
duData.back().accumNalsDU += 1;
}
}
void EncGOP::xUpdateTimingSEI(SEIPictureTiming *pictureTimingSEI, std::deque<DUData> &duData, const SPS *sps)
{
if (!pictureTimingSEI)
{
return;
}
#if JVET_P0202_P0203_FIX_HRD_RELATED_SEI
if( hrd->getGeneralDecodingUnitHrdParamsPresentFlag() )
#else
if( hrd->getDecodingUnitHrdParamsPresentFlag() )

Karsten Suehring
committed
{
int i;
uint64_t ui64Tmp;
uint32_t uiPrev = 0;
uint32_t numDU = ( pictureTimingSEI->m_numDecodingUnitsMinus1 + 1 );
std::vector<uint32_t> &rDuCpbRemovalDelayMinus1 = pictureTimingSEI->m_duCpbRemovalDelayMinus1;
uint32_t maxDiff = ( hrd->getTickDivisorMinus2() + 2 ) - 1;
#if JVET_P0202_P0203_FIX_HRD_RELATED_SEI
int maxNumSubLayers = sps->getMaxTLayers();
for( int j = 0; j < maxNumSubLayers - 1; j++ )
pictureTimingSEI->m_ptSubLayerDelaysPresentFlag[j] = false;
#endif

Karsten Suehring
committed
for( i = 0; i < numDU; i ++ )
{
pictureTimingSEI->m_numNalusInDuMinus1[ i ] = ( i == 0 ) ? ( duData[i].accumNalsDU - 1 ) : ( duData[i].accumNalsDU- duData[i-1].accumNalsDU - 1 );
}
if( numDU == 1 )
{
#if JVET_P0202_P0203_FIX_HRD_RELATED_SEI
rDuCpbRemovalDelayMinus1[ 0 + maxNumSubLayers - 1 ] = 0; /* don't care */
#else

Karsten Suehring
committed
rDuCpbRemovalDelayMinus1[ 0 ] = 0; /* don't care */

Karsten Suehring
committed
}
else
{
#if JVET_P0202_P0203_FIX_HRD_RELATED_SEI
rDuCpbRemovalDelayMinus1[ (numDU - 1) * maxNumSubLayers + maxNumSubLayers - 1 ] = 0;/* by definition */
#else

Karsten Suehring
committed
rDuCpbRemovalDelayMinus1[ numDU - 1 ] = 0;/* by definition */

Karsten Suehring
committed
uint32_t tmp = 0;
uint32_t accum = 0;
for( i = ( numDU - 2 ); i >= 0; i -- )
{
ui64Tmp = ( ( ( duData[numDU - 1].accumBitsDU - duData[i].accumBitsDU ) * ( sps->getTimingInfo()->getTimeScale() / sps->getTimingInfo()->getNumUnitsInTick() ) * ( hrd->getTickDivisorMinus2() + 2 ) ) / ( m_pcCfg->getTargetBitrate() ) );

Karsten Suehring
committed
if( (uint32_t)ui64Tmp > maxDiff )
{
tmp ++;
}
}
uiPrev = 0;
uint32_t flag = 0;
for( i = ( numDU - 2 ); i >= 0; i -- )
{
flag = 0;
ui64Tmp = ( ( ( duData[numDU - 1].accumBitsDU - duData[i].accumBitsDU ) * ( sps->getTimingInfo()->getTimeScale() / sps->getTimingInfo()->getNumUnitsInTick() ) * ( hrd->getTickDivisorMinus2() + 2 ) ) / ( m_pcCfg->getTargetBitrate() ) );

Karsten Suehring
committed
if( (uint32_t)ui64Tmp > maxDiff )
{
if(uiPrev >= maxDiff - tmp)
{
ui64Tmp = uiPrev + 1;
flag = 1;
}
else ui64Tmp = maxDiff - tmp + 1;
}
#if JVET_P0202_P0203_FIX_HRD_RELATED_SEI
rDuCpbRemovalDelayMinus1[ i * maxNumSubLayers + maxNumSubLayers - 1 ] = (uint32_t)ui64Tmp - uiPrev - 1;
if( (int)rDuCpbRemovalDelayMinus1[ i * maxNumSubLayers + maxNumSubLayers - 1 ] < 0 )
{
rDuCpbRemovalDelayMinus1[ i * maxNumSubLayers + maxNumSubLayers - 1 ] = 0;
}
else if (tmp > 0 && flag == 1)
{
tmp --;
}
accum += rDuCpbRemovalDelayMinus1[ i * maxNumSubLayers + maxNumSubLayers - 1 ] + 1;
#else

Karsten Suehring
committed
rDuCpbRemovalDelayMinus1[ i ] = (uint32_t)ui64Tmp - uiPrev - 1;
if( (int)rDuCpbRemovalDelayMinus1[ i ] < 0 )
{
rDuCpbRemovalDelayMinus1[ i ] = 0;
}
else if (tmp > 0 && flag == 1)
{
tmp --;
}
accum += rDuCpbRemovalDelayMinus1[ i ] + 1;

Karsten Suehring
committed
uiPrev = accum;
}
}
}
}
#if JVET_P0202_P0203_FIX_HRD_RELATED_SEI
void EncGOP::xUpdateDuInfoSEI(SEIMessages &duInfoSeiMessages, SEIPictureTiming *pictureTimingSEI, int maxSubLayers)
#else

Karsten Suehring
committed
void EncGOP::xUpdateDuInfoSEI(SEIMessages &duInfoSeiMessages, SEIPictureTiming *pictureTimingSEI)

Karsten Suehring
committed
{
if (duInfoSeiMessages.empty() || (pictureTimingSEI == NULL))
{
return;
}
int i=0;
for (SEIMessages::iterator du = duInfoSeiMessages.begin(); du!= duInfoSeiMessages.end(); du++)
{
SEIDecodingUnitInfo *duInfoSEI = (SEIDecodingUnitInfo*) (*du);
duInfoSEI->m_decodingUnitIdx = i;
#if JVET_P0202_P0203_FIX_HRD_RELATED_SEI
for ( int j = 0; j < maxSubLayers; j++ )
{
duInfoSEI->m_duiSubLayerDelaysPresentFlag[j] = pictureTimingSEI->m_ptSubLayerDelaysPresentFlag[j];
duInfoSEI->m_duSptCpbRemovalDelayIncrement[j] = pictureTimingSEI->m_duCpbRemovalDelayMinus1[i*maxSubLayers+j] + 1;
}
#else

Karsten Suehring
committed
duInfoSEI->m_duSptCpbRemovalDelay = pictureTimingSEI->m_duCpbRemovalDelayMinus1[i] + 1;

Karsten Suehring
committed
duInfoSEI->m_dpbOutputDuDelayPresentFlag = false;
i++;
}
}
static void
cabac_zero_word_padding(Slice *const pcSlice, Picture *const pcPic, const std::size_t binCountsInNalUnits, const std::size_t numBytesInVclNalUnits, std::ostringstream &nalUnitData, const bool cabacZeroWordPaddingEnabled)
{
const SPS &sps=*(pcSlice->getSPS());
const ChromaFormat format = sps.getChromaFormatIdc();
const int log2subWidthCxsubHeightC = (::getComponentScaleX(COMPONENT_Cb, format)+::getComponentScaleY(COMPONENT_Cb, format));
const int minCuWidth = pcPic->cs->pcv->minCUWidth;
const int minCuHeight = pcPic->cs->pcv->minCUHeight;
const int paddedWidth = ( ( pcSlice->getPPS()->getPicWidthInLumaSamples() + minCuWidth - 1 ) / minCuWidth ) * minCuWidth;
const int paddedHeight = ( ( pcSlice->getPPS()->getPicHeightInLumaSamples() + minCuHeight - 1 ) / minCuHeight ) * minCuHeight;

Karsten Suehring
committed
const int rawBits = paddedWidth * paddedHeight *
(sps.getBitDepth(CHANNEL_TYPE_LUMA) + ((2*sps.getBitDepth(CHANNEL_TYPE_CHROMA))>>log2subWidthCxsubHeightC));

Karsten Suehring
committed
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
const std::size_t threshold = (32/3)*numBytesInVclNalUnits + (rawBits/32);
if (binCountsInNalUnits >= threshold)
{
// need to add additional cabac zero words (each one accounts for 3 bytes (=00 00 03)) to increase numBytesInVclNalUnits
const std::size_t targetNumBytesInVclNalUnits = ((binCountsInNalUnits - (rawBits/32))*3+31)/32;
if (targetNumBytesInVclNalUnits>numBytesInVclNalUnits) // It should be!
{
const std::size_t numberOfAdditionalBytesNeeded=targetNumBytesInVclNalUnits - numBytesInVclNalUnits;
const std::size_t numberOfAdditionalCabacZeroWords=(numberOfAdditionalBytesNeeded+2)/3;
const std::size_t numberOfAdditionalCabacZeroBytes=numberOfAdditionalCabacZeroWords*3;
if (cabacZeroWordPaddingEnabled)
{
std::vector<uint8_t> zeroBytesPadding(numberOfAdditionalCabacZeroBytes, uint8_t(0));
for(std::size_t i=0; i<numberOfAdditionalCabacZeroWords; i++)
{
zeroBytesPadding[i*3+2]=3; // 00 00 03
}
nalUnitData.write(reinterpret_cast<const char*>(&(zeroBytesPadding[0])), numberOfAdditionalCabacZeroBytes);
msg( NOTICE, "Adding %d bytes of padding\n", uint32_t( numberOfAdditionalCabacZeroWords * 3 ) );
}
else
{
msg( NOTICE, "Standard would normally require adding %d bytes of padding\n", uint32_t( numberOfAdditionalCabacZeroWords * 3 ) );
}
}
}
}
class EfficientFieldIRAPMapping
{
private:
int IRAPGOPid;
bool IRAPtoReorder;
bool swapIRAPForward;
public:
EfficientFieldIRAPMapping() :
IRAPGOPid(-1),
IRAPtoReorder(false),
swapIRAPForward(false)
{ }
void initialize(const bool isField, const int gopSize, const int POCLast, const int numPicRcvd, const int lastIDR, EncGOP *pEncGop, EncCfg *pCfg);
int adjustGOPid(const int gopID);
int restoreGOPid(const int gopID);
int GetIRAPGOPid() const { return IRAPGOPid; }
};
void EfficientFieldIRAPMapping::initialize(const bool isField, const int gopSize, const int POCLast, const int numPicRcvd, const int lastIDR, EncGOP *pEncGop, EncCfg *pCfg )
{
if(isField)
{
int pocCurr;
for ( int iGOPid=0; iGOPid < gopSize; iGOPid++ )
{
// determine actual POC
if(POCLast == 0) //case first frame or first top field
{
pocCurr=0;
}
else if(POCLast == 1 && isField) //case first bottom field, just like the first frame, the poc computation is not right anymore, we set the right value
{
pocCurr = 1;
}
else
{
pocCurr = POCLast - numPicRcvd + pCfg->getGOPEntry(iGOPid).m_POC - isField;
}
// check if POC corresponds to IRAP
NalUnitType tmpUnitType = pEncGop->getNalUnitType(pocCurr, lastIDR, isField);
if (tmpUnitType >= NAL_UNIT_CODED_SLICE_IDR_W_RADL && tmpUnitType <= NAL_UNIT_CODED_SLICE_CRA) // if picture is an IRAP

Karsten Suehring
committed
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
{
if(pocCurr%2 == 0 && iGOPid < gopSize-1 && pCfg->getGOPEntry(iGOPid).m_POC == pCfg->getGOPEntry(iGOPid+1).m_POC-1)
{ // if top field and following picture in enc order is associated bottom field
IRAPGOPid = iGOPid;
IRAPtoReorder = true;
swapIRAPForward = true;
break;
}
if(pocCurr%2 != 0 && iGOPid > 0 && pCfg->getGOPEntry(iGOPid).m_POC == pCfg->getGOPEntry(iGOPid-1).m_POC+1)
{
// if picture is an IRAP remember to process it first
IRAPGOPid = iGOPid;
IRAPtoReorder = true;
swapIRAPForward = false;
break;
}
}
}
}
}
int EfficientFieldIRAPMapping::adjustGOPid(const int GOPid)
{
if(IRAPtoReorder)
{
if(swapIRAPForward)
{
if(GOPid == IRAPGOPid)
{
return IRAPGOPid +1;
}
else if(GOPid == IRAPGOPid +1)
{
return IRAPGOPid;
}
}
else
{
if(GOPid == IRAPGOPid -1)
{
return IRAPGOPid;
}
else if(GOPid == IRAPGOPid)
{
return IRAPGOPid -1;
}
}
}
return GOPid;
}
int EfficientFieldIRAPMapping::restoreGOPid(const int GOPid)
{
if(IRAPtoReorder)
{
if(swapIRAPForward)
{
if(GOPid == IRAPGOPid)
{
IRAPtoReorder = false;
return IRAPGOPid +1;
}
else if(GOPid == IRAPGOPid +1)
{
return GOPid -1;
}
}
else
{
if(GOPid == IRAPGOPid)
{
return IRAPGOPid -1;
}
else if(GOPid == IRAPGOPid -1)
{
IRAPtoReorder = false;
return IRAPGOPid;
}
}
}
return GOPid;
}
static void
printHash(const HashType hashType, const std::string &digestStr)
{
const char *decodedPictureHashModeName;
switch (hashType)
{
case HASHTYPE_MD5:
decodedPictureHashModeName = "MD5";
break;
case HASHTYPE_CRC:
decodedPictureHashModeName = "CRC";
break;
case HASHTYPE_CHECKSUM:
decodedPictureHashModeName = "Checksum";
break;
default:
decodedPictureHashModeName = NULL;
break;
}
if (decodedPictureHashModeName != NULL)
{
if (digestStr.empty())
{
msg( NOTICE, " [%s:%s]", decodedPictureHashModeName, "?");
}
else
{
msg( NOTICE, " [%s:%s]", decodedPictureHashModeName, digestStr.c_str());
}
}
}
bool isPicEncoded( int targetPoc, int curPoc, int curTLayer, int gopSize, int intraPeriod )
{
int tarGop = targetPoc / gopSize;
int curGop = curPoc / gopSize;
if( tarGop + 1 == curGop )
{
// part of next GOP only for tl0 pics
return curTLayer == 0;
}
int tarIFr = ( targetPoc / intraPeriod ) * intraPeriod;
int curIFr = ( curPoc / intraPeriod ) * intraPeriod;
if( curIFr != tarIFr )
{
return false;
}
int tarId = targetPoc - tarGop * gopSize;
if( tarGop > curGop )
{
return ( tarId == 0 ) ? ( 0 == curTLayer ) : ( 1 >= curTLayer );
}
if( tarGop + 1 < curGop )
{
return false;
}
int curId = curPoc - curGop * gopSize;
int tarTL = 0;
while( tarId != 0 )
{
gopSize /= 2;
if( tarId >= gopSize )
{
tarId -= gopSize;
if( curId != 0 ) curId -= gopSize;
}
else if( curId == gopSize )
{
curId = 0;
}
tarTL++;
}
return curTLayer <= tarTL && curId == 0;
}
void trySkipOrDecodePicture( bool& decPic, bool& encPic, const EncCfg& cfg, Picture* pcPic )
{
// check if we should decode a leading bitstream
if( !cfg.getDecodeBitstream( 0 ).empty() )
{
static bool bDecode1stPart = true; /* TODO: MT */
if( bDecode1stPart )
{
if( cfg.getForceDecodeBitstream1() )
{
if( ( bDecode1stPart = tryDecodePicture( pcPic, pcPic->getPOC(), cfg.getDecodeBitstream( 0 ), false ) ) )
{
decPic = bDecode1stPart;
}
}
else
{
// update decode decision
bool dbgCTU = cfg.getDebugCTU() != -1 && cfg.getSwitchPOC() == pcPic->getPOC();
if( ( bDecode1stPart = ( cfg.getSwitchPOC() != pcPic->getPOC() ) || dbgCTU ) && ( bDecode1stPart = tryDecodePicture( pcPic, pcPic->getPOC(), cfg.getDecodeBitstream( 0 ), false, cfg.getDebugCTU(), cfg.getSwitchPOC() ) ) )
{
if( dbgCTU )
{
encPic = true;
decPic = false;
bDecode1stPart = false;
return;
}
decPic = bDecode1stPart;
return;
}

Karsten Suehring
committed
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
else if( pcPic->getPOC() )
{
// reset decoder if used and not required any further
tryDecodePicture( NULL, 0, std::string( "" ) );
}
}
}
encPic |= cfg.getForceDecodeBitstream1() && !decPic;
if( cfg.getForceDecodeBitstream1() ) { return; }
}
// check if we should decode a trailing bitstream
if( ! cfg.getDecodeBitstream(1).empty() )
{
const int iNextKeyPOC = (1+cfg.getSwitchPOC() / cfg.getGOPSize()) *cfg.getGOPSize();
const int iNextIntraPOC = (1+(cfg.getSwitchPOC() / cfg.getIntraPeriod()))*cfg.getIntraPeriod();
const int iRestartIntraPOC = iNextIntraPOC + (((iNextKeyPOC == iNextIntraPOC) && cfg.getSwitchDQP() ) ? cfg.getIntraPeriod() : 0);
bool bDecode2ndPart = (pcPic->getPOC() >= iRestartIntraPOC);
int expectedPoc = pcPic->getPOC();
Slice slice0;
if ( cfg.getBs2ModPOCAndType() )
{
expectedPoc = pcPic->getPOC() - iRestartIntraPOC;
slice0.copySliceInfo( pcPic->slices[ 0 ], false );
}
if( bDecode2ndPart && (bDecode2ndPart = tryDecodePicture( pcPic, expectedPoc, cfg.getDecodeBitstream(1), true )) )
{
decPic = bDecode2ndPart;
if ( cfg.getBs2ModPOCAndType() )
{
for( int i = 0; i < pcPic->slices.size(); i++ )
{
pcPic->slices[ i ]->setPOC ( slice0.getPOC() );
if ( pcPic->slices[ i ]->getNalUnitType() != slice0.getNalUnitType()
&& pcPic->slices[ i ]->getIdrPicFlag()
&& slice0.getRapPicFlag()
&& slice0.isIntra() )
{
// patch IDR-slice to CRA-Intra-slice
pcPic->slices[ i ]->setNalUnitType ( slice0.getNalUnitType() );
pcPic->slices[ i ]->setLastIDR ( slice0.getLastIDR() );
#if JVET_P1006_PICTURE_HEADER
if ( pcPic->cs->picHeader->getEnableTMVPFlag() )
#else

Karsten Suehring
committed
pcPic->slices[ i ]->setEnableTMVPFlag ( slice0.getEnableTMVPFlag() );
if ( slice0.getEnableTMVPFlag() )

Karsten Suehring
committed
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
{
pcPic->slices[ i ]->setColFromL0Flag( slice0.getColFromL0Flag() );
pcPic->slices[ i ]->setColRefIdx ( slice0.getColRefIdx() );
}
}
}
}
return;
}
}
// leave here if we do not use forward to poc
if( ! cfg.useFastForwardToPOC() )
{
// let's encode
encPic = true;
return;
}
// this is the forward to poc section
static bool bHitFastForwardPOC = false; /* TODO: MT */
if( bHitFastForwardPOC || isPicEncoded( cfg.getFastForwardToPOC(), pcPic->getPOC(), pcPic->layer, cfg.getGOPSize(), cfg.getIntraPeriod() ) )
{
bHitFastForwardPOC |= cfg.getFastForwardToPOC() == pcPic->getPOC(); // once we hit the poc we continue encoding
if( bHitFastForwardPOC && cfg.getStopAfterFFtoPOC() && cfg.getFastForwardToPOC() != pcPic->getPOC() )
{
return;
}
//except if FastForwardtoPOC is meant to be a SwitchPOC in thist case drop all preceding pictures
if( bHitFastForwardPOC && ( cfg.getSwitchPOC() == cfg.getFastForwardToPOC() ) && ( cfg.getFastForwardToPOC() > pcPic->getPOC() ) )
{
return;
}
// let's encode
encPic = true;
}
}
void EncGOP::xPicInitHashME( Picture *pic, const PPS *pps, PicList &rcListPic )

Karsten Suehring
committed
{
if (! m_pcCfg->getUseHashME())
{
return;
}
PicList::iterator iterPic = rcListPic.begin();
while (iterPic != rcListPic.end())
{
Picture* refPic = *(iterPic++);

Karsten Suehring
committed
if (refPic->poc != pic->poc && refPic->referenced)
{
if (!refPic->getHashMap()->isInitial())
{
if (refPic->getPOC() == 0)
{
Pel* picSrc = refPic->getOrigBuf().get(COMPONENT_Y).buf;
int stridePic = refPic->getOrigBuf().get(COMPONENT_Y).stride;
int picWidth = pps->getPicWidthInLumaSamples();
int picHeight = pps->getPicHeightInLumaSamples();

Karsten Suehring
committed
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
int blockSize = 4;
int allNum = 0;
int simpleNum = 0;
for (int j = 0; j <= picHeight - blockSize; j += blockSize)
{
for (int i = 0; i <= picWidth - blockSize; i += blockSize)
{
Pel* curBlock = picSrc + j * stridePic + i;
bool isHorSame = true;
for (int m = 0; m < blockSize&&isHorSame; m++)
{
for (int n = 1; n < blockSize&&isHorSame; n++)
{
if (curBlock[m*stridePic] != curBlock[m*stridePic + n])
{
isHorSame = false;
}
}
}
bool isVerSame = true;
for (int m = 1; m < blockSize&&isVerSame; m++)
{
for (int n = 0; n < blockSize&&isVerSame; n++)
{
if (curBlock[n] != curBlock[m*stridePic + n])
{
isVerSame = false;
}
}
}
allNum++;
if (isHorSame || isVerSame)
{
simpleNum++;
}
}
}

Karsten Suehring
committed
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
if (simpleNum < 0.3*allNum)
{
m_pcCfg->setUseHashME(false);
break;
}
}
refPic->addPictureToHashMapForInter();
}
}
}
}
void EncGOP::xPicInitRateControl(int &estimatedBits, int gopId, double &lambda, Picture *pic, Slice *slice)
{
if ( !m_pcCfg->getUseRateCtrl() ) // TODO: does this work with multiple slices and slice-segments?
{
return;
}
int frameLevel = m_pcRateCtrl->getRCSeq()->getGOPID2Level( gopId );
if ( pic->slices[0]->isIRAP() )
{
frameLevel = 0;
}
m_pcRateCtrl->initRCPic( frameLevel );
estimatedBits = m_pcRateCtrl->getRCPic()->getTargetBits();

Karsten Suehring
committed
#if U0132_TARGET_BITS_SATURATION
if (m_pcRateCtrl->getCpbSaturationEnabled() && frameLevel != 0)
{
int estimatedCpbFullness = m_pcRateCtrl->getCpbState() + m_pcRateCtrl->getBufferingRate();

Karsten Suehring
committed
// prevent overflow
if (estimatedCpbFullness - estimatedBits > (int)(m_pcRateCtrl->getCpbSize()*0.9f))
{
estimatedBits = estimatedCpbFullness - (int)(m_pcRateCtrl->getCpbSize()*0.9f);
}

Karsten Suehring
committed
estimatedCpbFullness -= m_pcRateCtrl->getBufferingRate();
// prevent underflow
#if V0078_ADAPTIVE_LOWER_BOUND
if (estimatedCpbFullness - estimatedBits < m_pcRateCtrl->getRCPic()->getLowerBound())
{
estimatedBits = std::max(200, estimatedCpbFullness - m_pcRateCtrl->getRCPic()->getLowerBound());
}
#else
if (estimatedCpbFullness - estimatedBits < (int)(m_pcRateCtrl->getCpbSize()*0.1f))
{
estimatedBits = std::max(200, estimatedCpbFullness - (int)(m_pcRateCtrl->getCpbSize()*0.1f));
}
#endif

Karsten Suehring
committed
m_pcRateCtrl->getRCPic()->setTargetBits(estimatedBits);
}
#endif

Karsten Suehring
committed
int sliceQP = m_pcCfg->getInitialQP();
if ( ( slice->getPOC() == 0 && m_pcCfg->getInitialQP() > 0 ) || ( frameLevel == 0 && m_pcCfg->getForceIntraQP() ) ) // QP is specified
{
int NumberBFrames = ( m_pcCfg->getGOPSize() - 1 );
double dLambda_scale = 1.0 - Clip3( 0.0, 0.5, 0.05*(double)NumberBFrames );
double dQPFactor = 0.57*dLambda_scale;
int SHIFT_QP = 12;
int bitdepth_luma_qp_scale = 6 * (slice->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA) - 8
- DISTORTION_PRECISION_ADJUSTMENT(slice->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA)));
double qp_temp = (double) sliceQP + bitdepth_luma_qp_scale - SHIFT_QP;
lambda = dQPFactor*pow( 2.0, qp_temp/3.0 );
}
else if ( frameLevel == 0 ) // intra case, but use the model
{
m_pcSliceEncoder->calCostSliceI(pic); // TODO: This only analyses the first slice segment - what about the others?

Karsten Suehring
committed
if ( m_pcCfg->getIntraPeriod() != 1 ) // do not refine allocated bits for all intra case
{
int bits = m_pcRateCtrl->getRCSeq()->getLeftAverageBits();
bits = m_pcRateCtrl->getRCPic()->getRefineBitsForIntra( bits );

Karsten Suehring
committed
#if U0132_TARGET_BITS_SATURATION
if (m_pcRateCtrl->getCpbSaturationEnabled() )
{
int estimatedCpbFullness = m_pcRateCtrl->getCpbState() + m_pcRateCtrl->getBufferingRate();

Karsten Suehring
committed
// prevent overflow
if (estimatedCpbFullness - bits > (int)(m_pcRateCtrl->getCpbSize()*0.9f))
{
bits = estimatedCpbFullness - (int)(m_pcRateCtrl->getCpbSize()*0.9f);
}

Karsten Suehring
committed
estimatedCpbFullness -= m_pcRateCtrl->getBufferingRate();
// prevent underflow
#if V0078_ADAPTIVE_LOWER_BOUND
if (estimatedCpbFullness - bits < m_pcRateCtrl->getRCPic()->getLowerBound())
{
bits = estimatedCpbFullness - m_pcRateCtrl->getRCPic()->getLowerBound();
}
#else
if (estimatedCpbFullness - bits < (int)(m_pcRateCtrl->getCpbSize()*0.1f))
{
bits = estimatedCpbFullness - (int)(m_pcRateCtrl->getCpbSize()*0.1f);
}
#endif
}
#endif

Karsten Suehring
committed
if ( bits < 200 )
{
bits = 200;
}
m_pcRateCtrl->getRCPic()->setTargetBits( bits );
}

Karsten Suehring
committed
list<EncRCPic*> listPreviousPicture = m_pcRateCtrl->getPicList();
m_pcRateCtrl->getRCPic()->getLCUInitTargetBits();
lambda = m_pcRateCtrl->getRCPic()->estimatePicLambda( listPreviousPicture, slice->isIRAP());
sliceQP = m_pcRateCtrl->getRCPic()->estimatePicQP( lambda, listPreviousPicture );
}
else // normal case
{
list<EncRCPic*> listPreviousPicture = m_pcRateCtrl->getPicList();
lambda = m_pcRateCtrl->getRCPic()->estimatePicLambda( listPreviousPicture, slice->isIRAP());
sliceQP = m_pcRateCtrl->getRCPic()->estimatePicQP( lambda, listPreviousPicture );
}

Karsten Suehring
committed
sliceQP = Clip3( -slice->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, sliceQP );
m_pcRateCtrl->getRCPic()->setPicEstQP( sliceQP );

Karsten Suehring
committed
m_pcSliceEncoder->resetQP( pic, sliceQP, lambda );
}
#if JVET_P1006_PICTURE_HEADER
void EncGOP::xPicInitLMCS(Picture *pic, PicHeader *picHeader, Slice *slice)
#else

Karsten Suehring
committed
void EncGOP::xPicInitLMCS(Picture *pic, Slice *slice)

Karsten Suehring
committed
{
if (slice->getSPS()->getUseLmcs())

Karsten Suehring
committed
{
const SliceType sliceType = slice->getSliceType();

Karsten Suehring
committed
m_pcReshaper->getReshapeCW()->rspTid = slice->getTLayer() + (slice->isIntra() ? 0 : 1);
m_pcReshaper->getReshapeCW()->rspSliceQP = slice->getSliceQp();

Karsten Suehring
committed
m_pcReshaper->setSrcReshaped(false);
m_pcReshaper->setRecReshaped(true);
#if JVET_P0371_CHROMA_SCALING_OFFSET
m_pcReshaper->getSliceReshaperInfo().chrResScalingOffset = m_pcCfg->getReshapeCSoffset();
#endif

Karsten Suehring
committed
if (m_pcCfg->getReshapeSignalType() == RESHAPE_SIGNAL_PQ)
{
m_pcReshaper->preAnalyzerHDR(pic, sliceType, m_pcCfg->getReshapeCW(), m_pcCfg->getDualITree());
}
else if (m_pcCfg->getReshapeSignalType() == RESHAPE_SIGNAL_SDR || m_pcCfg->getReshapeSignalType() == RESHAPE_SIGNAL_HLG)
{
m_pcReshaper->preAnalyzerLMCS(pic, m_pcCfg->getReshapeSignalType(), sliceType, m_pcCfg->getReshapeCW());
}
else
{
THROW("Reshaper for other signal currently not defined!");
}

Karsten Suehring
committed
if (sliceType == I_SLICE )