IntraSearch.cpp 101 KB
Newer Older
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
  //--- inverse transform ---
  if (uiAbsSum > 0)
  {
    m_pcTrQuant->invTransformNxN(tu, compID, piResi, cQP);
  }
  else
  {
    piResi.fill(0);
  }

  //===== reconstruction =====
Taoran Lu's avatar
Taoran Lu committed
2012
#if JVET_M0427_INLOOP_RESHAPER
Taoran Lu's avatar
Taoran Lu committed
2013
  if (flag && uiAbsSum > 0 && isChroma(compID) && slice.getReshapeInfo().getSliceReshapeChromaAdj() )
Taoran Lu's avatar
Taoran Lu committed
2014
  {
2015
    piResi.scaleSignal(tu.getChromaAdj(), 0, tu.cu->cs->slice->clpRng(compID));
Taoran Lu's avatar
Taoran Lu committed
2016
2017
  }
#endif
2018
2019
2020
2021
2022
  if (bUseCrossCPrediction)
  {
    CrossComponentPrediction::crossComponentPrediction(tu, compID, cs.getResiBuf(tu.Y()), piResi, piResi, true);
  }

Taoran Lu's avatar
Taoran Lu committed
2023
2024
2025
2026
2027
2028
2029
2030
#if JVET_M0427_INLOOP_RESHAPER
  if (slice.getReshapeInfo().getUseSliceReshaper() && m_pcReshape->getCTUFlag() && compID == COMPONENT_Y)
  {
    CompArea      tmpArea(COMPONENT_Y, area.chromaFormat, Position(0,0), area.size());
    PelBuf tmpPred = m_tmpStorageLCU.getBuf(tmpArea);
    tmpPred.copyFrom(piPred);
    piReco.reconstruct(tmpPred, piResi, cs.slice->clpRng(compID));
  }
2031
  else
Taoran Lu's avatar
Taoran Lu committed
2032
#endif
2033
2034
2035
2036
  piReco.reconstruct(piPred, piResi, cs.slice->clpRng( compID ));

  //===== update distortion =====
#if WCG_EXT
Taoran Lu's avatar
Taoran Lu committed
2037
2038
2039
2040
#if JVET_M0427_INLOOP_RESHAPER
  if (m_pcEncCfg->getLumaLevelToDeltaQPMapping().isEnabled() || (m_pcEncCfg->getReshaper()
    && slice.getReshapeInfo().getUseSliceReshaper() && (m_pcReshape->getCTUFlag() || (isChroma(compID) && m_pcEncCfg->getReshapeIntraCMD()))))
#else
2041
  if( m_pcEncCfg->getLumaLevelToDeltaQPMapping().isEnabled() )
Taoran Lu's avatar
Taoran Lu committed
2042
#endif
2043
2044
  {
    const CPelBuf orgLuma = cs.getOrgBuf( cs.area.blocks[COMPONENT_Y] );
Taoran Lu's avatar
Taoran Lu committed
2045
#if JVET_M0427_INLOOP_RESHAPER
Taoran Lu's avatar
Taoran Lu committed
2046
    if (compID == COMPONENT_Y  && !(m_pcEncCfg->getLumaLevelToDeltaQPMapping().isEnabled()))
Taoran Lu's avatar
Taoran Lu committed
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
    {
      CompArea      tmpArea1(COMPONENT_Y, area.chromaFormat, Position(0, 0), area.size());
      PelBuf tmpRecLuma = m_tmpStorageLCU.getBuf(tmpArea1);
      tmpRecLuma.copyFrom(piReco);
      tmpRecLuma.rspSignal(m_pcReshape->getInvLUT());
      ruiDist += m_pcRdCost->getDistPart(piOrg, tmpRecLuma, sps.getBitDepth(toChannelType(compID)), compID, DF_SSE_WTD, &orgLuma);
    }
    else
#endif
      ruiDist += m_pcRdCost->getDistPart(piOrg, piReco, bitDepth, compID, DF_SSE_WTD, &orgLuma);
2057
2058
2059
2060
2061
2062
2063
2064
  }
  else
#endif
  {
    ruiDist += m_pcRdCost->getDistPart( piOrg, piReco, bitDepth, compID, DF_SSE );
  }
}

