source: 3DVCSoftware/branches/HTM-DEV-0.1-dev/source/Lib/TLibEncoder/WeightPredAnalysis.cpp @ 331

Last change on this file since 331 was 324, checked in by tech, 12 years ago

Initial development version for update to latest HM version.
Includes MV-HEVC and basic extensions for 3D-HEVC.

  • Property svn:eol-style set to native
File size: 15.4 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
77  // calculate DC/AC value for Y
78  Pel*  pOrg    = pPic->getLumaAddr();
79  Int64  iOrgDCY = xCalcDCValueSlice(slice, pOrg, &iSample);
80  Int64  iOrgNormDCY = ((iOrgDCY+(iSample>>1)) / iSample);
81  pOrg = pPic->getLumaAddr();
82  Int64  iOrgACY  = xCalcACValueSlice(slice, pOrg, iOrgNormDCY);
83
84  // calculate DC/AC value for Cb
85  pOrg = pPic->getCbAddr();
86  Int64  iOrgDCCb = xCalcDCValueUVSlice(slice, pOrg, &iSample);
87  Int64  iOrgNormDCCb = ((iOrgDCCb+(iSample>>1)) / (iSample));
88  pOrg = pPic->getCbAddr();
89  Int64  iOrgACCb  = xCalcACValueUVSlice(slice, pOrg, iOrgNormDCCb);
90
91  // calculate DC/AC value for Cr
92  pOrg = pPic->getCrAddr();
93  Int64  iOrgDCCr = xCalcDCValueUVSlice(slice, pOrg, &iSample);
94  Int64  iOrgNormDCCr = ((iOrgDCCr+(iSample>>1)) / (iSample));
95  pOrg = pPic->getCrAddr();
96  Int64  iOrgACCr  = xCalcACValueUVSlice(slice, pOrg, iOrgNormDCCr);
97
98  wpACDCParam weightACDCParam[3];
99  weightACDCParam[0].iAC = iOrgACY;
100  weightACDCParam[0].iDC = iOrgNormDCY;
101  weightACDCParam[1].iAC = iOrgACCb;
102  weightACDCParam[1].iDC = iOrgNormDCCb;
103  weightACDCParam[2].iAC = iOrgACCr;
104  weightACDCParam[2].iDC = iOrgNormDCCr;
105
106  slice->setWpAcDcParam(weightACDCParam);
107  return (true);
108}
109
110/** store weighted_pred_flag and weighted_bipred_idc values
111 * \param weighted_pred_flag
112 * \param weighted_bipred_idc
113 * \returns Void
114 */
115Void  WeightPredAnalysis::xStoreWPparam(Bool weighted_pred_flag, Bool weighted_bipred_flag)
116{
117  m_weighted_pred_flag = weighted_pred_flag;
118  m_weighted_bipred_flag = weighted_bipred_flag;
119}
120
121/** restore weighted_pred_flag and weighted_bipred_idc values
122 * \param TComSlice *slice
123 * \returns Void
124 */
125Void  WeightPredAnalysis::xRestoreWPparam(TComSlice *slice)
126{
127  slice->getPPS()->setUseWP(m_weighted_pred_flag);
128  slice->getPPS()->setWPBiPred(m_weighted_bipred_flag);
129}
130
131/** check weighted pred or non-weighted pred
132 * \param TComSlice *slice
133 * \returns Void
134 */
135Void  WeightPredAnalysis::xCheckWPEnable(TComSlice *slice)
136{
137  Int iPresentCnt = 0;
138  for ( Int iList=0 ; iList<2 ; iList++ )
139  {
140    for ( Int iRefIdx=0 ; iRefIdx<MAX_NUM_REF ; iRefIdx++ ) 
141    {
142      for ( Int iComp=0 ; iComp<3 ;iComp++ ) 
143      {
144        wpScalingParam  *pwp = &(m_wp[iList][iRefIdx][iComp]);
145        iPresentCnt += (Int)pwp->bPresentFlag;
146      }
147    }
148  }
149
150  if(iPresentCnt==0)
151  {
152    slice->getPPS()->setUseWP(false);
153    slice->getPPS()->setWPBiPred(false);
154    for ( Int iList=0 ; iList<2 ; iList++ )
155    {
156      for ( Int iRefIdx=0 ; iRefIdx<MAX_NUM_REF ; iRefIdx++ ) 
157      {
158        for ( Int iComp=0 ; iComp<3 ;iComp++ ) 
159        {
160          wpScalingParam  *pwp = &(m_wp[iList][iRefIdx][iComp]);
161          pwp->bPresentFlag      = false;
162          pwp->uiLog2WeightDenom = 0;
163          pwp->iWeight           = 1;
164          pwp->iOffset           = 0;
165        }
166      }
167    }
168    slice->setWpScaling( m_wp );
169  }
170}
171
172/** estimate wp tables for explicit wp
173 * \param TComSlice *slice
174 * \returns Bool
175 */
176Bool  WeightPredAnalysis::xEstimateWPParamSlice(TComSlice *slice)
177{
178  Int iDenom  = 6;
179  Bool validRangeFlag = false;
180
181  if(slice->getNumRefIdx(REF_PIC_LIST_0)>3)
182  {
183    iDenom  = 7;
184  }
185
186  do
187  {
188    validRangeFlag = xUpdatingWPParameters(slice, m_wp, iDenom);
189    if (!validRangeFlag)
190    {
191      iDenom--; // decrement to satisfy the range limitation
192    }
193  } while (validRangeFlag == false);
194
195  // selecting whether WP is used, or not
196  xSelectWP(slice, m_wp, iDenom);
197 
198  slice->setWpScaling( m_wp );
199
200  return (true);
201}
202
203/** update wp tables for explicit wp w.r.t ramge limitation
204 * \param TComSlice *slice
205 * \returns Bool
206 */
207Bool WeightPredAnalysis::xUpdatingWPParameters(TComSlice *slice, wpScalingParam weightPredTable[2][MAX_NUM_REF][3], Int log2Denom)
208{
209  Int numPredDir = slice->isInterP() ? 1 : 2;
210  for ( Int refList = 0; refList < numPredDir; refList++ )
211  {
212    RefPicList  eRefPicList = ( refList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
213    for ( Int refIdxTemp = 0; refIdxTemp < slice->getNumRefIdx(eRefPicList); refIdxTemp++ )
214    {
215      wpACDCParam *currWeightACDCParam, *refWeightACDCParam;
216      slice->getWpAcDcParam(currWeightACDCParam);
217      slice->getRefPic(eRefPicList, refIdxTemp)->getSlice(0)->getWpAcDcParam(refWeightACDCParam);
218
219      for ( Int comp = 0; comp < 3; comp++ )
220      {
221        Int bitDepth = comp ? g_bitDepthC : g_bitDepthY;
222        Int realLog2Denom = log2Denom + bitDepth-8;
223        Int realOffset = ((Int)1<<(realLog2Denom-1));
224
225        // current frame
226        Int64 currDC = currWeightACDCParam[comp].iDC;
227        Int64 currAC = currWeightACDCParam[comp].iAC;
228        // reference frame
229        Int64 refDC = refWeightACDCParam[comp].iDC;
230        Int64 refAC = refWeightACDCParam[comp].iAC;
231
232        // calculating iWeight and iOffset params
233        Double dWeight = (refAC==0) ? (Double)1.0 : Clip3( -16.0, 15.0, ((Double)currAC / (Double)refAC) );
234        Int weight = (Int)( 0.5 + dWeight * (Double)(1<<log2Denom) );
235        Int offset = (Int)( ((currDC<<log2Denom) - ((Int64)weight * refDC) + (Int64)realOffset) >> realLog2Denom );
236
237        // Chroma offset range limination
238        if(comp)
239        {
240          Int shift = 1 << (g_bitDepthC - 1);
241          Int pred = ( shift - ( ( shift*weight)>>(log2Denom) ) );
242          Int deltaOffset = Clip3( -512, 511, (offset - pred) );    // signed 10bit
243          offset = Clip3( -128, 127, (deltaOffset + pred) );        // signed 8bit
244        }
245
246        // Weighting factor limitation
247        Int defaultWeight = (1<<log2Denom);
248        Int deltaWeight = (defaultWeight - weight);
249        if(deltaWeight > 127 || deltaWeight < -128)
250          return (false);
251
252        m_wp[refList][refIdxTemp][comp].bPresentFlag = true;
253        m_wp[refList][refIdxTemp][comp].iWeight = (Int)weight;
254        m_wp[refList][refIdxTemp][comp].iOffset = (Int)offset;
255        m_wp[refList][refIdxTemp][comp].uiLog2WeightDenom = (Int)log2Denom;
256      }
257    }
258  }
259  return (true);
260}
261
262/** select whether weighted pred enables or not.
263 * \param TComSlice *slice
264 * \param wpScalingParam
265 * \param iDenom
266 * \returns Bool
267 */
268Bool WeightPredAnalysis::xSelectWP(TComSlice *slice, wpScalingParam weightPredTable[2][MAX_NUM_REF][3], Int iDenom)
269{
270  TComPicYuv*   pPic = slice->getPic()->getPicYuvOrg();
271  Int iWidth  = pPic->getWidth();
272  Int iHeight = pPic->getHeight();
273  Int iDefaultWeight = ((Int)1<<iDenom);
274  Int iNumPredDir = slice->isInterP() ? 1 : 2;
275
276  for ( Int iRefList = 0; iRefList < iNumPredDir; iRefList++ )
277  {
278    Int64 iSADWP = 0, iSADnoWP = 0;
279    RefPicList  eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
280    for ( Int iRefIdxTemp = 0; iRefIdxTemp < slice->getNumRefIdx(eRefPicList); iRefIdxTemp++ )
281    {
282      Pel*  pOrg    = pPic->getLumaAddr();
283      Pel*  pRef    = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getLumaAddr();
284      Int   iOrgStride = pPic->getStride();
285      Int   iRefStride = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getStride();
286
287      // calculate SAD costs with/without wp for luma
288      iSADWP   = this->xCalcSADvalueWP(g_bitDepthY, pOrg, pRef, iWidth, iHeight, iOrgStride, iRefStride, iDenom, weightPredTable[iRefList][iRefIdxTemp][0].iWeight, weightPredTable[iRefList][iRefIdxTemp][0].iOffset);
289      iSADnoWP = this->xCalcSADvalueWP(g_bitDepthY, pOrg, pRef, iWidth, iHeight, iOrgStride, iRefStride, iDenom, iDefaultWeight, 0);
290
291      pOrg = pPic->getCbAddr();
292      pRef = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getCbAddr();
293      iOrgStride = pPic->getCStride();
294      iRefStride = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getCStride();
295
296      // calculate SAD costs with/without wp for chroma cb
297      iSADWP   += this->xCalcSADvalueWP(g_bitDepthC, pOrg, pRef, iWidth>>1, iHeight>>1, iOrgStride, iRefStride, iDenom, weightPredTable[iRefList][iRefIdxTemp][1].iWeight, weightPredTable[iRefList][iRefIdxTemp][1].iOffset);
298      iSADnoWP += this->xCalcSADvalueWP(g_bitDepthC, pOrg, pRef, iWidth>>1, iHeight>>1, iOrgStride, iRefStride, iDenom, iDefaultWeight, 0);
299
300      pOrg = pPic->getCrAddr();
301      pRef = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getCrAddr();
302
303      // calculate SAD costs with/without wp for chroma cr
304      iSADWP   += this->xCalcSADvalueWP(g_bitDepthC, pOrg, pRef, iWidth>>1, iHeight>>1, iOrgStride, iRefStride, iDenom, weightPredTable[iRefList][iRefIdxTemp][2].iWeight, weightPredTable[iRefList][iRefIdxTemp][2].iOffset);
305      iSADnoWP += this->xCalcSADvalueWP(g_bitDepthC, pOrg, pRef, iWidth>>1, iHeight>>1, iOrgStride, iRefStride, iDenom, iDefaultWeight, 0);
306
307      Double dRatio = ((Double)iSADWP / (Double)iSADnoWP);
308      if(dRatio >= (Double)DTHRESH)
309      {
310        for ( Int iComp = 0; iComp < 3; iComp++ )
311        {
312          weightPredTable[iRefList][iRefIdxTemp][iComp].bPresentFlag = false;
313          weightPredTable[iRefList][iRefIdxTemp][iComp].iOffset = (Int)0;
314          weightPredTable[iRefList][iRefIdxTemp][iComp].iWeight = (Int)iDefaultWeight;
315          weightPredTable[iRefList][iRefIdxTemp][iComp].uiLog2WeightDenom = (Int)iDenom;
316        }
317      }
318    }
319  }
320  return (true);
321}
322
323/** calculate DC value of original image for luma.
324 * \param TComSlice *slice
325 * \param Pel *pPel
326 * \param Int *iSample
327 * \returns Int64
328 */
329Int64 WeightPredAnalysis::xCalcDCValueSlice(TComSlice *slice, Pel *pPel, Int *iSample)
330{
331  TComPicYuv* pPic = slice->getPic()->getPicYuvOrg();
332  Int iStride = pPic->getStride();
333
334  *iSample = 0;
335  Int iWidth  = pPic->getWidth();
336  Int iHeight = pPic->getHeight();
337  *iSample = iWidth*iHeight;
338  Int64 iDC = xCalcDCValue(pPel, iWidth, iHeight, iStride);
339
340  return (iDC);
341}
342
343/** calculate AC value of original image for luma.
344 * \param TComSlice *slice
345 * \param Pel *pPel
346 * \param Int iDC
347 * \returns Int64
348 */
349Int64 WeightPredAnalysis::xCalcACValueSlice(TComSlice *slice, Pel *pPel, Int64 iDC)
350{
351  TComPicYuv* pPic = slice->getPic()->getPicYuvOrg();
352  Int iStride = pPic->getStride();
353
354  Int iWidth  = pPic->getWidth();
355  Int iHeight = pPic->getHeight();
356  Int64 iAC = xCalcACValue(pPel, iWidth, iHeight, iStride, iDC);
357
358  return (iAC);
359}
360
361/** calculate DC value of original image for chroma.
362 * \param TComSlice *slice
363 * \param Pel *pPel
364 * \param Int *iSample
365 * \returns Int64
366 */
367Int64 WeightPredAnalysis::xCalcDCValueUVSlice(TComSlice *slice, Pel *pPel, Int *iSample)
368{
369  TComPicYuv* pPic = slice->getPic()->getPicYuvOrg();
370  Int iCStride = pPic->getCStride();
371
372  *iSample = 0;
373  Int iWidth  = pPic->getWidth()>>1;
374  Int iHeight = pPic->getHeight()>>1;
375  *iSample = iWidth*iHeight;
376  Int64 iDC = xCalcDCValue(pPel, iWidth, iHeight, iCStride);
377
378  return (iDC);
379}
380
381/** calculate AC value of original image for chroma.
382 * \param TComSlice *slice
383 * \param Pel *pPel
384 * \param Int iDC
385 * \returns Int64
386 */
387Int64 WeightPredAnalysis::xCalcACValueUVSlice(TComSlice *slice, Pel *pPel, Int64 iDC)
388{
389  TComPicYuv* pPic = slice->getPic()->getPicYuvOrg();
390  Int iCStride = pPic->getCStride();
391
392  Int iWidth  = pPic->getWidth()>>1;
393  Int iHeight = pPic->getHeight()>>1;
394  Int64 iAC = xCalcACValue(pPel, iWidth, iHeight, iCStride, iDC);
395
396  return (iAC);
397}
398
399/** calculate DC value.
400 * \param Pel *pPel
401 * \param Int iWidth
402 * \param Int iHeight
403 * \param Int iStride
404 * \returns Int64
405 */
406Int64 WeightPredAnalysis::xCalcDCValue(Pel *pPel, Int iWidth, Int iHeight, Int iStride)
407{
408  Int x, y;
409  Int64 iDC = 0;
410  for( y = 0; y < iHeight; y++ )
411  {
412    for( x = 0; x < iWidth; x++ )
413    {
414      iDC += (Int)( pPel[x] );
415    }
416    pPel += iStride;
417  }
418  return (iDC);
419}
420
421/** calculate AC value.
422 * \param Pel *pPel
423 * \param Int iWidth
424 * \param Int iHeight
425 * \param Int iStride
426 * \param Int iDC
427 * \returns Int64
428 */
429Int64 WeightPredAnalysis::xCalcACValue(Pel *pPel, Int iWidth, Int iHeight, Int iStride, Int64 iDC)
430{
431  Int x, y;
432  Int64 iAC = 0;
433  for( y = 0; y < iHeight; y++ )
434  {
435    for( x = 0; x < iWidth; x++ )
436    {
437      iAC += abs( (Int)pPel[x] - (Int)iDC );
438    }
439    pPel += iStride;
440  }
441  return (iAC);
442}
443
444/** calculate SAD values for both WP version and non-WP version.
445 * \param Pel *pOrgPel
446 * \param Pel *pRefPel
447 * \param Int iWidth
448 * \param Int iHeight
449 * \param Int iOrgStride
450 * \param Int iRefStride
451 * \param Int iDenom
452 * \param Int iWeight
453 * \param Int iOffset
454 * \returns Int64
455 */
456Int64 WeightPredAnalysis::xCalcSADvalueWP(Int bitDepth, Pel *pOrgPel, Pel *pRefPel, Int iWidth, Int iHeight, Int iOrgStride, Int iRefStride, Int iDenom, Int iWeight, Int iOffset)
457{
458  Int x, y;
459  Int64 iSAD = 0;
460  Int64 iSize   = iWidth*iHeight;
461  Int64 iRealDenom = iDenom + bitDepth-8;
462  for( y = 0; y < iHeight; y++ )
463  {
464    for( x = 0; x < iWidth; x++ )
465    {
466      iSAD += ABS(( ((Int64)pOrgPel[x]<<(Int64)iDenom) - ( (Int64)pRefPel[x] * (Int64)iWeight + ((Int64)iOffset<<iRealDenom) ) ) );
467    }
468    pOrgPel += iOrgStride;
469    pRefPel += iRefStride;
470  }
471  return (iSAD/iSize);
472}
473
474
Note: See TracBrowser for help on using the repository browser.