source: 3DVCSoftware/branches/HTM-DEV-0.2-dev/source/Lib/TLibCommon/TComWeightPrediction.cpp @ 456

Last change on this file since 456 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: 13.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     TComWeightPrediction.h
35    \brief    weighting prediction class (header)
36*/
37
38// Include files
39#include "TComSlice.h"
40#include "TComWeightPrediction.h"
41#include "TComInterpolationFilter.h"
42
43static inline Pel weightBidirY( Int w0, Pel P0, Int w1, Pel P1, Int round, Int shift, Int offset)
44{
45  return ClipY( ( (w0*(P0 + IF_INTERNAL_OFFS) + w1*(P1 + IF_INTERNAL_OFFS) + round + (offset << (shift-1))) >> shift ) );
46}
47static inline Pel weightBidirC( Int w0, Pel P0, Int w1, Pel P1, Int round, Int shift, Int offset)
48{
49  return ClipC( ( (w0*(P0 + IF_INTERNAL_OFFS) + w1*(P1 + IF_INTERNAL_OFFS) + round + (offset << (shift-1))) >> shift ) );
50}
51
52static inline Pel weightUnidirY( Int w0, Pel P0, Int round, Int shift, Int offset)
53{
54  return ClipY( ( (w0*(P0 + IF_INTERNAL_OFFS) + round) >> shift ) + offset );
55}
56static inline Pel weightUnidirC( Int w0, Pel P0, Int round, Int shift, Int offset)
57{
58  return ClipC( ( (w0*(P0 + IF_INTERNAL_OFFS) + round) >> shift ) + offset );
59}
60
61// ====================================================================================================================
62// Class definition
63// ====================================================================================================================
64TComWeightPrediction::TComWeightPrediction()
65{
66}
67
68/** weighted averaging for bi-pred
69 * \param TComYuv* pcYuvSrc0
70 * \param TComYuv* pcYuvSrc1
71 * \param iPartUnitIdx
72 * \param iWidth
73 * \param iHeight
74 * \param wpScalingParam *wp0
75 * \param wpScalingParam *wp1
76 * \param TComYuv* rpcYuvDst
77 * \returns Void
78 */
79Void TComWeightPrediction::addWeightBi( TComYuv* pcYuvSrc0, TComYuv* pcYuvSrc1, UInt iPartUnitIdx, UInt iWidth, UInt iHeight, wpScalingParam *wp0, wpScalingParam *wp1, TComYuv* rpcYuvDst, Bool bRound )
80{
81  Int x, y;
82
83  Pel* pSrcY0  = pcYuvSrc0->getLumaAddr( iPartUnitIdx );
84  Pel* pSrcU0  = pcYuvSrc0->getCbAddr  ( iPartUnitIdx );
85  Pel* pSrcV0  = pcYuvSrc0->getCrAddr  ( iPartUnitIdx );
86 
87  Pel* pSrcY1  = pcYuvSrc1->getLumaAddr( iPartUnitIdx );
88  Pel* pSrcU1  = pcYuvSrc1->getCbAddr  ( iPartUnitIdx );
89  Pel* pSrcV1  = pcYuvSrc1->getCrAddr  ( iPartUnitIdx );
90 
91  Pel* pDstY   = rpcYuvDst->getLumaAddr( iPartUnitIdx );
92  Pel* pDstU   = rpcYuvDst->getCbAddr  ( iPartUnitIdx );
93  Pel* pDstV   = rpcYuvDst->getCrAddr  ( iPartUnitIdx );
94 
95  // Luma : --------------------------------------------
96  Int w0      = wp0[0].w;
97  Int offset  = wp0[0].offset;
98  Int shiftNum = IF_INTERNAL_PREC - g_bitDepthY;
99  Int shift   = wp0[0].shift + shiftNum;
100  Int round   = shift?(1<<(shift-1)) * bRound:0;
101  Int w1      = wp1[0].w;
102
103  UInt  iSrc0Stride = pcYuvSrc0->getStride();
104  UInt  iSrc1Stride = pcYuvSrc1->getStride();
105  UInt  iDstStride  = rpcYuvDst->getStride();
106  for ( y = iHeight-1; y >= 0; y-- )
107  {
108    for ( x = iWidth-1; x >= 0; )
109    {
110      // note: luma min width is 4
111      pDstY[x] = weightBidirY(w0,pSrcY0[x], w1,pSrcY1[x], round, shift, offset); x--;
112      pDstY[x] = weightBidirY(w0,pSrcY0[x], w1,pSrcY1[x], round, shift, offset); x--;
113      pDstY[x] = weightBidirY(w0,pSrcY0[x], w1,pSrcY1[x], round, shift, offset); x--;
114      pDstY[x] = weightBidirY(w0,pSrcY0[x], w1,pSrcY1[x], round, shift, offset); x--;
115    }
116    pSrcY0 += iSrc0Stride;
117    pSrcY1 += iSrc1Stride;
118    pDstY  += iDstStride;
119  }
120
121 
122  // Chroma U : --------------------------------------------
123  w0      = wp0[1].w;
124  offset  = wp0[1].offset;
125  shiftNum = IF_INTERNAL_PREC - g_bitDepthC;
126  shift   = wp0[1].shift + shiftNum;
127  round   = shift?(1<<(shift-1)):0;
128  w1      = wp1[1].w;
129
130  iSrc0Stride = pcYuvSrc0->getCStride();
131  iSrc1Stride = pcYuvSrc1->getCStride();
132  iDstStride  = rpcYuvDst->getCStride();
133 
134  iWidth  >>=1;
135  iHeight >>=1;
136 
137  for ( y = iHeight-1; y >= 0; y-- )
138  {
139    for ( x = iWidth-1; x >= 0; )
140    {
141      // note: chroma min width is 2
142      pDstU[x] = weightBidirC(w0,pSrcU0[x], w1,pSrcU1[x], round, shift, offset); x--;
143      pDstU[x] = weightBidirC(w0,pSrcU0[x], w1,pSrcU1[x], round, shift, offset); x--;
144    }
145    pSrcU0 += iSrc0Stride;
146    pSrcU1 += iSrc1Stride;
147    pDstU  += iDstStride;
148  }
149
150  // Chroma V : --------------------------------------------
151  w0      = wp0[2].w;
152  offset  = wp0[2].offset;
153  shift   = wp0[2].shift + shiftNum;
154  round   = shift?(1<<(shift-1)):0;
155  w1      = wp1[2].w;
156
157  for ( y = iHeight-1; y >= 0; y-- )
158  {
159    for ( x = iWidth-1; x >= 0; )
160    {
161      // note: chroma min width is 2
162      pDstV[x] = weightBidirC(w0,pSrcV0[x], w1,pSrcV1[x], round, shift, offset); x--;
163      pDstV[x] = weightBidirC(w0,pSrcV0[x], w1,pSrcV1[x], round, shift, offset); x--;
164    }
165    pSrcV0 += iSrc0Stride;
166    pSrcV1 += iSrc1Stride;
167    pDstV  += iDstStride;
168  }
169}
170
171/** weighted averaging for uni-pred
172 * \param TComYuv* pcYuvSrc0
173 * \param iPartUnitIdx
174 * \param iWidth
175 * \param iHeight
176 * \param wpScalingParam *wp0
177 * \param TComYuv* rpcYuvDst
178 * \returns Void
179 */
180Void TComWeightPrediction::addWeightUni( TComYuv* pcYuvSrc0, UInt iPartUnitIdx, UInt iWidth, UInt iHeight, wpScalingParam *wp0, TComYuv* rpcYuvDst )
181{
182  Int x, y;
183 
184  Pel* pSrcY0  = pcYuvSrc0->getLumaAddr( iPartUnitIdx );
185  Pel* pSrcU0  = pcYuvSrc0->getCbAddr  ( iPartUnitIdx );
186  Pel* pSrcV0  = pcYuvSrc0->getCrAddr  ( iPartUnitIdx );
187 
188  Pel* pDstY   = rpcYuvDst->getLumaAddr( iPartUnitIdx );
189  Pel* pDstU   = rpcYuvDst->getCbAddr  ( iPartUnitIdx );
190  Pel* pDstV   = rpcYuvDst->getCrAddr  ( iPartUnitIdx );
191 
192  // Luma : --------------------------------------------
193  Int w0      = wp0[0].w;
194  Int offset  = wp0[0].offset;
195  Int shiftNum = IF_INTERNAL_PREC - g_bitDepthY;
196  Int shift   = wp0[0].shift + shiftNum;
197  Int round   = shift?(1<<(shift-1)):0;
198  UInt  iSrc0Stride = pcYuvSrc0->getStride();
199  UInt  iDstStride  = rpcYuvDst->getStride();
200 
201  for ( y = iHeight-1; y >= 0; y-- )
202  {
203    for ( x = iWidth-1; x >= 0; )
204    {
205      // note: luma min width is 4
206      pDstY[x] = weightUnidirY(w0,pSrcY0[x], round, shift, offset); x--;
207      pDstY[x] = weightUnidirY(w0,pSrcY0[x], round, shift, offset); x--;
208      pDstY[x] = weightUnidirY(w0,pSrcY0[x], round, shift, offset); x--;
209      pDstY[x] = weightUnidirY(w0,pSrcY0[x], round, shift, offset); x--;
210    }
211    pSrcY0 += iSrc0Stride;
212    pDstY  += iDstStride;
213  }
214 
215  // Chroma U : --------------------------------------------
216  w0      = wp0[1].w;
217  offset  = wp0[1].offset;
218  shiftNum = IF_INTERNAL_PREC - g_bitDepthC;
219  shift   = wp0[1].shift + shiftNum;
220  round   = shift?(1<<(shift-1)):0;
221
222  iSrc0Stride = pcYuvSrc0->getCStride();
223  iDstStride  = rpcYuvDst->getCStride();
224 
225  iWidth  >>=1;
226  iHeight >>=1;
227 
228  for ( y = iHeight-1; y >= 0; y-- )
229  {
230    for ( x = iWidth-1; x >= 0; )
231    {
232      // note: chroma min width is 2
233      pDstU[x] = weightUnidirC(w0,pSrcU0[x], round, shift, offset); x--;
234      pDstU[x] = weightUnidirC(w0,pSrcU0[x], round, shift, offset); x--;
235    }
236    pSrcU0 += iSrc0Stride;
237    pDstU  += iDstStride;
238  }
239
240  // Chroma V : --------------------------------------------
241  w0      = wp0[2].w;
242  offset  = wp0[2].offset;
243  shift   = wp0[2].shift + shiftNum;
244  round   = shift?(1<<(shift-1)):0;
245
246  for ( y = iHeight-1; y >= 0; y-- )
247  {
248    for ( x = iWidth-1; x >= 0; )
249    {
250      // note: chroma min width is 2
251      pDstV[x] = weightUnidirC(w0,pSrcV0[x], round, shift, offset); x--;
252      pDstV[x] = weightUnidirC(w0,pSrcV0[x], round, shift, offset); x--;
253    }
254    pSrcV0 += iSrc0Stride;
255    pDstV  += iDstStride;
256  }
257}
258
259//=======================================================
260//  getWpScaling()
261//=======================================================
262/** derivation of wp tables
263 * \param TComDataCU* pcCU
264 * \param iRefIdx0
265 * \param iRefIdx1
266 * \param wpScalingParam *&wp0
267 * \param wpScalingParam *&wp1
268 * \param ibdi
269 * \returns Void
270 */
271Void TComWeightPrediction::getWpScaling( TComDataCU* pcCU, Int iRefIdx0, Int iRefIdx1, wpScalingParam *&wp0, wpScalingParam *&wp1)
272{
273  TComSlice*      pcSlice       = pcCU->getSlice();
274  TComPPS*        pps           = pcCU->getSlice()->getPPS();
275  Bool            wpBiPred = pps->getWPBiPred();
276  wpScalingParam* pwp;
277  Bool            bBiDir        = (iRefIdx0>=0 && iRefIdx1>=0);
278  Bool            bUniDir       = !bBiDir;
279
280  if ( bUniDir || wpBiPred )
281  { // explicit --------------------
282    if ( iRefIdx0 >= 0 )
283    {
284      pcSlice->getWpScaling(REF_PIC_LIST_0, iRefIdx0, wp0);
285    }
286    if ( iRefIdx1 >= 0 )
287    {
288      pcSlice->getWpScaling(REF_PIC_LIST_1, iRefIdx1, wp1);
289    }
290  }
291  else
292  {
293    assert(0);
294  }
295
296  if ( iRefIdx0 < 0 )
297  {
298    wp0 = NULL;
299  }
300  if ( iRefIdx1 < 0 )
301  {
302    wp1 = NULL;
303  }
304
305  if ( bBiDir )
306  { // Bi-Dir case
307    for ( Int yuv=0 ; yuv<3 ; yuv++ )
308    {
309      Int bitDepth = yuv ? g_bitDepthC : g_bitDepthY;
310      wp0[yuv].w      = wp0[yuv].iWeight;
311      wp0[yuv].o      = wp0[yuv].iOffset * (1 << (bitDepth-8));
312      wp1[yuv].w      = wp1[yuv].iWeight;
313      wp1[yuv].o      = wp1[yuv].iOffset * (1 << (bitDepth-8));
314      wp0[yuv].offset = wp0[yuv].o + wp1[yuv].o;
315      wp0[yuv].shift  = wp0[yuv].uiLog2WeightDenom + 1;
316      wp0[yuv].round  = (1 << wp0[yuv].uiLog2WeightDenom);
317      wp1[yuv].offset = wp0[yuv].offset;
318      wp1[yuv].shift  = wp0[yuv].shift;
319      wp1[yuv].round  = wp0[yuv].round;
320    }
321  }
322  else
323  {  // Unidir
324    pwp = (iRefIdx0>=0) ? wp0 : wp1 ;
325    for ( Int yuv=0 ; yuv<3 ; yuv++ )
326    {
327      Int bitDepth = yuv ? g_bitDepthC : g_bitDepthY;
328      pwp[yuv].w      = pwp[yuv].iWeight;
329      pwp[yuv].offset = pwp[yuv].iOffset * (1 << (bitDepth-8));
330      pwp[yuv].shift  = pwp[yuv].uiLog2WeightDenom;
331      pwp[yuv].round  = (pwp[yuv].uiLog2WeightDenom>=1) ? (1 << (pwp[yuv].uiLog2WeightDenom-1)) : (0);
332    }
333  }
334}
335
336/** weighted prediction for bi-pred
337 * \param TComDataCU* pcCU
338 * \param TComYuv* pcYuvSrc0
339 * \param TComYuv* pcYuvSrc1
340 * \param iRefIdx0
341 * \param iRefIdx1
342 * \param uiPartIdx
343 * \param iWidth
344 * \param iHeight
345 * \param TComYuv* rpcYuvDst
346 * \returns Void
347 */
348Void TComWeightPrediction::xWeightedPredictionBi( TComDataCU* pcCU, TComYuv* pcYuvSrc0, TComYuv* pcYuvSrc1, Int iRefIdx0, Int iRefIdx1, UInt uiPartIdx, Int iWidth, Int iHeight, TComYuv* rpcYuvDst )
349{
350  wpScalingParam  *pwp0, *pwp1;
351  TComPPS         *pps = pcCU->getSlice()->getPPS();
352  assert( pps->getWPBiPred());
353
354  getWpScaling(pcCU, iRefIdx0, iRefIdx1, pwp0, pwp1);
355
356  if( iRefIdx0 >= 0 && iRefIdx1 >= 0 )
357  {
358    addWeightBi(pcYuvSrc0, pcYuvSrc1, uiPartIdx, iWidth, iHeight, pwp0, pwp1, rpcYuvDst );
359  }
360  else if ( iRefIdx0 >= 0 && iRefIdx1 <  0 )
361  {
362    addWeightUni( pcYuvSrc0, uiPartIdx, iWidth, iHeight, pwp0, rpcYuvDst );
363  }
364  else if ( iRefIdx0 <  0 && iRefIdx1 >= 0 )
365  {
366    addWeightUni( pcYuvSrc1, uiPartIdx, iWidth, iHeight, pwp1, rpcYuvDst );
367  }
368  else
369  {
370    assert (0);
371  }
372}
373
374/** weighted prediction for uni-pred
375 * \param TComDataCU* pcCU
376 * \param TComYuv* pcYuvSrc
377 * \param uiPartAddr
378 * \param iWidth
379 * \param iHeight
380 * \param eRefPicList
381 * \param TComYuv*& rpcYuvPred
382 * \param iPartIdx
383 * \param iRefIdx
384 * \returns Void
385 */
386Void TComWeightPrediction::xWeightedPredictionUni( TComDataCU* pcCU, TComYuv* pcYuvSrc, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Int iRefIdx)
387{ 
388  wpScalingParam  *pwp, *pwpTmp;
389  if ( iRefIdx < 0 )
390  {
391    iRefIdx   = pcCU->getCUMvField( eRefPicList )->getRefIdx( uiPartAddr );
392  }
393  assert (iRefIdx >= 0);
394
395  if ( eRefPicList == REF_PIC_LIST_0 )
396  {
397    getWpScaling(pcCU, iRefIdx, -1, pwp, pwpTmp);
398  }
399  else
400  {
401    getWpScaling(pcCU, -1, iRefIdx, pwpTmp, pwp);
402  }
403  addWeightUni( pcYuvSrc, uiPartAddr, iWidth, iHeight, pwp, rpcYuvPred );
404}
405
406
Note: See TracBrowser for help on using the repository browser.