Ignore:
Timestamp:
8 Feb 2014, 01:07:38 (10 years ago)
Author:
tech
Message:

Further fixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncRateCtrl.cpp

    r837 r838  
    16601660}
    16611661
    1662 #else
    1663 
    1664 #define ADJUSTMENT_FACTOR       0.60
    1665 #define HIGH_QSTEP_THRESHOLD    9.5238
    1666 #define HIGH_QSTEP_ALPHA        4.9371
    1667 #define HIGH_QSTEP_BETA         0.0922
    1668 #define LOW_QSTEP_ALPHA         16.7429
    1669 #define LOW_QSTEP_BETA          -1.1494
    1670 
    1671 #define MAD_PRED_Y1             1.0
    1672 #define MAD_PRED_Y2             0.0
    1673 
    1674 enum MAD_HISOTRY {
    1675   MAD_PPPrevious = 0,
    1676   MAD_PPrevious  = 1,
    1677   MAD_Previous   = 2
    1678 };
    1679 
    1680 Void    MADLinearModel::initMADLinearModel()
    1681 {
    1682   m_activeOn = false;
    1683   m_paramY1  = 1.0;
    1684   m_paramY2  = 0.0;
    1685   m_costMADs[0] = m_costMADs[1] = m_costMADs[2] = 0.0;
    1686 }
    1687 
    1688 Double  MADLinearModel::getMAD()
    1689 {
    1690   Double costPredMAD = m_paramY1 * m_costMADs[MAD_Previous] + m_paramY2;
    1691 
    1692   if(costPredMAD < 0)
    1693   {
    1694     costPredMAD = m_costMADs[MAD_Previous];
    1695     m_paramY1   = MAD_PRED_Y1;
    1696     m_paramY2   = MAD_PRED_Y2;
    1697   }
    1698   return costPredMAD;
    1699 }
    1700 
    1701 Void    MADLinearModel::updateMADLiearModel()
    1702 {
    1703   Double dNewY1 = ((m_costMADs[MAD_Previous] - m_costMADs[MAD_PPrevious]) / (m_costMADs[MAD_PPrevious] - m_costMADs[MAD_PPPrevious]));
    1704   Double dNewY2 =  (m_costMADs[MAD_Previous] - (dNewY1*m_costMADs[MAD_PPrevious]));
    1705  
    1706   m_paramY1 = 0.70+0.20*m_paramY1+ 0.10*dNewY1;
    1707   m_paramY2 =      0.20*m_paramY2+ 0.10*dNewY2;
    1708 }
    1709 
    1710 Void    MADLinearModel::updateMADHistory(Double dMAD)
    1711 {
    1712   m_costMADs[MAD_PPPrevious] = m_costMADs[MAD_PPrevious];
    1713   m_costMADs[MAD_PPrevious ] = m_costMADs[MAD_Previous ];
    1714   m_costMADs[MAD_Previous  ] = dMAD;
    1715   m_activeOn = (m_costMADs[MAD_Previous  ] && m_costMADs[MAD_PPrevious ] && m_costMADs[MAD_PPPrevious]);
    1716 }
    1717 
    1718 
    1719 Void    PixelBaseURQQuadraticModel::initPixelBaseQuadraticModel()
    1720 {
    1721   m_paramHighX1 = HIGH_QSTEP_ALPHA;
    1722   m_paramHighX2 = HIGH_QSTEP_BETA;
    1723   m_paramLowX1  = LOW_QSTEP_ALPHA;
    1724   m_paramLowX2  = LOW_QSTEP_BETA;
    1725 }
    1726 
    1727 Int     PixelBaseURQQuadraticModel::getQP(Int qp, Int targetBits, Int numberOfPixels, Double costPredMAD)
    1728 {
    1729   Double qStep;
    1730   Double bppPerMAD = (Double)(targetBits/(numberOfPixels*costPredMAD));
    1731  
    1732   if(xConvertQP2QStep(qp) >= HIGH_QSTEP_THRESHOLD)
    1733   {
    1734     qStep = 1/( sqrt((bppPerMAD/m_paramHighX1)+((m_paramHighX2*m_paramHighX2)/(4*m_paramHighX1*m_paramHighX1))) - (m_paramHighX2/(2*m_paramHighX1)));
    1735   }
    1736   else
    1737   {
    1738     qStep = 1/( sqrt((bppPerMAD/m_paramLowX1)+((m_paramLowX2*m_paramLowX2)/(4*m_paramLowX1*m_paramLowX1))) - (m_paramLowX2/(2*m_paramLowX1)));
    1739   }
    1740  
    1741   return xConvertQStep2QP(qStep);
    1742 }
    1743 
    1744 Void    PixelBaseURQQuadraticModel::updatePixelBasedURQQuadraticModel (Int qp, Int bits, Int numberOfPixels, Double costMAD)
    1745 {
    1746   Double qStep     = xConvertQP2QStep(qp);
    1747   Double invqStep = (1/qStep);
    1748   Double paramNewX1, paramNewX2;
    1749  
    1750   if(qStep >= HIGH_QSTEP_THRESHOLD)
    1751   {
    1752     paramNewX2    = (((bits/(numberOfPixels*costMAD))-(23.3772*invqStep*invqStep))/((1-200*invqStep)*invqStep));
    1753     paramNewX1    = (23.3772-200*paramNewX2);
    1754     m_paramHighX1 = 0.70*HIGH_QSTEP_ALPHA + 0.20 * m_paramHighX1 + 0.10 * paramNewX1;
    1755     m_paramHighX2 = 0.70*HIGH_QSTEP_BETA  + 0.20 * m_paramHighX2 + 0.10 * paramNewX2;
    1756   }
    1757   else
    1758   {
    1759     paramNewX2   = (((bits/(numberOfPixels*costMAD))-(5.8091*invqStep*invqStep))/((1-9.5455*invqStep)*invqStep));
    1760     paramNewX1   = (5.8091-9.5455*paramNewX2);
    1761     m_paramLowX1 = 0.90*LOW_QSTEP_ALPHA + 0.09 * m_paramLowX1 + 0.01 * paramNewX1;
    1762     m_paramLowX2 = 0.90*LOW_QSTEP_BETA  + 0.09 * m_paramLowX2 + 0.01 * paramNewX2;
    1763   }
    1764 }
    1765 
    1766 Bool    PixelBaseURQQuadraticModel::checkUpdateAvailable(Int qpReference )
    1767 {
    1768   Double qStep = xConvertQP2QStep(qpReference);
    1769 
    1770   if (qStep > xConvertQP2QStep(MAX_QP)
    1771     ||qStep < xConvertQP2QStep(MIN_QP) )
    1772   {
    1773     return false;
    1774   }
    1775 
    1776   return true;
    1777 }
    1778 
    1779 Double  PixelBaseURQQuadraticModel::xConvertQP2QStep(Int qp )
    1780 {
    1781   Int i;
    1782   Double qStep;
    1783   static const Double mapQP2QSTEP[6] = { 0.625, 0.703, 0.797, 0.891, 1.000, 1.125 };
    1784 
    1785   qStep = mapQP2QSTEP[qp % 6];
    1786   for( i=0; i<(qp/6); i++)
    1787   {
    1788     qStep *= 2;
    1789   }
    1790 
    1791   return qStep;
    1792 }
    1793 
    1794 Int     PixelBaseURQQuadraticModel::xConvertQStep2QP(Double qStep )
    1795 {
    1796   Int per = 0, rem = 0;
    1797 
    1798   if( qStep < xConvertQP2QStep(MIN_QP))
    1799   {
    1800     return MIN_QP;
    1801   }
    1802   else if (qStep > xConvertQP2QStep(MAX_QP) )
    1803   {
    1804     return MAX_QP;
    1805   }
    1806 
    1807   while( qStep > xConvertQP2QStep(5) )
    1808   {
    1809     qStep /= 2.0;
    1810     per++;
    1811   }
    1812 
    1813   if (qStep <= 0.625)
    1814   {
    1815     rem = 0;
    1816   }
    1817   else if (qStep <= 0.703)
    1818   {
    1819     rem = 1;
    1820   }
    1821   else if (qStep <= 0.797)
    1822   {
    1823     rem = 2;
    1824   }
    1825   else if (qStep <= 0.891)
    1826   {
    1827     rem = 3;
    1828   }
    1829   else if (qStep <= 1.000)
    1830   {
    1831     rem = 4;
    1832   }
    1833   else
    1834   {
    1835     rem = 5;
    1836   }
    1837   return (per * 6 + rem);
    1838 }
    1839 
    1840 
    1841 Void  TEncRateCtrl::create(Int sizeIntraPeriod, Int sizeGOP, Int frameRate, Int targetKbps, Int qp, Int numLCUInBasicUnit, Int sourceWidth, Int sourceHeight, Int maxCUWidth, Int maxCUHeight)
    1842 {
    1843   Int leftInHeight, leftInWidth;
    1844 
    1845   m_sourceWidthInLCU         = (sourceWidth  / maxCUWidth  ) + (( sourceWidth  %  maxCUWidth ) ? 1 : 0);
    1846   m_sourceHeightInLCU        = (sourceHeight / maxCUHeight) + (( sourceHeight %  maxCUHeight) ? 1 : 0); 
    1847   m_isLowdelay               = (sizeIntraPeriod == -1) ? true : false;
    1848 
    1849   m_prevBitrate              = ( targetKbps << 10 );  // in units of 1,024 bps
    1850   m_currBitrate              = ( targetKbps << 10 );
    1851 
    1852   m_frameRate                = frameRate;
    1853   m_refFrameNum              = m_isLowdelay ? (sizeGOP) : (sizeGOP>>1);
    1854   m_nonRefFrameNum           = sizeGOP-m_refFrameNum;
    1855   m_sizeGOP                  = sizeGOP;
    1856   m_numOfPixels              = ((sourceWidth*sourceHeight*3)>>1);
    1857   m_indexGOP                 = 0;
    1858   m_indexFrame               = 0;
    1859   m_indexLCU                 = 0;
    1860   m_indexUnit                = 0;
    1861   m_indexRefFrame            = 0;
    1862   m_indexNonRefFrame         = 0;
    1863   m_occupancyVB              = 0;
    1864   m_initialOVB               = 0;
    1865   m_targetBufLevel           = 0;
    1866   m_initialTBL               = 0;
    1867   m_occupancyVBInFrame       = 0;
    1868   m_remainingBitsInGOP       = (m_currBitrate*sizeGOP/m_frameRate);
    1869   m_remainingBitsInFrame     = 0;
    1870   m_numUnitInFrame           = m_sourceWidthInLCU*m_sourceHeightInLCU;
    1871   m_cMADLinearModel.        initMADLinearModel();
    1872   m_cPixelURQQuadraticModel.initPixelBaseQuadraticModel();
    1873 
    1874   m_costRefAvgWeighting      = 0.0;
    1875   m_costNonRefAvgWeighting   = 0.0;
    1876   m_costAvgbpp               = 0.0; 
    1877   m_activeUnitLevelOn        = false;
    1878 
    1879   m_pcFrameData              = new FrameData   [sizeGOP+1];         initFrameData(qp);
    1880   m_pcLCUData                = new LCUData     [m_numUnitInFrame];  initUnitData (qp);
    1881 
    1882   for(Int i = 0, addressUnit = 0; i < m_sourceHeightInLCU*maxCUHeight; i += maxCUHeight) 
    1883   {
    1884     leftInHeight = sourceHeight - i;
    1885     leftInHeight = min(leftInHeight, maxCUHeight);
    1886     for(Int j = 0; j < m_sourceWidthInLCU*maxCUWidth; j += maxCUWidth, addressUnit++)
    1887     {
    1888       leftInWidth = sourceWidth - j;
    1889       leftInWidth = min(leftInWidth, maxCUWidth);
    1890       m_pcLCUData[addressUnit].m_widthInPixel = leftInWidth;
    1891       m_pcLCUData[addressUnit].m_heightInPixel= leftInHeight;
    1892       m_pcLCUData[addressUnit].m_pixels       = ((leftInHeight*leftInWidth*3)>>1);
    1893     }
    1894   }
    1895 }
    1896 
    1897 Void  TEncRateCtrl::destroy()
    1898 {
    1899   if(m_pcFrameData)
    1900   {
    1901     delete [] m_pcFrameData;
    1902     m_pcFrameData = NULL;
    1903   }
    1904   if(m_pcLCUData)
    1905   {
    1906     delete [] m_pcLCUData;
    1907     m_pcLCUData = NULL;
    1908   }
    1909 }
    1910 
    1911 Void  TEncRateCtrl::initFrameData   (Int qp)
    1912 {
    1913   for(Int i = 0 ; i <= m_sizeGOP; i++)
    1914   {
    1915     m_pcFrameData[i].m_isReferenced = false;
    1916     m_pcFrameData[i].m_costMAD      = 0.0;
    1917     m_pcFrameData[i].m_bits         = 0;
    1918     m_pcFrameData[i].m_qp           = qp;
    1919   }
    1920 }
    1921 
    1922 Void  TEncRateCtrl::initUnitData    (Int qp)
    1923 {
    1924   for(Int i = 1 ; i < m_numUnitInFrame; i++)
    1925   {
    1926     m_pcLCUData[i].m_qp            = qp;
    1927     m_pcLCUData[i].m_bits          = 0;
    1928     m_pcLCUData[i].m_pixels        = 0;
    1929     m_pcLCUData[i].m_widthInPixel  = 0;
    1930     m_pcLCUData[i].m_heightInPixel = 0;
    1931     m_pcLCUData[i].m_costMAD       = 0.0;
    1932   }
    1933 }
    1934 
    1935 Int  TEncRateCtrl::getFrameQP(Bool isReferenced, Int POC)
    1936 {
    1937   Int numofReferenced = 0;
    1938   Int finalQP = 0;
    1939   FrameData* pcFrameData;
    1940 
    1941   m_indexPOCInGOP = (POC%m_sizeGOP) == 0 ? m_sizeGOP : (POC%m_sizeGOP);
    1942   pcFrameData     = &m_pcFrameData[m_indexPOCInGOP];
    1943    
    1944   if(m_indexFrame != 0)
    1945   {
    1946     if(isReferenced)
    1947     {
    1948       Double gamma = m_isLowdelay ? 0.5 : 0.25;
    1949       Double beta  = m_isLowdelay ? 0.9 : 0.6;
    1950       Int    numRemainingRefFrames  = m_refFrameNum    - m_indexRefFrame;
    1951       Int    numRemainingNRefFrames = m_nonRefFrameNum - m_indexNonRefFrame;
    1952      
    1953       Double targetBitsOccupancy  = (m_currBitrate/(Double)m_frameRate) + gamma*(m_targetBufLevel-m_occupancyVB - (m_initialOVB/(Double)m_frameRate));
    1954       Double targetBitsLeftBudget = ((m_costRefAvgWeighting*m_remainingBitsInGOP)/((m_costRefAvgWeighting*numRemainingRefFrames)+(m_costNonRefAvgWeighting*numRemainingNRefFrames)));
    1955 
    1956       m_targetBits = (Int)(beta * targetBitsLeftBudget + (1-beta) * targetBitsOccupancy);
    1957  
    1958       if(m_targetBits <= 0 || m_remainingBitsInGOP <= 0)
    1959       {
    1960         finalQP = m_pcFrameData[m_indexPrevPOCInGOP].m_qp + 2;
    1961       }
    1962       else
    1963       {
    1964         Double costPredMAD   = m_cMADLinearModel.getMAD();
    1965         Int    qpLowerBound = m_pcFrameData[m_indexPrevPOCInGOP].m_qp-2;
    1966         Int    qpUpperBound = m_pcFrameData[m_indexPrevPOCInGOP].m_qp+2;
    1967         finalQP = m_cPixelURQQuadraticModel.getQP(m_pcFrameData[m_indexPrevPOCInGOP].m_qp, m_targetBits, m_numOfPixels, costPredMAD);
    1968         finalQP = max(qpLowerBound, min(qpUpperBound, finalQP));
    1969         m_activeUnitLevelOn    = true;
    1970         m_remainingBitsInFrame = m_targetBits;
    1971         m_costAvgbpp           = (m_targetBits/(Double)m_numOfPixels);
    1972       }
    1973 
    1974       m_indexRefFrame++;
    1975     }
    1976     else
    1977     {
    1978       Int bwdQP = m_pcFrameData[m_indexPOCInGOP-1].m_qp;
    1979       Int fwdQP = m_pcFrameData[m_indexPOCInGOP+1].m_qp;
    1980        
    1981       if( (fwdQP+bwdQP) == m_pcFrameData[m_indexPOCInGOP-1].m_qp
    1982         ||(fwdQP+bwdQP) == m_pcFrameData[m_indexPOCInGOP+1].m_qp)
    1983       {
    1984         finalQP = (fwdQP+bwdQP);
    1985       }
    1986       else if(bwdQP != fwdQP)
    1987       {
    1988         finalQP = ((bwdQP+fwdQP+2)>>1);
    1989       }
    1990       else
    1991       {
    1992         finalQP = bwdQP+2;
    1993       }
    1994       m_indexNonRefFrame++;
    1995     }
    1996   }
    1997   else
    1998   {
    1999     Int lastQPminus2 = m_pcFrameData[0].m_qp - 2;
    2000     Int lastQPplus2  = m_pcFrameData[0].m_qp + 2;
    2001 
    2002     for(Int idx = 1; idx <= m_sizeGOP; idx++)
    2003     {
    2004       if(m_pcFrameData[idx].m_isReferenced)
    2005       {
    2006         finalQP += m_pcFrameData[idx].m_qp;
    2007         numofReferenced++;
    2008       }
    2009     }
    2010    
    2011     finalQP = (numofReferenced == 0) ? m_pcFrameData[0].m_qp : ((finalQP + (1<<(numofReferenced>>1)))/numofReferenced);
    2012     finalQP = max( lastQPminus2, min( lastQPplus2, finalQP));
    2013 
    2014     Double costAvgFrameBits = m_remainingBitsInGOP/(Double)m_sizeGOP;
    2015     Int    bufLevel  = m_occupancyVB + m_initialOVB;
    2016 
    2017     if(abs(bufLevel) > costAvgFrameBits)
    2018     {
    2019       if(bufLevel < 0)
    2020       {
    2021         finalQP -= 2;
    2022       }
    2023       else
    2024       {
    2025         finalQP += 2;
    2026       }
    2027     }
    2028     m_indexRefFrame++;
    2029   }
    2030   finalQP = max(MIN_QP, min(MAX_QP, finalQP));
    2031 
    2032   for(Int indexLCU = 0 ; indexLCU < m_numUnitInFrame; indexLCU++)
    2033   {
    2034     m_pcLCUData[indexLCU].m_qp = finalQP;
    2035   }
    2036 
    2037   pcFrameData->m_isReferenced = isReferenced;
    2038   pcFrameData->m_qp           = finalQP;
    2039 
    2040   return finalQP;
    2041 }
    2042 
    2043 Bool  TEncRateCtrl::calculateUnitQP ()
    2044 {
    2045   if(!m_activeUnitLevelOn || m_indexLCU == 0)
    2046   {
    2047     return false;
    2048   }
    2049   Int upperQPBound, lowerQPBound, finalQP;
    2050   Int    colQP        = m_pcLCUData[m_indexLCU].m_qp;
    2051   Double colMAD       = m_pcLCUData[m_indexLCU].m_costMAD;
    2052   Double budgetInUnit = m_pcLCUData[m_indexLCU].m_pixels*m_costAvgbpp;
    2053 
    2054 
    2055   Int targetBitsOccupancy = (Int)(budgetInUnit - (m_occupancyVBInFrame/(m_numUnitInFrame-m_indexUnit)));
    2056   Int targetBitsLeftBudget= (Int)((m_remainingBitsInFrame*m_pcLCUData[m_indexLCU].m_pixels)/(Double)(m_numOfPixels-m_codedPixels));
    2057   Int targetBits = (targetBitsLeftBudget>>1) + (targetBitsOccupancy>>1);
    2058  
    2059 
    2060   if( m_indexLCU >= m_sourceWidthInLCU)
    2061   {
    2062     upperQPBound = ( (m_pcLCUData[m_indexLCU-1].m_qp + m_pcLCUData[m_indexLCU - m_sourceWidthInLCU].m_qp)>>1) + MAX_DELTA_QP;
    2063     lowerQPBound = ( (m_pcLCUData[m_indexLCU-1].m_qp + m_pcLCUData[m_indexLCU - m_sourceWidthInLCU].m_qp)>>1) - MAX_DELTA_QP;
    2064   }
    2065   else
    2066   {
    2067     upperQPBound = m_pcLCUData[m_indexLCU-1].m_qp + MAX_DELTA_QP;
    2068     lowerQPBound = m_pcLCUData[m_indexLCU-1].m_qp - MAX_DELTA_QP;
    2069   }
    2070 
    2071   if(targetBits < 0)
    2072   {
    2073     finalQP = m_pcLCUData[m_indexLCU-1].m_qp + 1;
    2074   }
    2075   else
    2076   {
    2077     finalQP = m_cPixelURQQuadraticModel.getQP(colQP, targetBits, m_pcLCUData[m_indexLCU].m_pixels, colMAD);
    2078   }
    2079  
    2080   finalQP = max(lowerQPBound, min(upperQPBound, finalQP));
    2081   m_pcLCUData[m_indexLCU].m_qp = max(MIN_QP, min(MAX_QP, finalQP));
    2082  
    2083   return true;
    2084 }
    2085 
    2086 Void  TEncRateCtrl::updateRCGOPStatus()
    2087 {
    2088   m_remainingBitsInGOP = ((m_currBitrate/m_frameRate)*m_sizeGOP) - m_occupancyVB;
    2089  
    2090   FrameData cFrameData = m_pcFrameData[m_sizeGOP];
    2091   initFrameData();
    2092 
    2093   m_pcFrameData[0]   = cFrameData;
    2094   m_indexGOP++;
    2095   m_indexFrame       = 0;
    2096   m_indexRefFrame    = 0;
    2097   m_indexNonRefFrame = 0;
    2098 }
    2099 
    2100 Void  TEncRateCtrl::updataRCFrameStatus(Int frameBits, SliceType eSliceType)
    2101 {
    2102   FrameData* pcFrameData = &m_pcFrameData[m_indexPOCInGOP];
    2103   Int occupancyBits;
    2104   Double adjustmentBits;
    2105 
    2106   m_remainingBitsInGOP = m_remainingBitsInGOP + ( ((m_currBitrate-m_prevBitrate)/m_frameRate)*(m_sizeGOP-m_indexFrame) ) - frameBits;
    2107   occupancyBits        = (Int)((Double)frameBits - (m_currBitrate/(Double)m_frameRate));
    2108  
    2109   if( (occupancyBits < 0) && (m_initialOVB > 0) )
    2110   {
    2111     adjustmentBits = xAdjustmentBits(occupancyBits, m_initialOVB );
    2112 
    2113     if(m_initialOVB < 0)
    2114     {
    2115       adjustmentBits = m_initialOVB;
    2116       occupancyBits += (Int)adjustmentBits;
    2117       m_initialOVB   =  0;
    2118     }
    2119   }
    2120   else if( (occupancyBits > 0) && (m_initialOVB < 0) )
    2121   {
    2122     adjustmentBits = xAdjustmentBits(m_initialOVB, occupancyBits );
    2123    
    2124     if(occupancyBits < 0)
    2125     {
    2126       adjustmentBits = occupancyBits;
    2127       m_initialOVB  += (Int)adjustmentBits;
    2128       occupancyBits  =  0;
    2129     }
    2130   }
    2131 
    2132   if(m_indexGOP == 0)
    2133   {
    2134     m_initialOVB = occupancyBits;
    2135   }
    2136   else
    2137   {
    2138     m_occupancyVB= m_occupancyVB + occupancyBits;
    2139   }
    2140 
    2141   if(pcFrameData->m_isReferenced)
    2142   {
    2143     m_costRefAvgWeighting  = ((pcFrameData->m_bits*pcFrameData->m_qp)/8.0) + (7.0*(m_costRefAvgWeighting)/8.0);
    2144 
    2145     if(m_indexFrame == 0)
    2146     {
    2147       m_initialTBL = m_targetBufLevel  = (frameBits - (m_currBitrate/m_frameRate));
    2148     }
    2149     else
    2150     {
    2151       Int distance = (m_costNonRefAvgWeighting == 0) ? 0 : 1;
    2152       m_targetBufLevel =  m_targetBufLevel
    2153                             - (m_initialTBL/(m_refFrameNum-1))
    2154                             + (Int)((m_costRefAvgWeighting*(distance+1)*m_currBitrate)/(m_frameRate*(m_costRefAvgWeighting+(m_costNonRefAvgWeighting*distance))))
    2155                             - (m_currBitrate/m_frameRate);
    2156     }
    2157 
    2158     if(m_cMADLinearModel.IsUpdateAvailable())
    2159     {
    2160       m_cMADLinearModel.updateMADLiearModel();
    2161     }
    2162 
    2163     if(eSliceType != I_SLICE &&
    2164        m_cPixelURQQuadraticModel.checkUpdateAvailable(pcFrameData->m_qp))
    2165     {
    2166       m_cPixelURQQuadraticModel.updatePixelBasedURQQuadraticModel(pcFrameData->m_qp, pcFrameData->m_bits, m_numOfPixels, pcFrameData->m_costMAD);
    2167     }
    2168   }
    2169   else
    2170   {
    2171     m_costNonRefAvgWeighting = ((pcFrameData->m_bits*pcFrameData->m_qp)/8.0) + (7.0*(m_costNonRefAvgWeighting)/8.0);
    2172   }
    2173 
    2174   m_indexFrame++;
    2175   m_indexLCU             = 0;
    2176   m_indexUnit            = 0;
    2177   m_occupancyVBInFrame   = 0;
    2178   m_remainingBitsInFrame = 0;
    2179   m_codedPixels          = 0;
    2180   m_activeUnitLevelOn    = false;
    2181   m_costAvgbpp           = 0.0;
    2182 }
    2183 Void  TEncRateCtrl::updataRCUnitStatus ()
    2184 {
    2185   if(!m_activeUnitLevelOn || m_indexLCU == 0)
    2186   {
    2187     return;
    2188   }
    2189 
    2190   m_codedPixels  += m_pcLCUData[m_indexLCU-1].m_pixels;
    2191   m_remainingBitsInFrame = m_remainingBitsInFrame - m_pcLCUData[m_indexLCU-1].m_bits;
    2192   m_occupancyVBInFrame   = (Int)(m_occupancyVBInFrame + m_pcLCUData[m_indexLCU-1].m_bits - m_pcLCUData[m_indexLCU-1].m_pixels*m_costAvgbpp);
    2193 
    2194   if( m_cPixelURQQuadraticModel.checkUpdateAvailable(m_pcLCUData[m_indexLCU-1].m_qp) )
    2195   {
    2196     m_cPixelURQQuadraticModel.updatePixelBasedURQQuadraticModel(m_pcLCUData[m_indexLCU-1].m_qp, m_pcLCUData[m_indexLCU-1].m_bits, m_pcLCUData[m_indexLCU-1].m_pixels, m_pcLCUData[m_indexLCU-1].m_costMAD);
    2197   }
    2198 
    2199   m_indexUnit++;
    2200 }
    2201 
    2202 Void  TEncRateCtrl::updateFrameData(UInt64 actualFrameBits)
    2203 {
    2204   Double costMAD = 0.0;
    2205  
    2206   for(Int i = 0; i < m_numUnitInFrame; i++)
    2207   {
    2208     costMAD    += m_pcLCUData[i].m_costMAD;
    2209   }
    2210  
    2211   m_pcFrameData[m_indexPOCInGOP].m_costMAD = (costMAD/(Double)m_numUnitInFrame);
    2212   m_pcFrameData[m_indexPOCInGOP].m_bits    = (Int)actualFrameBits;
    2213  
    2214   if(m_pcFrameData[m_indexPOCInGOP].m_isReferenced)
    2215   {
    2216     m_indexPrevPOCInGOP = m_indexPOCInGOP;
    2217     m_cMADLinearModel.updateMADHistory(m_pcFrameData[m_indexPOCInGOP].m_costMAD);
    2218   }
    2219 }
    2220 
    2221 Void  TEncRateCtrl::updateLCUData(TComDataCU* pcCU, UInt64 actualLCUBits, Int qp)
    2222 {
    2223   Int     x, y;
    2224   Double  costMAD = 0.0;
    2225 
    2226   Pel*  pOrg   = pcCU->getPic()->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0);
    2227   Pel*  pRec   = pcCU->getPic()->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), 0);
    2228   Int   stride = pcCU->getPic()->getStride();
    2229 
    2230   Int   width  = m_pcLCUData[m_indexLCU].m_widthInPixel;
    2231   Int   height = m_pcLCUData[m_indexLCU].m_heightInPixel;
    2232 
    2233   for( y = 0; y < height; y++ )
    2234   {
    2235     for( x = 0; x < width; x++ )
    2236     {
    2237       costMAD += abs( pOrg[x] - pRec[x] );
    2238     }
    2239     pOrg += stride;
    2240     pRec += stride;
    2241   }
    2242   m_pcLCUData[m_indexLCU  ].m_qp      = qp;
    2243   m_pcLCUData[m_indexLCU  ].m_costMAD = (costMAD /(Double)(width*height));
    2244   m_pcLCUData[m_indexLCU++].m_bits    = (Int)actualLCUBits;
    2245 }
    2246 
    2247 
    2248 #if KWU_RC_MADPRED_E0227
    2249 Void  TEncRateCtrl::updateLCUDataEnhancedView(TComDataCU* pcCU, UInt64 uiBits, Int qp, Double basePos, Double curPos, Double focalLen, Double znear, Double zfar, Int direction)
    2250 {
    2251   Int     x, y;
    2252   Double dMAD = 0.0;
    2253   Int Sum = 0;
    2254   Double SAD = 0.0;
    2255 
    2256   Pel*  pOrg    = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0);
    2257   Pel*  pRec    = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), 0);
    2258   Pel*  pDep    = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0);
    2259   Int   iStride = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getStride();
    2260 
    2261   Int   width  = m_pcLCUData[m_indexLCU].m_widthInPixel;
    2262   Int   height = m_pcLCUData[m_indexLCU].m_heightInPixel;
    2263 
    2264   for( y = 0 ; y < pcCU->getSlice()->getSPS()->getMaxCUHeight() ; y+=8)
    2265   {
    2266     for( x = 0 ; x < pcCU->getSlice()->getSPS()->getMaxCUWidth() ; x+=8)
    2267     {
    2268       Sum += pDep[x];
    2269     }
    2270     pDep += iStride;
    2271   }
    2272 
    2273   Double AvgDepth = (Double)Sum/((pcCU->getSlice()->getSPS()->getMaxCUHeight()/8)*(pcCU->getSlice()->getSPS()->getMaxCUWidth()/8));
    2274   Double fL = focalLen * abs( basePos - curPos );
    2275   Double z  = abs( 1.0 / znear - 1.0 / zfar ) * ((Double)(AvgDepth) / (( 1 << g_bitDepthY ) - 1) ) + abs(1.0 / zfar);
    2276   Int   disparity = (Int)(direction*fL * z);
    2277   Int shift = DISTORTION_PRECISION_ADJUSTMENT(g_bitDepthY-8);
    2278   Int disp = disparity;
    2279 
    2280   for( y = 0; y < height; y++ )
    2281   {
    2282     for( x = 0; x < width; x++ )
    2283     {
    2284       SAD += abs( pOrg[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + disp)]
    2285       - pRec[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + disp)] )>>shift;
    2286     }
    2287     pOrg += iStride;
    2288     pRec += iStride;
    2289   }
    2290   m_pcLCUData[m_indexLCU].m_qp   = qp;
    2291   m_pcLCUData[m_indexLCU].m_costMAD  = (SAD /(Double)(width*height));
    2292   m_pcLCUData[m_indexLCU].m_bits = (Int)uiBits;
    2293 }
    2294 #endif
    2295 
    2296 
    2297 Double TEncRateCtrl::xAdjustmentBits(Int& reductionBits, Int& compensationBits)
    2298 {
    2299   Double adjustment  = ADJUSTMENT_FACTOR*reductionBits;
    2300   reductionBits     -= (Int)adjustment;
    2301   compensationBits  += (Int)adjustment;
    2302 
    2303   return adjustment;
    2304 }
    2305 
    2306 #endif
    2307 
Note: See TracChangeset for help on using the changeset viewer.