source: 3DVCSoftware/trunk/source/Lib/TLibCommon/TComWeightPrediction.cpp

Last change on this file was 1413, checked in by tech, 6 years ago

Merged HTM-16.2-dev@1412

  • Property svn:eol-style set to native
File size: 15.3 KB
RevLine 
[5]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
[1313]4 * granted under this license.
[5]5 *
[1413]6 * Copyright (c) 2010-2017, ITU/ISO/IEC
[5]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.
[56]17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
[5]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 */
[2]33
34/** \file     TComWeightPrediction.h
35    \brief    weighting prediction class (header)
36*/
37
38// Include files
[1313]39#include "CommonDef.h"
40#include "TComYuv.h"
41#include "TComPic.h"
42#include "TComInterpolationFilter.h"
[2]43#include "TComWeightPrediction.h"
44
[1313]45
46static inline Pel weightBidir( Int w0, Pel P0, Int w1, Pel P1, Int round, Int shift, Int offset, Int clipBD)
[608]47{
[1313]48  return ClipBD( ( (w0*(P0 + IF_INTERNAL_OFFS) + w1*(P1 + IF_INTERNAL_OFFS) + round + (offset << (shift-1))) >> shift ), clipBD );
[608]49}
50
[1313]51
52static inline Pel weightUnidir( Int w0, Pel P0, Int round, Int shift, Int offset, Int clipBD)
[608]53{
[1313]54  return ClipBD( ( (w0*(P0 + IF_INTERNAL_OFFS) + round) >> shift ) + offset, clipBD );
[608]55}
56
[1386]57static inline Pel noWeightUnidir( Pel P0, Int round, Int shift, Int offset, Int clipBD)
58{
59  return ClipBD( ( ((P0 + IF_INTERNAL_OFFS) + round) >> shift ) + offset, clipBD );
60}
61
62static inline Pel noWeightOffsetUnidir( Pel P0, Int round, Int shift, Int clipBD)
63{
64  return ClipBD( ( ((P0 + IF_INTERNAL_OFFS) + round) >> shift ), clipBD );
65}
66
67
[2]68// ====================================================================================================================
69// Class definition
70// ====================================================================================================================
[1313]71
[2]72TComWeightPrediction::TComWeightPrediction()
73{
74}
75
[1313]76
77//! weighted averaging for bi-pred
78Void TComWeightPrediction::addWeightBi( const TComYuv              *pcYuvSrc0,
79                                        const TComYuv              *pcYuvSrc1,
80                                        const BitDepths            &bitDepths,
81                                        const UInt                  iPartUnitIdx,
82                                        const UInt                  uiWidth,
83                                        const UInt                  uiHeight,
84                                        const WPScalingParam *const wp0,
85                                        const WPScalingParam *const wp1,
86                                              TComYuv        *const rpcYuvDst,
87                                        const Bool                  bRoundLuma)
[2]88{
89
[1313]90  const Bool enableRounding[MAX_NUM_COMPONENT]={ bRoundLuma, true, true };
[2]91
[1313]92  const UInt numValidComponent = pcYuvSrc0->getNumberValidComponents();
93
94  for(Int componentIndex=0; componentIndex<numValidComponent; componentIndex++)
[2]95  {
[1313]96    const ComponentID compID=ComponentID(componentIndex);
[56]97
[1313]98    const Pel* pSrc0       = pcYuvSrc0->getAddr( compID,  iPartUnitIdx );
99    const Pel* pSrc1       = pcYuvSrc1->getAddr( compID,  iPartUnitIdx );
100          Pel* pDst        = rpcYuvDst->getAddr( compID,  iPartUnitIdx );
[2]101
[1313]102    // Luma : --------------------------------------------
103    const Int  w0          = wp0[compID].w;
104    const Int  offset      = wp0[compID].offset;
105    const Int  clipBD      = bitDepths.recon[toChannelType(compID)];
106    const Int  shiftNum    = std::max<Int>(2, (IF_INTERNAL_PREC - clipBD));
107    const Int  shift       = wp0[compID].shift + shiftNum;
108    const Int  round       = (enableRounding[compID] && (shift > 0)) ? (1<<(shift-1)) : 0;
109    const Int  w1          = wp1[compID].w;
110    const UInt csx         = pcYuvSrc0->getComponentScaleX(compID);
111    const UInt csy         = pcYuvSrc0->getComponentScaleY(compID);
112    const Int  iHeight     = uiHeight>>csy;
113    const Int  iWidth      = uiWidth>>csx;
[2]114
[1313]115    const UInt iSrc0Stride = pcYuvSrc0->getStride(compID);
116    const UInt iSrc1Stride = pcYuvSrc1->getStride(compID);
117    const UInt iDstStride  = rpcYuvDst->getStride(compID);
[2]118
[1313]119    for ( Int y = iHeight-1; y >= 0; y-- )
[2]120    {
[1313]121      // do it in batches of 4 (partial unroll)
122      Int x = iWidth-1;
123      for ( ; x >= 3; )
124      {
125        pDst[x] = weightBidir(w0,pSrc0[x], w1,pSrc1[x], round, shift, offset, clipBD); x--;
126        pDst[x] = weightBidir(w0,pSrc0[x], w1,pSrc1[x], round, shift, offset, clipBD); x--;
127        pDst[x] = weightBidir(w0,pSrc0[x], w1,pSrc1[x], round, shift, offset, clipBD); x--;
128        pDst[x] = weightBidir(w0,pSrc0[x], w1,pSrc1[x], round, shift, offset, clipBD); x--;
129      }
130      for( ; x >= 0; x-- )
131      {
132        pDst[x] = weightBidir(w0,pSrc0[x], w1,pSrc1[x], round, shift, offset, clipBD);
133      }
134
135      pSrc0 += iSrc0Stride;
136      pSrc1 += iSrc1Stride;
137      pDst  += iDstStride;
138    } // y loop
139  } // compID loop
[2]140}
141
[1313]142
143//! weighted averaging for uni-pred
144Void TComWeightPrediction::addWeightUni( const TComYuv        *const pcYuvSrc0,
145                                         const BitDepths            &bitDepths,
146                                         const UInt                  iPartUnitIdx,
147                                         const UInt                  uiWidth,
148                                         const UInt                  uiHeight,
149                                         const WPScalingParam *const wp0,
150                                               TComYuv        *const pcYuvDst )
[2]151{
[1313]152  const UInt numValidComponent = pcYuvSrc0->getNumberValidComponents();
[2]153
[1313]154  for(Int componentIndex=0; componentIndex<numValidComponent; componentIndex++)
[2]155  {
[1313]156    const ComponentID compID=ComponentID(componentIndex);
[2]157
[1313]158    const Pel* pSrc0       = pcYuvSrc0->getAddr( compID,  iPartUnitIdx );
159          Pel* pDst        = pcYuvDst->getAddr( compID,  iPartUnitIdx );
[2]160
[1313]161    // Luma : --------------------------------------------
162    const Int  w0          = wp0[compID].w;
163    const Int  offset      = wp0[compID].offset;
164    const Int  clipBD      = bitDepths.recon[toChannelType(compID)];
165    const Int  shiftNum    = std::max<Int>(2, (IF_INTERNAL_PREC - clipBD));
166    const Int  shift       = wp0[compID].shift + shiftNum;
167    const UInt iSrc0Stride = pcYuvSrc0->getStride(compID);
168    const UInt iDstStride  = pcYuvDst->getStride(compID);
169    const UInt csx         = pcYuvSrc0->getComponentScaleX(compID);
170    const UInt csy         = pcYuvSrc0->getComponentScaleY(compID);
171    const Int  iHeight     = uiHeight>>csy;
172    const Int  iWidth      = uiWidth>>csx;
173
[1386]174    if (w0 != 1 << wp0[compID].shift)
175    {
176      const Int  round       = (shift > 0) ? (1<<(shift-1)) : 0;
[1313]177    for (Int y = iHeight-1; y >= 0; y-- )
[2]178    {
[1313]179      Int x = iWidth-1;
180      for ( ; x >= 3; )
181      {
182        pDst[x] = weightUnidir(w0, pSrc0[x], round, shift, offset, clipBD); x--;
183        pDst[x] = weightUnidir(w0, pSrc0[x], round, shift, offset, clipBD); x--;
184        pDst[x] = weightUnidir(w0, pSrc0[x], round, shift, offset, clipBD); x--;
185        pDst[x] = weightUnidir(w0, pSrc0[x], round, shift, offset, clipBD); x--;
186      }
187      for( ; x >= 0; x--)
188      {
189        pDst[x] = weightUnidir(w0, pSrc0[x], round, shift, offset, clipBD);
190      }
191      pSrc0 += iSrc0Stride;
192      pDst  += iDstStride;
[56]193    }
[2]194  }
[1386]195    else
196    {
197      const Int  round       = (shiftNum > 0) ? (1<<(shiftNum-1)) : 0;
198      if (offset == 0)
199      {
200        for (Int y = iHeight-1; y >= 0; y-- )
201        {
202          Int x = iWidth-1;
203          for ( ; x >= 3; )
204          {
205            pDst[x] = noWeightOffsetUnidir(pSrc0[x], round, shiftNum, clipBD); x--;
206            pDst[x] = noWeightOffsetUnidir(pSrc0[x], round, shiftNum, clipBD); x--;
207            pDst[x] = noWeightOffsetUnidir(pSrc0[x], round, shiftNum, clipBD); x--;
208            pDst[x] = noWeightOffsetUnidir(pSrc0[x], round, shiftNum, clipBD); x--;
209          }
210          for( ; x >= 0; x--)
211          {
212            pDst[x] = noWeightOffsetUnidir(pSrc0[x], round, shiftNum, clipBD);
213          }
214          pSrc0 += iSrc0Stride;
215          pDst  += iDstStride;
216        }
217      }
218      else
219      {
220        for (Int y = iHeight-1; y >= 0; y-- )
221        {
222          Int x = iWidth-1;
223          for ( ; x >= 3; )
224          {
225            pDst[x] = noWeightUnidir(pSrc0[x], round, shiftNum, offset, clipBD); x--;
226            pDst[x] = noWeightUnidir(pSrc0[x], round, shiftNum, offset, clipBD); x--;
227            pDst[x] = noWeightUnidir(pSrc0[x], round, shiftNum, offset, clipBD); x--;
228            pDst[x] = noWeightUnidir(pSrc0[x], round, shiftNum, offset, clipBD); x--;
229          }
230          for( ; x >= 0; x--)
231          {
232            pDst[x] = noWeightUnidir(pSrc0[x], round, shiftNum, offset, clipBD);
233          }
234          pSrc0 += iSrc0Stride;
235          pDst  += iDstStride;
236        }
237      }
238    }
239  }
[2]240}
241
[1313]242
[2]243//=======================================================
244//  getWpScaling()
245//=======================================================
[1313]246//! derivation of wp tables
247Void TComWeightPrediction::getWpScaling(       TComDataCU *const pcCU,
248                                         const Int               iRefIdx0,
249                                         const Int               iRefIdx1,
250                                               WPScalingParam  *&wp0,
251                                               WPScalingParam  *&wp1)
[2]252{
[608]253  assert(iRefIdx0 >= 0 || iRefIdx1 >= 0);
[2]254
[1313]255        TComSlice *const pcSlice  = pcCU->getSlice();
256  const Bool             wpBiPred = pcCU->getSlice()->getPPS()->getWPBiPred();
[1386]257  const Bool             bBiPred  = (iRefIdx0>=0 && iRefIdx1>=0);
258  const Bool             bUniPred = !bBiPred;
[1313]259
[1386]260  if ( bUniPred || wpBiPred )
[2]261  { // explicit --------------------
262    if ( iRefIdx0 >= 0 )
263    {
264      pcSlice->getWpScaling(REF_PIC_LIST_0, iRefIdx0, wp0);
265    }
266    if ( iRefIdx1 >= 0 )
267    {
268      pcSlice->getWpScaling(REF_PIC_LIST_1, iRefIdx1, wp1);
269    }
270  }
271  else
272  {
273    assert(0);
274  }
275
276  if ( iRefIdx0 < 0 )
277  {
278    wp0 = NULL;
279  }
280  if ( iRefIdx1 < 0 )
281  {
282    wp1 = NULL;
283  }
284
[1313]285  const UInt numValidComponent                    = pcCU->getPic()->getNumberValidComponents();
286  const Bool bUseHighPrecisionPredictionWeighting = pcSlice->getSPS()->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag();
287
[1386]288  if ( bBiPred )
289  { // Bi-predictive case
[1313]290    for ( Int yuv=0 ; yuv<numValidComponent ; yuv++ )
[2]291    {
[1313]292      const Int bitDepth            = pcSlice->getSPS()->getBitDepth(toChannelType(ComponentID(yuv)));
293      const Int offsetScalingFactor = bUseHighPrecisionPredictionWeighting ? 1 : (1 << (bitDepth-8));
294
[2]295      wp0[yuv].w      = wp0[yuv].iWeight;
296      wp1[yuv].w      = wp1[yuv].iWeight;
[1313]297      wp0[yuv].o      = wp0[yuv].iOffset * offsetScalingFactor;
298      wp1[yuv].o      = wp1[yuv].iOffset * offsetScalingFactor;
[56]299      wp0[yuv].offset = wp0[yuv].o + wp1[yuv].o;
[2]300      wp0[yuv].shift  = wp0[yuv].uiLog2WeightDenom + 1;
301      wp0[yuv].round  = (1 << wp0[yuv].uiLog2WeightDenom);
302      wp1[yuv].offset = wp0[yuv].offset;
303      wp1[yuv].shift  = wp0[yuv].shift;
304      wp1[yuv].round  = wp0[yuv].round;
305    }
306  }
307  else
[1386]308  {  // UniPred
[1313]309    WPScalingParam *const pwp = (iRefIdx0>=0) ? wp0 : wp1 ;
310
311    for ( Int yuv=0 ; yuv<numValidComponent ; yuv++ )
[2]312    {
[1313]313      const Int bitDepth            = pcSlice->getSPS()->getBitDepth(toChannelType(ComponentID(yuv)));
314      const Int offsetScalingFactor = bUseHighPrecisionPredictionWeighting ? 1 : (1 << (bitDepth-8));
315
[608]316      pwp[yuv].w      = pwp[yuv].iWeight;
[1313]317      pwp[yuv].offset = pwp[yuv].iOffset * offsetScalingFactor;
[608]318      pwp[yuv].shift  = pwp[yuv].uiLog2WeightDenom;
319      pwp[yuv].round  = (pwp[yuv].uiLog2WeightDenom>=1) ? (1 << (pwp[yuv].uiLog2WeightDenom-1)) : (0);
[2]320    }
321  }
322}
323
[1313]324
325//! weighted prediction for bi-pred
326Void TComWeightPrediction::xWeightedPredictionBi(       TComDataCU *const pcCU,
327                                                  const TComYuv    *const pcYuvSrc0,
328                                                  const TComYuv    *const pcYuvSrc1,
329                                                  const Int               iRefIdx0,
330                                                  const Int               iRefIdx1,
331                                                  const UInt              uiPartIdx,
332                                                  const Int               iWidth,
333                                                  const Int               iHeight,
334                                                        TComYuv          *rpcYuvDst )
[2]335{
[1313]336  WPScalingParam  *pwp0;
337  WPScalingParam  *pwp1;
[2]338
[1313]339  assert(pcCU->getSlice()->getPPS()->getWPBiPred());
340
[608]341  getWpScaling(pcCU, iRefIdx0, iRefIdx1, pwp0, pwp1);
[2]342
343  if( iRefIdx0 >= 0 && iRefIdx1 >= 0 )
344  {
[1313]345    addWeightBi(pcYuvSrc0, pcYuvSrc1, pcCU->getSlice()->getSPS()->getBitDepths(), uiPartIdx, iWidth, iHeight, pwp0, pwp1, rpcYuvDst );
[2]346  }
347  else if ( iRefIdx0 >= 0 && iRefIdx1 <  0 )
348  {
[1313]349    addWeightUni( pcYuvSrc0, pcCU->getSlice()->getSPS()->getBitDepths(), uiPartIdx, iWidth, iHeight, pwp0, rpcYuvDst );
[2]350  }
351  else if ( iRefIdx0 <  0 && iRefIdx1 >= 0 )
352  {
[1313]353    addWeightUni( pcYuvSrc1, pcCU->getSlice()->getSPS()->getBitDepths(), uiPartIdx, iWidth, iHeight, pwp1, rpcYuvDst );
[2]354  }
355  else
[56]356  {
[2]357    assert (0);
[56]358  }
359}
[2]360
[1313]361
362//! weighted prediction for uni-pred
363Void TComWeightPrediction::xWeightedPredictionUni(       TComDataCU *const pcCU,
364                                                   const TComYuv    *const pcYuvSrc,
365                                                   const UInt              uiPartAddr,
366                                                   const Int               iWidth,
367                                                   const Int               iHeight,
368                                                   const RefPicList        eRefPicList,
369                                                         TComYuv          *pcYuvPred,
370                                                   const Int               iRefIdx_input)
371{
372  WPScalingParam  *pwp, *pwpTmp;
373
374  Int iRefIdx=iRefIdx_input;
[56]375  if ( iRefIdx < 0 )
376  {
377    iRefIdx   = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );
378  }
379  assert (iRefIdx >= 0);
380
381  if ( eRefPicList == REF_PIC_LIST_0 )
382  {
[608]383    getWpScaling(pcCU, iRefIdx, -1, pwp, pwpTmp);
[56]384  }
385  else
386  {
[608]387    getWpScaling(pcCU, -1, iRefIdx, pwpTmp, pwp);
[56]388  }
[1313]389  addWeightUni( pcYuvSrc, pcCU->getSlice()->getSPS()->getBitDepths(), uiPartAddr, iWidth, iHeight, pwp, pcYuvPred );
[2]390}
Note: See TracBrowser for help on using the repository browser.