source: SHVCSoftware/branches/SHM-1.0-dev/source/Lib/TLibCommon/TComWeightPrediction.cpp @ 1165

Last change on this file since 1165 was 2, checked in by seregin, 12 years ago

Initial import by Vadim Seregin <vseregin@…>

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