source: 3DVCSoftware/trunk/source/Lib/TLibCommon/TComRdCostWeightPrediction.cpp @ 1313

Last change on this file since 1313 was 1313, checked in by tech, 9 years ago

Merged 14.1-update-dev1@1312.

  • Property svn:eol-style set to native
File size: 14.8 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-2015, 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     TComRdCostWeightPrediction.cpp
35    \brief    RD cost computation class with Weighted-Prediction
36*/
37
38#include <math.h>
39#include <assert.h>
40#include "TComRdCost.h"
41#include "TComRdCostWeightPrediction.h"
42
43static Distortion xCalcHADs2x2w( const WPScalingParam &wpCur, const Pel *piOrg, const Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep );
44static Distortion xCalcHADs4x4w( const WPScalingParam &wpCur, const Pel *piOrg, const Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep );
45static Distortion xCalcHADs8x8w( const WPScalingParam &wpCur, const Pel *piOrg, const Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep );
46
47
48// --------------------------------------------------------------------------------------------------------------------
49// SAD
50// --------------------------------------------------------------------------------------------------------------------
51/** get weighted SAD cost
52 * \param pcDtParam
53 * \returns Distortion
54 */
55Distortion TComRdCostWeightPrediction::xGetSADw( DistParam* pcDtParam )
56{
57  const Pel            *piOrg      = pcDtParam->pOrg;
58  const Pel            *piCur      = pcDtParam->pCur;
59  const Int             iCols      = pcDtParam->iCols;
60  const Int             iStrideCur = pcDtParam->iStrideCur;
61  const Int             iStrideOrg = pcDtParam->iStrideOrg;
62  const ComponentID     compID     = pcDtParam->compIdx;
63
64  assert(compID<MAX_NUM_COMPONENT);
65
66  const WPScalingParam &wpCur      = pcDtParam->wpCur[compID];
67
68  const Int             w0         = wpCur.w;
69  const Int             offset     = wpCur.offset;
70  const Int             shift      = wpCur.shift;
71  const Int             round      = wpCur.round;
72
73  Distortion uiSum = 0;
74
75  for(Int iRows = pcDtParam->iRows; iRows != 0; iRows-- )
76  {
77    for (Int n = 0; n < iCols; n++ )
78    {
79      const Pel pred = ( (w0*piCur[n] + round) >> shift ) + offset ;
80
81      uiSum += abs( piOrg[n] - pred );
82    }
83    piOrg += iStrideOrg;
84    piCur += iStrideCur;
85  }
86
87  pcDtParam->compIdx = MAX_NUM_COMPONENT;  // reset for DEBUG (assert test)
88
89  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
90}
91
92
93// --------------------------------------------------------------------------------------------------------------------
94// SSE
95// --------------------------------------------------------------------------------------------------------------------
96/** get weighted SSD cost
97 * \param pcDtParam
98 * \returns Distortion
99 */
100Distortion TComRdCostWeightPrediction::xGetSSEw( DistParam* pcDtParam )
101{
102  const Pel            *piOrg           = pcDtParam->pOrg;
103  const Pel            *piCur           = pcDtParam->pCur;
104  const Int             iCols           = pcDtParam->iCols;
105  const Int             iStrideOrg      = pcDtParam->iStrideOrg;
106  const Int             iStrideCur      = pcDtParam->iStrideCur;
107  const ComponentID     compIdx         = pcDtParam->compIdx;
108
109  assert( pcDtParam->iSubShift == 0 ); // NOTE: what is this protecting?
110
111  assert(compIdx<MAX_NUM_COMPONENT);
112  const WPScalingParam &wpCur           = pcDtParam->wpCur[compIdx];
113  const Int             w0              = wpCur.w;
114  const Int             offset          = wpCur.offset;
115  const Int             shift           = wpCur.shift;
116  const Int             round           = wpCur.round;
117  const UInt            distortionShift = DISTORTION_PRECISION_ADJUSTMENT((pcDtParam->bitDepth-8) << 1);
118
119  Distortion sum = 0;
120
121  for(Int iRows = pcDtParam->iRows ; iRows != 0; iRows-- )
122  {
123    for (Int n = 0; n < iCols; n++ )
124    {
125      const Pel pred     = ( (w0*piCur[n] + round) >> shift ) + offset ;
126      const Pel residual = piOrg[n] - pred;
127      sum += ( Distortion(residual) * Distortion(residual) ) >> distortionShift;
128    }
129    piOrg += iStrideOrg;
130    piCur += iStrideCur;
131  }
132
133  pcDtParam->compIdx = MAX_NUM_COMPONENT; // reset for DEBUG (assert test)
134
135  return sum;
136}
137
138
139// --------------------------------------------------------------------------------------------------------------------
140// HADAMARD with step (used in fractional search)
141// --------------------------------------------------------------------------------------------------------------------
142//! get weighted Hadamard cost for 2x2 block
143Distortion xCalcHADs2x2w( const WPScalingParam &wpCur, const Pel *piOrg, const Pel *piCur, Int iStrideOrg, Int iStrideCur, Int iStep )
144{
145  const Int round  = wpCur.round;
146  const Int shift  = wpCur.shift;
147  const Int offset = wpCur.offset;
148  const Int w0     = wpCur.w;
149
150  Distortion satd  = 0;
151  TCoeff     diff[4];
152  TCoeff     m[4];
153
154  Pel   pred;
155
156  pred    = ( (w0*piCur[0*iStep             ] + round) >> shift ) + offset ;
157  diff[0] = piOrg[0             ] - pred;
158  pred    = ( (w0*piCur[1*iStep             ] + round) >> shift ) + offset ;
159  diff[1] = piOrg[1             ] - pred;
160  pred    = ( (w0*piCur[0*iStep + iStrideCur] + round) >> shift ) + offset ;
161  diff[2] = piOrg[iStrideOrg    ] - pred;
162  pred    = ( (w0*piCur[1*iStep + iStrideCur] + round) >> shift ) + offset ;
163  diff[3] = piOrg[iStrideOrg + 1] - pred;
164
165  m[0] = diff[0] + diff[2];
166  m[1] = diff[1] + diff[3];
167  m[2] = diff[0] - diff[2];
168  m[3] = diff[1] - diff[3];
169
170  satd += abs(m[0] + m[1]);
171  satd += abs(m[0] - m[1]);
172  satd += abs(m[2] + m[3]);
173  satd += abs(m[2] - m[3]);
174
175  return satd;
176}
177
178
179//! get weighted Hadamard cost for 4x4 block
180Distortion xCalcHADs4x4w( const WPScalingParam &wpCur, const Pel *piOrg, const Pel *piCur, Int iStrideOrg, Int iStrideCur, Int iStep )
181{
182  const Int round  = wpCur.round;
183  const Int shift  = wpCur.shift;
184  const Int offset = wpCur.offset;
185  const Int w0     = wpCur.w;
186
187  Distortion satd = 0;
188  TCoeff     diff[16];
189  TCoeff     m[16];
190  TCoeff     d[16];
191
192
193  for(Int k = 0; k < 16; k+=4 )
194  {
195    Pel pred;
196    pred      = ( (w0*piCur[0*iStep] + round) >> shift ) + offset ;
197    diff[k+0] = piOrg[0] - pred;
198    pred      = ( (w0*piCur[1*iStep] + round) >> shift ) + offset ;
199    diff[k+1] = piOrg[1] - pred;
200    pred      = ( (w0*piCur[2*iStep] + round) >> shift ) + offset ;
201    diff[k+2] = piOrg[2] - pred;
202    pred      = ( (w0*piCur[3*iStep] + round) >> shift ) + offset ;
203    diff[k+3] = piOrg[3] - pred;
204
205    piCur += iStrideCur;
206    piOrg += iStrideOrg;
207  }
208
209  /*===== hadamard transform =====*/
210  m[ 0] = diff[ 0] + diff[12];
211  m[ 1] = diff[ 1] + diff[13];
212  m[ 2] = diff[ 2] + diff[14];
213  m[ 3] = diff[ 3] + diff[15];
214  m[ 4] = diff[ 4] + diff[ 8];
215  m[ 5] = diff[ 5] + diff[ 9];
216  m[ 6] = diff[ 6] + diff[10];
217  m[ 7] = diff[ 7] + diff[11];
218  m[ 8] = diff[ 4] - diff[ 8];
219  m[ 9] = diff[ 5] - diff[ 9];
220  m[10] = diff[ 6] - diff[10];
221  m[11] = diff[ 7] - diff[11];
222  m[12] = diff[ 0] - diff[12];
223  m[13] = diff[ 1] - diff[13];
224  m[14] = diff[ 2] - diff[14];
225  m[15] = diff[ 3] - diff[15];
226
227  d[ 0] = m[ 0] + m[ 4];
228  d[ 1] = m[ 1] + m[ 5];
229  d[ 2] = m[ 2] + m[ 6];
230  d[ 3] = m[ 3] + m[ 7];
231  d[ 4] = m[ 8] + m[12];
232  d[ 5] = m[ 9] + m[13];
233  d[ 6] = m[10] + m[14];
234  d[ 7] = m[11] + m[15];
235  d[ 8] = m[ 0] - m[ 4];
236  d[ 9] = m[ 1] - m[ 5];
237  d[10] = m[ 2] - m[ 6];
238  d[11] = m[ 3] - m[ 7];
239  d[12] = m[12] - m[ 8];
240  d[13] = m[13] - m[ 9];
241  d[14] = m[14] - m[10];
242  d[15] = m[15] - m[11];
243
244  m[ 0] = d[ 0] + d[ 3];
245  m[ 1] = d[ 1] + d[ 2];
246  m[ 2] = d[ 1] - d[ 2];
247  m[ 3] = d[ 0] - d[ 3];
248  m[ 4] = d[ 4] + d[ 7];
249  m[ 5] = d[ 5] + d[ 6];
250  m[ 6] = d[ 5] - d[ 6];
251  m[ 7] = d[ 4] - d[ 7];
252  m[ 8] = d[ 8] + d[11];
253  m[ 9] = d[ 9] + d[10];
254  m[10] = d[ 9] - d[10];
255  m[11] = d[ 8] - d[11];
256  m[12] = d[12] + d[15];
257  m[13] = d[13] + d[14];
258  m[14] = d[13] - d[14];
259  m[15] = d[12] - d[15];
260
261  d[ 0] = m[ 0] + m[ 1];
262  d[ 1] = m[ 0] - m[ 1];
263  d[ 2] = m[ 2] + m[ 3];
264  d[ 3] = m[ 3] - m[ 2];
265  d[ 4] = m[ 4] + m[ 5];
266  d[ 5] = m[ 4] - m[ 5];
267  d[ 6] = m[ 6] + m[ 7];
268  d[ 7] = m[ 7] - m[ 6];
269  d[ 8] = m[ 8] + m[ 9];
270  d[ 9] = m[ 8] - m[ 9];
271  d[10] = m[10] + m[11];
272  d[11] = m[11] - m[10];
273  d[12] = m[12] + m[13];
274  d[13] = m[12] - m[13];
275  d[14] = m[14] + m[15];
276  d[15] = m[15] - m[14];
277
278  for (Int k=0; k<16; ++k)
279  {
280    satd += abs(d[k]);
281  }
282  satd = ((satd+1)>>1);
283
284  return satd;
285}
286
287
288//! get weighted Hadamard cost for 8x8 block
289Distortion xCalcHADs8x8w( const WPScalingParam &wpCur, const Pel *piOrg, const Pel *piCur, Int iStrideOrg, Int iStrideCur, Int iStep )
290{
291  Distortion sad=0;
292  TCoeff diff[64], m1[8][8], m2[8][8], m3[8][8];
293  Int iStep2 = iStep<<1;
294  Int iStep3 = iStep2 + iStep;
295  Int iStep4 = iStep3 + iStep;
296  Int iStep5 = iStep4 + iStep;
297  Int iStep6 = iStep5 + iStep;
298  Int iStep7 = iStep6 + iStep;
299  const Int round  = wpCur.round;
300  const Int shift  = wpCur.shift;
301  const Int offset = wpCur.offset;
302  const Int w0     = wpCur.w;
303
304  Pel   pred;
305
306  for(Int k = 0; k < 64; k+=8 )
307  {
308    pred      = ( (w0*piCur[     0] + round) >> shift ) + offset ;
309    diff[k+0] = piOrg[0] - pred;
310    pred      = ( (w0*piCur[iStep ] + round) >> shift ) + offset ;
311    diff[k+1] = piOrg[1] - pred;
312    pred      = ( (w0*piCur[iStep2] + round) >> shift ) + offset ;
313    diff[k+2] = piOrg[2] - pred;
314    pred      = ( (w0*piCur[iStep3] + round) >> shift ) + offset ;
315    diff[k+3] = piOrg[3] - pred;
316    pred      = ( (w0*piCur[iStep4] + round) >> shift ) + offset ;
317    diff[k+4] = piOrg[4] - pred;
318    pred      = ( (w0*piCur[iStep5] + round) >> shift ) + offset ;
319    diff[k+5] = piOrg[5] - pred;
320    pred      = ( (w0*piCur[iStep6] + round) >> shift ) + offset ;
321    diff[k+6] = piOrg[6] - pred;
322    pred      = ( (w0*piCur[iStep7] + round) >> shift ) + offset ;
323    diff[k+7] = piOrg[7] - pred;
324
325    piCur += iStrideCur;
326    piOrg += iStrideOrg;
327  }
328
329  //horizontal
330  for (Int j=0; j < 8; j++)
331  {
332    const Int jj = j << 3;
333    m2[j][0] = diff[jj  ] + diff[jj+4];
334    m2[j][1] = diff[jj+1] + diff[jj+5];
335    m2[j][2] = diff[jj+2] + diff[jj+6];
336    m2[j][3] = diff[jj+3] + diff[jj+7];
337    m2[j][4] = diff[jj  ] - diff[jj+4];
338    m2[j][5] = diff[jj+1] - diff[jj+5];
339    m2[j][6] = diff[jj+2] - diff[jj+6];
340    m2[j][7] = diff[jj+3] - diff[jj+7];
341
342    m1[j][0] = m2[j][0] + m2[j][2];
343    m1[j][1] = m2[j][1] + m2[j][3];
344    m1[j][2] = m2[j][0] - m2[j][2];
345    m1[j][3] = m2[j][1] - m2[j][3];
346    m1[j][4] = m2[j][4] + m2[j][6];
347    m1[j][5] = m2[j][5] + m2[j][7];
348    m1[j][6] = m2[j][4] - m2[j][6];
349    m1[j][7] = m2[j][5] - m2[j][7];
350
351    m2[j][0] = m1[j][0] + m1[j][1];
352    m2[j][1] = m1[j][0] - m1[j][1];
353    m2[j][2] = m1[j][2] + m1[j][3];
354    m2[j][3] = m1[j][2] - m1[j][3];
355    m2[j][4] = m1[j][4] + m1[j][5];
356    m2[j][5] = m1[j][4] - m1[j][5];
357    m2[j][6] = m1[j][6] + m1[j][7];
358    m2[j][7] = m1[j][6] - m1[j][7];
359  }
360
361  //vertical
362  for (Int i=0; i < 8; i++)
363  {
364    m3[0][i] = m2[0][i] + m2[4][i];
365    m3[1][i] = m2[1][i] + m2[5][i];
366    m3[2][i] = m2[2][i] + m2[6][i];
367    m3[3][i] = m2[3][i] + m2[7][i];
368    m3[4][i] = m2[0][i] - m2[4][i];
369    m3[5][i] = m2[1][i] - m2[5][i];
370    m3[6][i] = m2[2][i] - m2[6][i];
371    m3[7][i] = m2[3][i] - m2[7][i];
372
373    m1[0][i] = m3[0][i] + m3[2][i];
374    m1[1][i] = m3[1][i] + m3[3][i];
375    m1[2][i] = m3[0][i] - m3[2][i];
376    m1[3][i] = m3[1][i] - m3[3][i];
377    m1[4][i] = m3[4][i] + m3[6][i];
378    m1[5][i] = m3[5][i] + m3[7][i];
379    m1[6][i] = m3[4][i] - m3[6][i];
380    m1[7][i] = m3[5][i] - m3[7][i];
381
382    m2[0][i] = m1[0][i] + m1[1][i];
383    m2[1][i] = m1[0][i] - m1[1][i];
384    m2[2][i] = m1[2][i] + m1[3][i];
385    m2[3][i] = m1[2][i] - m1[3][i];
386    m2[4][i] = m1[4][i] + m1[5][i];
387    m2[5][i] = m1[4][i] - m1[5][i];
388    m2[6][i] = m1[6][i] + m1[7][i];
389    m2[7][i] = m1[6][i] - m1[7][i];
390  }
391
392  for (Int j=0; j < 8; j++)
393  {
394    for (Int i=0; i < 8; i++)
395    {
396      sad += (abs(m2[j][i]));
397    }
398  }
399
400  sad=((sad+2)>>2);
401
402  return sad;
403}
404
405
406//! get weighted Hadamard cost
407Distortion TComRdCostWeightPrediction::xGetHADsw( DistParam* pcDtParam )
408{
409  const Pel        *piOrg      = pcDtParam->pOrg;
410  const Pel        *piCur      = pcDtParam->pCur;
411  const Int         iRows      = pcDtParam->iRows;
412  const Int         iCols      = pcDtParam->iCols;
413  const Int         iStrideCur = pcDtParam->iStrideCur;
414  const Int         iStrideOrg = pcDtParam->iStrideOrg;
415  const Int         iStep      = pcDtParam->iStep;
416  const ComponentID compIdx    = pcDtParam->compIdx;
417  assert(compIdx<MAX_NUM_COMPONENT);
418  const WPScalingParam  wpCur    = pcDtParam->wpCur[compIdx];
419
420  Distortion uiSum = 0;
421
422  if( ( iRows % 8 == 0) && (iCols % 8 == 0) )
423  {
424    const Int iOffsetOrg = iStrideOrg<<3;
425    const Int iOffsetCur = iStrideCur<<3;
426    for (Int y=0; y<iRows; y+= 8 )
427    {
428      for (Int x=0; x<iCols; x+= 8 )
429      {
430        uiSum += xCalcHADs8x8w( wpCur, &piOrg[x], &piCur[x*iStep], iStrideOrg, iStrideCur, iStep );
431      }
432      piOrg += iOffsetOrg;
433      piCur += iOffsetCur;
434    }
435  }
436  else if( ( iRows % 4 == 0) && (iCols % 4 == 0) )
437  {
438    const Int iOffsetOrg = iStrideOrg<<2;
439    const Int iOffsetCur = iStrideCur<<2;
440
441    for (Int y=0; y<iRows; y+= 4 )
442    {
443      for (Int x=0; x<iCols; x+= 4 )
444      {
445        uiSum += xCalcHADs4x4w( wpCur, &piOrg[x], &piCur[x*iStep], iStrideOrg, iStrideCur, iStep );
446      }
447      piOrg += iOffsetOrg;
448      piCur += iOffsetCur;
449    }
450  }
451  else
452  {
453    for (Int y=0; y<iRows; y+=2 )
454    {
455      for (Int x=0; x<iCols; x+=2 )
456      {
457        uiSum += xCalcHADs2x2w( wpCur, &piOrg[x], &piCur[x*iStep], iStrideOrg, iStrideCur, iStep );
458      }
459      piOrg += iStrideOrg;
460      piCur += iStrideCur;
461    }
462  }
463
464  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
465}
Note: See TracBrowser for help on using the repository browser.