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

Last change on this file since 466 was 466, checked in by nokia, 12 years ago

Integration of O0194: Support different bit-depth values for different layers, enable weighted prediction for ILR for color gamut scalability.

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