Skip to content
Snippets Groups Projects
EncReshape.cpp 39.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • Taoran Lu's avatar
    Taoran Lu committed
    /* 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     EncReshape.cpp
    \brief    encoder reshaper class
    */
    #include "EncReshape.h"
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #if JVET_M0427_INLOOP_RESHAPER
    //! \ingroup EncLib
    //! \{
    
    // ====================================================================================================================
    // Constructor / destructor / create / destroy
    // ====================================================================================================================
    
    EncReshape::EncReshape()
    {
      m_bCTUFlag     = false;
      m_bSrcReshaped = false;
      m_bRecReshaped = false;
      m_bReshape     = true;
      m_bExceedSTD   = false;
      m_tcase        = 0;
      m_rateAdpMode  = 0;
      m_chromaAdj    = 0;
    }
    
    EncReshape::~EncReshape()
    {
    }
    
    
    void  EncReshape::createEnc(int picWidth, int picHeight, uint32_t maxCUWidth, uint32_t maxCUHeight, int bitDepth)
    
      m_lumaBD = bitDepth;
      m_reshapeLUTSize = 1 << m_lumaBD;
      m_initCWAnalyze = m_reshapeLUTSize / PIC_ANALYZE_CW_BINS;
      m_initCW = m_reshapeLUTSize / PIC_CODE_CW_BINS;
    
    
    Taoran Lu's avatar
    Taoran Lu committed
      if (forwardReshapingLUT.empty())
    
        forwardReshapingLUT.resize(m_reshapeLUTSize, 0);
    
    Taoran Lu's avatar
    Taoran Lu committed
      if (inverseReshapingLUT.empty())
    
        inverseReshapingLUT.resize(m_reshapeLUTSize,0);
      if (m_binCW.empty())
        m_binCW.resize(PIC_ANALYZE_CW_BINS);
    
    Taoran Lu's avatar
    Taoran Lu committed
      if (m_uiBinImportance.empty())
        m_uiBinImportance.resize(PIC_ANALYZE_CW_BINS);
    
      if (m_reshapePivot.empty())
        m_reshapePivot.resize(PIC_CODE_CW_BINS + 1, 0);
      if (m_chromaAdjHelpLUT.empty())
        m_chromaAdjHelpLUT.resize(PIC_CODE_CW_BINS, 1<<CSCALE_FP_PREC);
    
    Taoran Lu's avatar
    Taoran Lu committed
    
      m_sliceReshapeInfo.setUseSliceReshaper(true);
      m_sliceReshapeInfo.setSliceReshapeChromaAdj(true);
      m_sliceReshapeInfo.setSliceReshapeModelPresentFlag(true);
      m_sliceReshapeInfo.reshape_model_min_bin_idx = 0;
      m_sliceReshapeInfo.reshape_model_max_bin_idx = PIC_CODE_CW_BINS - 1;
      memset(m_sliceReshapeInfo.reshape_model_bin_CW_delta, 0, (PIC_CODE_CW_BINS) * sizeof(int));
    
      m_picWidth = picWidth;
      m_picHeight = picHeight;
      m_maxCUWidth = maxCUWidth;
      m_maxCUHeight = maxCUHeight;
      m_widthInCtus = (m_picWidth + m_maxCUWidth - 1) / m_maxCUWidth;
      m_heightInCtus = (m_picHeight + m_maxCUHeight - 1) / m_maxCUHeight;
      m_numCtuInFrame = m_widthInCtus * m_heightInCtus;
    }
    
    void  EncReshape::destroy()
    {
    }
    
    /**
    -Perform HDR set up
    \param   pcPic describe pointer of current coding picture
    \param   sliceType describe the slice type
    */
    void EncReshape::preAnalyzerHDR(Picture *pcPic, const SliceType sliceType, const ReshapeCW& reshapeCW, bool isDualT, bool isCPR)
    {
    
      if (m_lumaBD == 10)
    
        m_sliceReshapeInfo.slice_reshaper_enable_flag = true;
        if (reshapeCW.RspIntraPeriod == 1)
        {
          if (pcPic->getPOC() == 0)          { m_sliceReshapeInfo.slice_reshaper_model_present_flag = true;  }
          else                               { m_sliceReshapeInfo.slice_reshaper_model_present_flag = false; }
        }
        else
        {
          if (sliceType == I_SLICE || (sliceType==P_SLICE && isCPR) )             { m_sliceReshapeInfo.slice_reshaper_model_present_flag = true;  }
          else                                                                    { m_sliceReshapeInfo.slice_reshaper_model_present_flag = false; }
        }
        if ((sliceType == I_SLICE || (sliceType == P_SLICE && isCPR)) && isDualT) { m_sliceReshapeInfo.uiReshapeChromaAdj = 0;                    }
        else                                                                      { m_sliceReshapeInfo.uiReshapeChromaAdj = 1;                    }
    
        m_sliceReshapeInfo.slice_reshaper_enable_flag = false;
        m_sliceReshapeInfo.slice_reshaper_model_present_flag = false;
    
    Taoran Lu's avatar
    Taoran Lu committed
      }
    }
    
    /**
    -Perform picture analysis for SDR
    \param   pcPic describe pointer of current coding picture
    \param   sliceType describe the slice type
    \param   reshapeCW describe some input info
    */
    void EncReshape::preAnalyzerSDR(Picture *pcPic, const SliceType sliceType, const ReshapeCW& reshapeCW, bool isDualT, bool isCPR)
    {
      m_sliceReshapeInfo.slice_reshaper_model_present_flag = true;
      m_sliceReshapeInfo.slice_reshaper_enable_flag = true;
    
      int modIP = pcPic->getPOC() - pcPic->getPOC() / reshapeCW.RspFpsToIp * reshapeCW.RspFpsToIp;
    
      if (sliceType == I_SLICE || (reshapeCW.RspIntraPeriod == -1 && modIP == 0) || (sliceType== P_SLICE && isCPR))
      {
        if (m_sliceReshapeInfo.slice_reshaper_model_present_flag == true)
        {
    
          uint32_t uiStdMin = 16 <<(m_lumaBD-8);
          uint32_t uiStdMax = 235 << (m_lumaBD - 8);
          int  binLen = m_reshapeLUTSize / PIC_ANALYZE_CW_BINS;
    
    Taoran Lu's avatar
    Taoran Lu committed
    
          m_reshapeCW = reshapeCW;
    
          for (int b = 0; b < PIC_ANALYZE_CW_BINS; b++)
          {
            m_uiBinImportance[b] = 0;
    
            m_binCW[b] = binLen;
    
          int startBinIdx =  int(floor((double(uiStdMin) / double(binLen))));
          int endBinIdx = int(floor((double(uiStdMax) / double(binLen))));
          m_sliceReshapeInfo.reshape_model_min_bin_idx = startBinIdx;
          m_sliceReshapeInfo.reshape_model_max_bin_idx = endBinIdx;
    
    Taoran Lu's avatar
    Taoran Lu committed
    
          PelBuf picY = pcPic->getOrigBuf(COMPONENT_Y);
          const int iWidth = picY.width;
          const int iHeight = picY.height;
          const int iStride = picY.stride;
    
          double dBlockBinVarSum[PIC_ANALYZE_CW_BINS] = { 0.0 };
          uint32_t   dBlockBinCnt[PIC_ANALYZE_CW_BINS] = { 0 };
    
          
          const int PIC_ANALYZE_WIN_SIZE = 5;
    
    Taoran Lu's avatar
    Taoran Lu committed
          const uint32_t uiWinSize = PIC_ANALYZE_WIN_SIZE;
          const uint32_t uiWinLens = (uiWinSize - 1) >> 1;
    
          int64_t tempSq = 0;
          int64_t leftSum = 0, leftSumSq = 0;
          int64_t *leftColSum = new int64_t[iWidth];
          int64_t *leftColSumSq = new int64_t[iWidth];
          memset(leftColSum, 0, iWidth * sizeof(int64_t));
          memset(leftColSumSq, 0, iWidth * sizeof(int64_t));
          int64_t topSum = 0, topSumSq = 0;
          int64_t *topRowSum = new int64_t[iHeight];
          int64_t *topRowSumSq = new int64_t[iHeight];
          memset(topRowSum, 0, iHeight * sizeof(int64_t));
          memset(topRowSumSq, 0, iHeight * sizeof(int64_t));
          int64_t *topColSum = new int64_t[iWidth];
          int64_t *topColSumSq = new int64_t[iWidth];
          memset(topColSum, 0, iWidth * sizeof(int64_t));
          memset(topColSumSq, 0, iWidth * sizeof(int64_t));
    
          for (uint32_t y = 0; y < iHeight; y++)
          {
            for (uint32_t x = 0; x < iWidth; x++)
            {
              const Pel pPxlY = picY.buf[x];
              int64_t uiSum = 0;
              int64_t uiSumSq = 0;
              uint32_t uiNumPixInPart = 0;
    
              uint32_t y1 = std::max((int)(y - uiWinLens), 0);
              uint32_t y2 = std::min((int)(y + uiWinLens), (iHeight - 1));
              uint32_t x1 = std::max((int)(x - uiWinLens), 0);
              uint32_t x2 = std::min((int)(x + uiWinLens), (iWidth - 1));
    
    
              uint32_t bx = 0, by = 0;
              const Pel *pWinY = &picY.buf[0];
              uiNumPixInPart = (x2 - x1 + 1) * (y2 - y1 + 1);
    
              if (x == 0 && y == 0)           // for the 1st Pixel, calc all points
              {
                for (by = y1; by <= y2; by++)
                {
                  for (bx = x1; bx <= x2; bx++)
                  {
                    tempSq = pWinY[bx] * pWinY[bx];
                    leftSum += pWinY[bx];
                    leftSumSq += tempSq;
                    leftColSum[bx] += pWinY[bx];
                    leftColSumSq[bx] += tempSq;
                    topColSum[bx] += pWinY[bx];
                    topColSumSq[bx] += tempSq;
                    topRowSum[by] += pWinY[bx];
                    topRowSumSq[by] += tempSq;
                  }
                  pWinY += iStride;
                }
                topSum = leftSum;
                topSumSq = leftSumSq;
                uiSum = leftSum;
                uiSumSq = leftSumSq;
              }
              else if (x == 0 && y > 0)       // for the 1st column, calc the bottom stripe
              {
                if (y < iHeight - uiWinLens)
                {
                  pWinY += uiWinLens*iStride;
                  topRowSum[y + uiWinLens] = 0;
                  topRowSumSq[y + uiWinLens] = 0;
                  for (bx = x1; bx <= x2; bx++)
                  {
                    topRowSum[y + uiWinLens] += pWinY[bx];
                    topRowSumSq[y + uiWinLens] += pWinY[bx] * pWinY[bx];
                  }
                  topSum += topRowSum[y + uiWinLens];
                  topSumSq += topRowSumSq[y + uiWinLens];
                }
                if (y > uiWinLens)
                {
                  topSum -= topRowSum[y - 1 - uiWinLens];
                  topSumSq -= topRowSumSq[y - 1 - uiWinLens];
                }
    
                memset(leftColSum, 0, iWidth * sizeof(int64_t));
                memset(leftColSumSq, 0, iWidth * sizeof(int64_t));
                pWinY = &picY.buf[0];
                pWinY -= (y <= uiWinLens ? y : uiWinLens)*iStride;
                for (by = y1; by <= y2; by++)
                {
                  for (bx = x1; bx <= x2; bx++)
                  {
                    leftColSum[bx] += pWinY[bx];
                    leftColSumSq[bx] += pWinY[bx] * pWinY[bx];
                  }
                  pWinY += iStride;
                }
    
                leftSum = topSum;
                leftSumSq = topSumSq;
                uiSum = topSum;
                uiSumSq = topSumSq;
              }
    
              else if (x > 0)
              {
                if (x < iWidth - uiWinLens)
                {
                  pWinY -= (y <= uiWinLens ? y : uiWinLens)*iStride;
                  if (y == 0)                 // for the 1st row, calc the right stripe
                  {
                    leftColSum[x + uiWinLens] = 0;
                    leftColSumSq[x + uiWinLens] = 0;
                    for (by = y1; by <= y2; by++)
                    {
                      leftColSum[x + uiWinLens] += pWinY[x + uiWinLens];
                      leftColSumSq[x + uiWinLens] += pWinY[x + uiWinLens] * pWinY[x + uiWinLens];
                      pWinY += iStride;
                    }
                  }
                  else                        // for the main area, calc the B-R point 
                  {
                    leftColSum[x + uiWinLens] = topColSum[x + uiWinLens];
                    leftColSumSq[x + uiWinLens] = topColSumSq[x + uiWinLens];
                    if (y < iHeight - uiWinLens)
                    {
                      pWinY = &picY.buf[0];
                      pWinY += uiWinLens * iStride;
                      leftColSum[x + uiWinLens] += pWinY[x + uiWinLens];
                      leftColSumSq[x + uiWinLens] += pWinY[x + uiWinLens] * pWinY[x + uiWinLens];
                    }
                    if (y > uiWinLens)
                    {
                      pWinY = &picY.buf[0];
                      pWinY -= (uiWinLens + 1) * iStride;
                      leftColSum[x + uiWinLens] -= pWinY[x + uiWinLens];
                      leftColSumSq[x + uiWinLens] -= pWinY[x + uiWinLens] * pWinY[x + uiWinLens];
                    }
                  }
                  topColSum[x + uiWinLens] = leftColSum[x + uiWinLens];
                  topColSumSq[x + uiWinLens] = leftColSumSq[x + uiWinLens];
                  leftSum += leftColSum[x + uiWinLens];
                  leftSumSq += leftColSumSq[x + uiWinLens];
                }
                if (x > uiWinLens)
                {
                  leftSum -= leftColSum[x - 1 - uiWinLens];
                  leftSumSq -= leftColSumSq[x - 1 - uiWinLens];
                }
                uiSum = leftSum;
                uiSumSq = leftSumSq;
              }
    
              double dAverage = double(uiSum) / uiNumPixInPart;
              double dVariance = double(uiSumSq) / uiNumPixInPart - dAverage * dAverage;
    
    
              if (m_lumaBD > 10)
              {
                dAverage = dAverage / (double)(1<<(m_lumaBD - 10));
                dVariance = dVariance / (double)(1 << (2*m_lumaBD - 20));
              }
              else if (m_lumaBD < 10)
              {
                dAverage = dAverage * (double)(1 << (10 - m_lumaBD));
                dVariance = dVariance * (double)(1 << (20-2*m_lumaBD));
              }
    
    Taoran Lu's avatar
    Taoran Lu committed
              double dVarLog10 = log10(dVariance + 1.0);
    
              uint32_t uiBinNum = (uint32_t)floor((double)pPxlY / (double)PIC_ANALYZE_CW_BINS);
              dBlockBinVarSum[uiBinNum] += dVarLog10;
              dBlockBinCnt[uiBinNum]++;
            }
            picY.buf += iStride;
          }
    
          delete[] topColSum;
          delete[] topColSumSq;
          delete[] topRowSum;
          delete[] topRowSumSq;
          delete[] leftColSum;
          delete[] leftColSumSq;
    
          for (int b = 0; b < PIC_ANALYZE_CW_BINS; b++)
          {
            if (dBlockBinCnt[b] > 0)
              dBlockBinVarSum[b] = dBlockBinVarSum[b] / dBlockBinCnt[b];
          }
    
          m_bReshape = true;
          m_bExceedSTD = false;
          m_bUseAdpCW = false;
          m_chromaWeight = 1.0;
          m_sliceReshapeInfo.uiReshapeChromaAdj = 1;
          bool   bIntraAdp = false;
          bool   bInterAdp = true;
          double dReshapeTH1 = 0.0;
          double dReshapeTH2 = 5.0;
          deriveReshapeParametersSDRfromStats(dBlockBinCnt, dBlockBinVarSum, &dReshapeTH1, &dReshapeTH2, &bIntraAdp, &bInterAdp);
    
          if (m_rateAdpMode == 2 && reshapeCW.RspBaseQP <= 22)
          {
            bIntraAdp = false;
            bInterAdp = false;
          }
    
          m_sliceReshapeInfo.slice_reshaper_enable_flag = bIntraAdp;
    
          if (!bIntraAdp && !bInterAdp)
          {
            m_sliceReshapeInfo.slice_reshaper_model_present_flag = false;
            m_bReshape = false;
            return;
          }
    
          if (m_bExceedSTD)
          {
    
            startBinIdx = 2;
            endBinIdx = 29;
    
    Taoran Lu's avatar
    Taoran Lu committed
            for (int b = 0; b < PIC_ANALYZE_CW_BINS; b++)
            {
    
              if (dBlockBinCnt[b] > 0 && b < startBinIdx)
                startBinIdx = b;
              if (dBlockBinCnt[b] > 0 && b > endBinIdx)
                endBinIdx = b;
    
            m_sliceReshapeInfo.reshape_model_min_bin_idx = startBinIdx;
            m_sliceReshapeInfo.reshape_model_max_bin_idx = endBinIdx;
    
    Taoran Lu's avatar
    Taoran Lu committed
          }
    
          if (reshapeCW.RspBaseQP <= 22 && m_rateAdpMode == 1)
          {
            for (int i = 0; i < PIC_ANALYZE_CW_BINS; i++)
            {
    
              if (i >= startBinIdx && i <= endBinIdx)
                m_binCW[i] = m_initCWAnalyze + 1;
    
    Taoran Lu's avatar
    Taoran Lu committed
              else
    
                m_binCW[i] = 0;
    
    Taoran Lu's avatar
    Taoran Lu committed
            }
          }
          else if (m_bUseAdpCW)
          {
            double Alpha = 1.0, Beta = 0.0;
    
            deriveReshapeParameters(dBlockBinVarSum, startBinIdx, endBinIdx, m_reshapeCW, Alpha, Beta);
    
    Taoran Lu's avatar
    Taoran Lu committed
            for (int i = 0; i < PIC_ANALYZE_CW_BINS; i++)
            {
    
              if (i >= startBinIdx && i <= endBinIdx)
                m_binCW[i] = (uint32_t)round(Alpha*dBlockBinVarSum[i] + Beta);
    
    Taoran Lu's avatar
    Taoran Lu committed
              else
    
                m_binCW[i] = 0;
    
            for (int b = startBinIdx; b <= endBinIdx; b++)
    
    Taoran Lu's avatar
    Taoran Lu committed
            {
              if (dBlockBinVarSum[b] < dReshapeTH1)
                m_uiBinImportance[b] = 2;
              else if (dBlockBinVarSum[b] > dReshapeTH2)
                m_uiBinImportance[b] = 3;
              else
                m_uiBinImportance[b] = 1;
            }
    
            for (int i = 0; i < PIC_ANALYZE_CW_BINS; i++)
            {
              if (m_uiBinImportance[i] == 0)
    
                m_binCW[i] = 0;
    
    Taoran Lu's avatar
    Taoran Lu committed
              else if (m_uiBinImportance[i] == 1)
    
                m_binCW[i] = m_initCWAnalyze + 1;
    
    Taoran Lu's avatar
    Taoran Lu committed
              else if (m_uiBinImportance[i] == 2)
    
                m_binCW[i] = m_reshapeCW.BinCW[0];
    
    Taoran Lu's avatar
    Taoran Lu committed
              else if (m_uiBinImportance[i] == 3)
    
                m_binCW[i] = m_reshapeCW.BinCW[1];
    
    Taoran Lu's avatar
    Taoran Lu committed
    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 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
              else
                THROW("SDR Reshape Bin Importance not supported");
            }
          }
          if (m_reshapeCW.RspPicSize <= 1497600 && reshapeCW.RspIntraPeriod == -1 && modIP == 0 && sliceType != I_SLICE)
          {
            m_sliceReshapeInfo.slice_reshaper_enable_flag = false;
          }
    
        }
        m_chromaAdj = m_sliceReshapeInfo.uiReshapeChromaAdj;
        if ((sliceType == I_SLICE || (sliceType == P_SLICE && isCPR)) && isDualT)
        {
            m_sliceReshapeInfo.uiReshapeChromaAdj = 0;
        }
      }
      else // Inter slices
      {
        m_sliceReshapeInfo.slice_reshaper_model_present_flag = false;
        m_sliceReshapeInfo.uiReshapeChromaAdj = m_chromaAdj;
    
        if (!m_bReshape)
        {
          m_sliceReshapeInfo.slice_reshaper_enable_flag = false;
        }
        else
        {
          const int cTid = m_reshapeCW.Tid;
          bool enableRsp = m_tcase == 5 ? false : (m_tcase < 5 ? (cTid < m_tcase + 1 ? false : true) : (cTid <= 10 - m_tcase ? true : false));
          m_sliceReshapeInfo.slice_reshaper_enable_flag = enableRsp;
        }
      }
    }
    
    // Bubble Sort to  descending order with index
    void EncReshape::bubbleSortDsd(double* array, int * idx, int n)
    {
      int i, j;
      bool swapped;
      for (i = 0; i < n - 1; i++)
      {
        swapped = false;
        for (j = 0; j < n - i - 1; j++)
        {
          if (array[j] < array[j + 1])
          {
            swap(&array[j], &array[j + 1]);
            swap(&idx[j], &idx[j + 1]);
            swapped = true;
          }
        }
        if (swapped == false)
          break;
      }
    }
    
    void EncReshape::deriveReshapeParametersSDRfromStats(uint32_t * dBlockBinCnt, double *dBlockBinVarSum, double* dReshapeTH1, double* dReshapeTH2, bool *bIntraAdp, bool *bInterAdp)
    {
      int    BinIdxSortDsd[PIC_ANALYZE_CW_BINS] = { 0 };
      double BinVarSortDsd[PIC_ANALYZE_CW_BINS] = { 0.0 };
      double BinHist[PIC_ANALYZE_CW_BINS] = { 0.0 };
      double BinVarSortDsdCDF[PIC_ANALYZE_CW_BINS] = { 0.0 };
    
      double maxBinVar = 0.0, meanBinVar = 0.0, minBinVar = 5.0;
      int    nonZeroBinCt = 0;
      for (int b = 0; b < PIC_ANALYZE_CW_BINS; b++)
      {
        BinHist[b] = (double)dBlockBinCnt[b] / (double)(m_reshapeCW.RspPicSize);
        if (BinHist[b] > 0.001)
        {
          nonZeroBinCt++;
          meanBinVar += dBlockBinVarSum[b];
          if (dBlockBinVarSum[b] > maxBinVar)        {        maxBinVar = dBlockBinVarSum[b];      }
          if (dBlockBinVarSum[b] < minBinVar)        {        minBinVar = dBlockBinVarSum[b];      }
        }
        BinVarSortDsd[b] = dBlockBinVarSum[b];
        BinIdxSortDsd[b] = b;
      }
      if ((BinHist[0] + BinHist[1] + BinHist[PIC_ANALYZE_CW_BINS - 2] + BinHist[PIC_ANALYZE_CW_BINS - 1]) > 0.01)   {    m_bExceedSTD = true;  }
      if ((BinHist[PIC_ANALYZE_CW_BINS - 2] + BinHist[PIC_ANALYZE_CW_BINS - 1]) > 0.01)   {    *bInterAdp = false;    return;   }
      else                                                                                {    *bInterAdp = true;               }
    
      meanBinVar = meanBinVar / (double)nonZeroBinCt;
      bubbleSortDsd(BinVarSortDsd, BinIdxSortDsd, PIC_ANALYZE_CW_BINS);
      BinVarSortDsdCDF[0] = BinHist[BinIdxSortDsd[0]];
      for (int b = 1; b < PIC_ANALYZE_CW_BINS; b++)
      {
        BinVarSortDsdCDF[b] = BinVarSortDsdCDF[b - 1] + BinHist[BinIdxSortDsd[b]];
      }
    
      int firstBinVarLessThanVal1 = 0; // Val1 = 3.5
      int firstBinVarLessThanVal2 = 0; // Val2 = 3.0
      int firstBinVarLessThanVal3 = 0; // Val3 = 2.5
      int firstBinVarLessThanVal4 = 0; // Val4 = 2.0
      for (int b = 0; b < PIC_ANALYZE_CW_BINS - 1; b++)
      {
        if (BinVarSortDsd[b] > 3.5)     {      firstBinVarLessThanVal1 = b + 1;    }
        if (BinVarSortDsd[b] > 3.0)     {      firstBinVarLessThanVal2 = b + 1;    }
        if (BinVarSortDsd[b] > 2.5)     {      firstBinVarLessThanVal3 = b + 1;    }
        if (BinVarSortDsd[b] > 2.0)     {      firstBinVarLessThanVal4 = b + 1;    }
      }
    
      m_reshapeCW.BinCW[0] = 38;
      m_reshapeCW.BinCW[1] = 28;
    
      if (m_reshapeCW.RspIntraPeriod == -1)
      {
        *bIntraAdp = true;
        if (m_reshapeCW.RspPicSize > 1497600)
        {
          m_reshapeCW.BinCW[0] = 36;
          *dReshapeTH1 = 2.4;
          *dReshapeTH2 = 4.5;
          m_rateAdpMode = 2;
    
          if (meanBinVar >= 2.52)
          {
            if (BinVarSortDsdCDF[firstBinVarLessThanVal2] > 0.5)
            {
              *dReshapeTH1 = 2.5;
              *dReshapeTH2 = 3.0;
            }
            else if (BinVarSortDsdCDF[firstBinVarLessThanVal2] < 0.1 && BinVarSortDsdCDF[firstBinVarLessThanVal1] > 0.02)
            {
              *dReshapeTH1 = 2.2;
            }
            else if (BinVarSortDsdCDF[firstBinVarLessThanVal2] > 0.25)
            {
              m_reshapeCW.BinCW[1] = 30;
              *dReshapeTH1 = 2.0;
              m_rateAdpMode = 0;
            }
            else
            {
              m_reshapeCW.BinCW[1] = 30;
              m_rateAdpMode = 1;
            }
          }
        }
        else if (m_reshapeCW.RspPicSize > 660480)
        {
          m_reshapeCW.BinCW[0] = 34;
          *dReshapeTH1 = 3.4;
          *dReshapeTH2 = 4.0;
          m_rateAdpMode = 2;
    
          if (BinVarSortDsdCDF[firstBinVarLessThanVal4] > 0.6)
          {
            if (maxBinVar < 3.5)
            {
              m_bUseAdpCW = true;
              m_reshapeCW.BinCW[0] = 38;
            }
            else
            {
              m_reshapeCW.BinCW[0] = 40;
              *dReshapeTH1 = 2.2;
              *dReshapeTH2 = 4.5;
              m_rateAdpMode = 0;
            }
          }
          else
          {
            if (maxBinVar > 3.3)
            {
              m_reshapeCW.BinCW[1] = 30;
            }
            else
            {
              m_reshapeCW.BinCW[1] = 28;
            }
          }
        }
        else if (m_reshapeCW.RspPicSize > 249600)
        {
          m_reshapeCW.BinCW[0] = 36;
          *dReshapeTH1 = 2.5;
          *dReshapeTH2 = 4.5;
    
          if (m_bExceedSTD)
          {
            m_reshapeCW.BinCW[0] = 36;
            m_reshapeCW.BinCW[1] = 30;
          }
          if (minBinVar > 2.6)
          {
            *dReshapeTH1 = 3.0;
          }
          else {
            double diff1 = BinVarSortDsdCDF[firstBinVarLessThanVal4] - BinVarSortDsdCDF[firstBinVarLessThanVal3];
            double diff2 = BinVarSortDsdCDF[firstBinVarLessThanVal2] - BinVarSortDsdCDF[firstBinVarLessThanVal1];
            if (diff1 > 0.4 || BinVarSortDsdCDF[firstBinVarLessThanVal1] > 0.1)
            {
              m_bUseAdpCW = true;
              m_rateAdpMode = 1;
            }
            else if (diff2 <= 0.1 && BinVarSortDsdCDF[firstBinVarLessThanVal4] > 0.99 && BinVarSortDsdCDF[firstBinVarLessThanVal3] > 0.642 && BinVarSortDsdCDF[firstBinVarLessThanVal2] > 0.03)
            {
              m_bUseAdpCW = true;
              m_rateAdpMode = 1;
            }
            else
            {
              m_rateAdpMode = 2;
            }
          }
        }
        else
        {
          m_reshapeCW.BinCW[0] = 36;
          *dReshapeTH1 = 2.6;
          *dReshapeTH2 = 4.5;
    
          if (BinVarSortDsdCDF[firstBinVarLessThanVal2] > 0.5 && maxBinVar < 4.7)
          {
            *dReshapeTH1 = 3.2;
            m_rateAdpMode = 1;
          }
        }
      }
      else if (m_reshapeCW.RspIntraPeriod == 1)
      {
        *bIntraAdp = true;
        if (m_reshapeCW.RspPicSize > 5184000)
        {
          *dReshapeTH1 = 2.0;
          *dReshapeTH2 = 3.0;
          m_rateAdpMode = 2;
    
          if (maxBinVar > 2.4)
          {
            if (BinVarSortDsdCDF[firstBinVarLessThanVal4] > 0.88) 
            {
              if (maxBinVar < 2.695)
              {
                *dReshapeTH2 = 2.2;
              }
              else 
              {
                if (BinVarSortDsdCDF[firstBinVarLessThanVal3] < 0.45)
                {
                  *dReshapeTH1 = 2.5;
                  *dReshapeTH2 = 4.0;
                  m_reshapeCW.BinCW[0] = 36;
                  m_sliceReshapeInfo.uiReshapeChromaAdj = 0;
                  m_rateAdpMode = 0;
                }
                else
                {
                  m_bUseAdpCW = true;
                  m_reshapeCW.BinCW[0] = 36;
                  m_reshapeCW.BinCW[1] = 30;
                }
              }
            }
            else 
            {
              if (maxBinVar > 2.8)
              {
                *dReshapeTH1 = 2.2;
                *dReshapeTH2 = 4.0;
                m_reshapeCW.BinCW[0] = 36;
                m_sliceReshapeInfo.uiReshapeChromaAdj = 0;
              }
              else
              {
                m_bUseAdpCW = true;
                m_reshapeCW.BinCW[0] = 38;
                m_reshapeCW.BinCW[1] = 28;
              }
            }
          }
          else
          {
            if (maxBinVar > 2.24)
            {
              m_bUseAdpCW = true;
              m_reshapeCW.BinCW[0] = 34;
              m_reshapeCW.BinCW[1] = 30;
            }
          }
        }
        else if (m_reshapeCW.RspPicSize > 1497600)
        {
          *dReshapeTH1 = 2.0;
          *dReshapeTH2 = 4.5;
          m_rateAdpMode = 2;
    
          if (BinVarSortDsdCDF[firstBinVarLessThanVal2] > 0.25)
          {
            int firstVarCDFLargerThanVal = 1;
            for (int b = 0; b < PIC_ANALYZE_CW_BINS; b++)
            {
              if (BinVarSortDsdCDF[b] > 0.7)
              {
                firstVarCDFLargerThanVal = b;
                break;
              }
            }
            if (meanBinVar < 2.52 || BinVarSortDsdCDF[firstBinVarLessThanVal2] > 0.5)
            {
              *dReshapeTH1 = 2.2;
              *dReshapeTH2 = (BinVarSortDsd[firstVarCDFLargerThanVal] + BinVarSortDsd[firstVarCDFLargerThanVal - 1]) / 2.0;
            }
            else
            {
              m_reshapeCW.BinCW[1] = 30;
              *dReshapeTH2 = 2.8;
            }
          }
          else if (BinVarSortDsdCDF[firstBinVarLessThanVal2] < 0.1 && BinVarSortDsdCDF[firstBinVarLessThanVal1] > 0.02)
          {
            m_reshapeCW.BinCW[0] = 36;
            *dReshapeTH1 = 3.5;
            m_rateAdpMode = 1;
          }
        }
        else if (m_reshapeCW.RspPicSize > 660480)
        {
          *dReshapeTH1 = 2.5;
          *dReshapeTH2 = 4.5;
          m_rateAdpMode = 1;
    
          if (BinVarSortDsdCDF[firstBinVarLessThanVal4] > 0.6)
          {
            if (maxBinVar < 3.5)
            {
              *dReshapeTH1 = 2.0;
            }
          }
          else
          {
            if (maxBinVar > 3.3)
            {
              m_reshapeCW.BinCW[0] = 35;
            }
            else
            {
              *dReshapeTH1 = 2.8;
              m_reshapeCW.BinCW[0] = 35;
            }
          }
        }
        else if (m_reshapeCW.RspPicSize > 249600)
        {
          m_rateAdpMode = 1;
          m_reshapeCW.BinCW[0] = 36;
          *dReshapeTH1 = 2.5;
          *dReshapeTH2 = 4.5;
        }
        else
        {
          if (BinVarSortDsdCDF[firstBinVarLessThanVal2] < 0.33 && m_reshapeCW.RspFps>40)
          {
            *bIntraAdp = false;
            *bInterAdp = false;
          }
          else
          {
            m_rateAdpMode = 1;
            m_reshapeCW.BinCW[0] = 36;
            *dReshapeTH1 = 3.0;
            *dReshapeTH2 = 4.0;
          }
        }
      }
      else
      {
        if (m_reshapeCW.RspPicSize > 5184000)
        {
          m_reshapeCW.BinCW[0] = 40;
          *dReshapeTH2 = 4.0;
          m_rateAdpMode = 2;
    
          if (maxBinVar < 2.4)
          {
            *dReshapeTH1 = 3.0;
            if (m_reshapeCW.RspBaseQP <= 22)
              m_tcase = 3;
          }
          else if (maxBinVar > 3.0)
          {
            if (minBinVar > 1)
            {
              m_reshapeCW.BinCW[0] = 36;
              *dReshapeTH1 = 2.8;
              *dReshapeTH2 = 3.5;
              m_sliceReshapeInfo.uiReshapeChromaAdj = 0;
              m_chromaWeight = 1.05;
              m_rateAdpMode = 0;
            }
            else
            {
              m_reshapeCW.BinCW[0] = 36;
              *dReshapeTH1 = 2.2;
              *dReshapeTH2 = 3.5;
              m_sliceReshapeInfo.uiReshapeChromaAdj = 0;
              m_chromaWeight = 0.95;
              if (m_reshapeCW.RspBaseQP <= 27 && m_reshapeCW.RspBaseQP >=25)
                m_tcase = 3;
            }
          }
          else
          {
            *dReshapeTH1 = 1.5;
          }
        }
        else if (m_reshapeCW.RspPicSize > 1497600)
        {
          *dReshapeTH1 = 2.5;
          *dReshapeTH2 = 4.5;
          m_rateAdpMode = 1; 
    
          if (meanBinVar < 2.52)
          {
            *bIntraAdp = true;
            m_rateAdpMode = 0;
            m_tcase = 9;
          }
          else
          {
            if (BinVarSortDsdCDF[firstBinVarLessThanVal2] > 0.5)
            {
              *dReshapeTH2 = 3.0;
              *bIntraAdp = true;
            }
            else if (BinVarSortDsdCDF[firstBinVarLessThanVal2] < 0.1 && BinVarSortDsdCDF[firstBinVarLessThanVal1] > 0.02)
            {
              *dReshapeTH1 = 3.0;
              *bIntraAdp = true;
              m_rateAdpMode = 0;
              m_tcase = 9;
            }
            else if (BinVarSortDsdCDF[firstBinVarLessThanVal2] > 0.25)
            {
              *dReshapeTH1 = 2.4;
              m_reshapeCW.BinCW[0] = 36;
            }
            else
            {
              *dReshapeTH1 = 2.4;
              m_reshapeCW.BinCW[0] = 36;
            }
          }
        }
        else if (m_reshapeCW.RspPicSize > 660480)
        {
          *bIntraAdp = true;
          m_rateAdpMode = 1;  
    
          if (BinVarSortDsdCDF[firstBinVarLessThanVal4] > 0.6)
          {
            if (maxBinVar < 3.5)
            {
              *dReshapeTH1 = 2.1;
              *dReshapeTH2 = 3.5;
            }
            else
            {
              *dReshapeTH1 = 2.4;
              *dReshapeTH2 = 4.5;
              m_reshapeCW.BinCW[0] = 40;
              m_rateAdpMode = 0;  
            }
          }
          else
          {
            if (maxBinVar > 3.3)
            {
              *dReshapeTH1 = 3.5;
              *dReshapeTH2 = 3.8;
            }
            else
            {
              *dReshapeTH1 = 3.0;
              *dReshapeTH2 = 4.0;
              m_reshapeCW.BinCW[1] = 30;
            }
          }
        }
        else if (m_reshapeCW.RspPicSize > 249600)
        {
          m_reshapeCW.BinCW[1] = 30;
          *dReshapeTH1 = 2.5;
          *dReshapeTH2 = 4.5;
          *bIntraAdp = true;
          m_rateAdpMode = 1;
    
          if (minBinVar > 2.6)
          {
            *dReshapeTH1 = 3.2;
            m_rateAdpMode = 0;
            m_tcase = 9;
          }
          else {
            double diff1 = BinVarSortDsdCDF[firstBinVarLessThanVal4] - BinVarSortDsdCDF[firstBinVarLessThanVal3];
            double diff2 = BinVarSortDsdCDF[firstBinVarLessThanVal2] - BinVarSortDsdCDF[firstBinVarLessThanVal1];
            if (diff1 > 0.4 || BinVarSortDsdCDF[firstBinVarLessThanVal1] > 0.1)
            {
              *dReshapeTH1 = 2.9;
              *bIntraAdp = false;
            }
            else
            {
              if (diff2 > 0.1)
              {
                *dReshapeTH1 = 2.5;
              }
              else
              {
                *dReshapeTH1 = 2.9;
                if (BinVarSortDsdCDF[firstBinVarLessThanVal4] > 0.99 && BinVarSortDsdCDF[firstBinVarLessThanVal3] > 0.642 && BinVarSortDsdCDF[firstBinVarLessThanVal2] > 0.03)
                {
                  m_rateAdpMode = 0;
                  m_tcase = 9;
                }
              }
            }
          }
        }
        else
        {
          m_reshapeCW.BinCW[0] = 36;
          m_reshapeCW.BinCW[1] = 30;
          *dReshapeTH1 = 2.6;
          *dReshapeTH2 = 4.5;
          *bIntraAdp = true;
          m_rateAdpMode = 1;
          if (BinVarSortDsdCDF[firstBinVarLessThanVal2] > 0.5 && maxBinVar < 4.7)
          {
            *dReshapeTH1 = 3.4;
          }
        }
      }
    }
    
    void EncReshape::deriveReshapeParameters(double *array, int start, int end, ReshapeCW respCW, double &alpha, double &beta)
    {
      double minVar = 10.0, maxVar = 0.0;
      for (int b = start; b <= end; b++)
      {
        if (array[b] < minVar)       minVar = array[b];
        if (array[b] > maxVar)       maxVar = array[b];
      }
      double maxCW = (double)respCW.BinCW[0];
      double minCW = (double)respCW.BinCW[1];
      alpha = (minCW - maxCW) / (maxVar - minVar);
      beta = (maxCW*maxVar - minCW*minVar) / (maxVar - minVar);
    }
    
    /**
    -Init reshaping LUT  from dQP model