decoder.cpp 10.3 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
36
37
38
39
40
41
42
43
/* 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.
 */

#include "PCCTMC3Decoder.h"

#include <cassert>
#include <string>

#include "AttributeDecoder.h"
#include "PayloadBuffer.h"
#include "PCCPointSet.h"
44
#include "geometry.h"
45
46
47
48
49
50
51
52
53
54
55
56
#include "io_hls.h"
#include "io_tlv.h"
#include "pcc_chrono.h"
#include "osspecific.h"

namespace pcc {

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

void
PCCTMC3Decoder3::init()
{
57
  _currentFrameIdx = -1;
58
59
60
61
62
63
64
65
66
  _sps = nullptr;
  _gps = nullptr;
  _spss.clear();
  _gpss.clear();
  _apss.clear();
}

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

67
68
69
70
71
72
73
74
75
static bool
payloadStartsNewSlice(PayloadType type)
{
  return type == PayloadType::kGeometryBrick
    || type == PayloadType::kFrameBoundaryMarker;
}

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

76
77
int
PCCTMC3Decoder3::decompress(
78
  const PayloadBuffer* buf, PCCTMC3Decoder3::Callbacks* callback)
79
{
80
81
  // Starting a new geometry brick/slice/tile, transfer any
  // finished points to the output accumulator
82
  if (!buf || payloadStartsNewSlice(buf->type)) {
83
84
85
86
    if (size_t numPoints = _currentPointCloud.getPointCount()) {
      for (size_t i = 0; i < numPoints; i++)
        for (int k = 0; k < 3; k++)
          _currentPointCloud[i][k] += _sliceOrigin[k];
87
      _accumCloud.append(_currentPointCloud);
88
89
90
    }
  }

91
92
  if (!buf) {
    // flush decoder, output pending cloud if any
93
    callback->onOutputCloud(*_sps, _accumCloud);
94
    _accumCloud.clear();
95
96
97
98
99
100
101
102
103
104
    return 0;
  }

  switch (buf->type) {
  case PayloadType::kSequenceParameterSet: storeSps(parseSps(*buf)); return 0;

  case PayloadType::kGeometryParameterSet: storeGps(parseGps(*buf)); return 0;

  case PayloadType::kAttributeParameterSet: storeAps(parseAps(*buf)); return 0;

105
106
107
108
  // the frame boundary marker flushes the current frame.
  // NB: frame counter is reset to avoid outputing a runt point cloud
  //     on the next slice.
  case PayloadType::kFrameBoundaryMarker:
109
110
    // todo(df): if no sps is activated ...
    callback->onOutputCloud(*_sps, _accumCloud);
111
112
113
114
    _accumCloud.clear();
    _currentFrameIdx = -1;
    return 0;

115
  case PayloadType::kGeometryBrick:
116
117
    activateParameterSets(parseGbhIds(*buf));
    if (frameIdxChanged(parseGbh(*_sps, *_gps, *buf, nullptr))) {
118
      callback->onOutputCloud(*_sps, _accumCloud);
119
120
      _accumCloud.clear();
    }
121
122
123
    return decodeGeometryBrick(*buf);

  case PayloadType::kAttributeBrick: decodeAttributeBrick(*buf); return 0;
124
125
126
127

  case PayloadType::kTileInventory:
    storeTileInventory(parseTileInventory(*buf));
    return 0;
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  }

  // todo(df): error, unhandled payload type
  return 1;
}

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

void
PCCTMC3Decoder3::storeSps(SequenceParameterSet&& sps)
{
  // todo(df): handle replacement semantics
  _spss.emplace(std::make_pair(sps.sps_seq_parameter_set_id, sps));
}

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

void
PCCTMC3Decoder3::storeGps(GeometryParameterSet&& gps)
{
  // todo(df): handle replacement semantics
  _gpss.emplace(std::make_pair(gps.gps_geom_parameter_set_id, gps));
}

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

void
PCCTMC3Decoder3::storeAps(AttributeParameterSet&& aps)
{
  // todo(df): handle replacement semantics
  _apss.emplace(std::make_pair(aps.aps_attr_parameter_set_id, aps));
}

161
162
163
164
165
166
167
168
169
//--------------------------------------------------------------------------

void
PCCTMC3Decoder3::storeTileInventory(TileInventory&& inventory)
{
  // todo(df): handle replacement semantics
  _tileInventory = inventory;
}

170
171
//==========================================================================

172
173
bool
PCCTMC3Decoder3::frameIdxChanged(const GeometryBrickHeader& gbh) const
174
{
175
176
177
178
179
  // dont treat the first frame in the sequence as a frame boundary
  if (_currentFrameIdx < 0)
    return false;
  return _currentFrameIdx != gbh.frame_idx;
}
180

181
182
183
184
185
//==========================================================================

void
PCCTMC3Decoder3::activateParameterSets(const GeometryBrickHeader& gbh)
{
186
187
188
189
190
191
192
  // HACK: assume activation of the first SPS and GPS
  // todo(df): parse brick header here for propper sps & gps activation
  //  -- this is currently inconsistent between trisoup and octree
  assert(!_spss.empty());
  assert(!_gpss.empty());
  _sps = &_spss.cbegin()->second;
  _gps = &_gpss.cbegin()->second;
193
194
195
196
197
198
199
200
201
202
}

//==========================================================================
// Initialise the point cloud storage and decode a single geometry slice.

int
PCCTMC3Decoder3::decodeGeometryBrick(const PayloadBuffer& buf)
{
  assert(buf.type == PayloadType::kGeometryBrick);
  std::cout << "positions bitstream size " << buf.size() << " B\n";
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

  // todo(df): replace with attribute mapping
  bool hasColour = std::any_of(
    _sps->attributeSets.begin(), _sps->attributeSets.end(),
    [](const AttributeDescription& desc) {
      return desc.attributeLabel == KnownAttributeLabel::kColour;
    });

  bool hasReflectance = std::any_of(
    _sps->attributeSets.begin(), _sps->attributeSets.end(),
    [](const AttributeDescription& desc) {
      return desc.attributeLabel == KnownAttributeLabel::kReflectance;
    });

  _currentPointCloud.clear();
  _currentPointCloud.addRemoveAttributes(hasColour, hasReflectance);

  pcc::chrono::Stopwatch<pcc::chrono::utime_inc_children_clock> clock_user;
  clock_user.start();

223
  int gbhSize;
224
  _gbh = parseGbh(*_sps, *_gps, buf, &gbhSize);
225
226
  _sliceId = _gbh.geom_slice_id;
  _sliceOrigin = _gbh.geomBoxOrigin;
227
  _currentFrameIdx = _gbh.frame_idx;
228

229
  EntropyDecoder arithmeticDecoder;
230
  arithmeticDecoder.enableBypassStream(_sps->cabac_bypass_stream_enabled_flag);
231
232
  arithmeticDecoder.setBuffer(int(buf.size()) - gbhSize, buf.data() + gbhSize);
  arithmeticDecoder.start();
233

234
  if (_gps->trisoup_node_size_log2 == 0) {
235
236
237
238
239
240
241
242
243
244
    _currentPointCloud.resize(_gbh.geom_num_points);

    if (!_params.minGeomNodeSizeLog2) {
      decodeGeometryOctree(
        *_gps, _gbh, _currentPointCloud, &arithmeticDecoder);
    } else {
      decodeGeometryOctreeScalable(
        *_gps, _gbh, _params.minGeomNodeSizeLog2, _currentPointCloud,
        &arithmeticDecoder);
    }
245
  } else {
246
    decodeGeometryTrisoup(*_gps, _gbh, _currentPointCloud, &arithmeticDecoder);
247
  }
248

249
  arithmeticDecoder.stop();
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
  clock_user.stop();

  auto total_user =
    std::chrono::duration_cast<std::chrono::milliseconds>(clock_user.count());
  std::cout << "positions processing time (user): "
            << total_user.count() / 1000.0 << " s\n";
  std::cout << std::endl;

  return 0;
}

//--------------------------------------------------------------------------
// Initialise the point cloud storage and decode a single geometry brick.

void
PCCTMC3Decoder3::decodeAttributeBrick(const PayloadBuffer& buf)
{
  assert(buf.type == PayloadType::kAttributeBrick);
  // todo(df): replace assertions with error handling
  assert(_sps);
  assert(_gps);

272
  // verify that this corresponds to the correct geometry slice
273
  AttributeBrickHeader abh = parseAbhIds(buf);
274
275
276
  assert(abh.attr_geom_slice_id == _sliceId);

  // todo(df): validate that sps activation is not changed via the APS
277
278
279
280
281
282
283
284
285
286
287
288
289
  const auto it_attr_aps = _apss.find(abh.attr_attr_parameter_set_id);

  assert(it_attr_aps != _apss.cend());
  const auto& attr_aps = it_attr_aps->second;

  assert(abh.attr_sps_attr_idx < _sps->attributeSets.size());
  const auto& attr_sps = _sps->attributeSets[abh.attr_sps_attr_idx];
  const auto& label = attr_sps.attributeLabel;

  AttributeDecoder attrDecoder;
  pcc::chrono::Stopwatch<pcc::chrono::utime_inc_children_clock> clock_user;

  clock_user.start();
290
291
292
  attrDecoder.decode(
    *_sps, attr_sps, attr_aps, _gbh.geom_num_points,
    _params.minGeomNodeSizeLog2, buf, _currentPointCloud);
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  clock_user.stop();

  std::cout << label << "s bitstream size " << buf.size() << " B\n";

  auto total_user =
    std::chrono::duration_cast<std::chrono::milliseconds>(clock_user.count());
  std::cout << label
            << "s processing time (user): " << total_user.count() / 1000.0
            << " s\n";
  std::cout << std::endl;
}

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

void
PCCTMC3Decoder3::inverseQuantization(PCCPointSet3& pointCloud)
{
  const size_t pointCount = pointCloud.getPointCount();
  const double invScale = 1.0 / _sps->seq_source_geom_scale_factor;

  for (size_t i = 0; i < pointCount; ++i) {
    auto& point = pointCloud[i];
    for (size_t k = 0; k < 3; ++k) {
316
      point[k] = point[k] * invScale + _sps->seq_bounding_box_xyz0[k];
317
318
319
320
321
322
323
    }
  }
}

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

}  // namespace pcc