Skip to content
Snippets Groups Projects
EncGOP.cpp 126 KiB
Newer Older
  • Learn to ignore specific revisions
  • 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612
          msg(NOTICE, " [WY %6.4lf dB    WU %6.4lf dB    WV %6.4lf dB]", dPSNRWeighted[COMPONENT_Y], dPSNRWeighted[COMPONENT_Cb], dPSNRWeighted[COMPONENT_Cr]);
        }
    #endif
        msg( NOTICE, " [ET %5.0f ]", dEncTime );
    
        // msg( SOME, " [WP %d]", pcSlice->getUseWeightedPrediction());
    
        for( int iRefList = 0; iRefList < 2; iRefList++ )
        {
          msg( NOTICE, " [L%d ", iRefList );
          for( int iRefIndex = 0; iRefIndex < pcSlice->getNumRefIdx( RefPicList( iRefList ) ); iRefIndex++ )
          {
            msg( NOTICE, "%d ", pcSlice->getRefPOC( RefPicList( iRefList ), iRefIndex ) - pcSlice->getLastIDR() );
          }
          msg( NOTICE, "]" );
        }
      }
      else if( g_verbosity >= INFO )
      {
        std::cout << "\r\t" << pcSlice->getPOC();
        std::cout.flush();
      }
    }
    
    void EncGOP::xCalculateInterlacedAddPSNR( Picture* pcPicOrgFirstField, Picture* pcPicOrgSecondField,
                                              PelUnitBuf cPicRecFirstField, PelUnitBuf cPicRecSecondField,
                                              const InputColourSpaceConversion conversion, const bool printFrameMSE, double* PSNR_Y )
    {
      const SPS &sps = *pcPicOrgFirstField->cs->sps;
      const ChromaFormat format = sps.getChromaFormatIdc();
      double  dPSNR[MAX_NUM_COMPONENT];
      Picture    *apcPicOrgFields[2] = {pcPicOrgFirstField, pcPicOrgSecondField};
      PelUnitBuf acPicRecFields[2]   = {cPicRecFirstField, cPicRecSecondField};
    #if ENABLE_QPA
      const bool    useWPSNR = m_pcEncLib->getUseWPSNR();
    #endif
      for(int i=0; i<MAX_NUM_COMPONENT; i++)
      {
        dPSNR[i]=0.0;
      }
    
      PelStorage cscd[2 /* first/second field */];
      if (conversion!=IPCOLOURSPACE_UNCHANGED)
      {
        for(uint32_t fieldNum=0; fieldNum<2; fieldNum++)
        {
          PelUnitBuf& reconField= (acPicRecFields[fieldNum]);
          cscd[fieldNum].create( reconField.chromaFormat, Area( Position(), reconField.Y()) );
          VideoIOYuv::ColourSpaceConvert(reconField, cscd[fieldNum], conversion, false);
          acPicRecFields[fieldNum]=cscd[fieldNum];
        }
      }
    
      //===== calculate PSNR =====
      double MSEyuvframe[MAX_NUM_COMPONENT] = {0, 0, 0};
    
      CHECK(!(acPicRecFields[0].chromaFormat==acPicRecFields[1].chromaFormat), "Unspecified error");
      const uint32_t numValidComponents = ::getNumberValidComponents( acPicRecFields[0].chromaFormat );
    #if ENABLE_QPA && FRAME_WEIGHTING
      const Slice*  pcSlice      = pcPicOrgFirstField->slices[0];
      const uint32_t    currDQP      = (pcSlice->getPOC() % m_pcEncLib->getIntraPeriod()) == 0 ? 0 : DQP[pcSlice->getPOC() % m_pcEncLib->getGOPSize()];
      const double  frameWeight  = pow(2.0, (double)currDQP / -3.0);
    
      if (useWPSNR) m_gcAnalyzeAll_in.addWeight(frameWeight);
    #endif
      for (int chan = 0; chan < numValidComponents; chan++)
      {
        const ComponentID ch=ComponentID(chan);
        CHECK(!(acPicRecFields[0].get(ch).width==acPicRecFields[1].get(ch).width), "Unspecified error");
        CHECK(!(acPicRecFields[0].get(ch).height==acPicRecFields[0].get(ch).height), "Unspecified error");
    
        uint64_t uiSSDtemp=0;
        const uint32_t width    = acPicRecFields[0].get(ch).width - (m_pcEncLib->getPad(0) >> ::getComponentScaleX(ch, format));
        const uint32_t height   = acPicRecFields[0].get(ch).height - ((m_pcEncLib->getPad(1) >> 1) >> ::getComponentScaleY(ch, format));
        const uint32_t bitDepth = sps.getBitDepth(toChannelType(ch));
    
        for(uint32_t fieldNum=0; fieldNum<2; fieldNum++)
        {
          CHECK(!(conversion == IPCOLOURSPACE_UNCHANGED), "Unspecified error");
    #if ENABLE_QPA
          uiSSDtemp += xFindDistortionPlane( acPicRecFields[fieldNum].get(ch), apcPicOrgFields[fieldNum]->getOrigBuf().get(ch), useWPSNR ? bitDepth : 0, ::getComponentScaleX(ch, format) );
    #else
          uiSSDtemp += xFindDistortionPlane( acPicRecFields[fieldNum].get(ch), apcPicOrgFields[fieldNum]->getOrigBuf().get(ch), 0 );
    #endif
        }
    #if ENABLE_QPA
        const uint32_t maxval = /*useWPSNR ? (1 << bitDepth) - 1 :*/ 255 << (bitDepth - 8); // fix with WPSNR: 1023 (4095) instead of 1020 (4080) for bit-depth 10 (12)
    #else
        const uint32_t maxval = 255 << (bitDepth - 8);
    #endif
        const uint32_t size   = width * height * 2;
        const double fRefValue = (double)maxval * maxval * size;
        dPSNR[ch]         = uiSSDtemp ? 10.0 * log10(fRefValue / (double)uiSSDtemp) : 999.99;
        MSEyuvframe[ch]   = (double)uiSSDtemp / size;
    #if ENABLE_QPA && FRAME_WEIGHTING
        if (useWPSNR) m_gcAnalyzeAll_in.addWeightedSSD(frameWeight * (double)uiSSDtemp / fRefValue, ch);
    #endif
      }
    
      uint32_t uibits = 0; // the number of bits for the pair is not calculated here - instead the overall total is used elsewhere.
    
      //===== add PSNR =====
      m_gcAnalyzeAll_in.addResult (dPSNR, (double)uibits, MSEyuvframe);
    
      *PSNR_Y = dPSNR[COMPONENT_Y];
    
      msg( DETAILS, "\n                                      Interlaced frame %d: [Y %6.4lf dB    U %6.4lf dB    V %6.4lf dB]", pcPicOrgSecondField->getPOC()/2 , dPSNR[COMPONENT_Y], dPSNR[COMPONENT_Cb], dPSNR[COMPONENT_Cr] );
      if (printFrameMSE)
      {
        msg( DETAILS, " [Y MSE %6.4lf  U MSE %6.4lf  V MSE %6.4lf]", MSEyuvframe[COMPONENT_Y], MSEyuvframe[COMPONENT_Cb], MSEyuvframe[COMPONENT_Cr] );
      }
    
      for(uint32_t fieldNum=0; fieldNum<2; fieldNum++)
      {
        cscd[fieldNum].destroy();
      }
    }
    
    /** Function for deciding the nal_unit_type.
     * \param pocCurr POC of the current picture
     * \param lastIDR  POC of the last IDR picture
     * \param isField  true to indicate field coding
     * \returns the NAL unit type of the picture
     * This function checks the configuration and returns the appropriate nal_unit_type for the picture.
     */
    NalUnitType EncGOP::getNalUnitType(int pocCurr, int lastIDR, bool isField)
    {
      if (pocCurr == 0)
      {
        return NAL_UNIT_CODED_SLICE_IDR_W_RADL;
      }
    
      if(m_pcCfg->getEfficientFieldIRAPEnabled() && isField && pocCurr == 1)
      {
        // to avoid the picture becoming an IRAP
        return NAL_UNIT_CODED_SLICE_TRAIL_R;
      }
    
      if(m_pcCfg->getDecodingRefreshType() != 3 && (pocCurr - isField) % m_pcCfg->getIntraPeriod() == 0)
      {
        if (m_pcCfg->getDecodingRefreshType() == 1)
        {
          return NAL_UNIT_CODED_SLICE_CRA;
        }
        else if (m_pcCfg->getDecodingRefreshType() == 2)
        {
          return NAL_UNIT_CODED_SLICE_IDR_W_RADL;
        }
      }
      if(m_pocCRA>0)
      {
        if(pocCurr<m_pocCRA)
        {
          // All leading pictures are being marked as TFD pictures here since current encoder uses all
          // reference pictures while encoding leading pictures. An encoder can ensure that a leading
          // picture can be still decodable when random accessing to a CRA/CRANT/BLA/BLANT picture by
          // controlling the reference pictures used for encoding that leading picture. Such a leading
          // picture need not be marked as a TFD picture.
          return NAL_UNIT_CODED_SLICE_RASL_R;
        }
      }
      if (lastIDR>0)
      {
        if (pocCurr < lastIDR)
        {
          return NAL_UNIT_CODED_SLICE_RADL_R;
        }
      }
      return NAL_UNIT_CODED_SLICE_TRAIL_R;
    }
    
    void EncGOP::xUpdateRasInit(Slice* slice)
    {
      slice->setPendingRasInit( false );
      if ( slice->getPOC() > m_lastRasPoc )
      {
        m_lastRasPoc = MAX_INT;
        slice->setPendingRasInit( true );
      }
      if ( slice->isIRAP() )
      {
        m_lastRasPoc = slice->getPOC();
      }
    }
    
    double EncGOP::xCalculateRVM()
    {
      double dRVM = 0;
    
      if( m_pcCfg->getGOPSize() == 1 && m_pcCfg->getIntraPeriod() != 1 && m_pcCfg->getFramesToBeEncoded() > RVM_VCEGAM10_M * 2 )
      {
        // calculate RVM only for lowdelay configurations
        std::vector<double> vRL , vB;
        size_t N = m_vRVM_RP.size();
        vRL.resize( N );
        vB.resize( N );
    
        int i;
        double dRavg = 0 , dBavg = 0;
        vB[RVM_VCEGAM10_M] = 0;
        for( i = RVM_VCEGAM10_M + 1 ; i < N - RVM_VCEGAM10_M + 1 ; i++ )
        {
          vRL[i] = 0;
          for( int j = i - RVM_VCEGAM10_M ; j <= i + RVM_VCEGAM10_M - 1 ; j++ )
          {
            vRL[i] += m_vRVM_RP[j];
          }
          vRL[i] /= ( 2 * RVM_VCEGAM10_M );
          vB[i] = vB[i-1] + m_vRVM_RP[i] - vRL[i];
          dRavg += m_vRVM_RP[i];
          dBavg += vB[i];
        }
    
        dRavg /= ( N - 2 * RVM_VCEGAM10_M );
        dBavg /= ( N - 2 * RVM_VCEGAM10_M );
    
        double dSigamB = 0;
        for( i = RVM_VCEGAM10_M + 1 ; i < N - RVM_VCEGAM10_M + 1 ; i++ )
        {
          double tmp = vB[i] - dBavg;
          dSigamB += tmp * tmp;
        }
        dSigamB = sqrt( dSigamB / ( N - 2 * RVM_VCEGAM10_M ) );
    
        double f = sqrt( 12.0 * ( RVM_VCEGAM10_M - 1 ) / ( RVM_VCEGAM10_M + 1 ) );
    
        dRVM = dSigamB / dRavg * f;
      }
    
      return( dRVM );
    }
    
    /** Attaches the input bitstream to the stream in the output NAL unit
        Updates rNalu to contain concatenated bitstream. rpcBitstreamRedirect is cleared at the end of this function call.
     *  \param codedSliceData contains the coded slice data (bitstream) to be concatenated to rNalu
     *  \param rNalu          target NAL unit
     */
    void EncGOP::xAttachSliceDataToNalUnit (OutputNALUnit& rNalu, OutputBitstream* codedSliceData)
    {
      // Byte-align
      rNalu.m_Bitstream.writeByteAlignment();   // Slice header byte-alignment
    
      // Perform bitstream concatenation
      if (codedSliceData->getNumberOfWrittenBits() > 0)
      {
        rNalu.m_Bitstream.addSubstream(codedSliceData);
      }
      codedSliceData->clear();
    }
    
    // Function will arrange the long-term pictures in the decreasing order of poc_lsb_lt,
    // and among the pictures with the same lsb, it arranges them in increasing delta_poc_msb_cycle_lt value
    void EncGOP::arrangeLongtermPicturesInRPS(Slice *pcSlice, PicList& rcListPic)
    {
      if(pcSlice->getRPS()->getNumberOfLongtermPictures() == 0)
      {
        return;
      }
      // we can only modify the local RPS!
      CHECK(!(pcSlice->getRPSidx()==-1), "Unspecified error");
      ReferencePictureSet *rps = pcSlice->getLocalRPS();
    
      // Arrange long-term reference pictures in the correct order of LSB and MSB,
      // and assign values for pocLSBLT and MSB present flag
      int longtermPicsPoc[MAX_NUM_REF_PICS], longtermPicsLSB[MAX_NUM_REF_PICS], indices[MAX_NUM_REF_PICS];
      int longtermPicsMSB[MAX_NUM_REF_PICS];
      bool mSBPresentFlag[MAX_NUM_REF_PICS];
      ::memset(longtermPicsPoc, 0, sizeof(longtermPicsPoc));    // Store POC values of LTRP
      ::memset(longtermPicsLSB, 0, sizeof(longtermPicsLSB));    // Store POC LSB values of LTRP
      ::memset(longtermPicsMSB, 0, sizeof(longtermPicsMSB));    // Store POC LSB values of LTRP
      ::memset(indices        , 0, sizeof(indices));            // Indices to aid in tracking sorted LTRPs
      ::memset(mSBPresentFlag , 0, sizeof(mSBPresentFlag));     // Indicate if MSB needs to be present
    
      // Get the long-term reference pictures
      int offset = rps->getNumberOfNegativePictures() + rps->getNumberOfPositivePictures();
      int i, ctr = 0;
      int maxPicOrderCntLSB = 1 << pcSlice->getSPS()->getBitsForPOC();
      for(i = rps->getNumberOfPictures() - 1; i >= offset; i--, ctr++)
      {
        longtermPicsPoc[ctr] = rps->getPOC(i);                                  // LTRP POC
        longtermPicsLSB[ctr] = getLSB(longtermPicsPoc[ctr], maxPicOrderCntLSB); // LTRP POC LSB
        indices[ctr]      = i;
        longtermPicsMSB[ctr] = longtermPicsPoc[ctr] - longtermPicsLSB[ctr];
      }
      int numLongPics = rps->getNumberOfLongtermPictures();
      CHECK(!(ctr == numLongPics), "Unspecified error");
    
      // Arrange pictures in decreasing order of MSB;
      for(i = 0; i < numLongPics; i++)
      {
        for(int j = 0; j < numLongPics - 1; j++)
        {
          if(longtermPicsMSB[j] < longtermPicsMSB[j+1])
          {
            std::swap(longtermPicsPoc[j], longtermPicsPoc[j+1]);
            std::swap(longtermPicsLSB[j], longtermPicsLSB[j+1]);
            std::swap(longtermPicsMSB[j], longtermPicsMSB[j+1]);
            std::swap(indices[j]        , indices[j+1]        );
          }
        }
      }
    
      for(i = 0; i < numLongPics; i++)
      {
        // Check if MSB present flag should be enabled.
        // Check if the buffer contains any pictures that have the same LSB.
        PicList::iterator  iterPic = rcListPic.begin();
        Picture*                      pcPic;
        while ( iterPic != rcListPic.end() )
        {
          pcPic = *iterPic;
          if( (getLSB(pcPic->getPOC(), maxPicOrderCntLSB) == longtermPicsLSB[i])   &&     // Same LSB
                                          (pcPic->referenced)     &&    // Reference picture
                                            (pcPic->getPOC() != longtermPicsPoc[i])    )  // Not the LTRP itself
          {
            mSBPresentFlag[i] = true;
            break;
          }
          iterPic++;
        }
      }
    
      // tempArray for usedByCurr flag
      bool tempArray[MAX_NUM_REF_PICS]; ::memset(tempArray, 0, sizeof(tempArray));
      for(i = 0; i < numLongPics; i++)
      {
        tempArray[i] = rps->getUsed(indices[i]);
      }
      // Now write the final values;
      ctr = 0;
      int currMSB = 0, currLSB = 0;
      // currPicPoc = currMSB + currLSB
      currLSB = getLSB(pcSlice->getPOC(), maxPicOrderCntLSB);
      currMSB = pcSlice->getPOC() - currLSB;
    
      for(i = rps->getNumberOfPictures() - 1; i >= offset; i--, ctr++)
      {
        rps->setPOC                   (i, longtermPicsPoc[ctr]);
        rps->setDeltaPOC              (i, - pcSlice->getPOC() + longtermPicsPoc[ctr]);
        rps->setUsed                  (i, tempArray[ctr]);
        rps->setPocLSBLT              (i, longtermPicsLSB[ctr]);
        rps->setDeltaPocMSBCycleLT    (i, (currMSB - (longtermPicsPoc[ctr] - longtermPicsLSB[ctr])) / maxPicOrderCntLSB);
        rps->setDeltaPocMSBPresentFlag(i, mSBPresentFlag[ctr]);
    
        CHECK(!(rps->getDeltaPocMSBCycleLT(i) >= 0), "Unspecified error");   // Non-negative value
      }
      for(i = rps->getNumberOfPictures() - 1, ctr = 1; i >= offset; i--, ctr++)
      {
        for(int j = rps->getNumberOfPictures() - 1 - ctr; j >= offset; j--)
        {
          // Here at the encoder we know that we have set the full POC value for the LTRPs, hence we
          // don't have to check the MSB present flag values for this constraint.
          CHECK(!( rps->getPOC(i) != rps->getPOC(j) ), "Unspecified error"); // If assert fails, LTRP entry repeated in RPS!!!
        }
      }
    }
    
    void EncGOP::applyDeblockingFilterMetric( Picture* pcPic, uint32_t uiNumSlices )
    {
      PelBuf cPelBuf = pcPic->getRecoBuf().get( COMPONENT_Y );
      Pel* Rec    = cPelBuf.buf;
      const int  stride = cPelBuf.stride;
      const uint32_t picWidth = cPelBuf.width;
      const uint32_t picHeight = cPelBuf.height;
    
      Pel* tempRec = Rec;
      const Slice* pcSlice = pcPic->slices[0];
      uint32_t log2maxTB = pcSlice->getSPS()->getQuadtreeTULog2MaxSize();
      uint32_t maxTBsize = (1<<log2maxTB);
      const uint32_t minBlockArtSize = 8;
      const uint32_t noCol = (picWidth>>log2maxTB);
      const uint32_t noRows = (picHeight>>log2maxTB);
      CHECK(!(noCol > 1), "Unspecified error");
      CHECK(!(noRows > 1), "Unspecified error");
      std::vector<uint64_t> colSAD(noCol,  uint64_t(0));
      std::vector<uint64_t> rowSAD(noRows, uint64_t(0));
      uint32_t colIdx = 0;
      uint32_t rowIdx = 0;
      Pel p0, p1, p2, q0, q1, q2;
    
      int qp = pcSlice->getSliceQp();
      const int bitDepthLuma=pcSlice->getSPS()->getBitDepth(CHANNEL_TYPE_LUMA);
      int bitdepthScale = 1 << (bitDepthLuma-8);
      int beta = LoopFilter::getBeta( qp ) * bitdepthScale;
      const int thr2 = (beta>>2);
      const int thr1 = 2*bitdepthScale;
      uint32_t a = 0;
    
      if (maxTBsize > minBlockArtSize)
      {
        // Analyze vertical artifact edges
        for(int c = maxTBsize; c < picWidth; c += maxTBsize)
        {
          for(int r = 0; r < picHeight; r++)
          {
            p2 = Rec[c-3];
            p1 = Rec[c-2];
            p0 = Rec[c-1];
            q0 = Rec[c];
            q1 = Rec[c+1];
            q2 = Rec[c+2];
            a = ((abs(p2-(p1<<1)+p0)+abs(q0-(q1<<1)+q2))<<1);
            if ( thr1 < a && a < thr2)
            {
              colSAD[colIdx] += abs(p0 - q0);
            }
            Rec += stride;
          }
          colIdx++;
          Rec = tempRec;
        }
    
        // Analyze horizontal artifact edges
        for(int r = maxTBsize; r < picHeight; r += maxTBsize)
        {
          for(int c = 0; c < picWidth; c++)
          {
            p2 = Rec[c + (r-3)*stride];
            p1 = Rec[c + (r-2)*stride];
            p0 = Rec[c + (r-1)*stride];
            q0 = Rec[c + r*stride];
            q1 = Rec[c + (r+1)*stride];
            q2 = Rec[c + (r+2)*stride];
            a = ((abs(p2-(p1<<1)+p0)+abs(q0-(q1<<1)+q2))<<1);
            if (thr1 < a && a < thr2)
            {
              rowSAD[rowIdx] += abs(p0 - q0);
            }
          }
          rowIdx++;
        }
      }
    
      uint64_t colSADsum = 0;
      uint64_t rowSADsum = 0;
      for(int c = 0; c < noCol-1; c++)
      {
        colSADsum += colSAD[c];
      }
      for(int r = 0; r < noRows-1; r++)
      {
        rowSADsum += rowSAD[r];
      }
    
      colSADsum <<= 10;
      rowSADsum <<= 10;
      colSADsum /= (noCol-1);
      colSADsum /= picHeight;
      rowSADsum /= (noRows-1);
      rowSADsum /= picWidth;
    
      uint64_t avgSAD = ((colSADsum + rowSADsum)>>1);
      avgSAD >>= (bitDepthLuma-8);
    
      if ( avgSAD > 2048 )
      {
        avgSAD >>= 9;
        int offset = Clip3(2,6,(int)avgSAD);
        for (int i=0; i<uiNumSlices; i++)
        {
          Slice* pcLocalSlice = pcPic->slices[i];
          pcLocalSlice->setDeblockingFilterOverrideFlag   ( true);
          pcLocalSlice->setDeblockingFilterDisable        ( false);
          pcLocalSlice->setDeblockingFilterBetaOffsetDiv2 ( offset );
          pcLocalSlice->setDeblockingFilterTcOffsetDiv2   ( offset );
        }
      }
      else
      {
        for (int i=0; i<uiNumSlices; i++)
        {
          Slice* pcLocalSlice = pcPic->slices[i];
          const PPS* pcPPS = pcSlice->getPPS();
          pcLocalSlice->setDeblockingFilterOverrideFlag  ( false);
          pcLocalSlice->setDeblockingFilterDisable       ( pcPPS->getPPSDeblockingFilterDisabledFlag() );
          pcLocalSlice->setDeblockingFilterBetaOffsetDiv2( pcPPS->getDeblockingFilterBetaOffsetDiv2() );
          pcLocalSlice->setDeblockingFilterTcOffsetDiv2  ( pcPPS->getDeblockingFilterTcOffsetDiv2()   );
        }
      }
    }
    
    #if W0038_DB_OPT
    void EncGOP::applyDeblockingFilterParameterSelection( Picture* pcPic, const uint32_t numSlices, const int gopID )
    {
      enum DBFltParam
      {
        DBFLT_PARAM_AVAILABLE = 0,
        DBFLT_DISABLE_FLAG,
        DBFLT_BETA_OFFSETD2,
        DBFLT_TC_OFFSETD2,
        //NUM_DBFLT_PARAMS
      };
      const int MAX_BETA_OFFSET = 3;
      const int MIN_BETA_OFFSET = -3;
      const int MAX_TC_OFFSET = 3;
      const int MIN_TC_OFFSET = -3;
    
      PelUnitBuf reco = pcPic->getRecoBuf();
    
      const int currQualityLayer = (pcPic->slices[0]->getSliceType() != I_SLICE) ? m_pcCfg->getGOPEntry(gopID).m_temporalId+1 : 0;
      CHECK(!(currQualityLayer <MAX_ENCODER_DEBLOCKING_QUALITY_LAYERS), "Unspecified error");
    
      CodingStructure& cs = *pcPic->cs;
    
      if(!m_pcDeblockingTempPicYuv)
      {
        m_pcDeblockingTempPicYuv = new PelStorage;
        m_pcDeblockingTempPicYuv->create( cs.area );
        memset(m_DBParam, 0, sizeof(m_DBParam));
      }
    
      //preserve current reconstruction
      m_pcDeblockingTempPicYuv->copyFrom ( reco );
    
      const bool bNoFiltering      = m_DBParam[currQualityLayer][DBFLT_PARAM_AVAILABLE] && m_DBParam[currQualityLayer][DBFLT_DISABLE_FLAG]==false /*&& pcPic->getTLayer()==0*/;
      const int  maxBetaOffsetDiv2 = bNoFiltering? Clip3(MIN_BETA_OFFSET, MAX_BETA_OFFSET, m_DBParam[currQualityLayer][DBFLT_BETA_OFFSETD2]+1) : MAX_BETA_OFFSET;
      const int  minBetaOffsetDiv2 = bNoFiltering? Clip3(MIN_BETA_OFFSET, MAX_BETA_OFFSET, m_DBParam[currQualityLayer][DBFLT_BETA_OFFSETD2]-1) : MIN_BETA_OFFSET;
      const int  maxTcOffsetDiv2   = bNoFiltering? Clip3(MIN_TC_OFFSET, MAX_TC_OFFSET, m_DBParam[currQualityLayer][DBFLT_TC_OFFSETD2]+2)       : MAX_TC_OFFSET;
      const int  minTcOffsetDiv2   = bNoFiltering? Clip3(MIN_TC_OFFSET, MAX_TC_OFFSET, m_DBParam[currQualityLayer][DBFLT_TC_OFFSETD2]-2)       : MIN_TC_OFFSET;
    
      uint64_t distBetaPrevious      = std::numeric_limits<uint64_t>::max();
      uint64_t distMin               = std::numeric_limits<uint64_t>::max();
      bool   bDBFilterDisabledBest = true;
      int    betaOffsetDiv2Best    = 0;
      int    tcOffsetDiv2Best      = 0;
    
      for(int betaOffsetDiv2=maxBetaOffsetDiv2; betaOffsetDiv2>=minBetaOffsetDiv2; betaOffsetDiv2--)
      {
        uint64_t distTcMin = std::numeric_limits<uint64_t>::max();
        for(int tcOffsetDiv2=maxTcOffsetDiv2; tcOffsetDiv2 >= minTcOffsetDiv2; tcOffsetDiv2--)
        {
          for (int i=0; i<numSlices; i++)
          {
            Slice* pcSlice = pcPic->slices[i];
            pcSlice->setDeblockingFilterOverrideFlag  ( true);
            pcSlice->setDeblockingFilterDisable       ( false);
            pcSlice->setDeblockingFilterBetaOffsetDiv2( betaOffsetDiv2 );
            pcSlice->setDeblockingFilterTcOffsetDiv2  ( tcOffsetDiv2 );
          }
    
          // restore reconstruction
          reco.copyFrom( *m_pcDeblockingTempPicYuv );
    
          const uint64_t dist = preLoopFilterPicAndCalcDist( pcPic );
    
          if(dist < distMin)
          {
            distMin = dist;
            bDBFilterDisabledBest = false;
            betaOffsetDiv2Best  = betaOffsetDiv2;
            tcOffsetDiv2Best = tcOffsetDiv2;
          }
          if(dist < distTcMin)
          {
            distTcMin = dist;
          }
          else if(tcOffsetDiv2 <-2)
          {
            break;
          }
        }
        if(betaOffsetDiv2<-1 && distTcMin >= distBetaPrevious)
        {
          break;
        }
        distBetaPrevious = distTcMin;
      }
    
      //update:
      m_DBParam[currQualityLayer][DBFLT_PARAM_AVAILABLE] = 1;
      m_DBParam[currQualityLayer][DBFLT_DISABLE_FLAG]    = bDBFilterDisabledBest;
      m_DBParam[currQualityLayer][DBFLT_BETA_OFFSETD2]   = betaOffsetDiv2Best;
      m_DBParam[currQualityLayer][DBFLT_TC_OFFSETD2]     = tcOffsetDiv2Best;
    
      // restore reconstruction
      reco.copyFrom( *m_pcDeblockingTempPicYuv );
    
      const PPS* pcPPS = pcPic->slices[0]->getPPS();
      if(bDBFilterDisabledBest)
      {
        for (int i=0; i<numSlices; i++)
        {
          Slice* pcSlice = pcPic->slices[i];
          pcSlice->setDeblockingFilterOverrideFlag( true);
          pcSlice->setDeblockingFilterDisable     ( true);
        }
      }
      else if(betaOffsetDiv2Best == pcPPS->getDeblockingFilterBetaOffsetDiv2() &&  tcOffsetDiv2Best == pcPPS->getDeblockingFilterTcOffsetDiv2())
      {
        for (int i=0; i<numSlices; i++)
        {
          Slice*      pcSlice = pcPic->slices[i];
          pcSlice->setDeblockingFilterOverrideFlag   ( false);
          pcSlice->setDeblockingFilterDisable        ( pcPPS->getPPSDeblockingFilterDisabledFlag() );
          pcSlice->setDeblockingFilterBetaOffsetDiv2 ( pcPPS->getDeblockingFilterBetaOffsetDiv2() );
          pcSlice->setDeblockingFilterTcOffsetDiv2   ( pcPPS->getDeblockingFilterTcOffsetDiv2()   );
        }
      }
      else
      {
        for (int i=0; i<numSlices; i++)
        {
          Slice* pcSlice = pcPic->slices[i];
          pcSlice->setDeblockingFilterOverrideFlag   ( true);
          pcSlice->setDeblockingFilterDisable        ( false );
          pcSlice->setDeblockingFilterBetaOffsetDiv2 ( betaOffsetDiv2Best);
          pcSlice->setDeblockingFilterTcOffsetDiv2   ( tcOffsetDiv2Best);
        }
      }
    }
    #endif
    //! \}