source: 3DVCSoftware/branches/HTM-13.1-dev2-Sony/source/Lib/TLibEncoder/WeightPredAnalysis.cpp @ 1154

Last change on this file since 1154 was 872, checked in by tech, 11 years ago

Merged HTM-10.0-dev0@871. (MV-HEVC 7 HLS)

  • Property svn:eol-style set to native
File size: 15.5 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-2014, 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 limitation
238        if(comp)
239        {
240          Int pred = ( 128 - ( ( 128*weight)>>(log2Denom) ) );
241          Int deltaOffset = Clip3( -512, 511, (offset - pred) );    // signed 10bit
242          offset = Clip3( -128, 127, (deltaOffset + pred) );        // signed 8bit
243        }
244        // Luma offset range limitation
245        else
246        {
247          offset = Clip3( -128, 127, offset);
248        }
249
250        // Weighting factor limitation
251        Int defaultWeight = (1<<log2Denom);
252        Int deltaWeight = (defaultWeight - weight);
253        if(deltaWeight > 127 || deltaWeight < -128)
254          return (false);
255
256        m_wp[refList][refIdxTemp][comp].bPresentFlag = true;
257        m_wp[refList][refIdxTemp][comp].iWeight = (Int)weight;
258        m_wp[refList][refIdxTemp][comp].iOffset = (Int)offset;
259        m_wp[refList][refIdxTemp][comp].uiLog2WeightDenom = (Int)log2Denom;
260      }
261    }
262  }
263  return (true);
264}
265
266/** select whether weighted pred enables or not.
267 * \param TComSlice *slice
268 * \param wpScalingParam
269 * \param iDenom
270 * \returns Bool
271 */
272Bool WeightPredAnalysis::xSelectWP(TComSlice *slice, wpScalingParam weightPredTable[2][MAX_NUM_REF][3], Int iDenom)
273{
274  TComPicYuv*   pPic = slice->getPic()->getPicYuvOrg();
275  Int iWidth  = pPic->getWidth();
276  Int iHeight = pPic->getHeight();
277  Int iDefaultWeight = ((Int)1<<iDenom);
278  Int iNumPredDir = slice->isInterP() ? 1 : 2;
279
280  for ( Int iRefList = 0; iRefList < iNumPredDir; iRefList++ )
281  {
282    Int64 iSADWP = 0, iSADnoWP = 0;
283    RefPicList  eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
284    for ( Int iRefIdxTemp = 0; iRefIdxTemp < slice->getNumRefIdx(eRefPicList); iRefIdxTemp++ )
285    {
286      Pel*  pOrg    = pPic->getLumaAddr();
287      Pel*  pRef    = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getLumaAddr();
288      Int   iOrgStride = pPic->getStride();
289      Int   iRefStride = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getStride();
290
291      // calculate SAD costs with/without wp for luma
292      iSADWP   = this->xCalcSADvalueWP(g_bitDepthY, pOrg, pRef, iWidth, iHeight, iOrgStride, iRefStride, iDenom, weightPredTable[iRefList][iRefIdxTemp][0].iWeight, weightPredTable[iRefList][iRefIdxTemp][0].iOffset);
293      iSADnoWP = this->xCalcSADvalueWP(g_bitDepthY, pOrg, pRef, iWidth, iHeight, iOrgStride, iRefStride, iDenom, iDefaultWeight, 0);
294
295      pOrg = pPic->getCbAddr();
296      pRef = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getCbAddr();
297      iOrgStride = pPic->getCStride();
298      iRefStride = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getCStride();
299
300      // calculate SAD costs with/without wp for chroma cb
301      iSADWP   += this->xCalcSADvalueWP(g_bitDepthC, pOrg, pRef, iWidth>>1, iHeight>>1, iOrgStride, iRefStride, iDenom, weightPredTable[iRefList][iRefIdxTemp][1].iWeight, weightPredTable[iRefList][iRefIdxTemp][1].iOffset);
302      iSADnoWP += this->xCalcSADvalueWP(g_bitDepthC, pOrg, pRef, iWidth>>1, iHeight>>1, iOrgStride, iRefStride, iDenom, iDefaultWeight, 0);
303
304      pOrg = pPic->getCrAddr();
305      pRef = slice->getRefPic(eRefPicList, iRefIdxTemp)->getPicYuvRec()->getCrAddr();
306
307      // calculate SAD costs with/without wp for chroma cr
308      iSADWP   += this->xCalcSADvalueWP(g_bitDepthC, pOrg, pRef, iWidth>>1, iHeight>>1, iOrgStride, iRefStride, iDenom, weightPredTable[iRefList][iRefIdxTemp][2].iWeight, weightPredTable[iRefList][iRefIdxTemp][2].iOffset);
309      iSADnoWP += this->xCalcSADvalueWP(g_bitDepthC, pOrg, pRef, iWidth>>1, iHeight>>1, iOrgStride, iRefStride, iDenom, iDefaultWeight, 0);
310
311      Double dRatio = ((Double)iSADWP / (Double)iSADnoWP);
312      if(dRatio >= (Double)DTHRESH)
313      {
314        for ( Int iComp = 0; iComp < 3; iComp++ )
315        {
316          weightPredTable[iRefList][iRefIdxTemp][iComp].bPresentFlag = false;
317          weightPredTable[iRefList][iRefIdxTemp][iComp].iOffset = (Int)0;
318          weightPredTable[iRefList][iRefIdxTemp][iComp].iWeight = (Int)iDefaultWeight;
319          weightPredTable[iRefList][iRefIdxTemp][iComp].uiLog2WeightDenom = (Int)iDenom;
320        }
321      }
322    }
323  }
324  return (true);
325}
326
327/** calculate DC value of original image for luma.
328 * \param TComSlice *slice
329 * \param Pel *pPel
330 * \param Int *iSample
331 * \returns Int64
332 */
333Int64 WeightPredAnalysis::xCalcDCValueSlice(TComSlice *slice, Pel *pPel, Int *iSample)
334{
335  TComPicYuv* pPic = slice->getPic()->getPicYuvOrg();
336  Int iStride = pPic->getStride();
337
338  *iSample = 0;
339  Int iWidth  = pPic->getWidth();
340  Int iHeight = pPic->getHeight();
341  *iSample = iWidth*iHeight;
342  Int64 iDC = xCalcDCValue(pPel, iWidth, iHeight, iStride);
343
344  return (iDC);
345}
346
347/** calculate AC value of original image for luma.
348 * \param TComSlice *slice
349 * \param Pel *pPel
350 * \param Int iDC
351 * \returns Int64
352 */
353Int64 WeightPredAnalysis::xCalcACValueSlice(TComSlice *slice, Pel *pPel, Int64 iDC)
354{
355  TComPicYuv* pPic = slice->getPic()->getPicYuvOrg();
356  Int iStride = pPic->getStride();
357
358  Int iWidth  = pPic->getWidth();
359  Int iHeight = pPic->getHeight();
360  Int64 iAC = xCalcACValue(pPel, iWidth, iHeight, iStride, iDC);
361
362  return (iAC);
363}
364
365/** calculate DC value of original image for chroma.
366 * \param TComSlice *slice
367 * \param Pel *pPel
368 * \param Int *iSample
369 * \returns Int64
370 */
371Int64 WeightPredAnalysis::xCalcDCValueUVSlice(TComSlice *slice, Pel *pPel, Int *iSample)
372{
373  TComPicYuv* pPic = slice->getPic()->getPicYuvOrg();
374  Int iCStride = pPic->getCStride();
375
376  *iSample = 0;
377  Int iWidth  = pPic->getWidth()>>1;
378  Int iHeight = pPic->getHeight()>>1;
379  *iSample = iWidth*iHeight;
380  Int64 iDC = xCalcDCValue(pPel, iWidth, iHeight, iCStride);
381
382  return (iDC);
383}
384
385/** calculate AC value of original image for chroma.
386 * \param TComSlice *slice
387 * \param Pel *pPel
388 * \param Int iDC
389 * \returns Int64
390 */
391Int64 WeightPredAnalysis::xCalcACValueUVSlice(TComSlice *slice, Pel *pPel, Int64 iDC)
392{
393  TComPicYuv* pPic = slice->getPic()->getPicYuvOrg();
394  Int iCStride = pPic->getCStride();
395
396  Int iWidth  = pPic->getWidth()>>1;
397  Int iHeight = pPic->getHeight()>>1;
398  Int64 iAC = xCalcACValue(pPel, iWidth, iHeight, iCStride, iDC);
399
400  return (iAC);
401}
402
403/** calculate DC value.
404 * \param Pel *pPel
405 * \param Int iWidth
406 * \param Int iHeight
407 * \param Int iStride
408 * \returns Int64
409 */
410Int64 WeightPredAnalysis::xCalcDCValue(Pel *pPel, Int iWidth, Int iHeight, Int iStride)
411{
412  Int x, y;
413  Int64 iDC = 0;
414  for( y = 0; y < iHeight; y++ )
415  {
416    for( x = 0; x < iWidth; x++ )
417    {
418      iDC += (Int)( pPel[x] );
419    }
420    pPel += iStride;
421  }
422  return (iDC);
423}
424
425/** calculate AC value.
426 * \param Pel *pPel
427 * \param Int iWidth
428 * \param Int iHeight
429 * \param Int iStride
430 * \param Int iDC
431 * \returns Int64
432 */
433Int64 WeightPredAnalysis::xCalcACValue(Pel *pPel, Int iWidth, Int iHeight, Int iStride, Int64 iDC)
434{
435  Int x, y;
436  Int64 iAC = 0;
437  for( y = 0; y < iHeight; y++ )
438  {
439    for( x = 0; x < iWidth; x++ )
440    {
441      iAC += abs( (Int)pPel[x] - (Int)iDC );
442    }
443    pPel += iStride;
444  }
445  return (iAC);
446}
447
448/** calculate SAD values for both WP version and non-WP version.
449 * \param Pel *pOrgPel
450 * \param Pel *pRefPel
451 * \param Int iWidth
452 * \param Int iHeight
453 * \param Int iOrgStride
454 * \param Int iRefStride
455 * \param Int iDenom
456 * \param Int iWeight
457 * \param Int iOffset
458 * \returns Int64
459 */
460Int64 WeightPredAnalysis::xCalcSADvalueWP(Int bitDepth, Pel *pOrgPel, Pel *pRefPel, Int iWidth, Int iHeight, Int iOrgStride, Int iRefStride, Int iDenom, Int iWeight, Int iOffset)
461{
462  Int x, y;
463  Int64 iSAD = 0;
464  Int64 iSize   = iWidth*iHeight;
465  Int64 iRealDenom = iDenom + bitDepth-8;
466  for( y = 0; y < iHeight; y++ )
467  {
468    for( x = 0; x < iWidth; x++ )
469    {
470      iSAD += ABS(( ((Int64)pOrgPel[x]<<(Int64)iDenom) - ( (Int64)pRefPel[x] * (Int64)iWeight + ((Int64)iOffset<<iRealDenom) ) ) );
471    }
472    pOrgPel += iOrgStride;
473    pRefPel += iRefStride;
474  }
475  return (iSAD/iSize);
476}
477
478
Note: See TracBrowser for help on using the repository browser.