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

Last change on this file since 1405 was 1405, checked in by tech, 8 years ago

Merged HTM-16.1-dev@1404.

  • Property svn:eol-style set to native
File size: 10.1 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-2016, 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 NH_3D_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_uiWidth  ( rcWedge.m_uiWidth   ),
67                                                            m_uiHeight ( rcWedge.m_uiHeight  ),
68                                                            m_pbPattern( (Bool*)xMalloc( Bool, (m_uiWidth * m_uiHeight) ) ),
69                                                            m_pbScaledPattern( g_wedgePattern )
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  m_pbScaledPattern = g_wedgePattern;
88}
89
90Void TComWedgelet::destroy()
91{
92  if( m_pbPattern ) { xFree( m_pbPattern ); m_pbPattern = NULL; }
93}
94
95Void TComWedgelet::clear()
96{
97  ::memset( m_pbPattern, 0, (m_uiWidth * m_uiHeight) * sizeof(Bool) );
98}
99
100Void TComWedgelet::setWedgelet( UChar uhXs, UChar uhYs, UChar uhXe, UChar uhYe, UChar uhOri, WedgeResolution eWedgeRes, Bool bIsCoarse )
101{
102  m_uhXs      = uhXs;
103  m_uhYs      = uhYs;
104  m_uhXe      = uhXe;
105  m_uhYe      = uhYe;
106  m_uhOri     = uhOri;
107  m_eWedgeRes = eWedgeRes;
108  m_bIsCoarse = bIsCoarse;
109
110  xGenerateWedgePattern();
111}
112
113Bool TComWedgelet::checkNotPlain()
114{
115  for( UInt k = 1; k < (m_uiWidth * m_uiHeight); k++ )
116  {
117    if( m_pbPattern[0] != m_pbPattern[k] )
118    {
119      return true;
120    }
121  }
122  return false;
123}
124
125Bool TComWedgelet::checkIdentical( Bool* pbRefPattern )
126{
127  for( UInt k = 0; k < (m_uiWidth * m_uiHeight); k++ )
128  {
129    if( m_pbPattern[k] != pbRefPattern[k] )
130    {
131      return false;
132    }
133  }
134  return true;
135}
136
137Bool TComWedgelet::checkInvIdentical( Bool* pbRefPattern )
138{
139  for( UInt k = 0; k < (m_uiWidth * m_uiHeight); k++ )
140  {
141    if( m_pbPattern[k] == pbRefPattern[k] )
142    {
143      return false;
144    }
145  }
146  return true;
147}
148
149Void TComWedgelet::generateWedgePatternByRotate(const TComWedgelet &rcWedge, Int rotate)
150{
151  Int stride = m_uiWidth;
152  Int sinc, offsetI, offsetJ;
153 
154  sinc = 1;
155  offsetI = ( sinc) < 0 ? stride-1 : 0; // 0
156  offsetJ = (-sinc) < 0 ? stride-1 : 0; // stride - 1
157
158  for (Int y = 0; y < stride; y++)
159  {
160    for (Int x = 0; x < stride; x++)
161    {
162      Int i = offsetI + sinc * y; // y
163      Int j = offsetJ - sinc * x; // stride - 1 - x
164      m_pbPattern[(y * stride) + x] = !rcWedge.m_pbPattern[(j * stride) + i];
165    }
166  }
167  Int blocksize = rcWedge.m_uiWidth * (rcWedge.m_eWedgeRes == HALF_PEL ? 2 : 1);
168  Int offsetX = (-sinc) < 0 ? blocksize - 1 : 0;
169  Int offsetY = ( sinc) < 0 ? blocksize - 1 : 0;
170  m_uhXs = offsetX - sinc * rcWedge.m_uhYs;
171  m_uhYs = offsetY + sinc * rcWedge.m_uhXs;
172  m_uhXe = offsetX - sinc * rcWedge.m_uhYe;
173  m_uhYe = offsetY + sinc * rcWedge.m_uhXe;
174  m_uhOri = rotate;
175  m_eWedgeRes = rcWedge.m_eWedgeRes;
176  m_bIsCoarse = rcWedge.m_bIsCoarse;
177  m_uiWidth  = rcWedge.m_uiWidth;
178  m_uiHeight = rcWedge.m_uiHeight;
179}
180
181Void TComWedgelet::xGenerateWedgePattern()
182{
183  UInt uiTempBlockSize = 0;
184  UChar uhXs = 0, uhYs = 0, uhXe = 0, uhYe = 0;
185  switch( m_eWedgeRes )
186  {
187  case(   FULL_PEL ): { uiTempBlockSize =  m_uiWidth;     uhXs =  m_uhXs;     uhYs =  m_uhYs;     uhXe =  m_uhXe;     uhYe =  m_uhYe;     } break;
188  case(   HALF_PEL ): { uiTempBlockSize = (m_uiWidth<<1); uhXs =  m_uhXs;     uhYs =  m_uhYs;     uhXe =  m_uhXe;     uhYe =  m_uhYe;     } break;
189  }
190
191  Bool* pbTempPattern = new Bool[ (uiTempBlockSize * uiTempBlockSize) ];
192  ::memset( pbTempPattern, 0, (uiTempBlockSize * uiTempBlockSize) * sizeof(Bool) );
193  Int iTempStride = uiTempBlockSize;
194
195  xDrawEdgeLine( uhXs, uhYs, uhXe, uhYe, pbTempPattern, iTempStride );
196
197  Int shift = (m_eWedgeRes == HALF_PEL) ? 1 : 0;
198  Int endPos = uhYe>>shift;
199  for (Int y = 0; y <= endPos; y++)
200  {
201    for (Int x = 0; x < m_uiWidth && pbTempPattern[(y * m_uiWidth) + x] == 0; x++)
202    {
203      pbTempPattern[(y * m_uiWidth) + x] = true;
204    }
205  }
206  for( UInt k = 0; k < (m_uiWidth * m_uiHeight); k++ )
207  {
208    m_pbPattern[k] = pbTempPattern[k];
209  };
210
211  if( pbTempPattern )
212  {
213    delete [] pbTempPattern;
214    pbTempPattern = NULL;
215  }
216}
217
218Void TComWedgelet::xDrawEdgeLine( UChar uhXs, UChar uhYs, UChar uhXe, UChar uhYe, Bool* pbPattern, Int iPatternStride )
219{
220  Int x0 = (Int)uhXs;
221  Int y0 = (Int)uhYs;
222  Int x1 = (Int)uhXe;
223  Int y1 = (Int)uhYe;
224
225  // direction independent Bresenham line
226  bool steep = (abs(y1 - y0) > abs(x1 - x0));
227  if( steep )
228  {
229    std::swap( x0, y0 );
230    std::swap( x1, y1 );
231  }
232
233  bool backward = ( x0 > x1 );
234  if( backward )
235  {
236    std::swap( x0, x1 );
237    std::swap( y0, y1 );
238  }
239
240  Int deltax = x1 - x0;
241  Int deltay = abs(y1 - y0);
242  Int error = 0;
243  Int deltaerr = (deltay<<1);
244
245  Int ystep;
246  Int y = y0;
247  if( y0 < y1 ) ystep =  1;
248  else          ystep = -1;
249
250  for( Int x = x0; x <= x1; x++ )
251  {
252    Int shift = (m_eWedgeRes == HALF_PEL) ? 1 : 0;
253    Int stride = iPatternStride >> shift;
254    if( steep ) { pbPattern[((x>>shift) * stride) + (y>>shift)] = true; }
255    else        { pbPattern[((y>>shift) * stride) + (x>>shift)] = true; }
256
257    error += deltaerr;
258    if( error >= deltax )
259    {
260      y += ystep;
261      error = error - (deltax<<1);
262    }
263  }
264}
265
266Bool* TComWedgelet::getPatternScaled( UInt dstSize )
267{
268  Bool *pbSrcPat = this->getPattern();
269  UInt uiSrcSize = this->getStride();
270
271  if( 16 >= dstSize )
272  {
273    assert( dstSize == uiSrcSize );
274    return pbSrcPat;
275  }
276  else
277  {
278    Int scale = (g_aucConvertToBit[dstSize] - g_aucConvertToBit[uiSrcSize]);
279    assert(scale>=0);
280    for (Int y=0; y<dstSize; y++)
281    {
282      for (Int x=0; x<dstSize; x++)
283      {
284        Int srcX = x>>scale;
285        Int srcY = y>>scale;
286        m_pbScaledPattern[y*dstSize + x] = pbSrcPat[ srcY*uiSrcSize + srcX ];
287      }
288    }
289    return m_pbScaledPattern;
290  }
291}
292
293Void TComWedgelet::getPatternScaledCopy( UInt dstSize, Bool* dstBuf )
294{
295  Bool *pbSrcPat = this->getPattern();
296  UInt uiSrcSize = this->getStride();
297
298  if( 16 >= dstSize )
299  {
300    assert( dstSize == uiSrcSize );
301    memcpy( dstBuf, pbSrcPat, (dstSize*dstSize) );
302  }
303  else
304  {
305    Int scale = (g_aucConvertToBit[dstSize] - g_aucConvertToBit[uiSrcSize]);
306    assert(scale>=0);
307    for (Int y=0; y<dstSize; y++)
308    {
309      for (Int x=0; x<dstSize; x++)
310      {
311        Int srcX = x>>scale;
312        Int srcY = y>>scale;
313        dstBuf[y*dstSize + x] = pbSrcPat[ srcY*uiSrcSize + srcX ];
314      }
315    }
316  }
317}
318
319
320TComWedgeNode::TComWedgeNode()
321{
322  m_uiPatternIdx = DMM_NO_WEDGE_IDX;
323  for( UInt uiPos = 0; uiPos < DMM_NUM_WEDGE_REFINES; uiPos++ )
324  {
325    m_uiRefineIdx[uiPos] = DMM_NO_WEDGE_IDX;
326  }
327}
328
329UInt TComWedgeNode::getPatternIdx()
330{
331  return m_uiPatternIdx;
332}
333UInt TComWedgeNode::getRefineIdx( UInt uiPos )
334{
335  assert( uiPos < DMM_NUM_WEDGE_REFINES );
336  return m_uiRefineIdx[uiPos];
337}
338Void TComWedgeNode::setPatternIdx( UInt uiIdx )
339{
340  m_uiPatternIdx = uiIdx;
341}
342Void TComWedgeNode::setRefineIdx( UInt uiIdx, UInt uiPos )
343{
344  assert( uiPos < DMM_NUM_WEDGE_REFINES );
345  m_uiRefineIdx[uiPos] = uiIdx; 
346}
347#endif //NH_3D_DMM
Note: See TracBrowser for help on using the repository browser.