2065
#if JVET_M0102_INTRA_SUBPARTITIONS
2066
2067
2068
#if JVET_M0464_UNI_MTS
void IntraSearch::xRecurIntraCodingLumaQT( CodingStructure &cs, Partitioner &partitioner, const double bestCostSoFar, const int subTuIdx, const PartSplit ispType, const bool ispIsCurrentWinnder )
#else
2069
void IntraSearch::xRecurIntraCodingLumaQT( CodingStructure &cs, Partitioner &partitioner, const double bestCostSoFar, const int subTuIdx, const PartSplit ispType )
2070
#endif
2071
2072
2073
2074
2075
2076
{
        int   subTuCounter = subTuIdx;
  const UnitArea &currArea = partitioner.currArea();
  const CodingUnit     &cu = *cs.getCU( currArea.lumaPos(), partitioner.chType );
        bool  earlySkipISP = false;
#else
2077
2078
2079
void IntraSearch::xRecurIntraCodingLumaQT( CodingStructure &cs, Partitioner &partitioner )
{
  const UnitArea &currArea = partitioner.currArea();
Tung Nguyen's avatar
Tung Nguyen committed
2080
#if !JVET_M0464_UNI_MTS
2081
  const CodingUnit &cu     = *cs.getCU(currArea.lumaPos(), partitioner.chType);
2082
#endif
Tung Nguyen's avatar
Tung Nguyen committed
2083
2084
#endif
  uint32_t currDepth       = partitioner.currTrDepth;
2085
2086
2087
2088
  const PPS &pps           = *cs.pps;
  const bool keepResi      = pps.getPpsRangeExtension().getCrossComponentPredictionEnabledFlag() || KEEP_PRED_AND_RESI_SIGNALS;
  bool bCheckFull          = true;
  bool bCheckSplit         = false;
Karsten Suehring's avatar
Karsten Suehring committed
2089
2090
  bCheckFull               = !partitioner.canSplit( TU_MAX_TR_SPLIT, cs );
  bCheckSplit              = partitioner.canSplit( TU_MAX_TR_SPLIT, cs );
2091

2092
2093
2094
2095
2096
2097
2098
#if JVET_M0102_INTRA_SUBPARTITIONS
  if( cu.ispMode )
  {
    bCheckSplit = partitioner.canSplit( ispType, cs );
    bCheckFull = !bCheckSplit;
  }
#endif
2099
2100
  uint32_t    numSig           = 0;

Tung Nguyen's avatar
Tung Nguyen committed
2101
2102
2103
2104
2105
2106
#if JVET_M0464_UNI_MTS
  double     dSingleCost                        = MAX_DOUBLE;
  Distortion uiSingleDistLuma                   = 0;
  uint64_t   singleFracBits                     = 0;
  int        bestModeId[MAX_NUM_COMPONENT]      = { 0, 0, 0 };
#else
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
  bool    checkInitTrDepth = false, checkInitTrDepthTransformSkipWinner = false;

  double     dSingleCost                        = MAX_DOUBLE;
  Distortion uiSingleDistLuma                   = 0;
  uint64_t     singleFracBits                     = 0;
  bool       checkTransformSkip                 = pps.getUseTransformSkip();
  int        bestModeId[MAX_NUM_COMPONENT]      = {0, 0, 0};
  uint8_t      nNumTransformCands                 = cu.emtFlag ? 4 : 1; //4 is the number of transforms of emt
  bool       isAllIntra                         = m_pcEncCfg->getIntraPeriod() == 1;

  uint8_t numTransformIndexCands                  = nNumTransformCands;
Tung Nguyen's avatar
Tung Nguyen committed
2118
#endif
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141

  const TempCtx ctxStart  ( m_CtxCache, m_CABACEstimator->getCtx() );
  TempCtx       ctxBest   ( m_CtxCache );

  CodingStructure *csSplit = nullptr;
  CodingStructure *csFull  = nullptr;

  if( bCheckSplit )
  {
    csSplit = &cs;
  }
  else if( bCheckFull )
  {
    csFull = &cs;
  }

  if( bCheckFull )
  {
    csFull->cost = 0.0;

    TransformUnit &tu = csFull->addTU( CS::getArea( *csFull, currArea, partitioner.chType ), partitioner.chType );
    tu.depth = currDepth;

Tung Nguyen's avatar
Tung Nguyen committed
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
#if JVET_M0464_UNI_MTS
    const bool tsAllowed  = TU::isTSAllowed ( tu, COMPONENT_Y );
    const bool mtsAllowed = TU::isMTSAllowed( tu, COMPONENT_Y );
    uint8_t nNumTransformCands = 1 + ( tsAllowed ? 1 : 0 ) + ( mtsAllowed ? 4 : 0 ); // DCT + TS + 4 MTS = 6 tests
    std::vector<TrMode> trModes;
    trModes.push_back( TrMode( 0, true ) ); //DCT2
    if( tsAllowed )
    {
      trModes.push_back( TrMode( 1, true ) );
    }
    if( mtsAllowed )
    {
      for( int i = 2; i < 6; i++ )
      {
        trModes.push_back( TrMode( i, true) );
      }
    }

    CHECK( !tu.Y().valid(), "Invalid TU" );
#else
2162
2163
2164
    checkTransformSkip &= TU::hasTransformSkipFlag( *tu.cs, tu.Y() );
    checkTransformSkip &= !cu.transQuantBypass;
    checkTransformSkip &= !cu.emtFlag;
2165
2166
2167
#if JVET_M0102_INTRA_SUBPARTITIONS
    checkTransformSkip &= !cu.ispMode;
#endif
2168
2169
2170
2171
2172
2173
2174
2175

    CHECK( !tu.Y().valid(), "Invalid TU" );

    //this prevents transformSkip from being checked because we already know it's not the best mode
    checkTransformSkip = ( checkInitTrDepth && !checkInitTrDepthTransformSkipWinner ) ? false : checkTransformSkip;


    CHECK( checkInitTrDepthTransformSkipWinner && !checkTransformSkip, "Transform Skip must be enabled if it was the winner in the previous call of xRecurIntraCodingLumaQT!" );
Tung Nguyen's avatar
Tung Nguyen committed
2176
#endif
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186

    CodingStructure &saveCS = *m_pSaveCS[0];

    TransformUnit *tmpTU = nullptr;

    Distortion singleDistTmpLuma = 0;
    uint64_t     singleTmpFracBits = 0;
    double     singleCostTmp     = 0;
    int        firstCheckId      = 0;

Tung Nguyen's avatar
Tung Nguyen committed
2187
2188
2189
2190
#if JVET_M0464_UNI_MTS
    int       lastCheckId        = trModes[nNumTransformCands-1].first;
    bool isNotOnlyOneMode        = nNumTransformCands != 1;
#else
2191
2192
2193
    //we add the EMT candidates to the loop. TransformSkip will still be the last one to be checked (when modeId == lastCheckId) as long as checkTransformSkip is true
    int        lastCheckId       = numTransformIndexCands - ( firstCheckId + 1 ) + ( int ) checkTransformSkip;
    bool isNotOnlyOneMode        = lastCheckId != firstCheckId && !checkInitTrDepthTransformSkipWinner;
Tung Nguyen's avatar
Tung Nguyen committed
2194
#endif
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204

    if( isNotOnlyOneMode )
    {
      saveCS.pcv     = cs.pcv;
      saveCS.picture = cs.picture;
      saveCS.area.repositionTo(cs.area);
      saveCS.clearTUs();
      tmpTU = &saveCS.addTU(currArea, partitioner.chType);
    }

Tung Nguyen's avatar
Tung Nguyen committed
2205
2206
2207
#if JVET_M0464_UNI_MTS
    bool    cbfDCT2  = true;
#else
2208
    bool cbfBestMode = false;
Tung Nguyen's avatar
Tung Nguyen committed
2209
#endif
2210

Tung Nguyen's avatar
Tung Nguyen committed
2211
#if JVET_M0464_UNI_MTS
2212
2213
2214
2215
#if JVET_M0102_INTRA_SUBPARTITIONS
    double bestDCT2cost = MAX_DOUBLE;
    double threshold = m_pcEncCfg->getUseFastISP() && !cu.ispMode && ispIsCurrentWinnder && nNumTransformCands > 1 ? 1 + 1.4 / sqrt( cu.lwidth() * cu.lheight() ) : 1;
#endif
Tung Nguyen's avatar
Tung Nguyen committed
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
    for( int modeId = firstCheckId; modeId < nNumTransformCands; modeId++ )
    {
      if( !cbfDCT2 || ( m_pcEncCfg->getUseTransformSkipFast() && bestModeId[COMPONENT_Y] == 1 ) )
      {
        break;
      }
      if( !trModes[modeId].second )
      {
        continue;
      }
2226
2227
2228
2229
2230
2231
2232
#if JVET_M0102_INTRA_SUBPARTITIONS
      //we compare the DCT-II cost against the best ISP cost so far (except for TS)
      if ( m_pcEncCfg->getUseFastISP() && !cu.ispMode && ispIsCurrentWinnder && trModes[modeId].first != 0 && ( trModes[modeId].first != 1 || !tsAllowed ) && bestDCT2cost > bestCostSoFar * threshold )
      {
        continue;
      }
#endif
Tung Nguyen's avatar
Tung Nguyen committed
2233
2234
      tu.mtsIdx = trModes[modeId].first;
#else
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
    for( int modeId = firstCheckId; modeId <= lastCheckId; modeId++ )
    {
      if( checkInitTrDepthTransformSkipWinner )
      {
        //If this is a full RQT call and the winner of the first call (checkFirst=true) was transformSkip, then we skip the first iteration of the loop, since transform skip always comes at the end
        if( modeId == firstCheckId )
        {
          continue;
        }
      }

      uint8_t transformIndex = modeId;


      if( ( transformIndex < lastCheckId ) || ( ( transformIndex == lastCheckId ) && !checkTransformSkip ) ) //we avoid this if the mode is transformSkip
      {
        // Skip checking other transform candidates if zero CBF is encountered and it is the best transform so far
        if( m_pcEncCfg->getFastIntraEMT() && isAllIntra && transformIndex && !cbfBestMode )
        {
          continue;
        }
      }
Tung Nguyen's avatar
Tung Nguyen committed
2257
#endif
2258
2259
2260
2261
2262
2263
2264
2265
2266

      if ((modeId != firstCheckId) && isNotOnlyOneMode)
      {
        m_CABACEstimator->getCtx() = ctxStart;
      }

      int default0Save1Load2 = 0;
      singleDistTmpLuma = 0;

Tung Nguyen's avatar
Tung Nguyen committed
2267
2268
2269
#if JVET_M0464_UNI_MTS
      if( modeId == firstCheckId && nNumTransformCands > 1 )
#else
2270
      if (modeId == firstCheckId && modeId != lastCheckId && !checkInitTrDepthTransformSkipWinner )
Tung Nguyen's avatar
Tung Nguyen committed
2271
#endif
2272
2273
2274
2275
2276
2277
2278
      {
        default0Save1Load2 = 1;
      }
      else if (modeId != firstCheckId)
      {
        default0Save1Load2 = 2;
      }
2279
2280
2281
2282
2283
2284
#if JVET_M0102_INTRA_SUBPARTITIONS
      if( cu.ispMode )
      {
        default0Save1Load2 = 0;
      }
#endif
Tung Nguyen's avatar
Tung Nguyen committed
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
#if JVET_M0464_UNI_MTS
      if( nNumTransformCands > 1 )
      {
        xIntraCodingTUBlock( tu, COMPONENT_Y, false, singleDistTmpLuma, default0Save1Load2, &numSig, modeId == 0 ? &trModes : nullptr, true );
        if( modeId == 0 )
        {
          for( int i = 0; i < nNumTransformCands; i++ )
          {
            if( trModes[i].second )
            {
              lastCheckId = trModes[i].first;
            }
          }
        }
      }
      else
      {
        xIntraCodingTUBlock( tu, COMPONENT_Y, false, singleDistTmpLuma, default0Save1Load2, &numSig );
      }
#else
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
      if (cu.emtFlag)
      {
        tu.emtIdx = transformIndex;
      }
      if( !checkTransformSkip )
      {
        tu.transformSkip[COMPONENT_Y] = false;
      }
      else
      {
        tu.transformSkip[COMPONENT_Y] = modeId == lastCheckId;
      }

      xIntraCodingTUBlock( tu, COMPONENT_Y, false, singleDistTmpLuma, default0Save1Load2, &numSig );
Tung Nguyen's avatar
Tung Nguyen committed
2319
#endif
2320
2321

      //----- determine rate and r-d cost -----
Tung Nguyen's avatar
Tung Nguyen committed
2322
2323
2324
#if JVET_M0464_UNI_MTS
      if( ( trModes[modeId].first != 0 && !TU::getCbfAtDepth( tu, COMPONENT_Y, currDepth ) ) )
#else
2325
      //the condition (transformIndex != DCT2_EMT) seems to be irrelevant, since DCT2_EMT=7 and the highest value of transformIndex is 4
2326
      if( ( modeId == lastCheckId && checkTransformSkip && !TU::getCbfAtDepth( tu, COMPONENT_Y, currDepth ) ) )
Tung Nguyen's avatar
Tung Nguyen committed
2327
#endif
2328
2329
2330
2331
2332
2333
      {
        //In order not to code TS flag when cbf is zero, the case for TS with cbf being zero is forbidden.
        singleCostTmp = MAX_DOUBLE;
      }
      else
      {
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
#if JVET_M0102_INTRA_SUBPARTITIONS
        if( cu.ispMode && m_pcRdCost->calcRdCost( csFull->fracBits, csFull->dist + singleDistTmpLuma ) > bestCostSoFar )
        {
          earlySkipISP = true;
        }
        else
        {
          singleTmpFracBits = xGetIntraFracBitsQT( *csFull, partitioner, true, false, subTuCounter, ispType );
        }
#else
2344
        singleTmpFracBits = xGetIntraFracBitsQT( *csFull, partitioner, true, false );
2345
#endif
2346
2347
2348
        singleCostTmp     = m_pcRdCost->calcRdCost( singleTmpFracBits, singleDistTmpLuma );
      }

2349
2350
2351
2352
2353
2354
2355
#if JVET_M0102_INTRA_SUBPARTITIONS && JVET_M0464_UNI_MTS
      if ( !cu.ispMode && nNumTransformCands > 1 && modeId == firstCheckId )
      {
        bestDCT2cost = singleCostTmp;
      }
#endif

2356
2357
2358
2359
2360
2361
      if (singleCostTmp < dSingleCost)
      {
        dSingleCost       = singleCostTmp;
        uiSingleDistLuma  = singleDistTmpLuma;
        singleFracBits    = singleTmpFracBits;

Tung Nguyen's avatar
Tung Nguyen committed
2362
2363
2364
2365
2366
2367
2368
#if JVET_M0464_UNI_MTS
        bestModeId[COMPONENT_Y] = trModes[modeId].first;
        if( trModes[modeId].first == 0 )
        {
          cbfDCT2  = TU::getCbfAtDepth( tu, COMPONENT_Y, currDepth );
        }
#else
2369
2370
        bestModeId[COMPONENT_Y] = modeId;
        cbfBestMode       = TU::getCbfAtDepth( tu, COMPONENT_Y, currDepth );
Tung Nguyen's avatar
Tung Nguyen committed
2371
#endif
2372
2373
2374

        if( bestModeId[COMPONENT_Y] != lastCheckId )
        {
Taoran Lu's avatar
Taoran Lu committed
2375
#if KEEP_PRED_AND_RESI_SIGNALS || JVET_M0427_INLOOP_RESHAPER
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
          saveCS.getPredBuf( tu.Y() ).copyFrom( csFull->getPredBuf( tu.Y() ) );
#endif
          saveCS.getRecoBuf( tu.Y() ).copyFrom( csFull->getRecoBuf( tu.Y() ) );

          if( keepResi )
          {
            saveCS.getResiBuf   ( tu.Y() ).copyFrom( csFull->getResiBuf   ( tu.Y() ) );
            saveCS.getOrgResiBuf( tu.Y() ).copyFrom( csFull->getOrgResiBuf( tu.Y() ) );
          }

          tmpTU->copyComponentFrom( tu, COMPONENT_Y );

          ctxBest = m_CABACEstimator->getCtx();
        }
      }
    }

    if( bestModeId[COMPONENT_Y] != lastCheckId )
    {
Taoran Lu's avatar
Taoran Lu committed
2395
#if KEEP_PRED_AND_RESI_SIGNALS || JVET_M0427_INLOOP_RESHAPER
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
      csFull->getPredBuf( tu.Y() ).copyFrom( saveCS.getPredBuf( tu.Y() ) );
#endif
      csFull->getRecoBuf( tu.Y() ).copyFrom( saveCS.getRecoBuf( tu.Y() ) );

      if( keepResi )
      {
        csFull->getResiBuf   ( tu.Y() ).copyFrom( saveCS.getResiBuf   ( tu.Y() ) );
        csFull->getOrgResiBuf( tu.Y() ).copyFrom( saveCS.getOrgResiBuf( tu.Y() ) );
      }

      tu.copyComponentFrom( *tmpTU, COMPONENT_Y );

      if( !bCheckSplit )
      {
        m_CABACEstimator->getCtx() = ctxBest;
      }
    }
    else if( bCheckSplit )
    {
      ctxBest = m_CABACEstimator->getCtx();
    }

    csFull->cost     += dSingleCost;
    csFull->dist     += uiSingleDistLuma;
    csFull->fracBits += singleFracBits;
  }

  if( bCheckSplit )
  {
    //----- store full entropy coding status, load original entropy coding status -----
    if( bCheckFull )
    {
      m_CABACEstimator->getCtx() = ctxStart;
    }
    //----- code splitted block -----
    csSplit->cost = 0;

    bool uiSplitCbfLuma  = false;
    bool splitIsSelected = true;
Karsten Suehring's avatar
Karsten Suehring committed
2435
    if( partitioner.canSplit( TU_MAX_TR_SPLIT, cs ) )
2436
2437
2438
2439
    {
      partitioner.splitCurrArea( TU_MAX_TR_SPLIT, cs );
    }

2440
2441
2442
2443
2444
2445
#if JVET_M0102_INTRA_SUBPARTITIONS
    if( cu.ispMode )
    {
      partitioner.splitCurrArea( ispType, *csSplit );
    }
#endif
2446
2447
    do
    {
2448
2449
2450
2451
#if JVET_M0102_INTRA_SUBPARTITIONS
      xRecurIntraCodingLumaQT( *csSplit, partitioner, bestCostSoFar, subTuCounter, ispType );
      subTuCounter += subTuCounter != -1 ? 1 : 0;
#else
2452
      xRecurIntraCodingLumaQT( *csSplit, partitioner );
2453
#endif
2454

2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
#if JVET_M0102_INTRA_SUBPARTITIONS
      if( !cu.ispMode )
      {
        csSplit->setDecomp( partitioner.currArea().Y() );
      }
      else if( CU::isISPFirst( cu, partitioner.currArea().Y(), COMPONENT_Y ) )
      {
        csSplit->setDecomp( cu.Y() );
      }
#else
2465
      csSplit->setDecomp( partitioner.currArea().Y() );
2466
#endif
2467

2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
#if JVET_M0102_INTRA_SUBPARTITIONS
      uiSplitCbfLuma |= TU::getCbfAtDepth( *csSplit->getTU( partitioner.currArea().lumaPos(), partitioner.chType, subTuCounter - 1 ), COMPONENT_Y, partitioner.currTrDepth );
      if( cu.ispMode )
      {
        //exit condition if the accumulated cost is already larger than the best cost so far (no impact in RD performance)
        if( csSplit->cost > bestCostSoFar )
        {
          earlySkipISP    = true;
          splitIsSelected = false;
          break;
        }
        else
        {
          //more restrictive exit condition
          bool tuIsDividedInRows = CU::divideTuInRows( cu );
          int nSubPartitions = tuIsDividedInRows ? cu.lheight() >> g_aucLog2[cu.firstTU->lheight()] : cu.lwidth() >> g_aucLog2[cu.firstTU->lwidth()];
2484
          double threshold = nSubPartitions == 2 ? 0.95 : subTuCounter == 1 ? 0.83 : 0.91;
2485
2486
2487
2488
2489
2490
2491
2492
2493
          if( subTuCounter < nSubPartitions && csSplit->cost > bestCostSoFar*threshold )
          {
            earlySkipISP    = true;
            splitIsSelected = false;
            break;
          }
        }
      }
#else
2494
      uiSplitCbfLuma |= TU::getCbfAtDepth( *csSplit->getTU( partitioner.currArea().lumaPos(), partitioner.chType ), COMPONENT_Y, partitioner.currTrDepth );
2495
#endif
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516



    } while( partitioner.nextPart( *csSplit ) );

    partitioner.exitCurrSplit();

    if( splitIsSelected )
    {
      for( auto &ptu : csSplit->tus )
      {
        if( currArea.Y().contains( ptu->Y() ) )
        {
          TU::setCbfAtDepth( *ptu, COMPONENT_Y, currDepth, uiSplitCbfLuma ? 1 : 0 );
        }
      }

      //----- restore context states -----
      m_CABACEstimator->getCtx() = ctxStart;

      //----- determine rate and r-d cost -----
2517
2518
2519
#if JVET_M0102_INTRA_SUBPARTITIONS
      csSplit->fracBits = xGetIntraFracBitsQT( *csSplit, partitioner, true, false, cu.ispMode ? 0 : -1, ispType );
#else
2520
      csSplit->fracBits = xGetIntraFracBitsQT(*csSplit, partitioner, true, false);
2521
#endif
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532

      //--- update cost ---
      csSplit->cost     = m_pcRdCost->calcRdCost(csSplit->fracBits, csSplit->dist);
    }
  }

  if( csFull || csSplit )
  {
    {
      // otherwise this would've happened in useSubStructure
      cs.picture->getRecoBuf( currArea.Y() ).copyFrom( cs.getRecoBuf( currArea.Y() ) );
Taoran Lu's avatar
Taoran Lu committed
2533
2534
2535
#if JVET_M0427_INLOOP_RESHAPER
      cs.picture->getPredBuf( currArea.Y() ).copyFrom( cs.getPredBuf( currArea.Y() ) );
#endif
2536
2537
    }

2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
#if JVET_M0102_INTRA_SUBPARTITIONS
    if( cu.ispMode && earlySkipISP )
    {
      cs.cost = MAX_DOUBLE;
    }
    else
    {
      cs.cost = m_pcRdCost->calcRdCost( cs.fracBits, cs.dist );
    }
#else
2548
    cs.cost = m_pcRdCost->calcRdCost( cs.fracBits, cs.dist );
2549
#endif
2550
2551
2552
  }
}

2553
2554
2555
#if JVET_M0102_INTRA_SUBPARTITIONS
ChromaCbfs IntraSearch::xRecurIntraChromaCodingQT( CodingStructure &cs, Partitioner& partitioner, const double bestCostSoFar, const PartSplit ispType )
#else
2556
ChromaCbfs IntraSearch::xRecurIntraChromaCodingQT(CodingStructure &cs, Partitioner& partitioner)
2557
#endif
2558
2559
{
  UnitArea currArea                   = partitioner.currArea();
2560
  const bool keepResi                 = cs.sps->getUseLMChroma() || KEEP_PRED_AND_RESI_SIGNALS;
2561
2562
2563
2564
2565
  if( !currArea.Cb().valid() ) return ChromaCbfs( false );


  TransformUnit &currTU               = *cs.getTU( currArea.chromaPos(), CHANNEL_TYPE_CHROMA );
  const PredictionUnit &pu            = *cs.getPU( currArea.chromaPos(), CHANNEL_TYPE_CHROMA );
Tung Nguyen's avatar
Tung Nguyen committed
2566
#if !JVET_M0464_UNI_MTS
2567
2568
2569
#if JVET_M0102_INTRA_SUBPARTITIONS
  const TransformUnit &currTULuma     = CS::isDualITree( cs ) ? *cs.picture->cs->getTU(currArea.lumaPos(), CHANNEL_TYPE_LUMA, 0 ) : currTU;
#else
2570
  const TransformUnit &currTULuma     = CS::isDualITree( cs ) ? *cs.picture->cs->getTU( currArea.lumaPos(), CHANNEL_TYPE_LUMA ) : currTU;
Tung Nguyen's avatar
Tung Nguyen committed
2571
#endif
2572
#endif
2573

2574
2575
2576
#if JVET_M0102_INTRA_SUBPARTITIONS
  bool lumaUsesISP                    = !CS::isDualITree( cs ) && currTU.cu->ispMode;
#endif
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
  uint32_t     currDepth                  = partitioner.currTrDepth;
  const PPS &pps                      = *cs.pps;
  ChromaCbfs cbfs                     ( false );

  if (currDepth == currTU.depth)
  {
    if (!currArea.Cb().valid() || !currArea.Cr().valid())
    {
      return cbfs;
    }

Tung Nguyen's avatar
Tung Nguyen committed
2588
#if !JVET_M0464_UNI_MTS
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
    bool checkTransformSkip = pps.getUseTransformSkip();
    checkTransformSkip &= TU::hasTransformSkipFlag( *currTU.cs, partitioner.currArea().Cb() );

    if( m_pcEncCfg->getUseTransformSkipFast() )
    {
      checkTransformSkip &= TU::hasTransformSkipFlag( *currTU.cs, partitioner.currArea().Y() );

      if( checkTransformSkip && cs.pcv->noChroma2x2 )
      {
        int nbLumaSkip = currTULuma.transformSkip[0] ? 1 : 0;

        {
          // the chroma blocks are co-located with the last luma block, so backwards references are needed
          nbLumaSkip += cs.getTU( currTULuma.Y().topLeft().offset( -1,  0 ), partitioner.chType )->transformSkip[0] ? 1 : 0;
          nbLumaSkip += cs.getTU( currTULuma.Y().topLeft().offset( -1, -1 ), partitioner.chType )->transformSkip[0] ? 1 : 0;
          nbLumaSkip += cs.getTU( currTULuma.Y().topLeft().offset(  0, -1 ), partitioner.chType )->transformSkip[0] ? 1 : 0;
        }

        checkTransformSkip &= ( nbLumaSkip > 0 );
      }
    }
Tung Nguyen's avatar
Tung Nguyen committed
2610
#endif
2611
2612
2613
2614
2615

    CodingStructure &saveCS = *m_pSaveCS[1];
    saveCS.pcv      = cs.pcv;
    saveCS.picture  = cs.picture;
    saveCS.area.repositionTo( cs.area );
2616
    saveCS.initStructData( MAX_INT, false, true );
2617

2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
#if JVET_M0102_INTRA_SUBPARTITIONS
    if( !CS::isDualITree( cs ) && currTU.cu->ispMode )
    {
      saveCS.clearCUs();
      CodingUnit& auxCU = saveCS.addCU( *currTU.cu, partitioner.chType );
      auxCU.ispMode = currTU.cu->ispMode;
      saveCS.sps = currTU.cs->sps;
      saveCS.clearPUs();
      saveCS.addPU( *currTU.cu->firstPU, partitioner.chType );
    }
#endif

2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
    TransformUnit &tmpTU = saveCS.addTU(currArea, partitioner.chType);


    cs.setDecomp(currArea.Cb(), true); // set in advance (required for Cb2/Cr2 in 4:2:2 video)

    const unsigned      numTBlocks  = ::getNumberValidTBlocks( *cs.pcv );

    for( uint32_t c = COMPONENT_Cb; c < numTBlocks; c++)
    {
      const ComponentID compID  = ComponentID(c);
      const CompArea&   area    = currTU.blocks[compID];

      double     dSingleCost    = MAX_DOUBLE;
      int        bestModeId     = 0;
      Distortion singleDistC    = 0;
      Distortion singleDistCTmp = 0;
      double     singleCostTmp  = 0;

      const bool checkCrossComponentPrediction = PU::isChromaIntraModeCrossCheckMode( pu ) && pps.getPpsRangeExtension().getCrossComponentPredictionEnabledFlag() && TU::getCbf( currTU, COMPONENT_Y );

      const int  crossCPredictionModesToTest = checkCrossComponentPrediction ? 2 : 1;
Tung Nguyen's avatar
Tung Nguyen committed
2651
2652
2653
#if JVET_M0464_UNI_MTS
      const int  totalModesToTest            = crossCPredictionModesToTest;
#else
2654
2655
      const int  transformSkipModesToTest    = checkTransformSkip ? 2 : 1;
      const int  totalModesToTest            = crossCPredictionModesToTest * transformSkipModesToTest;
Tung Nguyen's avatar
Tung Nguyen committed
2656
#endif
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
      const bool isOneMode                   = (totalModesToTest == 1);

      int currModeId = 0;
      int default0Save1Load2 = 0;

      TempCtx ctxStart  ( m_CtxCache );
      TempCtx ctxBest   ( m_CtxCache );

      if (!isOneMode)
      {
        ctxStart = m_CABACEstimator->getCtx();
      }

Tung Nguyen's avatar
Tung Nguyen committed
2670
#if !JVET_M0464_UNI_MTS
2671
      for (int transformSkipModeId = 0; transformSkipModeId < transformSkipModesToTest; transformSkipModeId++)
Tung Nguyen's avatar
Tung Nguyen committed
2672
#endif
2673
2674
2675
2676
      {
        for (int crossCPredictionModeId = 0; crossCPredictionModeId < crossCPredictionModesToTest; crossCPredictionModeId++)
        {
          currTU.compAlpha    [compID] = 0;
Tung Nguyen's avatar
Tung Nguyen committed
2677
#if !JVET_M0464_UNI_MTS
2678
          currTU.transformSkip[compID] = transformSkipModeId;
Tung Nguyen's avatar
Tung Nguyen committed
2679
#endif
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689

          currModeId++;

          const bool isFirstMode = (currModeId == 1);
          const bool isLastMode  = (currModeId == totalModesToTest); // currModeId is indexed from 1

          if (isOneMode)
          {
            default0Save1Load2 = 0;
          }
Tung Nguyen's avatar
Tung Nguyen committed
2690
2691
2692
#if JVET_M0464_UNI_MTS
          else if (!isOneMode && (crossCPredictionModeId == 0))
#else
2693
          else if (!isOneMode && (transformSkipModeId == 0) && (crossCPredictionModeId == 0))
Tung Nguyen's avatar
Tung Nguyen committed
2694
#endif
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
          {
            default0Save1Load2 = 1; //save prediction on first mode
          }
          else
          {
            default0Save1Load2 = 2; //load it on subsequent modes
          }

          if (!isFirstMode) // if not first mode to be tested
          {
            m_CABACEstimator->getCtx() = ctxStart;
          }

          singleDistCTmp = 0;

          xIntraCodingTUBlock( currTU, compID, crossCPredictionModeId != 0, singleDistCTmp, default0Save1Load2 );

Tung Nguyen's avatar
Tung Nguyen committed
2712
2713
2714
#if JVET_M0464_UNI_MTS
          if( ( ( crossCPredictionModeId == 1 ) && ( currTU.compAlpha[compID] == 0 ) ) ) //In order not to code TS flag when cbf is zero, the case for TS with cbf being zero is forbidden.
#else
2715
          if( ( ( crossCPredictionModeId == 1 ) && ( currTU.compAlpha[compID] == 0 ) ) || ( ( transformSkipModeId == 1 ) && !TU::getCbf( currTU, compID ) ) ) //In order not to code TS flag when cbf is zero, the case for TS with cbf being zero is forbidden.
Tung Nguyen's avatar
Tung Nguyen committed
2716
#endif
2717
2718
2719
          {
            singleCostTmp = MAX_DOUBLE;
          }
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
#if JVET_M0102_INTRA_SUBPARTITIONS
          else if( lumaUsesISP && bestCostSoFar != MAX_DOUBLE && c == COMPONENT_Cb )
          {
            uint64_t fracBitsTmp = xGetIntraFracBitsQTSingleChromaComponent( cs, partitioner, ComponentID( c ) );
            singleCostTmp = m_pcRdCost->calcRdCost( fracBitsTmp, singleDistCTmp );
            if( isOneMode || ( !isOneMode && !isLastMode ) )
            {
              m_CABACEstimator->getCtx() = ctxStart;
            }
          }
#endif
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
          else if( !isOneMode )
          {
            uint64_t fracBitsTmp = xGetIntraFracBitsQTChroma( currTU, compID );
            singleCostTmp = m_pcRdCost->calcRdCost( fracBitsTmp, singleDistCTmp );
          }

          if( singleCostTmp < dSingleCost )
          {
            dSingleCost = singleCostTmp;
            singleDistC = singleDistCTmp;
            bestModeId  = currModeId;

            if( !isLastMode )
            {
#if KEEP_PRED_AND_RESI_SIGNALS
              saveCS.getPredBuf   (area).copyFrom(cs.getPredBuf   (area));
              saveCS.getOrgResiBuf(area).copyFrom(cs.getOrgResiBuf(area));
Taoran Lu's avatar
Taoran Lu committed
2748
2749
2750
#endif
#if JVET_M0427_INLOOP_RESHAPER
              saveCS.getPredBuf   (area).copyFrom(cs.getPredBuf   (area));
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
#endif
              if( keepResi )
              {
                saveCS.getResiBuf (area).copyFrom(cs.getResiBuf   (area));
              }
              saveCS.getRecoBuf   (area).copyFrom(cs.getRecoBuf   (area));

              tmpTU.copyComponentFrom(currTU, compID);

              ctxBest = m_CABACEstimator->getCtx();
            }
          }
        }
      }

2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
#if JVET_M0102_INTRA_SUBPARTITIONS
      if( lumaUsesISP && dSingleCost > bestCostSoFar && c == COMPONENT_Cb )
      {
        //Luma + Cb cost is already larger than the best cost, so we don't need to test Cr
        cs.dist = MAX_UINT;
        m_CABACEstimator->getCtx() = ctxStart;
        break;
        //return cbfs;
      }
#endif

2777
2778
2779
2780
2781
      if (bestModeId < totalModesToTest)
      {
#if KEEP_PRED_AND_RESI_SIGNALS
        cs.getPredBuf   (area).copyFrom(saveCS.getPredBuf   (area));
        cs.getOrgResiBuf(area).copyFrom(saveCS.getOrgResiBuf(area));
Taoran Lu's avatar
Taoran Lu committed
2782
2783
2784
#endif
#if JVET_M0427_INLOOP_RESHAPER
        cs.getPredBuf   (area).copyFrom(saveCS.getPredBuf   (area));
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
#endif
        if( keepResi )
        {
          cs.getResiBuf (area).copyFrom(saveCS.getResiBuf   (area));
        }
        cs.getRecoBuf   (area).copyFrom(saveCS.getRecoBuf   (area));

        currTU.copyComponentFrom(tmpTU, compID);

        m_CABACEstimator->getCtx() = ctxBest;
      }

Taoran Lu's avatar
Taoran Lu committed
2797
2798
2799
#if JVET_M0427_INLOOP_RESHAPER
      cs.picture->getPredBuf(area).copyFrom(cs.getPredBuf(area));
#endif
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
      cs.picture->getRecoBuf(area).copyFrom(cs.getRecoBuf(area));

      cbfs.cbf(compID) = TU::getCbf(currTU, compID);

      cs.dist += singleDistC;
    }
  }
  else
  {
    unsigned    numValidTBlocks   = ::getNumberValidTBlocks( *cs.pcv );
    ChromaCbfs  SplitCbfs         ( false );

    if( partitioner.canSplit( TU_MAX_TR_SPLIT, cs ) )
    {
      partitioner.splitCurrArea( TU_MAX_TR_SPLIT, cs );
    }
2816
2817
2818
2819
2820
2821
#if JVET_M0102_INTRA_SUBPARTITIONS
    else if( currTU.cu->ispMode )
    {
      partitioner.splitCurrArea( ispType, cs );
    }
#endif
2822
2823
2824
2825
2826
    else
      THROW( "Implicit TU split not available" );

    do
    {
2827
2828
2829
#if JVET_M0102_INTRA_SUBPARTITIONS
      ChromaCbfs subCbfs = xRecurIntraChromaCodingQT( cs, partitioner, bestCostSoFar, ispType );
#else
2830
      ChromaCbfs subCbfs = xRecurIntraChromaCodingQT( cs, partitioner );
2831
#endif
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841

      for( uint32_t ch = COMPONENT_Cb; ch < numValidTBlocks; ch++ )
      {
        const ComponentID compID = ComponentID( ch );
        SplitCbfs.cbf( compID ) |= subCbfs.cbf( compID );
      }
    } while( partitioner.nextPart( cs ) );

    partitioner.exitCurrSplit();

2842
2843
2844
2845
2846
2847
#if JVET_M0102_INTRA_SUBPARTITIONS
    if( lumaUsesISP && cs.dist == MAX_UINT )
    {
      return cbfs;
    }
#endif
2848
2849
2850
2851
2852
    {

      cbfs.Cb |= SplitCbfs.Cb;
      cbfs.Cr |= SplitCbfs.Cr;

2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
#if JVET_M0102_INTRA_SUBPARTITIONS
      if( !lumaUsesISP )
      {
        for( auto &ptu : cs.tus )
        {
          if( currArea.Cb().contains( ptu->Cb() ) || ( !ptu->Cb().valid() && currArea.Y().contains( ptu->Y() ) ) )
          {
            TU::setCbfAtDepth( *ptu, COMPONENT_Cb, currDepth, SplitCbfs.Cb );
            TU::setCbfAtDepth( *ptu, COMPONENT_Cr, currDepth, SplitCbfs.Cr );
          }
        }
      }
#else
2866
2867
2868
2869
2870
2871
2872
2873
      for( auto &ptu : cs.tus )
      {
        if( currArea.Cb().contains( ptu->Cb() ) || ( !ptu->Cb().valid() && currArea.Y().contains( ptu->Y() ) ) )
        {
          TU::setCbfAtDepth( *ptu, COMPONENT_Cb, currDepth, SplitCbfs.Cb );
          TU::setCbfAtDepth( *ptu, COMPONENT_Cr, currDepth, SplitCbfs.Cr );
        }
      }
2874
#endif
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
    }
  }

  return cbfs;
}

uint64_t IntraSearch::xFracModeBitsIntra(PredictionUnit &pu, const uint32_t &uiMode, const ChannelType &chType)
{
  uint32_t orgMode = uiMode;

2885
  if (!pu.mhIntraFlag)
2886
2887
2888
2889
2890
2891
  std::swap(orgMode, pu.intraDir[chType]);

  m_CABACEstimator->resetBits();

  if( isLuma( chType ) )
  {
2892
    if ( pu.mhIntraFlag )
2893
2894
2895
      m_CABACEstimator->MHIntra_luma_pred_modes(*pu.cu);
    else
    {
2896
      m_CABACEstimator->extend_ref_line(pu);
2897
2898
      m_CABACEstimator->intra_luma_pred_mode(pu);
    }
2899
2900
2901
2902
2903
2904
  }
  else
  {
    m_CABACEstimator->intra_chroma_pred_mode( pu );
  }

2905
  if ( !pu.mhIntraFlag )
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
  std::swap(orgMode, pu.intraDir[chType]);

  return m_CABACEstimator->getEstFracBits();
}



void IntraSearch::encPredIntraDPCM( const ComponentID &compID, PelBuf &pOrg, PelBuf &pDst, const uint32_t &uiDirMode )
{
  CHECK( pOrg.buf == 0, "Encoder DPCM called without original buffer" );

  const int srcStride = m_topRefLength + 1;
  CPelBuf   pSrc = CPelBuf(getPredictorPtr(compID), srcStride, m_leftRefLength + 1);

  // Sample Adaptive intra-Prediction (SAP)
  if( uiDirMode == HOR_IDX )
  {
    // left column filled with reference samples, remaining columns filled with pOrg data
    for( int y = 0; y < pDst.height; y++ )
    {
      pDst.at( 0, y ) = pSrc.at( 0, 1 + y );
    }
    CPelBuf orgRest  = pOrg.subBuf( 0, 0, pOrg.width - 1, pOrg.height );
    PelBuf  predRest = pDst.subBuf( 1, 0, pDst.width - 1, pDst.height );

    predRest.copyFrom( orgRest );
  }
  else // VER_IDX
  {
    // top row filled with reference samples, remaining rows filled with pOrg data
    for( int x = 0; x < pDst.width; x++ )
    {
      pDst.at( x, 0 ) = pSrc.at( 1 + x, 0 );
    }
    CPelBuf orgRest  = pOrg.subBuf( 0, 0, pOrg.width, pOrg.height - 1 );
    PelBuf  predRest = pDst.subBuf( 0, 1, pDst.width, pDst.height - 1 );

    predRest.copyFrom( orgRest );
  }
}

bool IntraSearch::useDPCMForFirstPassIntraEstimation( const PredictionUnit &pu, const uint32_t &uiDirMode )
{
  return CU::isRDPCMEnabled( *pu.cu ) && pu.cu->transQuantBypass && (uiDirMode == HOR_IDX || uiDirMode == VER_IDX);
}