Ticket #1192: HM12.1_Ticket_1192.patch

File HM12.1_Ticket_1192.patch, 8.9 KB (added by karlsharman, 10 years ago)
  • source/Lib/TLibEncoder/TEncSampleAdaptiveOffset.cpp

     
    258258{
    259259  TComPicYuv* orgYuv= pPic->getPicYuvOrg();
    260260  TComPicYuv* resYuv= pPic->getPicYuvRec();
    261   m_labmda[SAO_Y]= lambdas[0]; m_labmda[SAO_Cb]= lambdas[1]; m_labmda[SAO_Cr]= lambdas[2];
     261  m_lambda[SAO_Y]= lambdas[0]; m_lambda[SAO_Cb]= lambdas[1]; m_lambda[SAO_Cr]= lambdas[2];
    262262  TComPicYuv* srcYuv = m_tempPicYuv;
    263263  resYuv->copyToPic(srcYuv);
    264264  srcYuv->setBorderExtension(false);
     
    508508
    509509          if( quantOffsets[classIdx] != 0 ) //iterative adjustment only when derived offset is not zero
    510510          {
    511             quantOffsets[classIdx] = estIterOffset( typeIdc, classIdx, m_labmda[compIdx], quantOffsets[classIdx], statData.count[classIdx], statData.diff[classIdx], shift, m_offsetStepLog2[compIdx], classDist , classCost , offsetTh );
     511            quantOffsets[classIdx] = estIterOffset( typeIdc, classIdx, m_lambda[compIdx], quantOffsets[classIdx], statData.count[classIdx], statData.diff[classIdx], shift, m_offsetStepLog2[compIdx], classDist , classCost , offsetTh );
    512512          }
    513513        }
    514514     
     
    522522        ::memset(distBOClasses, 0, sizeof(Int64)*NUM_SAO_BO_CLASSES);
    523523        for(Int classIdx=0; classIdx< NUM_SAO_BO_CLASSES; classIdx++)
    524524        {         
    525           costBOClasses[classIdx]= m_labmda[compIdx];
     525          costBOClasses[classIdx]= m_lambda[compIdx];
    526526          if( quantOffsets[classIdx] != 0 ) //iterative adjustment only when derived offset is not zero
    527527          {
    528             quantOffsets[classIdx] = estIterOffset( typeIdc, classIdx, m_labmda[compIdx], quantOffsets[classIdx], statData.count[classIdx], statData.diff[classIdx], shift, m_offsetStepLog2[compIdx], distBOClasses[classIdx], costBOClasses[classIdx], offsetTh );
     528            quantOffsets[classIdx] = estIterOffset( typeIdc, classIdx, m_lambda[compIdx], quantOffsets[classIdx], statData.count[classIdx], statData.diff[classIdx], shift, m_offsetStepLog2[compIdx], distBOClasses[classIdx], costBOClasses[classIdx], offsetTh );
    529529          }
    530530        }
    531531
     
    571571Void TEncSampleAdaptiveOffset::deriveModeNewRDO(Int ctu, std::vector<SAOBlkParam*>& mergeList, Bool* sliceEnabled, SAOStatData*** blkStats, SAOBlkParam& modeParam, Double& modeNormCost, TEncSbac** cabacCoderRDO, Int inCabacLabel)
    572572{
    573573  Double minCost, cost;
    574   Int rate, minRate;
     574  Int rate;
     575  UInt previousWrittenBits;
    575576  Int64 dist[NUM_SAO_COMPONENTS], modeDist[NUM_SAO_COMPONENTS];
    576577  SAOOffset testOffset[NUM_SAO_COMPONENTS];
    577578  Int compIdx;
     
    591592  modeParam[compIdx].modeIdc = SAO_MODE_OFF;
    592593  m_pcRDGoOnSbacCoder->resetBits();
    593594  m_pcRDGoOnSbacCoder->codeSAOOffsetParam(compIdx, modeParam[compIdx], sliceEnabled[compIdx]);
    594   minRate= m_pcRDGoOnSbacCoder->getNumberOfWrittenBits();
    595595  modeDist[compIdx] = 0;
    596   minCost= m_labmda[compIdx]*((Double)minRate);
     596  minCost= m_lambda[compIdx]*((Double)m_pcRDGoOnSbacCoder->getNumberOfWrittenBits());
    597597  m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_TEMP]);
    598598  if(sliceEnabled[compIdx])
    599599  {
     
    616616      m_pcRDGoOnSbacCoder->resetBits();
    617617      m_pcRDGoOnSbacCoder->codeSAOOffsetParam(compIdx, testOffset[compIdx], sliceEnabled[compIdx]);
    618618      rate = m_pcRDGoOnSbacCoder->getNumberOfWrittenBits();
    619       cost = (Double)dist[compIdx] + m_labmda[compIdx]*((Double)rate);
     619      cost = (Double)dist[compIdx] + m_lambda[compIdx]*((Double)rate);
    620620      if(cost < minCost)
    621621      {
    622622        minCost = cost;
    623         minRate = rate;
    624623        modeDist[compIdx] = dist[compIdx];
    625624        modeParam[compIdx]= testOffset[compIdx];
    626625        m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_TEMP]);
     
    631630  m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]);
    632631
    633632  //------ chroma --------//
    634   assert(m_labmda[SAO_Cb] == m_labmda[SAO_Cr]);
    635   Double chromaLambda = m_labmda[SAO_Cb];
    636633  //"off" case as initial cost
     634  cost = 0;
     635  previousWrittenBits = 0;
    637636  m_pcRDGoOnSbacCoder->resetBits();
    638   modeParam[SAO_Cb].modeIdc = SAO_MODE_OFF;
    639   m_pcRDGoOnSbacCoder->codeSAOOffsetParam(SAO_Cb, modeParam[SAO_Cb], sliceEnabled[SAO_Cb]);
    640   modeParam[SAO_Cr].modeIdc = SAO_MODE_OFF;
    641   m_pcRDGoOnSbacCoder->codeSAOOffsetParam(SAO_Cr, modeParam[SAO_Cr], sliceEnabled[SAO_Cr]);
    642   minRate= m_pcRDGoOnSbacCoder->getNumberOfWrittenBits();
    643   modeDist[SAO_Cb] = modeDist[SAO_Cr]= 0;
    644   minCost= chromaLambda*((Double)minRate);
     637  for (Int component = SAO_Cb; component < NUM_SAO_COMPONENTS; component++)
     638  {
     639    modeParam[component].modeIdc = SAO_MODE_OFF;
     640    modeDist [component] = 0;
    645641
     642    m_pcRDGoOnSbacCoder->codeSAOOffsetParam(component, modeParam[component], sliceEnabled[component]);
     643
     644    const UInt currentWrittenBits = m_pcRDGoOnSbacCoder->getNumberOfWrittenBits();
     645    cost += m_lambda[component] * (currentWrittenBits - previousWrittenBits);
     646    previousWrittenBits = currentWrittenBits;
     647  }
     648
     649  minCost = cost;
     650
    646651  //doesn't need to store cabac status here since the whole CTU parameters will be re-encoded at the end of this function
    647652
    648653  for(Int typeIdc=0; typeIdc< NUM_SAO_NEW_TYPES; typeIdc++)
    649654  {
     655    m_pcRDGoOnSbacCoder->load(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]);
     656    m_pcRDGoOnSbacCoder->resetBits();
     657    previousWrittenBits = 0;
     658    cost = 0;
     659
    650660    for(compIdx= SAO_Cb; compIdx< NUM_SAO_COMPONENTS; compIdx++)
    651661    {
    652662      if(!sliceEnabled[compIdx])
     
    662672      deriveOffsets(ctu, compIdx, typeIdc, blkStats[ctu][compIdx][typeIdc], testOffset[compIdx].offset, testOffset[compIdx].typeAuxInfo);
    663673      invertQuantOffsets(compIdx, typeIdc, testOffset[compIdx].typeAuxInfo, invQuantOffset, testOffset[compIdx].offset);
    664674      dist[compIdx]= getDistortion(ctu, compIdx, typeIdc, testOffset[compIdx].typeAuxInfo, invQuantOffset, blkStats[ctu][compIdx][typeIdc]);
     675     
     676      m_pcRDGoOnSbacCoder->codeSAOOffsetParam(compIdx, testOffset[compIdx], sliceEnabled[compIdx]);
     677
     678      const UInt currentWrittenBits = m_pcRDGoOnSbacCoder->getNumberOfWrittenBits();
     679      cost += dist[compIdx] + (m_lambda[compIdx] * (currentWrittenBits - previousWrittenBits));
     680      previousWrittenBits = currentWrittenBits;
    665681    }
    666682
    667     //get rate
    668     m_pcRDGoOnSbacCoder->load(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]);
    669     m_pcRDGoOnSbacCoder->resetBits();
    670     m_pcRDGoOnSbacCoder->codeSAOOffsetParam(SAO_Cb, testOffset[SAO_Cb], sliceEnabled[SAO_Cb]);
    671     m_pcRDGoOnSbacCoder->codeSAOOffsetParam(SAO_Cr, testOffset[SAO_Cr], sliceEnabled[SAO_Cr]);
    672     rate = m_pcRDGoOnSbacCoder->getNumberOfWrittenBits();
    673 
    674     cost = (Double)(dist[SAO_Cb]+ dist[SAO_Cr]) + chromaLambda*((Double)rate);
    675683    if(cost < minCost)
    676684    {
    677685      minCost = cost;
    678       minRate = rate;
    679       modeDist[SAO_Cb] = dist[SAO_Cb];
    680       modeDist[SAO_Cr] = dist[SAO_Cr];
    681       modeParam[SAO_Cb]= testOffset[SAO_Cb];
    682       modeParam[SAO_Cr]= testOffset[SAO_Cr];
     686      for(compIdx= SAO_Cb; compIdx< NUM_SAO_COMPONENTS; compIdx++)
     687      {
     688        modeDist [compIdx] = dist      [compIdx];
     689        modeParam[compIdx] = testOffset[compIdx];
     690      }
    683691    }
    684692  }
    685693
    686694
    687695  //----- re-gen rate & normalized cost----//
    688   modeNormCost  = (Double)modeDist[SAO_Y]/m_labmda[SAO_Y];
    689   modeNormCost += (Double)(modeDist[SAO_Cb]+ modeDist[SAO_Cr])/chromaLambda;
     696  modeNormCost = 0;
     697  for(UInt component = SAO_Y; component < NUM_SAO_COMPONENTS; component++)
     698  {
     699    modeNormCost += (Double)modeDist[component] / m_lambda[component];
     700  }
    690701  m_pcRDGoOnSbacCoder->load(cabacCoderRDO[inCabacLabel]);
    691702  m_pcRDGoOnSbacCoder->resetBits();
    692703  m_pcRDGoOnSbacCoder->codeSAOBlkParam(modeParam, sliceEnabled, (mergeList[SAO_MERGE_LEFT]!= NULL), (mergeList[SAO_MERGE_ABOVE]!= NULL), false);
     
    723734      {
    724735        //offsets have been reconstructed. Don't call inversed quantization function.
    725736        normDist += (((Double)getDistortion(ctu, compIdx, mergedOffsetParam.typeIdc, mergedOffsetParam.typeAuxInfo, mergedOffsetParam.offset, blkStats[ctu][compIdx][mergedOffsetParam.typeIdc]))
    726                        /m_labmda[compIdx]
     737                       /m_lambda[compIdx]
    727738                    );
    728739      }
    729740
  • source/Lib/TLibEncoder/TEncSampleAdaptiveOffset.h

     
    145145  TEncSbac**             m_pppcRDSbacCoder;           
    146146  TEncSbac*              m_pcRDGoOnSbacCoder;
    147147  TEncBinCABACCounter**  m_pppcBinCoderCABAC;   
    148   Double                 m_labmda[NUM_SAO_COMPONENTS];
     148  Double                 m_lambda[NUM_SAO_COMPONENTS];
    149149
    150150  //statistics
    151151  SAOStatData***         m_statData; //[ctu][comp][classes]