Skip to content
Snippets Groups Projects
InterSearch.cpp 223 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-2019, 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     EncSearch.cpp
     *  \brief    encoder inter search class
     */
    
    #include "InterSearch.h"
    
    
    #include "CommonLib/CommonDef.h"
    #include "CommonLib/Rom.h"
    #include "CommonLib/MotionInfo.h"
    #include "CommonLib/Picture.h"
    #include "CommonLib/UnitTools.h"
    #include "CommonLib/dtrace_next.h"
    #include "CommonLib/dtrace_buffer.h"
    
    #include "EncModeCtrl.h"
    #include "EncLib.h"
    
    #include <math.h>
    #include <limits>
    
    
     //! \ingroup EncoderLib
     //! \{
    
    static const Mv s_acMvRefineH[9] =
    {
      Mv(  0,  0 ), // 0
      Mv(  0, -1 ), // 1
      Mv(  0,  1 ), // 2
      Mv( -1,  0 ), // 3
      Mv(  1,  0 ), // 4
      Mv( -1, -1 ), // 5
      Mv(  1, -1 ), // 6
      Mv( -1,  1 ), // 7
      Mv(  1,  1 )  // 8
    };
    
    static const Mv s_acMvRefineQ[9] =
    {
      Mv(  0,  0 ), // 0
      Mv(  0, -1 ), // 1
      Mv(  0,  1 ), // 2
      Mv( -1, -1 ), // 5
      Mv(  1, -1 ), // 6
      Mv( -1,  0 ), // 3
      Mv(  1,  0 ), // 4
      Mv( -1,  1 ), // 7
      Mv(  1,  1 )  // 8
    };
    
    
    InterSearch::InterSearch()
      : m_modeCtrl                    (nullptr)
      , m_pSplitCS                    (nullptr)
      , m_pFullCS                     (nullptr)
      , m_pcEncCfg                    (nullptr)
      , m_pcTrQuant                   (nullptr)
    
    Taoran Lu's avatar
    Taoran Lu committed
    #if JVET_M0427_INLOOP_RESHAPER
      , m_pcReshape                   (nullptr)
    #endif
    
      , m_iSearchRange                (0)
      , m_bipredSearchRange           (0)
      , m_motionEstimationSearchMethod(MESEARCH_FULL)
      , m_CABACEstimator              (nullptr)
      , m_CtxCache                    (nullptr)
      , m_pTempPel                    (nullptr)
      , m_isInitialized               (false)
    {
      for (int i=0; i<MAX_NUM_REF_LIST_ADAPT_SR; i++)
      {
        memset (m_aaiAdaptSR[i], 0, MAX_IDX_ADAPT_SR * sizeof (int));
      }
      for (int i=0; i<AMVP_MAX_NUM_CANDS+1; i++)
      {
        memset (m_auiMVPIdxCost[i], 0, (AMVP_MAX_NUM_CANDS+1) * sizeof (uint32_t) );
      }
    
      setWpScalingDistParam( -1, REF_PIC_LIST_X, nullptr );
    
      m_affMVList = nullptr;
      m_affMVListSize = 0;
      m_affMVListIdx = 0;
    
    }
    
    
    void InterSearch::destroy()
    {
      CHECK(!m_isInitialized, "Not initialized");
      if ( m_pTempPel )
      {
        delete [] m_pTempPel;
        m_pTempPel = NULL;
      }
    
      m_pSplitCS = m_pFullCS = nullptr;
    
      m_pSaveCS = nullptr;
    
      for(uint32_t i = 0; i < NUM_REF_PIC_LIST_01; i++)
      {
        m_tmpPredStorage[i].destroy();
      }
      m_tmpStorageLCU.destroy();
      m_tmpAffiStorage.destroy();
    
      if ( m_tmpAffiError != NULL )
      {
        delete[] m_tmpAffiError;
      }
      if ( m_tmpAffiDeri[0] != NULL )
      {
        delete[] m_tmpAffiDeri[0];
      }
      if ( m_tmpAffiDeri[1] != NULL )
      {
        delete[] m_tmpAffiDeri[1];
      }
    
      if (m_affMVList)
      {
        delete[] m_affMVList;
        m_affMVList = nullptr;
      }
      m_affMVListIdx = 0;
      m_affMVListSize = 0;
    
      m_isInitialized = false;
    }
    
    void InterSearch::setTempBuffers( CodingStructure ****pSplitCS, CodingStructure ****pFullCS, CodingStructure **pSaveCS )
    {
      m_pSplitCS = pSplitCS;
      m_pFullCS  = pFullCS;
      m_pSaveCS  = pSaveCS;
    }
    
    #if ENABLE_SPLIT_PARALLELISM
    void InterSearch::copyState( const InterSearch& other )
    {
      if( !m_pcEncCfg->getQTBT() )
      {
        memcpy( m_integerMv2Nx2N, other.m_integerMv2Nx2N, sizeof( m_integerMv2Nx2N ) );
      }
    
      memcpy( m_aaiAdaptSR, other.m_aaiAdaptSR, sizeof( m_aaiAdaptSR ) );
    }
    #endif
    
    InterSearch::~InterSearch()
    {
      if (m_isInitialized)
      {
        destroy();
      }
    }
    
    void InterSearch::init( EncCfg*        pcEncCfg,
                            TrQuant*       pcTrQuant,
                            int            iSearchRange,
                            int            bipredSearchRange,
                            MESearchMethod motionEstimationSearchMethod,
                            const uint32_t     maxCUWidth,
                            const uint32_t     maxCUHeight,
                            const uint32_t     maxTotalCUDepth,
                            RdCost*        pcRdCost,
                            CABACWriter*   CABACEstimator,
                            CtxCache*      ctxCache
    
    Taoran Lu's avatar
    Taoran Lu committed
    #if JVET_M0427_INLOOP_RESHAPER
                          , EncReshape*    pcReshape
    #endif
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      m_numBVs = 0;
      m_numBV16s = 0;
    
      m_pcEncCfg                     = pcEncCfg;
      m_pcTrQuant                    = pcTrQuant;
      m_iSearchRange                 = iSearchRange;
      m_bipredSearchRange            = bipredSearchRange;
      m_motionEstimationSearchMethod = motionEstimationSearchMethod;
      m_CABACEstimator               = CABACEstimator;
      m_CtxCache                     = ctxCache;
    
    Taoran Lu's avatar
    Taoran Lu committed
    #if JVET_M0427_INLOOP_RESHAPER
      m_pcReshape                    = pcReshape;
    #endif
    
    
      for( uint32_t iDir = 0; iDir < MAX_NUM_REF_LIST_ADAPT_SR; iDir++ )
      {
        for( uint32_t iRefIdx = 0; iRefIdx < MAX_IDX_ADAPT_SR; iRefIdx++ )
        {
          m_aaiAdaptSR[iDir][iRefIdx] = iSearchRange;
        }
      }
    
      // initialize motion cost
      for( int iNum = 0; iNum < AMVP_MAX_NUM_CANDS + 1; iNum++ )
      {
        for( int iIdx = 0; iIdx < AMVP_MAX_NUM_CANDS; iIdx++ )
        {
          if( iIdx < iNum )
          {
            m_auiMVPIdxCost[iIdx][iNum] = xGetMvpIdxBits( iIdx, iNum );
          }
          else
          {
            m_auiMVPIdxCost[iIdx][iNum] = MAX_UINT;
          }
        }
      }
    
      const ChromaFormat cform = pcEncCfg->getChromaFormatIdc();
      InterPrediction::init( pcRdCost, cform );
    
      for( uint32_t i = 0; i < NUM_REF_PIC_LIST_01; i++ )
      {
        m_tmpPredStorage[i].create( UnitArea( cform, Area( 0, 0, MAX_CU_SIZE, MAX_CU_SIZE ) ) );
      }
      m_tmpStorageLCU.create( UnitArea( cform, Area( 0, 0, MAX_CU_SIZE, MAX_CU_SIZE ) ) );
      m_tmpAffiStorage.create( UnitArea( cform, Area( 0, 0, MAX_CU_SIZE, MAX_CU_SIZE ) ) );
      m_tmpAffiError = new Pel[MAX_CU_SIZE * MAX_CU_SIZE];
      m_tmpAffiDeri[0] = new int[MAX_CU_SIZE * MAX_CU_SIZE];
      m_tmpAffiDeri[1] = new int[MAX_CU_SIZE * MAX_CU_SIZE];
      m_pTempPel = new Pel[maxCUWidth*maxCUHeight];
    
      m_affMVListMaxSize = (pcEncCfg->getIntraPeriod() == (uint32_t)-1) ? AFFINE_ME_LIST_SIZE_LD : AFFINE_ME_LIST_SIZE;
      if (!m_affMVList)
        m_affMVList = new AffineMVInfo[m_affMVListMaxSize];
      m_affMVListIdx = 0;
      m_affMVListSize = 0;
    
    #if JVET_M0246_AFFINE_AMVR
    void InterSearch::resetSavedAffineMotion()
    {
      for ( int i = 0; i < 2; i++ )
      {
        for ( int j = 0; j < 2; j++ )
        {
          m_affineMotion.acMvAffine4Para[i][j] = Mv( 0, 0 );
          m_affineMotion.acMvAffine6Para[i][j] = Mv( 0, 0 );
        }
        m_affineMotion.acMvAffine6Para[i][2] = Mv( 0, 0 );
    
        m_affineMotion.affine4ParaRefIdx[i] = -1;
        m_affineMotion.affine6ParaRefIdx[i] = -1;
      }
      for ( int i = 0; i < 3; i++ )
      {
        m_affineMotion.hevcCost[i] = std::numeric_limits<Distortion>::max();
      }
      m_affineMotion.affine4ParaAvail = false;
      m_affineMotion.affine6ParaAvail = false;
    }
    
    void InterSearch::storeAffineMotion( Mv acAffineMv[2][3], int16_t affineRefIdx[2], EAffineModel affineType, int gbiIdx )
    {
      if ( ( gbiIdx == GBI_DEFAULT || !m_affineMotion.affine6ParaAvail ) && affineType == AFFINEMODEL_6PARAM )
      {
        for ( int i = 0; i < 2; i++ )
        {
          for ( int j = 0; j < 3; j++ )
          {
            m_affineMotion.acMvAffine6Para[i][j] = acAffineMv[i][j];
          }
          m_affineMotion.affine6ParaRefIdx[i] = affineRefIdx[i];
        }
        m_affineMotion.affine6ParaAvail = true;
      }
    
      if ( ( gbiIdx == GBI_DEFAULT || !m_affineMotion.affine4ParaAvail ) && affineType == AFFINEMODEL_4PARAM )
      {
        for ( int i = 0; i < 2; i++ )
        {
          for ( int j = 0; j < 2; j++ )
          {
            m_affineMotion.acMvAffine4Para[i][j] = acAffineMv[i][j];
          }
          m_affineMotion.affine4ParaRefIdx[i] = affineRefIdx[i];
        }
        m_affineMotion.affine4ParaAvail = true;
      }
    }
    #endif
    
    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 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
    
    inline void InterSearch::xTZSearchHelp( IntTZSearchStruct& rcStruct, const int iSearchX, const int iSearchY, const uint8_t ucPointNr, const uint32_t uiDistance )
    {
      Distortion  uiSad = 0;
    
    //  CHECK(!( !( rcStruct.searchRange.left > iSearchX || rcStruct.searchRange.right < iSearchX || rcStruct.searchRange.top > iSearchY || rcStruct.searchRange.bottom < iSearchY )), "Unspecified error");
    
      const Pel* const  piRefSrch = rcStruct.piRefY + iSearchY * rcStruct.iRefStride + iSearchX;
    
      m_cDistParam.cur.buf = piRefSrch;
    
      if( 1 == rcStruct.subShiftMode )
      {
        // motion cost
        Distortion uiBitCost = m_pcRdCost->getCostOfVectorWithPredictor( iSearchX, iSearchY, rcStruct.imvShift );
    
        // Skip search if bit cost is already larger than best SAD
        if (uiBitCost < rcStruct.uiBestSad)
        {
          Distortion uiTempSad = m_cDistParam.distFunc( m_cDistParam );
    
          if((uiTempSad + uiBitCost) < rcStruct.uiBestSad)
          {
            // it's not supposed that any member of DistParams is manipulated beside cur.buf
            int subShift = m_cDistParam.subShift;
            const Pel* pOrgCpy = m_cDistParam.org.buf;
            uiSad += uiTempSad >> m_cDistParam.subShift;
    
            while( m_cDistParam.subShift > 0 )
            {
              int isubShift           = m_cDistParam.subShift -1;
              m_cDistParam.org.buf = rcStruct.pcPatternKey->buf + (rcStruct.pcPatternKey->stride << isubShift);
              m_cDistParam.cur.buf = piRefSrch + (rcStruct.iRefStride << isubShift);
              uiTempSad            = m_cDistParam.distFunc( m_cDistParam );
              uiSad               += uiTempSad >> m_cDistParam.subShift;
    
              if(((uiSad << isubShift) + uiBitCost) > rcStruct.uiBestSad)
              {
                break;
              }
    
              m_cDistParam.subShift--;
            }
    
            if(m_cDistParam.subShift == 0)
            {
              uiSad += uiBitCost;
    
              if( uiSad < rcStruct.uiBestSad )
              {
                rcStruct.uiBestSad      = uiSad;
                rcStruct.iBestX         = iSearchX;
                rcStruct.iBestY         = iSearchY;
                rcStruct.uiBestDistance = uiDistance;
                rcStruct.uiBestRound    = 0;
                rcStruct.ucPointNr      = ucPointNr;
                m_cDistParam.maximumDistortionForEarlyExit = uiSad;
              }
            }
    
            // restore org ptr
            m_cDistParam.org.buf  = pOrgCpy;
            m_cDistParam.subShift = subShift;
          }
        }
      }
      else
      {
        uiSad = m_cDistParam.distFunc( m_cDistParam );
    
        // only add motion cost if uiSad is smaller than best. Otherwise pointless
        // to add motion cost.
        if( uiSad < rcStruct.uiBestSad )
        {
          // motion cost
          uiSad += m_pcRdCost->getCostOfVectorWithPredictor( iSearchX, iSearchY, rcStruct.imvShift );
    
          if( uiSad < rcStruct.uiBestSad )
          {
            rcStruct.uiBestSad      = uiSad;
            rcStruct.iBestX         = iSearchX;
            rcStruct.iBestY         = iSearchY;
            rcStruct.uiBestDistance = uiDistance;
            rcStruct.uiBestRound    = 0;
            rcStruct.ucPointNr      = ucPointNr;
            m_cDistParam.maximumDistortionForEarlyExit = uiSad;
          }
        }
      }
    }
    
    
    
    inline void InterSearch::xTZ2PointSearch( IntTZSearchStruct& rcStruct )
    {
      const SearchRange& sr = rcStruct.searchRange;
    
      static const int xOffset[2][9] = { {  0, -1, -1,  0, -1, +1, -1, -1, +1 }, {  0,  0, +1, +1, -1, +1,  0, +1,  0 } };
      static const int yOffset[2][9] = { {  0,  0, -1, -1, +1, -1,  0, +1,  0 }, {  0, -1, -1,  0, -1, +1, +1, +1, +1 } };
    
      // 2 point search,                   //   1 2 3
      // check only the 2 untested points  //   4 0 5
      // around the start point            //   6 7 8
      const int iX1 = rcStruct.iBestX + xOffset[0][rcStruct.ucPointNr];
      const int iX2 = rcStruct.iBestX + xOffset[1][rcStruct.ucPointNr];
    
      const int iY1 = rcStruct.iBestY + yOffset[0][rcStruct.ucPointNr];
      const int iY2 = rcStruct.iBestY + yOffset[1][rcStruct.ucPointNr];
    
      if( iX1 >= sr.left && iX1 <= sr.right && iY1 >= sr.top && iY1 <= sr.bottom )
      {
        xTZSearchHelp( rcStruct, iX1, iY1, 0, 2 );
      }
    
      if( iX2 >= sr.left && iX2 <= sr.right && iY2 >= sr.top && iY2 <= sr.bottom )
      {
        xTZSearchHelp( rcStruct, iX2, iY2, 0, 2 );
      }
    }
    
    
    inline void InterSearch::xTZ8PointSquareSearch( IntTZSearchStruct& rcStruct, const int iStartX, const int iStartY, const int iDist )
    {
      const SearchRange& sr = rcStruct.searchRange;
      // 8 point search,                   //   1 2 3
      // search around the start point     //   4 0 5
      // with the required  distance       //   6 7 8
      CHECK( iDist == 0 , "Invalid distance");
      const int iTop        = iStartY - iDist;
      const int iBottom     = iStartY + iDist;
      const int iLeft       = iStartX - iDist;
      const int iRight      = iStartX + iDist;
      rcStruct.uiBestRound += 1;
    
      if ( iTop >= sr.top ) // check top
      {
        if ( iLeft >= sr.left ) // check top left
        {
          xTZSearchHelp( rcStruct, iLeft, iTop, 1, iDist );
        }
        // top middle
        xTZSearchHelp( rcStruct, iStartX, iTop, 2, iDist );
    
        if ( iRight <= sr.right ) // check top right
        {
          xTZSearchHelp( rcStruct, iRight, iTop, 3, iDist );
        }
      } // check top
      if ( iLeft >= sr.left ) // check middle left
      {
        xTZSearchHelp( rcStruct, iLeft, iStartY, 4, iDist );
      }
      if ( iRight <= sr.right ) // check middle right
      {
        xTZSearchHelp( rcStruct, iRight, iStartY, 5, iDist );
      }
      if ( iBottom <= sr.bottom ) // check bottom
      {
        if ( iLeft >= sr.left ) // check bottom left
        {
          xTZSearchHelp( rcStruct, iLeft, iBottom, 6, iDist );
        }
        // check bottom middle
        xTZSearchHelp( rcStruct, iStartX, iBottom, 7, iDist );
    
        if ( iRight <= sr.right ) // check bottom right
        {
          xTZSearchHelp( rcStruct, iRight, iBottom, 8, iDist );
        }
      } // check bottom
    }
    
    
    
    
    inline void InterSearch::xTZ8PointDiamondSearch( IntTZSearchStruct& rcStruct,
                                                     const int iStartX,
                                                     const int iStartY,
                                                     const int iDist,
                                                     const bool bCheckCornersAtDist1 )
    {
      const SearchRange& sr = rcStruct.searchRange;
      // 8 point search,                   //   1 2 3
      // search around the start point     //   4 0 5
      // with the required  distance       //   6 7 8
      CHECK( iDist == 0, "Invalid distance" );
      const int iTop        = iStartY - iDist;
      const int iBottom     = iStartY + iDist;
      const int iLeft       = iStartX - iDist;
      const int iRight      = iStartX + iDist;
      rcStruct.uiBestRound += 1;
    
      if ( iDist == 1 )
      {
        if ( iTop >= sr.top ) // check top
        {
          if (bCheckCornersAtDist1)
          {
            if ( iLeft >= sr.left) // check top-left
            {
              xTZSearchHelp( rcStruct, iLeft, iTop, 1, iDist );
            }
            xTZSearchHelp( rcStruct, iStartX, iTop, 2, iDist );
            if ( iRight <= sr.right ) // check middle right
            {
              xTZSearchHelp( rcStruct, iRight, iTop, 3, iDist );
            }
          }
          else
          {
            xTZSearchHelp( rcStruct, iStartX, iTop, 2, iDist );
          }
        }
        if ( iLeft >= sr.left ) // check middle left
        {
          xTZSearchHelp( rcStruct, iLeft, iStartY, 4, iDist );
        }
        if ( iRight <= sr.right ) // check middle right
        {
          xTZSearchHelp( rcStruct, iRight, iStartY, 5, iDist );
        }
        if ( iBottom <= sr.bottom ) // check bottom
        {
          if (bCheckCornersAtDist1)
          {
            if ( iLeft >= sr.left) // check top-left
            {
              xTZSearchHelp( rcStruct, iLeft, iBottom, 6, iDist );
            }
            xTZSearchHelp( rcStruct, iStartX, iBottom, 7, iDist );
            if ( iRight <= sr.right ) // check middle right
            {
              xTZSearchHelp( rcStruct, iRight, iBottom, 8, iDist );
            }
          }
          else
          {
            xTZSearchHelp( rcStruct, iStartX, iBottom, 7, iDist );
          }
        }
      }
      else
      {
        if ( iDist <= 8 )
        {
          const int iTop_2      = iStartY - (iDist>>1);
          const int iBottom_2   = iStartY + (iDist>>1);
          const int iLeft_2     = iStartX - (iDist>>1);
          const int iRight_2    = iStartX + (iDist>>1);
    
          if (  iTop >= sr.top && iLeft >= sr.left &&
               iRight <= sr.right && iBottom <= sr.bottom ) // check border
          {
            xTZSearchHelp( rcStruct, iStartX,  iTop,      2, iDist    );
            xTZSearchHelp( rcStruct, iLeft_2,  iTop_2,    1, iDist>>1 );
            xTZSearchHelp( rcStruct, iRight_2, iTop_2,    3, iDist>>1 );
            xTZSearchHelp( rcStruct, iLeft,    iStartY,   4, iDist    );
            xTZSearchHelp( rcStruct, iRight,   iStartY,   5, iDist    );
            xTZSearchHelp( rcStruct, iLeft_2,  iBottom_2, 6, iDist>>1 );
            xTZSearchHelp( rcStruct, iRight_2, iBottom_2, 8, iDist>>1 );
            xTZSearchHelp( rcStruct, iStartX,  iBottom,   7, iDist    );
          }
          else // check border
          {
            if ( iTop >= sr.top ) // check top
            {
              xTZSearchHelp( rcStruct, iStartX, iTop, 2, iDist );
            }
            if ( iTop_2 >= sr.top ) // check half top
            {
              if ( iLeft_2 >= sr.left ) // check half left
              {
                xTZSearchHelp( rcStruct, iLeft_2, iTop_2, 1, (iDist>>1) );
              }
              if ( iRight_2 <= sr.right ) // check half right
              {
                xTZSearchHelp( rcStruct, iRight_2, iTop_2, 3, (iDist>>1) );
              }
            } // check half top
            if ( iLeft >= sr.left ) // check left
            {
              xTZSearchHelp( rcStruct, iLeft, iStartY, 4, iDist );
            }
            if ( iRight <= sr.right ) // check right
            {
              xTZSearchHelp( rcStruct, iRight, iStartY, 5, iDist );
            }
            if ( iBottom_2 <= sr.bottom ) // check half bottom
            {
              if ( iLeft_2 >= sr.left ) // check half left
              {
                xTZSearchHelp( rcStruct, iLeft_2, iBottom_2, 6, (iDist>>1) );
              }
              if ( iRight_2 <= sr.right ) // check half right
              {
                xTZSearchHelp( rcStruct, iRight_2, iBottom_2, 8, (iDist>>1) );
              }
            } // check half bottom
            if ( iBottom <= sr.bottom ) // check bottom
            {
              xTZSearchHelp( rcStruct, iStartX, iBottom, 7, iDist );
            }
          } // check border
        }
        else // iDist > 8
        {
          if ( iTop >= sr.top && iLeft >= sr.left &&
               iRight <= sr.right && iBottom <= sr.bottom ) // check border
          {
            xTZSearchHelp( rcStruct, iStartX, iTop,    0, iDist );
            xTZSearchHelp( rcStruct, iLeft,   iStartY, 0, iDist );
            xTZSearchHelp( rcStruct, iRight,  iStartY, 0, iDist );
            xTZSearchHelp( rcStruct, iStartX, iBottom, 0, iDist );
            for ( int index = 1; index < 4; index++ )
            {
              const int iPosYT = iTop    + ((iDist>>2) * index);
              const int iPosYB = iBottom - ((iDist>>2) * index);
              const int iPosXL = iStartX - ((iDist>>2) * index);
              const int iPosXR = iStartX + ((iDist>>2) * index);
              xTZSearchHelp( rcStruct, iPosXL, iPosYT, 0, iDist );
              xTZSearchHelp( rcStruct, iPosXR, iPosYT, 0, iDist );
              xTZSearchHelp( rcStruct, iPosXL, iPosYB, 0, iDist );
              xTZSearchHelp( rcStruct, iPosXR, iPosYB, 0, iDist );
            }
          }
          else // check border
          {
            if ( iTop >= sr.top ) // check top
            {
              xTZSearchHelp( rcStruct, iStartX, iTop, 0, iDist );
            }
            if ( iLeft >= sr.left ) // check left
            {
              xTZSearchHelp( rcStruct, iLeft, iStartY, 0, iDist );
            }
            if ( iRight <= sr.right ) // check right
            {
              xTZSearchHelp( rcStruct, iRight, iStartY, 0, iDist );
            }
            if ( iBottom <= sr.bottom ) // check bottom
            {
              xTZSearchHelp( rcStruct, iStartX, iBottom, 0, iDist );
            }
            for ( int index = 1; index < 4; index++ )
            {
              const int iPosYT = iTop    + ((iDist>>2) * index);
              const int iPosYB = iBottom - ((iDist>>2) * index);
              const int iPosXL = iStartX - ((iDist>>2) * index);
              const int iPosXR = iStartX + ((iDist>>2) * index);
    
              if ( iPosYT >= sr.top ) // check top
              {
                if ( iPosXL >= sr.left ) // check left
                {
                  xTZSearchHelp( rcStruct, iPosXL, iPosYT, 0, iDist );
                }
                if ( iPosXR <= sr.right ) // check right
                {
                  xTZSearchHelp( rcStruct, iPosXR, iPosYT, 0, iDist );
                }
              } // check top
              if ( iPosYB <= sr.bottom ) // check bottom
              {
                if ( iPosXL >= sr.left ) // check left
                {
                  xTZSearchHelp( rcStruct, iPosXL, iPosYB, 0, iDist );
                }
                if ( iPosXR <= sr.right ) // check right
                {
                  xTZSearchHelp( rcStruct, iPosXR, iPosYB, 0, iDist );
                }
              } // check bottom
            } // for ...
          } // check border
        } // iDist <= 8
      } // iDist == 1
    }
    
    Distortion InterSearch::xPatternRefinement( const CPelBuf* pcPatternKey,
                                                Mv baseRefMv,
                                                int iFrac, Mv& rcMvFrac,
                                                bool bAllowUseOfHadamard )
    {
      Distortion  uiDist;
      Distortion  uiDistBest  = std::numeric_limits<Distortion>::max();
      uint32_t        uiDirecBest = 0;
    
      Pel*  piRefPos;
      int iRefStride = pcPatternKey->width + 1;
      m_pcRdCost->setDistParam( m_cDistParam, *pcPatternKey, m_filteredBlock[0][0][0], iRefStride, m_lumaClpRng.bd, COMPONENT_Y, 0, 1, m_pcEncCfg->getUseHADME() && bAllowUseOfHadamard );
    
      const Mv* pcMvRefine = (iFrac == 2 ? s_acMvRefineH : s_acMvRefineQ);
      for (uint32_t i = 0; i < 9; i++)
      {
        Mv cMvTest = pcMvRefine[i];
        cMvTest += baseRefMv;
    
        int horVal = cMvTest.getHor() * iFrac;
        int verVal = cMvTest.getVer() * iFrac;
        piRefPos = m_filteredBlock[verVal & 3][horVal & 3][0];
    
        if (horVal == 2 && (verVal & 1) == 0)
        {
          piRefPos += 1;
        }
        if ((horVal & 1) == 0 && verVal == 2)
        {
          piRefPos += iRefStride;
        }
        cMvTest = pcMvRefine[i];
        cMvTest += rcMvFrac;
    
    
        m_cDistParam.cur.buf   = piRefPos;
        uiDist = m_cDistParam.distFunc( m_cDistParam );
        uiDist += m_pcRdCost->getCostOfVectorWithPredictor( cMvTest.getHor(), cMvTest.getVer(), 0 );
    
        if ( uiDist < uiDistBest )
        {
          uiDistBest  = uiDist;
          uiDirecBest = i;
          m_cDistParam.maximumDistortionForEarlyExit = uiDist;
        }
      }
    
      rcMvFrac = pcMvRefine[uiDirecBest];
    
      return uiDistBest;
    }
    
    Distortion InterSearch::xGetInterPredictionError( PredictionUnit& pu, PelUnitBuf& origBuf, const RefPicList &eRefPicList )
    {
      PelUnitBuf predBuf = m_tmpStorageLCU.getBuf( UnitAreaRelative(*pu.cu, pu) );
    
      motionCompensation( pu, predBuf, eRefPicList );
    
      DistParam cDistParam;
      cDistParam.applyWeight = false;
    
      m_pcRdCost->setDistParam( cDistParam, origBuf.Y(), predBuf.Y(), pu.cs->sps->getBitDepth(CHANNEL_TYPE_LUMA), COMPONENT_Y, m_pcEncCfg->getUseHADME() && !pu.cu->transQuantBypass );
    
      return (Distortion)cDistParam.distFunc( cDistParam );
    }
    
    
    Yu Han's avatar
    Yu Han committed
    /// add ibc search functions here
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
    
    
    Yu Han's avatar
    Yu Han committed
    void InterSearch::xIBCSearchMVCandUpdate(Distortion  sad, int x, int y, Distortion* sadBestCand, Mv* cMVCand)
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
    {
      int j = CHROMA_REFINEMENT_CANDIDATES - 1;
    
      if (sad < sadBestCand[CHROMA_REFINEMENT_CANDIDATES - 1])
      {
        for (int t = CHROMA_REFINEMENT_CANDIDATES - 1; t >= 0; t--)
        {
          if (sad < sadBestCand[t])
            j = t;
        }
    
        for (int k = CHROMA_REFINEMENT_CANDIDATES - 1; k > j; k--)
        {
          sadBestCand[k] = sadBestCand[k - 1];
    
          cMVCand[k].set(cMVCand[k - 1].getHor(), cMVCand[k - 1].getVer());
        }
        sadBestCand[j] = sad;
        cMVCand[j].set(x, y);
      }
    }
    
    
    Yu Han's avatar
    Yu Han committed
    int InterSearch::xIBCSearchMVChromaRefine(PredictionUnit& pu,
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      int         roiWidth,
      int         roiHeight,
      int         cuPelX,
      int         cuPelY,
      Distortion* sadBestCand,
      Mv*     cMVCand
    
    )
    {
      if (!pu.Cb().valid())
        return 0;
    
      int bestCandIdx = 0;
      Distortion  sadBest = std::numeric_limits<Distortion>::max();
      Distortion  tempSad;
    
      Pel* pRef;
      Pel* pOrg;
      int refStride, orgStride;
      int width, height;
    
      int picWidth = pu.cs->slice->getSPS()->getPicWidthInLumaSamples();
      int picHeight = pu.cs->slice->getSPS()->getPicHeightInLumaSamples();
    
      UnitArea allCompBlocks(pu.chromaFormat, (Area)pu.block(COMPONENT_Y));
      for (int cand = 0; cand < CHROMA_REFINEMENT_CANDIDATES; cand++)
      {
        if ((!cMVCand[cand].getHor()) && (!cMVCand[cand].getVer()))
          continue;
    
        if (((int)(cuPelY + cMVCand[cand].getVer() + roiHeight) >= picHeight) || ((cuPelY + cMVCand[cand].getVer()) < 0))
          continue;
    
        if (((int)(cuPelX + cMVCand[cand].getHor() + roiWidth) >= picWidth) || ((cuPelX + cMVCand[cand].getHor()) < 0))
          continue;
    
        tempSad = sadBestCand[cand];
    
    
        pu.mv[0] = cMVCand[cand];
        pu.mv[0].changePrecision(MV_PRECISION_INT, MV_PRECISION_INTERNAL);
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
        pu.interDir = 1;
        pu.refIdx[0] = pu.cs->slice->getNumRefIdx(REF_PIC_LIST_0) - 1; // last idx in the list
    
        PelUnitBuf predBufTmp = m_tmpPredStorage[REF_PIC_LIST_0].getBuf(UnitAreaRelative(*pu.cu, pu));
        motionCompensation(pu, predBufTmp, REF_PIC_LIST_0);
    
        for (unsigned int ch = COMPONENT_Cb; ch < ::getNumberValidComponents(pu.chromaFormat); ch++)
        {
          width = roiWidth >> ::getComponentScaleX(ComponentID(ch), pu.chromaFormat);
          height = roiHeight >> ::getComponentScaleY(ComponentID(ch), pu.chromaFormat);
    
          PelUnitBuf origBuf = pu.cs->getOrgBuf(allCompBlocks);
          PelUnitBuf* pBuf = &origBuf;
          CPelBuf  tmpPattern = pBuf->get(ComponentID(ch));
          pOrg = (Pel*)tmpPattern.buf;
    
          Picture* refPic = pu.cu->slice->getPic();
          const CPelBuf refBuf = refPic->getRecoBuf(allCompBlocks.blocks[ComponentID(ch)]);
          pRef = (Pel*)refBuf.buf;
    
          refStride = refBuf.stride;
          orgStride = tmpPattern.stride;
    
          //ComponentID compID = (ComponentID)ch;
          PelUnitBuf* pBufRef = &predBufTmp;
          CPelBuf  tmpPatternRef = pBufRef->get(ComponentID(ch));
          pRef = (Pel*)tmpPatternRef.buf;
          refStride = tmpPatternRef.stride;
    
    
          for (int row = 0; row < height; row++)
          {
            for (int col = 0; col < width; col++)
            {
              tempSad += ((abs(pRef[col] - pOrg[col])) >> (pu.cs->sps->getBitDepth(CHANNEL_TYPE_CHROMA) - 8));
            }
            pRef += refStride;
            pOrg += orgStride;
          }
        }
    
        if (tempSad < sadBest)
        {
          sadBest = tempSad;
          bestCandIdx = cand;
        }
      }
    
      return bestCandIdx;
    }
    
    static unsigned int xMergeCandLists(Mv *dst, unsigned int dn, Mv *src, unsigned int sn)
    {
    
    Yu Han's avatar
    Yu Han committed
      for (unsigned int cand = 0; cand < sn && dn<IBC_NUM_CANDIDATES; cand++)
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
      {
        bool found = false;
        for (int j = 0; j<dn; j++)
        {
          if (src[cand] == dst[j])
          {
            found = true;
            break;
          }
        }
    
        if (!found)
        {
          dst[dn] = src[cand];
          dn++;
        }
      }
    
      return dn;
    }
    
    void InterSearch::xIntraPatternSearch(PredictionUnit& pu, IntTZSearchStruct&  cStruct, Mv& rcMv, Distortion&  ruiCost, Mv*  pcMvSrchRngLT, Mv*  pcMvSrchRngRB, Mv* pcMvPred)
    {
      const int   srchRngHorLeft = pcMvSrchRngLT->getHor();
      const int   srchRngHorRight = pcMvSrchRngRB->getHor();
      const int   srchRngVerTop = pcMvSrchRngLT->getVer();
      const int   srchRngVerBottom = pcMvSrchRngRB->getVer();
    
      const unsigned int  lcuWidth = pu.cs->slice->getSPS()->getMaxCUWidth();
      const int   puPelOffsetX = 0;
      const int   puPelOffsetY = 0;
      const int   cuPelX = pu.Y().x;
      const int   cuPelY = pu.Y().y;
    
      int          roiWidth = pu.lwidth();
      int          roiHeight = pu.lheight();
    
      Distortion  sad;
      Distortion  sadBest = std::numeric_limits<Distortion>::max();
      int         bestX = 0;
      int         bestY = 0;
    
      const Pel*        piRefSrch = cStruct.piRefY;
    
      int         bestCandIdx = 0;
    
      Distortion  sadBestCand[CHROMA_REFINEMENT_CANDIDATES];
      Mv      cMVCand[CHROMA_REFINEMENT_CANDIDATES];
    
    
      for (int cand = 0; cand < CHROMA_REFINEMENT_CANDIDATES; cand++)
      {
        sadBestCand[cand] = std::numeric_limits<Distortion>::max();
        cMVCand[cand].set(0, 0);
      }
    
      m_cDistParam.useMR = false;
      m_pcRdCost->setDistParam(m_cDistParam, *cStruct.pcPatternKey, cStruct.piRefY, cStruct.iRefStride, m_lumaClpRng.bd, COMPONENT_Y, cStruct.subShiftMode);
    
    
      const int picWidth = pu.cs->slice->getSPS()->getPicWidthInLumaSamples();
      const int picHeight = pu.cs->slice->getSPS()->getPicHeightInLumaSamples();
    
    
      {
        m_cDistParam.subShift = 0;
    
        Distortion tempSadBest = 0;
    
        int srLeft = srchRngHorLeft, srRight = srchRngHorRight, srTop = srchRngVerTop, srBottom = srchRngVerBottom;
    
        if (roiWidth>8 || roiHeight>8)
        {
          m_numBVs = 0;
        }
        else if (roiWidth + roiHeight == 16)
        {
          m_numBVs = m_numBV16s;
        }
    
        Mv cMvPredEncOnly[16];
        int nbPreds = 0;
    
    Yu Han's avatar
    Yu Han committed
        PU::getIbcMVPsEncOnly(pu, cMvPredEncOnly, nbPreds);
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
        m_numBVs = xMergeCandLists(m_acBVs, m_numBVs, cMvPredEncOnly, nbPreds);
    
        for (unsigned int cand = 0; cand < m_numBVs; cand++)
        {
          int xPred = m_acBVs[cand].getHor();
          int yPred = m_acBVs[cand].getVer();
    
          if (!(xPred == 0 && yPred == 0)
            && !((yPred < srTop) || (yPred > srBottom))
            && !((xPred < srLeft) || (xPred > srRight)))
          {
            bool validCand = PU::isBlockVectorValid(pu, cuPelX, cuPelY, roiWidth, roiHeight, picWidth, picHeight, 0, 0, xPred, yPred, lcuWidth);
    
            if (validCand)
            {
              sad = m_pcRdCost->getBvCostMultiplePreds(xPred, yPred, pu.cs->sps->getSpsNext().getImvMode() == IMV_4PEL);
              m_cDistParam.cur.buf = piRefSrch + cStruct.iRefStride * yPred + xPred;
              sad += m_cDistParam.distFunc(m_cDistParam);
    
    
    Yu Han's avatar
    Yu Han committed
              xIBCSearchMVCandUpdate(sad, xPred, yPred, sadBestCand, cMVCand);
    
    Xiaozhong Xu's avatar
    Xiaozhong Xu committed
            }
          }
        }
    
        bestX = cMVCand[0].getHor();
        bestY = cMVCand[0].getVer();
        rcMv.set(bestX, bestY);
        sadBest = sadBestCand[0];
    
        const int boundY = (0 - roiHeight - puPelOffsetY);
        for (int y = std::max(srchRngVerTop, 0 - cuPelY); y <= boundY; ++y)
        {
          if (!PU::isBlockVectorValid(pu, cuPelX, cuPelY, roiWidth, roiHeight, picWidth, picHeight, 0, 0, 0, y, lcuWidth))
          {
            continue;
          }
    
          sad = m_pcRdCost->getBvCostMultiplePreds(0, y, pu.cs->sps->getSpsNext().getImvMode() == IMV_4PEL);
          m_cDistParam.cur.buf = piRefSrch + cStruct.iRefStride * y;