source: 3DVCSoftware/trunk/source/Lib/TLibCommon/TComWedgelet.cpp @ 655

Last change on this file since 655 was 655, checked in by tech, 11 years ago

Merged 8.1-Cleanup@654

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