geometry_octree_decoder.cpp 17.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* The copyright in this software is being made available under the BSD
 * Licence, included below.  This software may be subject to other third
 * party and contributor rights, including patent rights, and no such
 * rights are granted under this licence.
 *
 * Copyright (c) 2017-2018, ISO/IEC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of the ISO/IEC nor the names of its contributors
 *   may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

36
#include "geometry.h"
37

38
#include "DualLutCoder.h"
39
#include "OctreeNeighMap.h"
40
#include "geometry_octree.h"
41
#include "geometry_intra_pred.h"
42
43
44
#include "io_hls.h"
#include "tables.h"

45
46
namespace pcc {

47
48
49
50
//============================================================================

class GeometryOctreeDecoder {
public:
51
  GeometryOctreeDecoder(
52
    const GeometryParameterSet& gps, EntropyDecoder* arithmeticDecoder);
53
54

  void beginOctreeLevel();
55
56
57

  int decodePositionLeafNumPoints();

58
59
60
61
62
  int decodeOccupancyNeighZ(
    int mappedOccIsPredicted,
    int mappedOccPrediction,
    int mappedOccAdjGt0,
    int mappedOccAdjGt1);
63

64
  int decodeOccupancyNeighNZ(
65
66
67
68
69
    int neighPattern10,
    int mappedOccIsPredicted,
    int mappedOccPrediction,
    int mappedOccAdjGt0,
    int mappedOccAdjGt1);
70

71
  int decodeOccupancyBitwise(
72
73
74
75
76
    int neighPattern,
    int mappedOccIsPredicted,
    int mappedOccPrediction,
    int mappedOccAdjGt0,
    int mappedOccAdjGt1);
77

78
79
  int decodeOccupancyBytewise(int neighPattern);

80
  uint32_t decodeOccupancy(
81
82
83
84
85
    int neighPattern,
    int occupancyIsPredicted,
    int occupancyPrediction,
    int occupancyAdjGt0,
    int occupancyAdjGt1);
86

87
  Vec3<uint32_t> decodePointPosition(int nodeSizeLog2);
88
89
90
91
92
93

  template<class OutputIt>
  int decodeDirectPosition(
    int nodeSizeLog2, const PCCOctree3Node& node, OutputIt outputPoints);

private:
94
95
96
  // selects between the bitwise and bytewise occupancy coders
  const bool _useBitwiseOccupancyCoder;

97
98
  const uint8_t (&_neighPattern64toR1)[64];

99
100
101
102
103
104
105
  EntropyDecoder* _arithmeticDecoder;
  StaticBitModel _ctxEquiProb;
  AdaptiveBitModel _ctxSingleChild;
  AdaptiveBitModel _ctxSinglePointPerBlock;
  AdaptiveBitModel _ctxPointCountPerBlock;
  AdaptiveBitModel _ctxBlockSkipTh;
  AdaptiveBitModel _ctxNumIdcmPointsEq1;
106
107

  // For bitwise occupancy coding
108
109
110
111
112
113
  //   maps 0 + i: no adjacency
  //   maps 3 + i: adjacency > 0
  //   maps 6 + i: adjacency > 1
  //   map i = 0 = not predicted
  //   map i = 1 = predicted unnoccupied
  //   map i = 2 = predicted occupied
114
  CtxModelOctreeOccupancy _ctxOccupancy;
115
  CtxMapOctreeOccupancy _ctxIdxMaps[9];
116
117
118

