Skip to content
Snippets Groups Projects
CABACWriter.cpp 77.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • /* The copyright in this software is being made available under the BSD
    * License, included below. This software may be subject to other third party
    * and contributor rights, including patent rights, and no such rights are
    * granted under this license.
    *
    * Copyright (c) 2010-2018, ITU/ISO/IEC
    * All rights reserved.
    *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are met:
    *
    *  * Redistributions of source code must retain the above copyright notice,
    *    this list of conditions and the following disclaimer.
    *  * Redistributions in binary form must reproduce the above copyright notice,
    *    this list of conditions and the following disclaimer in the documentation
    *    and/or other materials provided with the distribution.
    *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
    *    be used to endorse or promote products derived from this software without
    *    specific prior written permission.
    *
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
    * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
    * THE POSSIBILITY OF SUCH DAMAGE.
    */
    
    /** \file     CABACWriter.cpp
     *  \brief    Writer for low level syntax
     */
    
    #include "CommonLib/Contexts.h"
    #include "CABACWriter.h"
    
    #include "EncLib.h"
    
    #include "CommonLib/UnitTools.h"
    #include "CommonLib/dtrace_buffer.h"
    #include "CommonLib/BinaryDecisionTree.h"
    
    #include <map>
    #include <algorithm>
    #include <limits>
    
    
    //! \ingroup EncoderLib
    //! \{
    
    void CABACWriter::initCtxModels( const Slice& slice )
    {
      int       qp                = slice.getSliceQp();
      SliceType sliceType         = slice.getSliceType();
      SliceType encCABACTableIdx  = slice.getEncCABACTableIdx();
      if( !slice.isIntra() && (encCABACTableIdx==B_SLICE || encCABACTableIdx==P_SLICE) && slice.getPPS()->getCabacInitPresentFlag() )
      {
        sliceType = encCABACTableIdx;
      }
      m_BinEncoder.reset( qp, (int)sliceType );
    }
    
    
    
    template <class BinProbModel>
    SliceType xGetCtxInitId( const Slice& slice, const BinEncIf& binEncoder, Ctx& ctxTest )
    {
      const CtxStore<BinProbModel>& ctxStoreTest = static_cast<const CtxStore<BinProbModel>&>( ctxTest );
      const CtxStore<BinProbModel>& ctxStoreRef  = static_cast<const CtxStore<BinProbModel>&>( binEncoder.getCtx() );
      int qp = slice.getSliceQp();
      if( !slice.isIntra() )
      {
        SliceType aSliceTypeChoices[] = { B_SLICE, P_SLICE };
        uint64_t  bestCost            = std::numeric_limits<uint64_t>::max();
        SliceType bestSliceType       = aSliceTypeChoices[0];
        for (uint32_t idx=0; idx<2; idx++)
        {
          uint64_t  curCost           = 0;
          SliceType curSliceType      = aSliceTypeChoices[idx];
          ctxTest.init( qp, (int)curSliceType );
          for( int k = 0; k < Ctx::NumberOfContexts; k++ )
          {
            if( binEncoder.getNumBins(k) > 0 )
            {
              curCost += uint64_t( binEncoder.getNumBins(k) ) * ctxStoreRef[k].estFracExcessBits( ctxStoreTest[k] );
            }
          }
          if (curCost < bestCost)
          {
            bestSliceType = curSliceType;
            bestCost      = curCost;
          }
        }
        return bestSliceType;
      }
      else
      {
        return I_SLICE;
      }
    }
    
    
    SliceType CABACWriter::getCtxInitId( const Slice& slice )
    {
      switch( m_TestCtx.getBPMType() )
      {
      case BPM_Std:   return  xGetCtxInitId<BinProbModel_Std>   ( slice, m_BinEncoder, m_TestCtx );
      default:        return  NUMBER_OF_SLICE_TYPES;
      }
    }
    
    
    
    unsigned estBits( BinEncIf& binEnc, const std::vector<bool>& bins, const Ctx& ctx, const int ctxId, const uint8_t winSize )
    {
      binEnc.initCtxAndWinSize( ctxId, ctx, winSize );
      binEnc.start();
      const std::size_t numBins   = bins.size();
      unsigned          startBits = binEnc.getNumWrittenBits();
      for( std::size_t binId = 0; binId < numBins; binId++ )
      {
        unsigned  bin = ( bins[binId] ? 1 : 0 );
        binEnc.encodeBin( bin, ctxId );
      }
      unsigned endBits    = binEnc.getNumWrittenBits();
      unsigned codedBits  = endBits - startBits;
      return   codedBits;
    }
    
    
    
    
    
    //================================================================================
    //  clause 7.3.8.1
    //--------------------------------------------------------------------------------
    //    void  end_of_slice()
    //================================================================================
    
    void CABACWriter::end_of_slice()
    {
      m_BinEncoder.encodeBinTrm ( 1 );
      m_BinEncoder.finish       ();
    }
    
    
    
    
    //================================================================================
    //  clause 7.3.8.2
    //--------------------------------------------------------------------------------
    //    bool  coding_tree_unit( cs, area, qp, ctuRsAddr, skipSao )
    //================================================================================
    
    void CABACWriter::coding_tree_unit( CodingStructure& cs, const UnitArea& area, int (&qps)[2], unsigned ctuRsAddr, bool skipSao /* = false */ )
    {
      CUCtx cuCtx( qps[CH_L] );
      Partitioner *partitioner = PartitionerFactory::get( *cs.slice );
    
      partitioner->initCtu( area, CH_L, *cs.slice );
    
      if( !skipSao )
      {
        sao( *cs.slice, ctuRsAddr );
      }
    
      for( int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++ )
      {
        codeAlfCtuEnableFlag( cs, ctuRsAddr, compIdx );
      }
    
    
      if ( CS::isDualITree(cs) && cs.pcv->chrFormat != CHROMA_400 && cs.pcv->maxCUWidth > 64 )
    
      {
        CUCtx chromaCuCtx(qps[CH_C]);
        Partitioner *chromaPartitioner = PartitionerFactory::get(*cs.slice);
        chromaPartitioner->initCtu(area, CH_C, *cs.slice);
        coding_tree(cs, *partitioner, cuCtx, chromaPartitioner, &chromaCuCtx);
        qps[CH_L] = cuCtx.qp;
        qps[CH_C] = chromaCuCtx.qp;
    
        delete chromaPartitioner;
      }
      else
      {
      coding_tree( cs, *partitioner, cuCtx );
      qps[CH_L] = cuCtx.qp;
      if( CS::isDualITree( cs ) && cs.pcv->chrFormat != CHROMA_400 )
      {
        CUCtx cuCtxChroma( qps[CH_C] );
        partitioner->initCtu( area, CH_C, *cs.slice );
        coding_tree( cs, *partitioner, cuCtxChroma );
        qps[CH_C] = cuCtxChroma.qp;
      }
    
    199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
    
      delete partitioner;
    }
    
    
    
    
    
    //================================================================================
    //  clause 7.3.8.3
    //--------------------------------------------------------------------------------
    //    void  sao             ( slice, ctuRsAddr )
    //    void  sao_block_pars  ( saoPars, bitDepths, sliceEnabled, leftMergeAvail, aboveMergeAvail, onlyEstMergeInfo )
    //    void  sao_offset_pars ( ctbPars, compID, sliceEnabled, bitDepth )
    //================================================================================
    
    void CABACWriter::sao( const Slice& slice, unsigned ctuRsAddr )
    {
      const SPS& sps = *slice.getSPS();
      if( !sps.getUseSAO() )
      {
        return;
      }
    
      CodingStructure&     cs                     = *slice.getPic()->cs;
      const PreCalcValues& pcv                    = *cs.pcv;
      const SAOBlkParam&  sao_ctu_pars            = cs.picture->getSAO()[ctuRsAddr];
      bool                slice_sao_luma_flag     = ( slice.getSaoEnabledFlag( CHANNEL_TYPE_LUMA ) );
      bool                slice_sao_chroma_flag   = ( slice.getSaoEnabledFlag( CHANNEL_TYPE_CHROMA ) && sps.getChromaFormatIdc() != CHROMA_400 );
      if( !slice_sao_luma_flag && !slice_sao_chroma_flag )
      {
        return;
      }
    
      bool                sliceEnabled[3]         = { slice_sao_luma_flag, slice_sao_chroma_flag, slice_sao_chroma_flag };
      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 unsigned      curSliceIdx             = slice.getIndependentSliceIdx();
    #if HEVC_TILES_WPP
      const unsigned      curTileIdx              = cs.picture->tileMap->getTileIdxMap( pos );
      bool                leftMergeAvail          = cs.getCURestricted( pos.offset( -(int)pcv.maxCUWidth, 0  ), curSliceIdx, curTileIdx, CH_L ) ? true : false;
      bool                aboveMergeAvail         = cs.getCURestricted( pos.offset( 0, -(int)pcv.maxCUHeight ), curSliceIdx, curTileIdx, CH_L ) ? true : false;
    #else
      bool                leftMergeAvail          = cs.getCURestricted( pos.offset( -(int)pcv.maxCUWidth, 0  ), curSliceIdx, CH_L ) ? true : false;
      bool                aboveMergeAvail         = cs.getCURestricted( pos.offset( 0, -(int)pcv.maxCUHeight ), curSliceIdx, CH_L ) ? true : false;
    #endif
      sao_block_pars( sao_ctu_pars, sps.getBitDepths(), sliceEnabled, leftMergeAvail, aboveMergeAvail, false );
    }
    
    
    void CABACWriter::sao_block_pars( const SAOBlkParam& saoPars, const BitDepths& bitDepths, bool* sliceEnabled, bool leftMergeAvail, bool aboveMergeAvail, bool onlyEstMergeInfo )
    {
      bool isLeftMerge  = false;
      bool isAboveMerge = false;
      if( leftMergeAvail )
      {
        // sao_merge_left_flag
        isLeftMerge   = ( saoPars[COMPONENT_Y].modeIdc == SAO_MODE_MERGE && saoPars[COMPONENT_Y].typeIdc == SAO_MERGE_LEFT );
        m_BinEncoder.encodeBin( (isLeftMerge), Ctx::SaoMergeFlag() );
      }
      if( aboveMergeAvail && !isLeftMerge )
      {
        // sao_merge_above_flag
        isAboveMerge  = ( saoPars[COMPONENT_Y].modeIdc == SAO_MODE_MERGE && saoPars[COMPONENT_Y].typeIdc == SAO_MERGE_ABOVE );
        m_BinEncoder.encodeBin( (isAboveMerge), Ctx::SaoMergeFlag() );
      }
      if( onlyEstMergeInfo )
      {
        return; //only for RDO
      }
      if( !isLeftMerge && !isAboveMerge )
      {
        // explicit parameters
        for( int compIdx=0; compIdx < MAX_NUM_COMPONENT; compIdx++ )
        {
          sao_offset_pars( saoPars[compIdx], ComponentID(compIdx), sliceEnabled[compIdx], bitDepths.recon[ toChannelType(ComponentID(compIdx)) ] );
        }
      }
    }
    
    
    void CABACWriter::sao_offset_pars( const SAOOffset& ctbPars, ComponentID compID, bool sliceEnabled, int bitDepth )
    {
      if( !sliceEnabled )
      {
        CHECK( ctbPars.modeIdc != SAO_MODE_OFF, "Sao must be off, if it is disabled on slice level" );
        return;
      }
      const bool isFirstCompOfChType = ( getFirstComponentOfChannel( toChannelType(compID) ) == compID );
    
      if( isFirstCompOfChType )
      {
        // sao_type_idx_luma / sao_type_idx_chroma
        if( ctbPars.modeIdc == SAO_MODE_OFF )
        {
          m_BinEncoder.encodeBin  ( 0, Ctx::SaoTypeIdx() );
        }
        else if( ctbPars.typeIdc == SAO_TYPE_BO )
        {
          m_BinEncoder.encodeBin  ( 1, Ctx::SaoTypeIdx() );
          m_BinEncoder.encodeBinEP( 0 );
        }
        else
        {
          CHECK(!( ctbPars.typeIdc < SAO_TYPE_START_BO ), "Unspecified error");
          m_BinEncoder.encodeBin  ( 1, Ctx::SaoTypeIdx() );
          m_BinEncoder.encodeBinEP( 1 );
        }
      }
    
      if( ctbPars.modeIdc == SAO_MODE_NEW )
      {
        const int maxOffsetQVal = SampleAdaptiveOffset::getMaxOffsetQVal( bitDepth );
        int       numClasses    = ( ctbPars.typeIdc == SAO_TYPE_BO ? 4 : NUM_SAO_EO_CLASSES );
        int       k             = 0;
        int       offset[4];
        for( int i = 0; i < numClasses; i++ )
        {
          if( ctbPars.typeIdc != SAO_TYPE_BO && i == SAO_CLASS_EO_PLAIN )
          {
            continue;
          }
          int classIdx = ( ctbPars.typeIdc == SAO_TYPE_BO ? ( ctbPars.typeAuxInfo + i ) % NUM_SAO_BO_CLASSES : i );
          offset[k++]  = ctbPars.offset[classIdx];
        }
    
        // sao_offset_abs
        for( int i = 0; i < 4; i++ )
        {
          unsigned absOffset = ( offset[i] < 0 ? -offset[i] : offset[i] );
          unary_max_eqprob( absOffset, maxOffsetQVal );
        }
    
        // band offset mode
        if( ctbPars.typeIdc == SAO_TYPE_BO )
        {
          // sao_offset_sign
          for( int i = 0; i < 4; i++ )
          {
            if( offset[i] )
            {
              m_BinEncoder.encodeBinEP( (offset[i] < 0) );
            }
          }
          // sao_band_position
          m_BinEncoder.encodeBinsEP( ctbPars.typeAuxInfo, NUM_SAO_BO_CLASSES_LOG2 );
        }
        // edge offset mode
        else
        {
          if( isFirstCompOfChType )
          {
            // sao_eo_class_luma / sao_eo_class_chroma
            CHECK( ctbPars.typeIdc - SAO_TYPE_START_EO < 0, "sao edge offset class is outside valid range" );
            m_BinEncoder.encodeBinsEP( ctbPars.typeIdc - SAO_TYPE_START_EO, NUM_SAO_EO_TYPES_LOG2 );
          }
        }
      }
    }
    
    
    
    //================================================================================
    //  clause 7.3.8.4
    //--------------------------------------------------------------------------------
    //    void  coding_tree       ( cs, partitioner, cuCtx )
    //    void  split_cu_flag     ( split, cs, partitioner )
    //    void  split_cu_mode_mt  ( split, cs, partitioner )
    //================================================================================
    
    void CABACWriter::coding_tree(const CodingStructure& cs, Partitioner& partitioner, CUCtx& cuCtx, Partitioner* pPartitionerChroma, CUCtx* pCuCtxChroma)
    {
      const PPS      &pps         = *cs.pps;
      const UnitArea &currArea    = partitioner.currArea();
      const CodingUnit &cu        = *cs.getCU( currArea.blocks[partitioner.chType], partitioner.chType );
    
      // Reset delta QP coding flag and ChromaQPAdjustemt coding flag
      if( pps.getUseDQP() && partitioner.currDepth <= pps.getMaxCuDQPDepth() )
      {
        cuCtx.isDQPCoded          = false;
      }
      if( cs.slice->getUseChromaQpAdj() && partitioner.currDepth <= pps.getPpsRangeExtension().getDiffCuChromaQpOffsetDepth() )
      {
        cuCtx.isChromaQpAdjCoded  = false;
      }
      // Reset delta QP coding flag and ChromaQPAdjustemt coding flag
      if (CS::isDualITree(cs) && pPartitionerChroma != nullptr)
      {
        if (pps.getUseDQP() && pPartitionerChroma->currDepth <= pps.getMaxCuDQPDepth())
        {
          pCuCtxChroma->isDQPCoded = false;
        }
        if (cs.slice->getUseChromaQpAdj() && pPartitionerChroma->currDepth <= pps.getPpsRangeExtension().getDiffCuChromaQpOffsetDepth())
        {
          pCuCtxChroma->isChromaQpAdjCoded = false;
        }
      }
    
      const PartSplit implicitSplit = partitioner.getImplicitSplit( cs );
    
      // QT
      bool canQtSplit = partitioner.canSplit( CU_QUAD_SPLIT, cs );
    
      if( canQtSplit )
      {
        // split_cu_flag
        bool qtSplit = implicitSplit == CU_QUAD_SPLIT;
    
        if( !qtSplit && implicitSplit != CU_QUAD_SPLIT )
        {
          qtSplit = ( cu.qtDepth > partitioner.currQtDepth );
          split_cu_flag( qtSplit, cs, partitioner );
        }
    
        // quad-tree split
        if( qtSplit )
        {
          if (CS::isDualITree(cs) && pPartitionerChroma != nullptr && (partitioner.currArea().lwidth() >= 64 || partitioner.currArea().lheight() >= 64))
          {
            partitioner.splitCurrArea(CU_QUAD_SPLIT, cs);
            pPartitionerChroma->splitCurrArea(CU_QUAD_SPLIT, cs);
            bool beContinue = true;
            bool lumaContinue = true;
            bool chromaContinue = true;
    
            while (beContinue)
            {
              if (partitioner.currArea().lwidth() > 64 || partitioner.currArea().lheight() > 64)
              {
                if (cs.picture->blocks[partitioner.chType].contains(partitioner.currArea().blocks[partitioner.chType].pos()))
                {
                  coding_tree(cs, partitioner, cuCtx, pPartitionerChroma, pCuCtxChroma);
                }
                lumaContinue = partitioner.nextPart(cs);
                chromaContinue = pPartitionerChroma->nextPart(cs);
                CHECK(lumaContinue != chromaContinue, "luma chroma partition should be matched");
                beContinue = lumaContinue;
              }
              else
              {
                //dual tree coding under 64x64 block
                if (cs.picture->blocks[partitioner.chType].contains(partitioner.currArea().blocks[partitioner.chType].pos()))
                {
                  coding_tree(cs, partitioner, cuCtx);
                }
                lumaContinue = partitioner.nextPart(cs);
                if (cs.picture->blocks[pPartitionerChroma->chType].contains(pPartitionerChroma->currArea().blocks[pPartitionerChroma->chType].pos()))
                {
                  coding_tree(cs, *pPartitionerChroma, *pCuCtxChroma);
                }
                chromaContinue = pPartitionerChroma->nextPart(cs);
                CHECK(lumaContinue != chromaContinue, "luma chroma partition should be matched");
                beContinue = lumaContinue;
              }
            }
            partitioner.exitCurrSplit();
            pPartitionerChroma->exitCurrSplit();
    
          }
          else
          {
          partitioner.splitCurrArea( CU_QUAD_SPLIT, cs );
    
          do
          {
            if( cs.picture->blocks[partitioner.chType].contains( partitioner.currArea().blocks[partitioner.chType].pos() ) )
            {
              coding_tree( cs, partitioner, cuCtx );
            }
          } while( partitioner.nextPart( cs ) );
    
          partitioner.exitCurrSplit();
          }
          return;
        }
      }
    
      {
        bool mtSplit = partitioner.canSplit( CU_MT_SPLIT, cs );
    
        if( mtSplit )
        {
          const PartSplit splitMode = CU::getSplitAtDepth( cu, partitioner.currDepth );
    
          split_cu_mode_mt( splitMode, cs, partitioner );
    
          if( splitMode != CU_DONT_SPLIT )
          {
            partitioner.splitCurrArea( splitMode, cs );
            do
            {
              if( cs.picture->blocks[partitioner.chType].contains( partitioner.currArea().blocks[partitioner.chType].pos() ) )
              {
                coding_tree( cs, partitioner, cuCtx );
              }
            } while( partitioner.nextPart( cs ) );
    
            partitioner.exitCurrSplit();
            return;
          }
        }
      }
    
      // Predict QP on start of quantization group
      if( pps.getUseDQP() && !cuCtx.isDQPCoded && CU::isQGStart( cu ) )
      {
        cuCtx.qp = CU::predictQP( cu, cuCtx.qp );
      }
    
      if (!cs.slice->isIntra() && m_EncCu)
      {
        PredictionUnit& pu = *cu.firstPU;
        if (pu.mergeFlag && (pu.mergeType == MRG_TYPE_SUBPU_ATMVP))
        {
          unsigned int layerId = cs.slice->getDepth();
          m_EncCu->incrementSubMergeBlkSize(layerId, cu.Y().width*cu.Y().height);
          m_EncCu->incrementSubMergeBlkNum(layerId, 1);
        }
      }
    
      // coding unit
      coding_unit( cu, partitioner, cuCtx );
    
      DTRACE_COND( ( isEncoding() ), g_trace_ctx, D_QP, "x=%d, y=%d, w=%d, h=%d, qp=%d\n", cu.Y().x, cu.Y().y, cu.Y().width, cu.Y().height, cu.qp );
      DTRACE_BLOCK_REC_COND( ( !isEncoding() ), cs.picture->getRecoBuf( cu ), cu, cu.predMode );
    }
    
    void CABACWriter::split_cu_flag( bool split, const CodingStructure& cs, Partitioner& partitioner )
    {
      unsigned maxQTDepth = ( cs.sps->getSpsNext().getUseQTBT()
        ? g_aucLog2[cs.sps->getSpsNext().getCTUSize()] - g_aucLog2[cs.sps->getSpsNext().getMinQTSize( cs.slice->getSliceType(), partitioner.chType )]
        : cs.sps->getLog2DiffMaxMinCodingBlockSize() );
      if( partitioner.currDepth == maxQTDepth )
      {
        return;
      }
      unsigned  ctxId = DeriveCtx::CtxCUsplit( cs, partitioner );
      m_BinEncoder.encodeBin( (split), Ctx::SplitFlag(ctxId) );
    
      DTRACE( g_trace_ctx, D_SYNTAX, "split_cu_flag() ctx=%d split=%d\n", ctxId, split ? 1 : 0 );
    }
    
    void CABACWriter::split_cu_mode_mt(const PartSplit split, const CodingStructure& cs, Partitioner& partitioner)
    {
      unsigned ctxIdBT = DeriveCtx::CtxBTsplit( cs, partitioner );
    
      unsigned width   = partitioner.currArea().lumaSize().width;
      unsigned height  = partitioner.currArea().lumaSize().height;
    
      DecisionTree dt( g_mtSplitDTT );
    
    #if HM_QTBT_AS_IN_JEM_SYNTAX
      unsigned minBTSize = cs.slice->isIntra() ? ( partitioner.chType == 0 ? MIN_BT_SIZE : MIN_BT_SIZE_C ) : MIN_BT_SIZE_INTER;
    
      dt.setAvail( DTT_SPLIT_BT_HORZ,  height > minBTSize && ( partitioner.canSplit( CU_HORZ_SPLIT, cs ) || width  == minBTSize ) );
      dt.setAvail( DTT_SPLIT_BT_VERT,  width  > minBTSize && ( partitioner.canSplit( CU_VERT_SPLIT, cs ) || height == minBTSize ) );
    #else                              
      dt.setAvail( DTT_SPLIT_BT_HORZ,  partitioner.canSplit( CU_HORZ_SPLIT, cs ) );
      dt.setAvail( DTT_SPLIT_BT_VERT,  partitioner.canSplit( CU_VERT_SPLIT, cs ) );
    #endif
    
      dt.setAvail( DTT_SPLIT_TT_HORZ,  partitioner.canSplit( CU_TRIH_SPLIT, cs ) );
      dt.setAvail( DTT_SPLIT_TT_VERT,  partitioner.canSplit( CU_TRIV_SPLIT, cs ) );
      dt.setAvail( DTT_SPLIT_NO_SPLIT, partitioner.canSplit( CU_DONT_SPLIT, cs ) );
    
      unsigned btSCtxId = width == height ? 0 : ( width > height ? 1 : 2 );
      dt.setCtxId( DTT_SPLIT_DO_SPLIT_DECISION,   Ctx::BTSplitFlag( ctxIdBT ) );
      dt.setCtxId( DTT_SPLIT_HV_DECISION,         Ctx::BTSplitFlag( 3 + btSCtxId ) );
    
      dt.setCtxId( DTT_SPLIT_H_IS_BT_12_DECISION, Ctx::BTSplitFlag( 6 + btSCtxId ) );
      dt.setCtxId( DTT_SPLIT_V_IS_BT_12_DECISION, Ctx::BTSplitFlag( 9 + btSCtxId ) );
    
    
      encode_sparse_dt( dt, split == CU_DONT_SPLIT ? ( unsigned ) DTT_SPLIT_NO_SPLIT : ( unsigned ) split );
    
      DTRACE(g_trace_ctx, D_SYNTAX, "split_cu_mode_mt() ctx=%d split=%d\n", ctxIdBT, split);
    }
    
    
    //================================================================================
    //  clause 7.3.8.5
    //--------------------------------------------------------------------------------
    //    void  coding_unit               ( cu, partitioner, cuCtx )
    //    void  cu_transquant_bypass_flag ( cu )
    //    void  cu_skip_flag              ( cu )
    //    void  pred_mode                 ( cu )
    //    void  part_mode                 ( cu )
    //    void  pcm_flag                  ( cu )
    //    void  pcm_samples               ( tu )
    //    void  cu_pred_data              ( pus )
    //    void  cu_lic_flag               ( cu )
    //    void  intra_luma_pred_modes     ( pus )
    //    void  intra_chroma_pred_mode    ( pu )
    //    void  cu_residual               ( cu, partitioner, cuCtx )
    //    void  rqt_root_cbf              ( cu )
    //    void  end_of_ctu                ( cu, cuCtx )
    //================================================================================
    
    void CABACWriter::coding_unit( const CodingUnit& cu, Partitioner& partitioner, CUCtx& cuCtx )
    {
      CodingStructure& cs = *cu.cs;
      // transquant bypass flag
      if( cs.pps->getTransquantBypassEnabledFlag() )
      {
        cu_transquant_bypass_flag( cu );
      }
    
      // skip flag
      if( !cs.slice->isIntra() )
      {
        cu_skip_flag( cu );
      }
    
    
      // skip data
      if( cu.skip )
      {
        CHECK( !cu.firstPU->mergeFlag, "Merge flag has to be on!" );
        PredictionUnit&   pu = *cu.firstPU;
        prediction_unit ( pu );
        end_of_ctu      ( cu, cuCtx );
        return;
      }
    
      // prediction mode and partitioning data
      pred_mode ( cu );
    
      // pcm samples
      if( CU::isIntra(cu) && cu.partSize == SIZE_2Nx2N )
      {
        pcm_data( cu );
        if( cu.ipcm )
        {
          end_of_ctu( cu, cuCtx );
          return;
        }
      }
    
      // prediction data ( intra prediction modes / reference indexes + motion vectors )
      cu_pred_data( cu );
    
      // residual data ( coded block flags + transform coefficient levels )
      cu_residual( cu, partitioner, cuCtx );
    
      // end of cu
      end_of_ctu( cu, cuCtx );
    }
    
    
    void CABACWriter::cu_transquant_bypass_flag( const CodingUnit& cu )
    {
      m_BinEncoder.encodeBin( (cu.transQuantBypass), Ctx::TransquantBypassFlag() );
    }
    
    
    void CABACWriter::cu_skip_flag( const CodingUnit& cu )
    {
      unsigned ctxId = DeriveCtx::CtxSkipFlag( cu );
      m_BinEncoder.encodeBin( ( cu.skip ), Ctx::SkipFlag( ctxId ) );
    
      DTRACE( g_trace_ctx, D_SYNTAX, "cu_skip_flag() ctx=%d skip=%d\n", ctxId, cu.skip ? 1 : 0 );
    }
    
    
    void CABACWriter::pred_mode( const CodingUnit& cu )
    {
      if( cu.cs->slice->isIntra() )
      {
        return;
      }
      m_BinEncoder.encodeBin( ( CU::isIntra( cu ) ), Ctx::PredMode() );
    }
    
    void CABACWriter::pcm_data( const CodingUnit& cu )
    {
      pcm_flag( cu );
      if( cu.ipcm )
      {
        m_BinEncoder.pcmAlignBits();
        pcm_samples( *cu.firstTU );
      }
    }
    
    
    void CABACWriter::pcm_flag( const CodingUnit& cu )
    {
      const SPS& sps = *cu.cs->sps;
      if( !sps.getUsePCM() || cu.lumaSize().width > (1 << sps.getPCMLog2MaxSize()) || cu.lumaSize().width < (1 << sps.getPCMLog2MinSize()) )
      {
        return;
      }
      m_BinEncoder.encodeBinTrm( cu.ipcm );
    }
    
    
    void CABACWriter::cu_pred_data( const CodingUnit& cu )
    {
      if( CU::isIntra( cu ) )
      {
        intra_luma_pred_modes  ( cu );
        intra_chroma_pred_modes( cu );
        return;
      }
      for( auto &pu : CU::traversePUs( cu ) )
      {
        prediction_unit( pu );
      }
    
      imv_mode   ( cu );
    
    }
    
    
    
    void CABACWriter::intra_luma_pred_modes( const CodingUnit& cu )
    {
      if( !cu.Y().valid() )
      {
        return;
      }
    
      unsigned numMPMs   = cu.cs->pcv->numMPMs;
      int      numBlocks = CU::getNumPUs( cu );
      unsigned *mpm_preds  [4];
      unsigned mpm_idxs    [4];
      unsigned ipred_modes [4];
    
      const PredictionUnit* pu = cu.firstPU;
    
      // prev_intra_luma_pred_flag
      for( int k = 0; k < numBlocks; k++ )
      {
        unsigned*& mpm_pred   = mpm_preds[k];
        unsigned&  mpm_idx    = mpm_idxs[k];
        unsigned&  ipred_mode = ipred_modes[k];
    
        mpm_pred = ( unsigned* ) alloca( numMPMs * sizeof( unsigned ) );
        PU::getIntraMPMs( *pu, mpm_pred );
    
        ipred_mode = pu->intraDir[0];
        mpm_idx    = numMPMs;
        for( unsigned idx = 0; idx < numMPMs; idx++ )
        {
          if( ipred_mode == mpm_pred[idx] )
          {
            mpm_idx = idx;
            break;
          }
        }
        m_BinEncoder.encodeBin( mpm_idx < numMPMs, Ctx::IPredMode[0]() );
    
        pu = pu->next;
      }
    
      pu = cu.firstPU;
    
      // mpm_idx / rem_intra_luma_pred_mode
      for( int k = 0; k < numBlocks; k++ )
      {
        const unsigned& mpm_idx = mpm_idxs[k];
        if( mpm_idx < numMPMs )
        {
          {
            m_BinEncoder.encodeBinEP( mpm_idx > 0 );
            if( mpm_idx )
            {
              m_BinEncoder.encodeBinEP( mpm_idx > 1 );
            }
          }
        }
        else
        {
          unsigned* mpm_pred   = mpm_preds[k];
          unsigned  ipred_mode = ipred_modes[k];
    
          // sorting of MPMs
          std::sort( mpm_pred, mpm_pred + numMPMs );
    
          {
            for (int idx = int(numMPMs) - 1; idx >= 0; idx--)
            {
              if (ipred_mode > mpm_pred[idx])
              {
                ipred_mode--;
              }
            }
            CHECK(ipred_mode >= 64, "Incorrect mode");
    
            m_BinEncoder.encodeBinsEP(ipred_mode, 6);
          }
        }
    
        DTRACE( g_trace_ctx, D_SYNTAX, "intra_luma_pred_modes() idx=%d pos=(%d,%d) mode=%d\n", k, pu->lumaPos().x, pu->lumaPos().y, pu->intraDir[0] );
        pu = pu->next;
      }
    }
    
    
    void CABACWriter::intra_luma_pred_mode( const PredictionUnit& pu )
    {
    
      // prev_intra_luma_pred_flag
      unsigned  numMPMs  = pu.cs->pcv->numMPMs;
      unsigned *mpm_pred = ( unsigned* ) alloca( numMPMs * sizeof( unsigned ) );
    
      PU::getIntraMPMs( pu, mpm_pred );
    
      unsigned ipred_mode = pu.intraDir[0];
      unsigned mpm_idx = numMPMs;
    
      for( unsigned idx = 0; idx < numMPMs; idx++ )
      {
        if( ipred_mode == mpm_pred[idx] )
        {
          mpm_idx = idx;
          break;
        }
      }
      m_BinEncoder.encodeBin( mpm_idx < numMPMs, Ctx::IPredMode[0]() );
    
      // mpm_idx / rem_intra_luma_pred_mode
      if( mpm_idx < numMPMs )
      {
        {
          m_BinEncoder.encodeBinEP( mpm_idx > 0 );
          if( mpm_idx )
          {
            m_BinEncoder.encodeBinEP( mpm_idx > 1 );
          }
        }
      }
      else
      {
        std::sort( mpm_pred, mpm_pred + numMPMs );
        {
          for (int idx = int(numMPMs) - 1; idx >= 0; idx--)
          {
            if (ipred_mode > mpm_pred[idx])
            {
              ipred_mode--;
            }
          }
          m_BinEncoder.encodeBinsEP(ipred_mode, 6);
        }
      }
    }
    
    
    void CABACWriter::intra_chroma_pred_modes( const CodingUnit& cu )
    {
      if( cu.chromaFormat == CHROMA_400 || ( CS::isDualITree( *cu.cs ) && cu.chType == CHANNEL_TYPE_LUMA ) )
      {
        return;
      }
    
      const PredictionUnit* pu = cu.firstPU;
    
      intra_chroma_pred_mode( *pu );
    }
    
    void CABACWriter::intra_chroma_lmc_mode( const PredictionUnit& pu )
    {
      const unsigned intraDir = pu.intraDir[1];
        int lmModeList[10];
        int maxSymbol = PU::getLMSymbolList( pu, lmModeList );
        int symbol    = -1;
        for ( int k = 0; k < LM_SYMBOL_NUM; k++ )
        {
          if ( lmModeList[k] == intraDir || ( lmModeList[k] == -1 && intraDir < LM_CHROMA_IDX ) )
          {
            symbol = k;
            break;
          }
        }
        CHECK( symbol < 0, "invalid symbol found" );
    
        unary_max_symbol( symbol, Ctx::IPredMode[1]( 2 ), Ctx::IPredMode[1]( 3 ), maxSymbol - 1 );
    }
    
    
    void CABACWriter::intra_chroma_pred_mode( const PredictionUnit& pu )
    {
      const unsigned intraDir = pu.intraDir[1];
      {
        if( intraDir == DM_CHROMA_IDX )
        {
          m_BinEncoder.encodeBin( 0, Ctx::IPredMode[1]( 1 ) );
          return;
        }
        m_BinEncoder.encodeBin( 1, Ctx::IPredMode[1]( 1 ) );
      }
    
      // LM chroma mode
      if( pu.cs->sps->getSpsNext().getUseLMChroma() )
      {
        intra_chroma_lmc_mode( pu );
        if ( PU::isLMCMode( intraDir ) )
        {
          return;
        }
      }
    
      // chroma candidate index
      unsigned chromaCandModes[ NUM_CHROMA_MODE ];
      PU::getIntraChromaCandModes( pu, chromaCandModes );
    
      int candId = 0;
      for ( ; candId < NUM_CHROMA_MODE; candId++ )
      {
        if( intraDir == chromaCandModes[ candId ] )
        {
          break;
        }
      }
    
      CHECK( candId >= NUM_CHROMA_MODE, "Chroma prediction mode index out of bounds" );
      CHECK( chromaCandModes[ candId ] == DM_CHROMA_IDX, "The intra dir cannot be DM_CHROMA for this path" );
      {
        m_BinEncoder.encodeBinsEP( candId, 2 );
      }
    }
    
    
    void CABACWriter::cu_residual( const CodingUnit& cu, Partitioner& partitioner, CUCtx& cuCtx )
    {
      if( CU::isInter( cu ) )
      {
        PredictionUnit& pu = *cu.firstPU;
        if( !( ( cu.cs->pcv->noRQT || cu.partSize == SIZE_2Nx2N ) && pu.mergeFlag ) )
        {
          rqt_root_cbf( cu );
        }
    
        if( !cu.rootCbf )
        {
          return;
        }
      }
    
    
      ChromaCbfs chromaCbfs;
      transform_tree( *cu.cs, partitioner, cuCtx, chromaCbfs );
    
    
    #if !HM_EMT_NSST_AS_IN_JEM
    
    #if !HM_EMT_NSST_AS_IN_JEM
    
    void CABACWriter::cu_emt_pertu_idx( const CodingUnit& cu )
    {
      bool anyCbf = false, anyNonTs = false;
    
      for( const auto &tu : CU::traverseTUs( cu ) )
      {
        anyCbf   |= tu.cbf[0] != 0;
        anyNonTs |= !tu.transformSkip[0];
      }
    
      if( !cu.cs->pcv->noRQT || !isLuma( cu.chType ) || cu.nsstIdx != 0 ||
          !( cu.cs->sps->getSpsNext().getUseIntraEMT() || cu.cs->sps->getSpsNext().getUseInterEMT() ) || !anyCbf || !anyNonTs )
      {
        return;
      }
    
      emt_cu_flag( cu );
    
      if( cu.emtFlag )
      {
        for( const auto &tu : CU::traverseTUs( cu ) )
        {
          if( CU::isIntra( cu ) )
          {
            if( TU::getNumNonZeroCoeffsNonTS( tu, true, false ) > g_EmtSigNumThr )
            {
              emt_tu_index( tu );
            }
            else
            {
              CHECK( tu.emtIdx != 0, "If the number of significant coefficients is <= g_EmtSigNumThr, then the tu index must be 0" );
            }
          }
          else
          {
            emt_tu_index( *cu.firstTU );
          }
        }
      }
    }
    
    #endif
    void CABACWriter::rqt_root_cbf( const CodingUnit& cu )
    {
      m_BinEncoder.encodeBin( cu.rootCbf, Ctx::QtRootCbf() );
    
      DTRACE( g_trace_ctx, D_SYNTAX, "rqt_root_cbf() ctx=0 root_cbf=%d pos=(%d,%d)\n", cu.rootCbf ? 1 : 0, cu.lumaPos().x, cu.lumaPos().y );
    }
    
    
    void CABACWriter::end_of_ctu( const CodingUnit& cu, CUCtx& cuCtx )