source: SHVCSoftware/branches/SHM-4.0-dev/source/Lib/TLibEncoder/WeightPredAnalysis.cpp @ 475

Last change on this file since 475 was 473, checked in by nokia, 11 years ago

Fix a compile time warning in WeightPredAnalysis::xUpdatingWPParameters

  • Property svn:eol-style set to native
File size: 17.0 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license.
5 *
6 * Copyright (c) 2010-2013, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     WeightPredAnalysis.cpp
35    \brief    weighted prediction encoder class
36*/
37
38#include "../TLibCommon/TypeDef.h"
39#include "../TLibCommon/TComSlice.h"
40#include "../TLibCommon/TComPic.h"
41#include "../TLibCommon/TComPicYuv.h"
42#include "WeightPredAnalysis.h"
43
44#define ABS(a)    ((a) < 0 ? - (a) : (a))
45#define DTHRESH (0.99)
46
47WeightPredAnalysis::WeightPredAnalysis()
48{
49  m_weighted_pred_flag = false;
50  m_weighted_bipred_flag = false;
51  for ( Int iList =0 ; iList<2 ; iList++ )
52  {
53    for ( Int iRefIdx=0 ; iRefIdx<MAX_NUM_REF ; iRefIdx++ )
54    {
55      for ( Int comp=0 ; comp<3 ;comp++ )
56      {
57        wpScalingParam  *pwp   = &(m_wp[iList][iRefIdx][comp]);
58        pwp->bPresentFlag      = false;
59        pwp->uiLog2WeightDenom = 0;
60        pwp->iWeight           = 1;
61        pwp->iOffset           = 0;
62      }
63    }
64  }
65}
66
67/** calculate AC and DC values for current original image
68 * \param TComSlice *slice
69 * \returns Void
70 */
71Bool  WeightPredAnalysis::xCalcACDCParamSlice(TComSlice *slice)
72{
73  //===== calculate AC/DC value =====
74  TComPicYuv*   pPic = slice->getPic()->getPicYuvOrg();
75  Int   iSample  = 0;
76#if O0194_WEIGHTED_PREDICTION_CGS
77  // Define here to assign the parameter of "iSample"
78  wpACDCParam weightACDCParam[3];
79#endif
80
81  // calculate DC/AC value for Y
82  Pel*  pOrg    = pPic->getLumaAddr();
83  Int64  iOrgDCY = xCalcDCValueSlice(slice, pOrg, &iSample);
84  Int64  iOrgNormDCY = ((iOrgDCY+(iSample>>1)) / iSample);
85  pOrg = pPic->getLumaAddr();
86  Int64  iOrgACY  = xCalcACValueSlice(slice, pOrg, iOrgNormDCY);
87#if O0194_WEIGHTED_PREDICTION_CGS
88  weightACDCParam[0].iSamples = iSample;
89#endif
90
91  // calculate DC/AC value for Cb
92  pOrg = pPic->getCbAddr();
93  Int64  iOrgDCCb = xCalcDCValueUVSlice(slice, pOrg, &iSample);
94  Int64  iOrgNormDCCb = ((iOrgDCCb+(iSample>>1)) / (iSample));
95  pOrg = pPic->getCbAddr();
96  Int64  iOrgACCb  = xCalcACValueUVSlice(slice, pOrg, iOrgNormDCCb);
97#if O0194_WEIGHTED_PREDICTION_CGS
98  weightACDCParam[1].iSamples = iSample;
99#endif
100
101  // calculate DC/AC value for Cr
102  pOrg = pPic->getCrAddr();
103  Int64  iOrgDCCr = xCalcDCValueUVSlice(slice, pOrg, &iSample);
104  Int64  iOrgNormDCCr = ((iOrgDCCr+(iSample>>1)) / (iSample));
105  pOrg = pPic->getCrAddr();
106  Int64  iOrgACCr  = xCalcACValueUVSlice(slice, pOrg, iOrgNormDCCr);
107#if O0194_WEIGHTED_PREDICTION_CGS
108  weightACDCParam[2].iSamples = iSample;
109#endif
110
111#if !O0194_WEIGHTED_PREDICTION_CGS
112  wpACDCParam weightACDCParam[3];
113#endif
114  weightACDCParam[0].iAC = iOrgACY;
115  weightACDCParam[0].iDC = iOrgNormDCY;
116  weightACDCParam[1].iAC = iOrgACCb;
117  weightACDCParam[1].iDC = iOrgNormDCCb;
118  weightACDCParam[2].iAC = iOrgACCr;
119  weightACDCParam[2].iDC = iOrgNormDCCr;
120
121  slice->setWpAcDcParam(weightACDCParam);
122  return (true);
123}
124
125/** store weighted_pred_flag and weighted_bipred_idc values
126 * \param weighted_pred_flag
127 * \param weighted_bipred_idc
128 * \returns Void
129 */
130Void  WeightPredAnalysis::xStoreWPparam(Bool weighted_pred_flag, Bool weighted_bipred_flag)
131{
132  m_weighted_pred_flag = weighted_pred_flag;
133  m_weighted_bipred_flag = weighted_bipred_flag;
134}
135
136/** restore weighted_pred_flag and weighted_bipred_idc values
137 * \param TComSlice *slice
138 * \returns Void
139 */
140Void  WeightPredAnalysis::xRestoreWPparam(TComSlice *slice)
141{
142  slice->getPPS()->setUseWP(m_weighted_pred_flag);
143  slice->getPPS()->setWPBiPred(m_weighted_bipred_flag);
144}
145
146/** check weighted pred or non-weighted pred
147 * \param TComSlice *slice
148 * \returns Void
149 */
150Void  WeightPredAnalysis::xCheckWPEnable(TComSlice *slice)
151{
152  Int iPresentCnt = 0;
153  for ( Int iList=0 ; iList<2 ; iList++ )
154  {
155    for ( Int iRefIdx=0 ; iRefIdx<MAX_NUM_REF ; iRefIdx++ )
156    {
157      for ( Int iComp=0 ; iComp<3 ;iComp++ )
158      {
159        wpScalingParam  *pwp = &(m_wp[iList][iRefIdx][iComp]);
160        iPresentCnt += (Int)pwp->bPresentFlag;
161      }
162    }
163  }
164
165  if(iPresentCnt==0)
166  {
167    slice->getPPS()->setUseWP(false);
168    slice->getPPS()->setWPBiPred(false);
169    for ( Int iList=0 ; iList<2 ; iList++ )
170    {
171      for ( Int iRefIdx=0 ; iRefIdx<MAX_NUM_REF ; iRefIdx++ )
172      {
173        for ( Int iComp=0 ; iComp<3 ;iComp++ )
174        {
175          wpScalingParam  *pwp = &(m_wp[iList][iRefIdx][iComp]);
176          pwp->bPresentFlag      = false;
177          pwp->uiLog2WeightDenom = 0;
178          pwp->iWeight           = 1;
179          pwp->iOffset           = 0;
180        }
181      }
182    }
183    slice->setWpScaling( m_wp );
184  }
185}
186
187/** estimate wp tables for explicit wp
188 * \param TComSlice *slice
189 * \returns Bool
190 */
191Bool  WeightPredAnalysis::xEstimateWPParamSlice(TComSlice *slice)
192{
193  Int iDenom  = 6;
194  Bool validRangeFlag = false;
195
196  if(slice->getNumRefIdx(REF_PIC_LIST_0)>3)
197  {
198    iDenom  = 7;
199  }
200
201  do
202  {
203    validRangeFlag = xUpdatingWPParameters(slice, m_wp, iDenom);
204    if (!validRangeFlag)
205    {
206      iDenom--; // decrement to satisfy the range limitation
207    }
208  } while (validRangeFlag == false);
209
210  // selecting whether WP is used, or not
211  xSelectWP(slice, m_wp, iDenom);
212
213  slice->setWpScaling( m_wp );
214
215  return (true);
216}
217
218/** update wp tables for explicit wp w.r.t ramge limitation
219 * \param TComSlice *slice
220 * \returns Bool
221 */
222Bool WeightPredAnalysis::xUpdatingWPParameters(TComSlice *slice, wpScalingParam weightPredTable[2][MAX_NUM_REF][3], Int log2Denom)
223{
224  Int numPredDir = slice->isInterP() ? 1 : 2;
225  for ( Int refList = 0; refList < numPredDir; refList++ )
226  {
227    RefPicList  eRefPicList = ( refList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
228    for ( Int refIdxTemp = 0; refIdxTemp < slice->getNumRefIdx(eRefPicList); refIdxTemp++ )
229    {
230      wpACDCParam *currWeightACDCParam, *refWeightACDCParam;
231      slice->getWpAcDcParam(currWeightACDCParam);
232      slice->getRefPic(eRefPicList, refIdxTemp)->getSlice(0)->getWpAcDcParam(refWeightACDCParam);
233#if O0194_WEIGHTED_PREDICTION_CGS
234      if (slice->getRefPic(eRefPicList, refIdxTemp)->isILR(slice->getLayerId()))
235      {
236        refWeightACDCParam = (wpACDCParam *)g_refWeightACDCParam;
237      }
238#endif
239
240      for ( Int comp = 0; comp < 3; comp++ )
241      {
242        Int bitDepth = comp ? g_bitDepthC : g_bitDepthY;
243        Int realLog2Denom = log2Denom + bitDepth-8;
244        Int realOffset = ((Int)1<<(realLog2Denom-1));
245
246        // current frame
247        Int64 currDC = currWeightACDCParam[comp].iDC;
248        Int64 currAC = currWeightACDCParam[comp].iAC;
249        // reference frame
250        Int64 refDC = refWeightACDCParam[comp].iDC;
251        Int64 refAC = refWeightACDCParam[comp].iAC;
252#if O0194_WEIGHTED_PREDICTION_CGS
253        if (slice->getRefPic(eRefPicList, refIdxTemp)->isILR(slice->getLayerId()))
254        {
255          refAC = ( refAC * currWeightACDCParam[comp].iSamples ) /refWeightACDCParam[comp].iSamples;
256#if O0194_JOINT_US_BITSHIFT
257          refAC <<= (g_bitDepthYLayer[1]-g_bitDepthYLayer[0]);
258          refDC <<= (g_bitDepthYLayer[1]-g_bitDepthYLayer[0]);
259#endif
260         }
261#endif
262
263        // calculating iWeight and iOffset params
264        Double dWeight = (refAC==0) ? (Double)1.0 : Clip3( -16.0, 15.0, ((Double)currAC / (Double)refAC) );
265        Int weight = (Int)( 0.5 + dWeight * (Double)(1<<log2Denom) );
266        Int offset = (Int)( ((currDC<<log2Denom) - ((Int64)weight * refDC) + (Int64)realOffset) >> realLog2Denom );
267#if O0194_WEIGHTED_PREDICTION_CGS
268        if (slice->getRefPic(eRefPicList, refIdxTemp)->isILR(slice->getLayerId())){
269        }
270        else{
271          dWeight = 1;
272          offset  = 0;
273        }
274        weight = (Int)( 0.5 + dWeight * (Double)(1<<log2Denom) );
275#endif
276
277        // Chroma offset range limitation
278        if(comp)
279        {
280          Int pred = ( 128 - ( ( 128*weight)>>(log2Denom) ) );
281          Int deltaOffset = Clip3( -512, 511, (offset - pred) );    // signed 10bit
282          offset = Clip3( -128, 127, (deltaOffset + pred) );        // signed 8bit
283        }
284        // Luma offset range limitation
285        else
286        {
287          offset = Clip3( -128, 127, offset);
288        }
289
290        // Weighting factor limitation
291        Int defaultWeight = (1<<log2Denom);
292        Int deltaWeight = (defaultWeight - weight);
293        if(deltaWeight > 127 || deltaWeight < -128)
294          return (false);
295#if O0194_WEIGHTED_PREDICTION_CGS
296        // make sure the reference frames other than ILR are not using weighted prediction
297        if (!(slice->getRefPic(eRefPicList, refIdxTemp)->isILR(slice->getLayerId()))){
298          continue;
299        }
300#endif
301
302        m_wp[refList][refIdxTemp][comp].bPresentFlag = true;
303        m_wp[refList][refIdxTemp][comp].iWeight = (Int)weight;
304        m_wp[refList][refIdxTemp][comp].iOffset = (Int)offset;
305        m_wp[refList][refIdxTemp][comp].uiLog2WeightDenom = (Int)log2Denom;
306      }
307    }
308  }
309  return (true);
310}
311
312/** select whether weighted pred enables or not.
313 * \param TComSlice *slice
314 * \param wpScalingParam
315 * \param iDenom
316 * \returns Bool
317 */
318Bool WeightPredAnalysis::xSelectWP(TComSlice *slice, wpScalingParam weightPredTable[2][MAX_NUM_REF][3], Int iDenom)
319{
320  TComPicYuv*   pPic = slice->getPic()->getPicYuvOrg();
321  Int iWidth  = pPic->getWidth();
322  Int iHeight = pPic->getHeight();
323  Int iDefaultWeight = ((Int)1<<iDenom);
324  Int iNumPredDir = slice->isInterP() ? 1 : 2;
325
326  for ( Int iRefList = 0; iRefList < iNumPredDir; iRefList++ )
327  {
328    Int64 iSADWP = 0, iSADnoWP = 0;
329    RefPicList  eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
330    for ( Int iRefIdxTemp = 0; iRefIdxTemp < slice->getNumRefIdx(eRefPicList); iRefIdxTemp++ )
331    {
332      Pel*  pOrg    = pPic->getLumaAddr();
333      Pel*  pRef    = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getLumaAddr();
334      Int   iOrgStride = pPic->getStride();
335      Int   iRefStride = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getStride();
336
337      // calculate SAD costs with/without wp for luma
338      iSADWP   = this->xCalcSADvalueWP(g_bitDepthY, pOrg, pRef, iWidth, iHeight, iOrgStride, iRefStride, iDenom, weightPredTable[iRefList][iRefIdxTemp][0].iWeight, weightPredTable[iRefList][iRefIdxTemp][0].iOffset);
339      iSADnoWP = this->xCalcSADvalueWP(g_bitDepthY, pOrg, pRef, iWidth, iHeight, iOrgStride, iRefStride, iDenom, iDefaultWeight, 0);
340
341      pOrg = pPic->getCbAddr();
342      pRef = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getCbAddr();
343      iOrgStride = pPic->getCStride();
344      iRefStride = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getCStride();
345
346      // calculate SAD costs with/without wp for chroma cb
347      iSADWP   += this->xCalcSADvalueWP(g_bitDepthC, pOrg, pRef, iWidth>>1, iHeight>>1, iOrgStride, iRefStride, iDenom, weightPredTable[iRefList][iRefIdxTemp][1].iWeight, weightPredTable[iRefList][iRefIdxTemp][1].iOffset);
348      iSADnoWP += this->xCalcSADvalueWP(g_bitDepthC, pOrg, pRef, iWidth>>1, iHeight>>1, iOrgStride, iRefStride, iDenom, iDefaultWeight, 0);
349
350      pOrg = pPic->getCrAddr();
351      pRef = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getCrAddr();
352
353      // calculate SAD costs with/without wp for chroma cr
354      iSADWP   += this->xCalcSADvalueWP(g_bitDepthC, pOrg, pRef, iWidth>>1, iHeight>>1, iOrgStride, iRefStride, iDenom, weightPredTable[iRefList][iRefIdxTemp][2].iWeight, weightPredTable[iRefList][iRefIdxTemp][2].iOffset);
355      iSADnoWP += this->xCalcSADvalueWP(g_bitDepthC, pOrg, pRef, iWidth>>1, iHeight>>1, iOrgStride, iRefStride, iDenom, iDefaultWeight, 0);
356
357      Double dRatio = ((Double)iSADWP / (Double)iSADnoWP);
358      if(dRatio >= (Double)DTHRESH)
359      {
360        for ( Int iComp = 0; iComp < 3; iComp++ )
361        {
362          weightPredTable[iRefList][iRefIdxTemp][iComp].bPresentFlag = false;
363          weightPredTable[iRefList][iRefIdxTemp][iComp].iOffset = (Int)0;
364          weightPredTable[iRefList][iRefIdxTemp][iComp].iWeight = (Int)iDefaultWeight;
365          weightPredTable[iRefList][iRefIdxTemp][iComp].uiLog2WeightDenom = (Int)iDenom;
366        }
367      }
368    }
369  }
370  return (true);
371}
372
373/** calculate DC value of original image for luma.
374 * \param TComSlice *slice
375 * \param Pel *pPel
376 * \param Int *iSample
377 * \returns Int64
378 */
379Int64 WeightPredAnalysis::xCalcDCValueSlice(TComSlice *slice, Pel *pPel, Int *iSample)
380{
381  TComPicYuv* pPic = slice->getPic()->getPicYuvOrg();
382  Int iStride = pPic->getStride();
383
384  *iSample = 0;
385  Int iWidth  = pPic->getWidth();
386  Int iHeight = pPic->getHeight();
387  *iSample = iWidth*iHeight;
388  Int64 iDC = xCalcDCValue(pPel, iWidth, iHeight, iStride);
389
390  return (iDC);
391}
392
393/** calculate AC value of original image for luma.
394 * \param TComSlice *slice
395 * \param Pel *pPel
396 * \param Int iDC
397 * \returns Int64
398 */
399Int64 WeightPredAnalysis::xCalcACValueSlice(TComSlice *slice, Pel *pPel, Int64 iDC)
400{
401  TComPicYuv* pPic = slice->getPic()->getPicYuvOrg();
402  Int iStride = pPic->getStride();
403
404  Int iWidth  = pPic->getWidth();
405  Int iHeight = pPic->getHeight();
406  Int64 iAC = xCalcACValue(pPel, iWidth, iHeight, iStride, iDC);
407
408  return (iAC);
409}
410
411/** calculate DC value of original image for chroma.
412 * \param TComSlice *slice
413 * \param Pel *pPel
414 * \param Int *iSample
415 * \returns Int64
416 */
417Int64 WeightPredAnalysis::xCalcDCValueUVSlice(TComSlice *slice, Pel *pPel, Int *iSample)
418{
419  TComPicYuv* pPic = slice->getPic()->getPicYuvOrg();
420  Int iCStride = pPic->getCStride();
421
422  *iSample = 0;
423  Int iWidth  = pPic->getWidth()>>1;
424  Int iHeight = pPic->getHeight()>>1;
425  *iSample = iWidth*iHeight;
426  Int64 iDC = xCalcDCValue(pPel, iWidth, iHeight, iCStride);
427
428  return (iDC);
429}
430
431/** calculate AC value of original image for chroma.
432 * \param TComSlice *slice
433 * \param Pel *pPel
434 * \param Int iDC
435 * \returns Int64
436 */
437Int64 WeightPredAnalysis::xCalcACValueUVSlice(TComSlice *slice, Pel *pPel, Int64 iDC)
438{
439  TComPicYuv* pPic = slice->getPic()->getPicYuvOrg();
440  Int iCStride = pPic->getCStride();
441
442  Int iWidth  = pPic->getWidth()>>1;
443  Int iHeight = pPic->getHeight()>>1;
444  Int64 iAC = xCalcACValue(pPel, iWidth, iHeight, iCStride, iDC);
445
446  return (iAC);
447}
448
449/** calculate DC value.
450 * \param Pel *pPel
451 * \param Int iWidth
452 * \param Int iHeight
453 * \param Int iStride
454 * \returns Int64
455 */
456Int64 WeightPredAnalysis::xCalcDCValue(Pel *pPel, Int iWidth, Int iHeight, Int iStride)
457{
458  Int x, y;
459  Int64 iDC = 0;
460  for( y = 0; y < iHeight; y++ )
461  {
462    for( x = 0; x < iWidth; x++ )
463    {
464      iDC += (Int)( pPel[x] );
465    }
466    pPel += iStride;
467  }
468  return (iDC);
469}
470
471/** calculate AC value.
472 * \param Pel *pPel
473 * \param Int iWidth
474 * \param Int iHeight
475 * \param Int iStride
476 * \param Int iDC
477 * \returns Int64
478 */
479Int64 WeightPredAnalysis::xCalcACValue(Pel *pPel, Int iWidth, Int iHeight, Int iStride, Int64 iDC)
480{
481  Int x, y;
482  Int64 iAC = 0;
483  for( y = 0; y < iHeight; y++ )
484  {
485    for( x = 0; x < iWidth; x++ )
486    {
487      iAC += abs( (Int)pPel[x] - (Int)iDC );
488    }
489    pPel += iStride;
490  }
491  return (iAC);
492}
493
494/** calculate SAD values for both WP version and non-WP version.
495 * \param Pel *pOrgPel
496 * \param Pel *pRefPel
497 * \param Int iWidth
498 * \param Int iHeight
499 * \param Int iOrgStride
500 * \param Int iRefStride
501 * \param Int iDenom
502 * \param Int iWeight
503 * \param Int iOffset
504 * \returns Int64
505 */
506Int64 WeightPredAnalysis::xCalcSADvalueWP(Int bitDepth, Pel *pOrgPel, Pel *pRefPel, Int iWidth, Int iHeight, Int iOrgStride, Int iRefStride, Int iDenom, Int iWeight, Int iOffset)
507{
508  Int x, y;
509  Int64 iSAD = 0;
510  Int64 iSize   = iWidth*iHeight;
511  Int64 iRealDenom = iDenom + bitDepth-8;
512  for( y = 0; y < iHeight; y++ )
513  {
514    for( x = 0; x < iWidth; x++ )
515    {
516      iSAD += ABS(( ((Int64)pOrgPel[x]<<(Int64)iDenom) - ( (Int64)pRefPel[x] * (Int64)iWeight + ((Int64)iOffset<<iRealDenom) ) ) );
517    }
518    pOrgPel += iOrgStride;
519    pRefPel += iRefStride;
520  }
521  return (iSAD/iSize);
522}
523
524
Note: See TracBrowser for help on using the repository browser.