source: 3DVCSoftware/branches/HTM-14.1-update-dev3-MediaTek-Qualcomm/source/Lib/TLibCommon/TComWedgelet.cpp @ 1259

Last change on this file since 1259 was 1196, checked in by tech, 10 years ago

Merged 14.0-dev0@1187.

  • Property svn:eol-style set to native
File size: 10.2 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 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
35
36
37// Include files
38#include "CommonDef.h"
39#include "TComYuv.h"
40#include "TComWedgelet.h"
41
42#include <stdlib.h>
43#include <memory.h>
44
45using namespace std;
46
47#if H_3D_DIM_DMM
48TComWedgelet::TComWedgelet( UInt uiWidth, UInt uiHeight ) : m_uhXs     ( 0 ),
49                                                            m_uhYs     ( 0 ),
50                                                            m_uhXe     ( 0 ),
51                                                            m_uhYe     ( 0 ),
52                                                            m_uhOri    ( 0 ),
53                                                            m_eWedgeRes( FULL_PEL ),
54                                                            m_bIsCoarse( false )
55{
56  create( uiWidth, uiHeight );
57}
58
59TComWedgelet::TComWedgelet( const TComWedgelet &rcWedge ) : m_uhXs     ( rcWedge.m_uhXs      ),
60                                                            m_uhYs     ( rcWedge.m_uhYs      ),
61                                                            m_uhXe     ( rcWedge.m_uhXe      ),
62                                                            m_uhYe     ( rcWedge.m_uhYe      ),
63                                                            m_uhOri    ( rcWedge.m_uhOri     ),
64                                                            m_eWedgeRes( rcWedge.m_eWedgeRes ),
65                                                            m_bIsCoarse( rcWedge.m_bIsCoarse ),
66                                                            m_uiAng    ( rcWedge.m_uiAng     ),
67                                                            m_uiWidth  ( rcWedge.m_uiWidth   ),
68                                                            m_uiHeight ( rcWedge.m_uiHeight  ),
69                                                            m_pbPattern( (Bool*)xMalloc( Bool, (m_uiWidth * m_uiHeight) ) )
70                                                            ,m_pbScaledPattern( g_wedgePattern )
71{
72  ::memcpy( m_pbPattern, rcWedge.m_pbPattern, sizeof(Bool) * (m_uiWidth * m_uiHeight));
73}
74
75TComWedgelet::~TComWedgelet(void)
76{
77  destroy();
78}
79
80Void TComWedgelet::create( UInt uiWidth, UInt uiHeight )
81{
82  assert( uiWidth > 0 && uiHeight > 0 );
83
84  m_uiWidth   = uiWidth;
85  m_uiHeight  = uiHeight;
86
87  m_pbPattern = (Bool*)xMalloc( Bool, (m_uiWidth * m_uiHeight) );
88  m_pbScaledPattern = g_wedgePattern;
89}
90
91Void TComWedgelet::destroy()
92{
93  if( m_pbPattern ) { xFree( m_pbPattern ); m_pbPattern = NULL; }
94}
95
96Void TComWedgelet::clear()
97{
98  ::memset( m_pbPattern, 0, (m_uiWidth * m_uiHeight) * sizeof(Bool) );
99}
100
101Void TComWedgelet::findClosestAngle()
102{
103  UInt uiAng=0,uiOptAng=0;
104  UInt uiMinD=MAX_UINT;
105  UInt uiTmpD=0;
106  Int angTable[9]    = {0,    2,    5,   9,  13,  17,  21,  26,  32};
107 
108  UChar uhXs = m_uhXs;
109  UChar uhYs = m_uhYs;
110  UChar uhXe = m_uhXe;
111  UChar uhYe = m_uhYe;
112
113  for(uiAng=2; uiAng<=34; uiAng++)
114  {
115    Int iSign    = (uiAng<VER_IDX && uiAng>HOR_IDX ) ? -1 : 1;
116    Int iVer     = uiAng>17 ? 32 : angTable[(uiAng>10) ? (uiAng-10) : (10-uiAng)];
117    Int iHor     = uiAng<19 ? 32 : angTable[(uiAng>26) ? (uiAng-26) : (26-uiAng)];
118
119    uiTmpD  = abs(iVer*iSign*(uhXs-uhXe) - iHor*(uhYe-uhYs));
120   
121    if( uiTmpD < uiMinD )
122    {
123      uiMinD = uiTmpD;
124      uiOptAng = uiAng;
125    }
126  }
127  m_uiAng = uiOptAng;
128}
129
130Void TComWedgelet::setWedgelet( UChar uhXs, UChar uhYs, UChar uhXe, UChar uhYe, UChar uhOri, WedgeResolution eWedgeRes, Bool bIsCoarse )
131{
132  m_uhXs      = uhXs;
133  m_uhYs      = uhYs;
134  m_uhXe      = uhXe;
135  m_uhYe      = uhYe;
136  m_uhOri     = uhOri;
137  m_eWedgeRes = eWedgeRes;
138  m_bIsCoarse = bIsCoarse;
139
140  xGenerateWedgePattern();
141}
142
143Bool TComWedgelet::checkNotPlain()
144{
145  for( UInt k = 1; k < (m_uiWidth * m_uiHeight); k++ )
146  {
147    if( m_pbPattern[0] != m_pbPattern[k] )
148    {
149      return true;
150    }
151  }
152  return false;
153}
154
155Bool TComWedgelet::checkIdentical( Bool* pbRefPattern )
156{
157  for( UInt k = 0; k < (m_uiWidth * m_uiHeight); k++ )
158  {
159    if( m_pbPattern[k] != pbRefPattern[k] )
160    {
161      return false;
162    }
163  }
164  return true;
165}
166
167Bool TComWedgelet::checkInvIdentical( Bool* pbRefPattern )
168{
169  for( UInt k = 0; k < (m_uiWidth * m_uiHeight); k++ )
170  {
171    if( m_pbPattern[k] == pbRefPattern[k] )
172    {
173      return false;
174    }
175  }
176  return true;
177}
178
179Void TComWedgelet::generateWedgePatternByRotate(const TComWedgelet &rcWedge, Int rotate)
180{
181  Int stride = m_uiWidth;
182  Int sinc, offsetI, offsetJ;
183 
184  sinc = 1;
185  offsetI = ( sinc) < 0 ? stride-1 : 0; // 0
186  offsetJ = (-sinc) < 0 ? stride-1 : 0; // stride - 1
187
188  for (Int y = 0; y < stride; y++)
189  {
190    for (Int x = 0; x < stride; x++)
191    {
192      Int i = offsetI + sinc * y; // y
193      Int j = offsetJ - sinc * x; // stride - 1 - x
194      m_pbPattern[(y * stride) + x] = !rcWedge.m_pbPattern[(j * stride) + i];
195    }
196  }
197  Int blocksize = rcWedge.m_uiWidth * (rcWedge.m_eWedgeRes == HALF_PEL ? 2 : 1);
198  Int offsetX = (-sinc) < 0 ? blocksize - 1 : 0;
199  Int offsetY = ( sinc) < 0 ? blocksize - 1 : 0;
200  m_uhXs = offsetX - sinc * rcWedge.m_uhYs;
201  m_uhYs = offsetY + sinc * rcWedge.m_uhXs;
202  m_uhXe = offsetX - sinc * rcWedge.m_uhYe;
203  m_uhYe = offsetY + sinc * rcWedge.m_uhXe;
204  m_uhOri = rotate;
205  m_eWedgeRes = rcWedge.m_eWedgeRes;
206  m_bIsCoarse = rcWedge.m_bIsCoarse;
207  m_uiAng = rcWedge.m_uiAng;
208  m_uiWidth  = rcWedge.m_uiWidth;
209  m_uiHeight = rcWedge.m_uiHeight;
210}
211
212Void TComWedgelet::xGenerateWedgePattern()
213{
214  UInt uiTempBlockSize = 0;
215  UChar uhXs = 0, uhYs = 0, uhXe = 0, uhYe = 0;
216  switch( m_eWedgeRes )
217  {
218  case(   FULL_PEL ): { uiTempBlockSize =  m_uiWidth;     uhXs =  m_uhXs;     uhYs =  m_uhYs;     uhXe =  m_uhXe;     uhYe =  m_uhYe;     } break;
219  case(   HALF_PEL ): { uiTempBlockSize = (m_uiWidth<<1); uhXs =  m_uhXs;     uhYs =  m_uhYs;     uhXe =  m_uhXe;     uhYe =  m_uhYe;     } break;
220  }
221
222  Bool* pbTempPattern = new Bool[ (uiTempBlockSize * uiTempBlockSize) ];
223  ::memset( pbTempPattern, 0, (uiTempBlockSize * uiTempBlockSize) * sizeof(Bool) );
224  Int iTempStride = uiTempBlockSize;
225
226  xDrawEdgeLine( uhXs, uhYs, uhXe, uhYe, pbTempPattern, iTempStride );
227
228  Int shift = (m_eWedgeRes == HALF_PEL) ? 1 : 0;
229  Int endPos = uhYe>>shift;
230  for (Int y = 0; y <= endPos; y++)
231  {
232    for (Int x = 0; x < m_uiWidth && pbTempPattern[(y * m_uiWidth) + x] == 0; x++)
233    {
234      pbTempPattern[(y * m_uiWidth) + x] = true;
235    }
236  }
237  for( UInt k = 0; k < (m_uiWidth * m_uiHeight); k++ )
238  {
239    m_pbPattern[k] = pbTempPattern[k];
240  };
241
242  if( pbTempPattern )
243  {
244    delete [] pbTempPattern;
245    pbTempPattern = NULL;
246  }
247}
248
249Void TComWedgelet::xDrawEdgeLine( UChar uhXs, UChar uhYs, UChar uhXe, UChar uhYe, Bool* pbPattern, Int iPatternStride )
250{
251  Int x0 = (Int)uhXs;
252  Int y0 = (Int)uhYs;
253  Int x1 = (Int)uhXe;
254  Int y1 = (Int)uhYe;
255
256  // direction independent Bresenham line
257  bool steep = (abs(y1 - y0) > abs(x1 - x0));
258  if( steep )
259  {
260    std::swap( x0, y0 );
261    std::swap( x1, y1 );
262  }
263
264  bool backward = ( x0 > x1 );
265  if( backward )
266  {
267    std::swap( x0, x1 );
268    std::swap( y0, y1 );
269  }
270
271  Int deltax = x1 - x0;
272  Int deltay = abs(y1 - y0);
273  Int error = 0;
274  Int deltaerr = (deltay<<1);
275
276  Int ystep;
277  Int y = y0;
278  if( y0 < y1 ) ystep =  1;
279  else          ystep = -1;
280
281  for( Int x = x0; x <= x1; x++ )
282  {
283    Int shift = (m_eWedgeRes == HALF_PEL) ? 1 : 0;
284    Int stride = iPatternStride >> shift;
285    if( steep ) { pbPattern[((x>>shift) * stride) + (y>>shift)] = true; }
286    else        { pbPattern[((y>>shift) * stride) + (x>>shift)] = true; }
287
288    error += deltaerr;
289    if( error >= deltax )
290    {
291      y += ystep;
292      error = error - (deltax<<1);
293    }
294  }
295}
296
297Bool* TComWedgelet::getScaledPattern(UInt uiDstSize)
298{
299  Bool *pbSrcPat = this->getPattern();
300  UInt uiSrcSize = this->getStride();
301
302  Int scale = (g_aucConvertToBit[uiDstSize] - g_aucConvertToBit[uiSrcSize]);
303  assert(scale>=0);
304  for (Int y=0; y<uiDstSize; y++)
305  {
306    for (Int x=0; x<uiDstSize; x++)
307    {
308      Int srcX = x>>scale;
309      Int srcY = y>>scale;
310      m_pbScaledPattern[y*uiDstSize + x] = pbSrcPat[ srcY*uiSrcSize + srcX ];
311    }
312  }
313  return m_pbScaledPattern;
314}
315
316TComWedgeNode::TComWedgeNode()
317{
318  m_uiPatternIdx = DMM_NO_WEDGEINDEX;
319  for( UInt uiPos = 0; uiPos < DMM_NUM_WEDGE_REFINES; uiPos++ )
320  {
321    m_uiRefineIdx[uiPos] = DMM_NO_WEDGEINDEX;
322  }
323}
324
325UInt TComWedgeNode::getPatternIdx()
326{
327  return m_uiPatternIdx;
328}
329UInt TComWedgeNode::getRefineIdx( UInt uiPos )
330{
331  assert( uiPos < DMM_NUM_WEDGE_REFINES );
332  return m_uiRefineIdx[uiPos];
333}
334Void TComWedgeNode::setPatternIdx( UInt uiIdx )
335{
336  m_uiPatternIdx = uiIdx;
337}
338Void TComWedgeNode::setRefineIdx( UInt uiIdx, UInt uiPos )
339{
340  assert( uiPos < DMM_NUM_WEDGE_REFINES );
341  m_uiRefineIdx[uiPos] = uiIdx; 
342}
343#endif //H_3D_DIM_DMM
Note: See TracBrowser for help on using the repository browser.