  // For bytewise occupancy coding
  DualLutCoder<true> _bytewiseOccupancyCoder[10];
119
120
121
122
123
};

//============================================================================

GeometryOctreeDecoder::GeometryOctreeDecoder(
124
  const GeometryParameterSet& gps, EntropyDecoder* arithmeticDecoder)
125
  : _useBitwiseOccupancyCoder(gps.bitwise_occupancy_coding_flag)
126
  , _neighPattern64toR1(neighPattern64toR1(gps))
127
  , _arithmeticDecoder(arithmeticDecoder)
128
  , _ctxOccupancy(gps.geom_occupancy_ctx_reduction_factor)
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
{
  if (!_useBitwiseOccupancyCoder) {
    for (int i = 0; i < 10; i++)
      _bytewiseOccupancyCoder[i].init(kDualLutOccupancyCoderInit[i]);
  }
}
//============================================================================

void
GeometryOctreeDecoder::beginOctreeLevel()
{
  for (int i = 0; i < 10; i++) {
    _bytewiseOccupancyCoder[i].resetLut();
  }
}
144

145
146
147
148
//============================================================================
// Decode the number of points in a leaf node of the octree.

int
149
GeometryOctreeDecoder::decodePositionLeafNumPoints()
150
151
{
  const bool isSinglePoint =
152
    _arithmeticDecoder->decode(_ctxSinglePointPerBlock) != 0;
153
154
155

  int count = 1;
  if (!isSinglePoint) {
156
157
158
    count += 1
      + _arithmeticDecoder->decodeExpGolomb(
          0, _ctxEquiProb, _ctxPointCountPerBlock);
159
160
161
162
163
164
165
166
167
  }

  return count;
}

//---------------------------------------------------------------------------
// decode occupancy bits (neighPattern10 == 0 case)

int
168
GeometryOctreeDecoder::decodeOccupancyNeighZ(
169
170
171
172
  int mappedOccIsPredicted,
  int mappedOccPrediction,
  int mappedOccAdjGt0,
  int mappedOccAdjGt1)
173
{
174
  int minOccupied = 2;
175
176
177
  int numOccupiedAcc = 0;
  int occupancy = 0;

178
179
  for (int i = 0; i < 8; i++) {
    int bit = 1;
180
181
182
183
    int bitIsPredicted = (mappedOccIsPredicted >> kOccBitCodingOrder[i]) & 1;
    int bitPrediction = (mappedOccPrediction >> kOccBitCodingOrder[i]) & 1;
    int bitAdjGt0 = (mappedOccAdjGt0 >> kOccBitCodingOrder[i]) & 1;
    int bitAdjGt1 = (mappedOccAdjGt1 >> kOccBitCodingOrder[i]) & 1;
184

185
186
    int ctxIdxMapIdx =
      3 * (bitAdjGt0 + bitAdjGt1) + bitIsPredicted + bitPrediction;
187
188
    auto& ctxIdxMap = _ctxIdxMaps[ctxIdxMapIdx];

189
190
191
    // NB: There must be at least two occupied child nodes
    //  -- avoid coding the occupancy bit if it is implied.
    if (numOccupiedAcc >= minOccupied + i - 7) {
192
      int ctxIdx = ctxIdxMap[i][numOccupiedAcc];
193
      bit = _arithmeticDecoder->decode(_ctxOccupancy[ctxIdx]);
194
    }
195
    ctxIdxMap.evolve(bit, &ctxIdxMap[i][numOccupiedAcc]);
196
    numOccupiedAcc += bit;
197
    occupancy |= bit << kOccBitCodingOrder[i];
198
  }
199
200
201
202
203
204
205
206

  return occupancy;
}

//---------------------------------------------------------------------------
// decode occupancy bits (neighPattern10 != 0 case)

int
207
GeometryOctreeDecoder::decodeOccupancyNeighNZ(
208
209
210
211
212
  int neighPattern10,
  int mappedOccIsPredicted,
  int mappedOccPrediction,
  int mappedOccAdjGt0,
  int mappedOccAdjGt1)
213
214
215
{
  int neighPattern7 = kNeighPattern10to7[neighPattern10];
  int neighPattern5 = kNeighPattern7to5[neighPattern7];
216
217
218
219
220
221
222

  int occupancy = 0;
  int partialOccupancy = 0;

  // NB: it is impossible for pattern to be 0 (handled in Z case).
  // NB: offsets are added since ctxIdxMap is shared between Z and NZ cases.
  for (int i = 0; i < 8; i++) {
223
224
225
    int idx;
    if (i < 6) {
      idx = ((neighPattern10 - 1) << i) + partialOccupancy + i + 1;
226
    } else if (i == 6) {
227
      idx = ((neighPattern7 - 1) << i) + partialOccupancy + i + 1;
228
    } else if (i == 7) {
229
      idx = ((neighPattern5 - 1) << i) + partialOccupancy + i + 1;
230
231
232
233
234
235
    } else {
      // work around clang -Wsometimes-uninitialized fault
      break;
    }

    // NB: if firt 7 bits are 0, then the last is implicitly 1.
236
    int bit = 1;
237
238
239
240
    int bitIsPredicted = (mappedOccIsPredicted >> kOccBitCodingOrder[i]) & 1;
    int bitPrediction = (mappedOccPrediction >> kOccBitCodingOrder[i]) & 1;
    int bitAdjGt0 = (mappedOccAdjGt0 >> kOccBitCodingOrder[i]) & 1;
    int bitAdjGt1 = (mappedOccAdjGt1 >> kOccBitCodingOrder[i]) & 1;
241

242
243
    int ctxIdxMapIdx =
      3 * (bitAdjGt0 + bitAdjGt1) + bitIsPredicted + bitPrediction;
244
245
    auto& ctxIdxMap = _ctxIdxMaps[ctxIdxMapIdx];

246
    if (i < 7 || partialOccupancy) {
247
      int ctxIdx = ctxIdxMap[i][idx];
248
249
250
      bit = _arithmeticDecoder->decode(_ctxOccupancy[ctxIdx]);
    }

251
    ctxIdxMap.evolve(bit, &ctxIdxMap[i][idx]);
252
    partialOccupancy |= bit << i;
253
    occupancy |= bit << kOccBitCodingOrder[i];
254
  }
255
256
257
258

  return occupancy;
}

259
260
261
//-------------------------------------------------------------------------

int
262
GeometryOctreeDecoder::decodeOccupancyBitwise(
263
264
265
266
267
  int neighPattern,
  int mappedOccIsPredicted,
  int mappedOccPrediction,
  int mappedOccAdjGt0,
  int mappedOccAdjGt1)
268
269
{
  if (neighPattern == 0) {
270
271
272
    return decodeOccupancyNeighZ(
      mappedOccIsPredicted, mappedOccPrediction, mappedOccAdjGt0,
      mappedOccAdjGt1);
273
274
275
  }

  // code occupancy using the neighbour configuration context
276
277
  // with reduction from 64 states to 10 (or 6).
  int neighPatternR1 = _neighPattern64toR1[neighPattern];
278
  return decodeOccupancyNeighNZ(
279
280
    neighPatternR1, mappedOccIsPredicted, mappedOccPrediction, mappedOccAdjGt0,
    mappedOccAdjGt1);
281
282
}

283
284
285
286
287
288
//-------------------------------------------------------------------------

int
GeometryOctreeDecoder::decodeOccupancyBytewise(int neighPattern)
{
  // code occupancy using the neighbour configuration context
289
290
291
  // with reduction from 64 states to 10 (or 6).
  int neighPatternR1 = _neighPattern64toR1[neighPattern];
  auto& bytewiseCoder = _bytewiseOccupancyCoder[neighPatternR1];
292
293
294
  return bytewiseCoder.decode(_arithmeticDecoder);
}

295
296
297
//-------------------------------------------------------------------------
// decode node occupancy bits
//
298

299
uint32_t
300
GeometryOctreeDecoder::decodeOccupancy(
301
302
303
304
305
  int neighPattern,
  int occupancyIsPred,
  int occupancyPred,
  int occupancyAdjGt0,
  int occupancyAdjGt1)
306
307
308
{
  // decode occupancy pattern
  uint32_t occupancy;
309
  if (neighPattern == 0) {
310
    // neighbour empty and only one point => decode index, not pattern
311
312
313
314
    if (_arithmeticDecoder->decode(_ctxSingleChild)) {
      uint32_t cnt = _arithmeticDecoder->decode(_ctxEquiProb);
      cnt |= _arithmeticDecoder->decode(_ctxEquiProb) << 1;
      cnt |= _arithmeticDecoder->decode(_ctxEquiProb) << 2;
315
      occupancy = 1 << cnt;
316
      return occupancy;
317
318
    }
  }
319

320
321
  uint32_t mapOccIsP = mapGeometryOccupancy(occupancyIsPred, neighPattern);
  uint32_t mapOccP = mapGeometryOccupancy(occupancyPred, neighPattern);
322
323
  uint32_t mapAdjGt0 = mapGeometryOccupancy(occupancyAdjGt0, neighPattern);
  uint32_t mapAdjGt1 = mapGeometryOccupancy(occupancyAdjGt1, neighPattern);
324
325
326
  uint32_t mappedOccupancy;

  if (_useBitwiseOccupancyCoder)
327
328
    mappedOccupancy = decodeOccupancyBitwise(
      neighPattern, mapOccIsP, mapOccP, mapAdjGt0, mapAdjGt1);
329
330
  else
    mappedOccupancy = decodeOccupancyBytewise(neighPattern);
331
332

  return mapGeometryOccupancyInv(mappedOccupancy, neighPattern);
333
334
335
336
337
}

//-------------------------------------------------------------------------
// Decode a position of a point in a given volume.

338
Vec3<uint32_t>
339
GeometryOctreeDecoder::decodePointPosition(int nodeSizeLog2)
340
{
341
  Vec3<uint32_t> delta{};
342
343
  for (int i = nodeSizeLog2; i > 0; i--) {
    delta <<= 1;
344
345
346
    delta[0] |= _arithmeticDecoder->decode(_ctxEquiProb);
    delta[1] |= _arithmeticDecoder->decode(_ctxEquiProb);
    delta[2] |= _arithmeticDecoder->decode(_ctxEquiProb);
347
348
349
350
351
352
353
354
355
356
357
358
  }

  return delta;
}

//-------------------------------------------------------------------------
// Direct coding of position of points in node (early tree termination).
// Decoded points are written to @outputPoints
// Returns the number of points emitted.

template<class OutputIt>
int
359
360
GeometryOctreeDecoder::decodeDirectPosition(
  int nodeSizeLog2, const PCCOctree3Node& node, OutputIt outputPoints)
361
{
362
  bool isDirectMode = _arithmeticDecoder->decode(_ctxBlockSkipTh);
363
364
365
366
367
  if (!isDirectMode) {
    return 0;
  }

  int numPoints = 1;
368
  if (_arithmeticDecoder->decode(_ctxNumIdcmPointsEq1))
369
370
371
372
    numPoints++;

  for (int i = 0; i < numPoints; i++) {
    // convert node-relative position to world position
373
    Vec3<uint32_t> pos = node.pos + decodePointPosition(nodeSizeLog2);
374
375
376
377
378
379
380
381
382
    *(outputPoints++) = {double(pos[0]), double(pos[1]), double(pos[2])};
  }

  return numPoints;
}

//-------------------------------------------------------------------------

void
383
384
385
386
decodeGeometryOctree(
  const GeometryParameterSet& gps,
  const GeometryBrickHeader& gbh,
  PCCPointSet3& pointCloud,
387
  EntropyDecoder* arithmeticDecoder,
388
  pcc::ringbuf<PCCOctree3Node>* nodesRemaining)
389
{
390
  GeometryOctreeDecoder decoder(gps, arithmeticDecoder);
391
392
393
394

  // init main fifo
  //  -- worst case size is the last level containing every input poit
  //     and each point being isolated in the previous level.
395
  pcc::ringbuf<PCCOctree3Node> fifo(gbh.geom_num_points + 1);
396
397
398
399
400
401
402
403

  // the current node dimension (log2)
  int nodeSizeLog2 = gbh.geom_max_node_size_log2;

  // push the first node
  fifo.emplace_back();
  PCCOctree3Node& node00 = fifo.back();
  node00.start = uint32_t(0);
404
  node00.end = uint32_t(0);
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  node00.pos = uint32_t(0);
  node00.neighPattern = 0;
  node00.numSiblingsPlus1 = 8;
  node00.siblingOccupancy = 0;

  size_t processedPointCount = 0;
  std::vector<uint32_t> values;

  auto fifoCurrLvlEnd = fifo.end();

  // this counter represents fifo.end() - fifoCurrLvlEnd().
  // ie, the number of nodes added to the next level of the tree
  int numNodesNextLvl = 0;

419
  Vec3<uint32_t> occupancyAtlasOrigin(0xffffffff);
420
  MortonMap3D occupancyAtlas;
421
422
  if (gps.neighbour_avail_boundary_log2) {
    occupancyAtlas.resize(gps.neighbour_avail_boundary_log2);
423
424
425
426
427
428
429
430
431
432
    occupancyAtlas.clear();
  }

  for (; !fifo.empty(); fifo.pop_front()) {
    if (fifo.begin() == fifoCurrLvlEnd) {
      // transition to the next level
      fifoCurrLvlEnd = fifo.end();
      nodeSizeLog2--;
      numNodesNextLvl = 0;
      occupancyAtlasOrigin = 0xffffffff;
433

434
435
      decoder.beginOctreeLevel();

436
      // allow partial tree encoding using trisoup
437
      if (nodeSizeLog2 == gps.trisoup_node_size_log2)
438
        break;
439
    }
440

441
442
    PCCOctree3Node& node0 = fifo.front();

443
444
445
    int occupancyAdjacencyGt0 = 0;
    int occupancyAdjacencyGt1 = 0;

446
    if (gps.neighbour_avail_boundary_log2) {
447
448
449
450
      updateGeometryOccupancyAtlas(
        node0.pos, nodeSizeLog2, fifo, fifoCurrLvlEnd, &occupancyAtlas,
        &occupancyAtlasOrigin);

451
452
453
454
      GeometryNeighPattern gnp = makeGeometryNeighPattern(
        gps.adjacent_child_contextualization_enabled_flag, node0.pos,
        nodeSizeLog2, occupancyAtlas);

455
456
457
      node0.neighPattern = gnp.neighPattern;
      occupancyAdjacencyGt0 = gnp.adjacencyGt0;
      occupancyAdjacencyGt1 = gnp.adjacencyGt1;
458
459
    }

460
461
462
    int occupancyIsPredicted = 0;
    int occupancyPrediction = 0;

463
464
465
466
467
468
469
    // generate intra prediction
    if (nodeSizeLog2 < gps.intra_pred_max_node_size_log2) {
      predictGeometryOccupancyIntra(
        occupancyAtlas, node0.pos, nodeSizeLog2, &occupancyIsPredicted,
        &occupancyPrediction);
    }

470
    // decode occupancy pattern
471
    uint8_t occupancy = decoder.decodeOccupancy(
472
473
      node0.neighPattern, occupancyIsPredicted, occupancyPrediction,
      occupancyAdjacencyGt0, occupancyAdjacencyGt1);
474
475
476

    assert(occupancy > 0);

477
    // update atlas for advanced neighbours
478
479
480
481
    if (gps.neighbour_avail_boundary_log2) {
      updateGeometryOccupancyAtlasOccChild(
        node0.pos, nodeSizeLog2, occupancy, &occupancyAtlas);
    }
482

483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
    // population count of occupancy for IDCM
    int numOccupied = popcnt(occupancy);

    // split the current node
    for (int i = 0; i < 8; i++) {
      uint32_t mask = 1 << i;
      if (!(occupancy & mask)) {
        // child is empty: skip
        continue;
      }

      int x = !!(i & 4);
      int y = !!(i & 2);
      int z = !!(i & 1);

      int childSizeLog2 = nodeSizeLog2 - 1;

      // point counts for leaf nodes are coded immediately upon
      // encountering the leaf node.
      if (childSizeLog2 == 0) {
        int numPoints = 1;

505
        if (!gps.geom_unique_points_flag) {
506
          numPoints = decoder.decodePositionLeafNumPoints();
507
508
        }

509
        const Vec3<double> point(
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
          node0.pos[0] + (x << childSizeLog2),
          node0.pos[1] + (y << childSizeLog2),
          node0.pos[2] + (z << childSizeLog2));

        for (int i = 0; i < numPoints; ++i)
          pointCloud[processedPointCount++] = point;

        // do not recurse into leaf nodes
        continue;
      }

      // create & enqueue new child.
      fifo.emplace_back();
      auto& child = fifo.back();

      child.pos[0] = node0.pos[0] + (x << childSizeLog2);
      child.pos[1] = node0.pos[1] + (y << childSizeLog2);
      child.pos[2] = node0.pos[2] + (z << childSizeLog2);
      child.numSiblingsPlus1 = numOccupied;
      child.siblingOccupancy = occupancy;

531
      bool idcmEnabled = gps.inferred_direct_coding_mode_enabled_flag;
532
      if (isDirectModeEligible(idcmEnabled, nodeSizeLog2, node0, child)) {
533
534
        int numPoints = decoder.decodeDirectPosition(
          childSizeLog2, child, &pointCloud[processedPointCount]);
535
536
537
538
539
540
541
542
543
544
545
546
547
548
        processedPointCount += numPoints;

        if (numPoints > 0) {
          // node fully decoded, do not split: discard child
          fifo.pop_back();

          // NB: no further siblings to decode by definition of IDCM
          assert(child.numSiblingsPlus1 == 1);
          break;
        }
      }

      numNodesNextLvl++;

549
      if (!gps.neighbour_avail_boundary_log2) {
550
        updateGeometryNeighState(
551
552
          gps.neighbour_context_restriction_flag, fifo.end(), numNodesNextLvl,
          childSizeLog2, child, i, node0.neighPattern, occupancy);
553
554
555
      }
    }
  }
556
557
558
559
560
561
562
563
564
565
566
567
568
569

  // return partial coding result
  if (nodesRemaining) {
    *nodesRemaining = std::move(fifo);
  }
}

//-------------------------------------------------------------------------

void
decodeGeometryOctree(
  const GeometryParameterSet& gps,
  const GeometryBrickHeader& gbh,
  PCCPointSet3& pointCloud,
570
  EntropyDecoder* arithmeticDecoder)
571
572
{
  decodeGeometryOctree(gps, gbh, pointCloud, arithmeticDecoder, nullptr);
573
574
575
576
577
}

//============================================================================

}  // namespace pcc