TMC3.cpp 26.8 KB
Newer Older
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
1
/* The copyright in this software is being made available under the BSD
David Flynn's avatar
David Flynn committed
2
3
4
 * 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.
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
5
 *
David Flynn's avatar
David Flynn committed
6
 * Copyright (c) 2017-2018, ISO/IEC
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
7
8
9
10
11
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
David Flynn's avatar
David Flynn committed
12
13
14
15
16
17
18
19
20
21
 * * 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.
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
22
23
24
25
 *
 * 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
David Flynn's avatar
David Flynn committed
26
27
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
28
29
30
31
 * 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)
David Flynn's avatar
David Flynn committed
32
33
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
34
35
36
 */

#include "TMC3.h"
David Flynn's avatar
David Flynn committed
37
38
39
40
41

#include <memory>

#include "PCCTMC3Encoder.h"
#include "PCCTMC3Decoder.h"
42
#include "constants.h"
43
#include "program_options_lite.h"
44
#include "io_tlv.h"
45
#include "version.h"
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
46
47
48
49

using namespace std;
using namespace pcc;

50
51
52
53
54
//============================================================================

struct Parameters {
  bool isDecoder;

55
56
57
  // command line parsing should adjust dist2 values according to PQS
  bool positionQuantizationScaleAdjustsDist2;

58
59
60
  // output mode for ply writing (binary or ascii)
  bool outputBinaryPly;

61
62
63
  // when true, configure the encoder as if no attributes are specified
  bool disableAttributeCoding;

64
65
66
67
  std::string uncompressedDataPath;
  std::string compressedStreamPath;
  std::string reconstructedDataPath;

68
69
70
  // Filename for saving pre inverse scaled point cloud.
  std::string preInvScalePath;

71
72
73
74
75
  pcc::EncoderParams encoder;
  pcc::DecoderParams decoder;

  // todo(df): this should be per-attribute
  ColorTransform colorTransform;
76
77
78

  // todo(df): this should be per-attribute
  int reflectanceScale;
79
80
81
82
};

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

83
84
85
int
main(int argc, char* argv[])
{
86
  cout << "MPEG PCC tmc3 version " << ::pcc::version << endl;
87

Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
88
89
90
91
  Parameters params;
  if (!ParseParameters(argc, argv, params)) {
    return -1;
  }
92
93
94
95
96
97
98

  // Timers to count elapsed wall/user time
  pcc::chrono::Stopwatch<std::chrono::steady_clock> clock_wall;
  pcc::chrono::Stopwatch<pcc::chrono::utime_inc_children_clock> clock_user;

  clock_wall.start();

Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
99
  int ret = 0;
100
  if (params.isDecoder) {
101
    ret = Decompress(params, clock_user);
102
103
  } else {
    ret = Compress(params, clock_user);
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
104
105
  }

106
107
108
109
110
111
112
113
  clock_wall.stop();

  using namespace std::chrono;
  auto total_wall = duration_cast<milliseconds>(clock_wall.count()).count();
  auto total_user = duration_cast<milliseconds>(clock_user.count()).count();
  std::cout << "Processing time (wall): " << total_wall / 1000.0 << " s\n";
  std::cout << "Processing time (user): " << total_user / 1000.0 << " s\n";

Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
114
115
116
  return ret;
}

117
118
119
//---------------------------------------------------------------------------
// :: Command line / config parsing helpers

120
121
122
123
template<typename T>
static std::istream&
readUInt(std::istream& in, T& val)
{
124
125
126
127
128
129
  unsigned int tmp;
  in >> tmp;
  val = T(tmp);
  return in;
}

130
131
132
static std::istream&
operator>>(std::istream& in, ColorTransform& val)
{
133
134
135
  return readUInt(in, val);
}

136
namespace pcc {
137
static std::istream&
138
operator>>(std::istream& in, AttributeEncoding& val)
139
{
140
  return readUInt(in, val);
141
142
}
}  // namespace pcc
143

144
145
146
147
148
149
150
151
namespace pcc {
static std::istream&
operator>>(std::istream& in, PartitionMethod& val)
{
  return readUInt(in, val);
}
}  // namespace pcc

152
namespace pcc {
153
static std::ostream&
154
operator<<(std::ostream& out, const AttributeEncoding& val)
155
{
156
  switch (val) {
157
158
159
  case AttributeEncoding::kPredictingTransform: out << "0 (Pred)"; break;
  case AttributeEncoding::kRAHTransform: out << "1 (RAHT)"; break;
  case AttributeEncoding::kLiftingTransform: out << "2 (Lift)"; break;
160
161
  }
  return out;
162
163
}
}  // namespace pcc
164

165
166
167
168
169
170
namespace pcc {
static std::ostream&
operator<<(std::ostream& out, const PartitionMethod& val)
{
  switch (val) {
  case PartitionMethod::kNone: out << "0 (None)"; break;
171
  case PartitionMethod::kUniformGeom: out << "0 (UniformGeom)"; break;
172
  case PartitionMethod::kOctreeUniform: out << "0 (UniformOctree)"; break;
173
  default: out << int(val) << " (Unknown)"; break;
174
175
176
177
178
  }
  return out;
}
}  // namespace pcc

179
180
181
namespace df {
namespace program_options_lite {
  template<typename T>
182
  struct option_detail<pcc::Vec3<T>> {
183
184
185
186
    static constexpr bool is_container = true;
    static constexpr bool is_fixed_size = true;
    typedef T* output_iterator;

187
188
    static void clear(pcc::Vec3<T>& container){};
    static output_iterator make_output_iterator(pcc::Vec3<T>& container)
189
190
191
192
193
194
195
    {
      return &container[0];
    }
  };
}  // namespace program_options_lite
}  // namespace df

196
197
198
//---------------------------------------------------------------------------
// :: Command line / config parsing

199
200
201
bool
ParseParameters(int argc, char* argv[], Parameters& params)
{
202
203
  namespace po = df::program_options_lite;

204
205
206
207
208
  struct {
    AttributeDescription desc;
    AttributeParameterSet aps;
  } params_attr;

209
210
211
212
213
214
215
216
217
218
219
220
221
  bool print_help = false;

  // a helper to set the attribute
  std::function<po::OptionFunc::Func> attribute_setter =
    [&](po::Options&, const std::string& name, po::ErrorReporter) {
      // copy the current state of parsed attribute parameters
      //
      // NB: this does not cause the default values of attr to be restored
      // for the next attribute block.  A side-effect of this is that the
      // following is allowed leading to attribute foo having both X=1 and
      // Y=2:
      //   "--attr.X=1 --attribute foo --attr.Y=2 --attribute foo"
      //
222
223
224
225
226
227
228
229
230
231
232
233
234
235

      // NB: insert returns any existing element
      const auto& it = params.encoder.attributeIdxMap.insert(
        {name, int(params.encoder.attributeIdxMap.size())});

      if (it.second) {
        params.encoder.sps.attributeSets.push_back(params_attr.desc);
        params.encoder.aps.push_back(params_attr.aps);
        return;
      }

      // update existing entry
      params.encoder.sps.attributeSets[it.first->second] = params_attr.desc;
      params.encoder.aps[it.first->second] = params_attr.aps;
236
237
    };

238
  /* clang-format off */
239
240
241
242
243
244
245
246
247
248
249
  // The definition of the program/config options, along with default values.
  //
  // NB: when updating the following tables:
  //      (a) please keep to 80-columns for easier reading at a glance,
  //      (b) do not vertically align values -- it breaks quickly
  //
  po::Options opts;
  opts.addOptions()
  ("help", print_help, false, "this help text")
  ("config,c", po::parseConfigFile, "configuration file name")

250
251
  (po::Section("General"))

252
  ("mode", params.isDecoder, false,
253
254
    "The encoding/decoding mode:\n"
    "  0: encode\n"
255
    "  1: decode")
256
257
258

  // i/o parameters
  ("reconstructedDataPath",
259
260
    params.reconstructedDataPath, {},
    "The ouput reconstructed pointcloud file path (decoder only)")
261
262

  ("uncompressedDataPath",
263
264
    params.uncompressedDataPath, {},
    "The input pointcloud file path")
265
266

  ("compressedStreamPath",
267
268
    params.compressedStreamPath, {},
    "The compressed bitstream path (encoder=output, decoder=input)")
269

270
  ("postRecolorPath",
271
    params.encoder.postRecolorPath, {},
272
    "Recolored pointcloud file path (encoder only)")
273
274

  ("preInvScalePath",
275
    params.preInvScalePath, {},
276
    "Pre inverse scaled pointcloud file path (decoder only)")
277

278
279
280
281
  ("outputBinaryPly",
    params.outputBinaryPly, false,
    "Output ply files using binary (or otherwise ascii) format")

282
  // general
283
  // todo(df): this should be per-attribute
284
  ("colorTransform",
285
286
287
288
    params.colorTransform, COLOR_TRANSFORM_RGB_TO_YCBCR,
    "The colour transform to be applied:\n"
    "  0: none\n"
    "  1: RGB to YCbCr (Rec.709)")
289

290
291
292
293
294
295
  // todo(df): this should be per-attribute
  ("hack.reflectanceScale",
    params.reflectanceScale, 1,
    "scale factor to be applied to reflectance "
    "pre encoding / post reconstruction")

296
297
  // NB: if adding decoder options, uncomment the Decoder section marker
  // (po::Section("Decoder"))
298
299
300

  (po::Section("Encoder"))

301
302
303
304
305
306
307
308
309
  ("seq_bounding_box_xyz0",
    params.encoder.sps.seq_bounding_box_xyz0, {0},
    "seq_bounding_box_xyz0.  NB: seq_bounding_box_whd must be set for this "
    "parameter to have an effect")

  ("seq_bounding_box_whd",
    params.encoder.sps.seq_bounding_box_whd, {0},
    "seq_bounding_box_whd")

310
  ("positionQuantizationScale",
311
    params.encoder.sps.seq_source_geom_scale_factor, 1.f,
312
    "Scale factor to be applied to point positions during quantization process")
313

314
315
316
317
  ("positionQuantizationScaleAdjustsDist2",
    params.positionQuantizationScaleAdjustsDist2, false,
    "Scale dist2 values by squared positionQuantizationScale")

318
  ("mergeDuplicatedPoints",
319
    params.encoder.gps.geom_unique_points_flag, true,
320
    "Enables removal of duplicated points")
321

322
323
324
  ("partitionMethod",
    params.encoder.partitionMethod, PartitionMethod::kNone,
    "Method used to partition input point cloud into slices/tiles:\n"
325
326
    "  0: none\n"
    "  1: none (deprecated)\n"
327
328
    "  2: n Uniform-Geometry partition bins along the longest edge\n"
    "  3: Uniform Geometry partition at n octree depth")
329
330
331
332
333
334

  ("partitionNumUniformGeom",
    params.encoder.partitionNumUniformGeom, 0,
    "Number of bins for partitionMethod=2:\n"
    "  0: slice partition with adaptive-defined bins\n"
    "  >=1: slice partition with user-defined bins\n")
335

336
337
338
339
  ("partitionOctreeDepth",
    params.encoder.partitionOctreeDepth, 2,
    "Depth of octree partition for partitionMethod=3")

340
341
342
343
  ("disableAttributeCoding",
    params.disableAttributeCoding, false,
    "Ignore attribute coding configuration")

344
  (po::Section("Geometry"))
345

346
  // tools
347
348
349
350
351
352
  ("bitwiseOccupancyCoding",
    params.encoder.gps.bitwise_occupancy_coding_flag, true,
    "Selects between bitwise and bytewise occupancy coding:\n"
    "  0: bytewise\n"
    "  1: bitwise")

353
  ("neighbourContextRestriction",
354
    params.encoder.gps.neighbour_context_restriction_flag, false,
355
    "Limit geometry octree occupancy contextualisation to sibling nodes")
356

357
  ("neighbourAvailBoundaryLog2",
358
    params.encoder.gps.neighbour_avail_boundary_log2, 0,
359
360
361
    "Defines the avaliability volume for neighbour occupancy lookups."
    " 0: unconstrained")

362
  ("inferredDirectCodingMode",
363
    params.encoder.gps.inferred_direct_coding_mode_enabled_flag, true,
364
    "Permits early termination of the geometry octree for isolated points")
365

366
367
368
369
  ("adjacentChildContextualization",
    params.encoder.gps.adjacent_child_contextualization_enabled_flag, true,
    "Occupancy contextualization using neighbouring adjacent children")

370
371
372
373
  ("intra_pred_max_node_size_log2",
    params.encoder.gps.intra_pred_max_node_size_log2, 0,
    "octree nodesizes eligible for occupancy intra prediction")

374
375
376
377
  ("ctxOccupancyReductionFactor",
     params.encoder.gps.geom_occupancy_ctx_reduction_factor, 3,
     "Adjusts the number of contexts used in occupancy coding")

378
379
380
381
  ("trisoup_node_size_log2",
    params.encoder.gps.trisoup_node_size_log2, 0,
    "Size of nodes for surface triangulation.\n"
    "  0: disabled\n")
382

383
384
  (po::Section("Attributes"))

385
386
387
  // attribute processing
  //   NB: Attribute options are special in the way they are applied (see above)
  ("attribute",
388
389
390
    attribute_setter,
    "Encode the given attribute (NB, must appear after the"
    "following attribute parameters)")
391

392
393
394
395
  ("bitdepth",
    params_attr.desc.attr_bitdepth, 8,
    "Attribute bitdepth")

396
  ("transformType",
397
    params_attr.aps.attr_encoding, AttributeEncoding::kPredictingTransform,
398
    "Coding method to use for attribute:\n"
399
    "  0: Hierarchical neighbourhood prediction\n"
400
    "  1: Region Adaptive Hierarchical Transform (RAHT)\n"
401
    "  2: Hierarichical neighbourhood prediction as lifting transform")
402
403

  ("rahtDepth",
404
    params_attr.aps.raht_depth, 21,
405
406
    "Number of bits for morton representation of an RAHT co-ordinate"
    "component")
407

408
409
410
411
  ("rahtPredictionEnabled",
    params_attr.aps.raht_prediction_enabled_flag, true,
    "Controls the use of transform-domain prediction")

412
  ("numberOfNearestNeighborsInPrediction",
413
    params_attr.aps.num_pred_nearest_neighbours, 3,
414
    "Attribute's maximum number of nearest neighbors to be used for prediction")
415

416
417
418
419
420
421
  ("adaptivePredictionThreshold",
    params_attr.aps.adaptive_prediction_threshold, -1,
    "Neighbouring attribute value difference that enables choice of "
    "single|multi predictors. Applies to transformType=2 only.\n"
    "  -1: auto = 2**(bitdepth-2)")

422
423
424
425
  ("attributeSearchRange",
    params_attr.aps.search_range, 128,
    "Range for nearest neighbor search")

426
427
428
429
430
431
  ("lodDecimation",
    params_attr.aps.lod_decimation_enabled_flag, false,
    "Controls LoD generation method:\n"
    " 0: distance based subsampling\n"
    " 1: decimation by 1:3")

432
433
434
435
436
  ("max_num_direct_predictors",
    params_attr.aps.max_num_direct_predictors, 3,
    "Maximum number of nearest neighbour candidates used in direct"
    "attribute prediction")

437
  ("levelOfDetailCount",
438
    params_attr.aps.num_detail_levels, 1,
439
    "Attribute's number of levels of detail")
440

441
442
  ("dist2",
    params_attr.aps.dist2, {},
443
444
    "Attribute's list of squared distances, or initial value for automatic"
    "derivation")
445

446
447
448
449
  ("intraLodPredictionEnabled",
    params_attr.aps.intra_lod_prediction_enabled_flag, false,
    "Permits referring to points in same LoD")

450
451
452
453
454
455
456
  ("qp",
    params_attr.aps.init_qp, 4,
    "Attribute's luma quantisation parameter")

  ("qpChromaOffset",
    params_attr.aps.aps_chroma_qp_offset, 0,
    "Attribute's chroma quantisation parameter offset (relative to luma)")
457
458
459
460

  ("aps_slice_qp_deltas_present_flag",
    params_attr.aps.aps_slice_qp_deltas_present_flag, false,
    "Enable signalling of per-slice QP values")
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517

  // This section is just dedicated to attribute recolouring (encoder only).
  // parameters are common to all attributes.
  (po::Section("Recolouring"))

  ("recolourSearchRange",
    params.encoder.recolour.searchRange, 8,
    "")

  ("recolourNumNeighboursFwd",
    params.encoder.recolour.numNeighboursFwd, 8,
    "")

  ("recolourNumNeighboursBwd",
    params.encoder.recolour.numNeighboursBwd, 1,
    "")

  ("recolourUseDistWeightedAvgFwd",
    params.encoder.recolour.useDistWeightedAvgFwd, true,
    "")

  ("recolourUseDistWeightedAvgBwd",
    params.encoder.recolour.useDistWeightedAvgBwd, true,
    "")

  ("recolourSkipAvgIfIdenticalSourcePointPresentFwd",
    params.encoder.recolour.skipAvgIfIdenticalSourcePointPresentFwd, true,
    "")

  ("recolourSkipAvgIfIdenticalSourcePointPresentBwd",
    params.encoder.recolour.skipAvgIfIdenticalSourcePointPresentBwd, false,
    "")

  ("recolourDistOffsetFwd",
    params.encoder.recolour.distOffsetFwd, 4.,
    "")

  ("recolourDistOffsetBwd",
    params.encoder.recolour.distOffsetBwd, 4.,
    "")

  ("recolourMaxGeometryDist2Fwd",
    params.encoder.recolour.maxGeometryDist2Fwd, 1000.,
    "")

  ("recolourMaxGeometryDist2Bwd",
    params.encoder.recolour.maxGeometryDist2Bwd, 1000.,
    "")

  ("recolourMaxAttributeDist2Fwd",
    params.encoder.recolour.maxAttributeDist2Fwd, 1000.,
    "")

  ("recolourMaxAttributeDist2Bwd",
    params.encoder.recolour.maxAttributeDist2Bwd, 1000.,
    "")

518
  ;
519
  /* clang-format on */
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534

  po::setDefaults(opts);
  po::ErrorReporter err;
  const list<const char*>& argv_unhandled =
    po::scanArgv(opts, argc, (const char**)argv, err);

  for (const auto arg : argv_unhandled) {
    err.warn() << "Unhandled argument ignored: " << arg << "\n";
  }

  if (argc == 1 || print_help) {
    po::doHelp(std::cout, opts, 78);
    return false;
  }

535
536
  // Certain coding modes are not available when trisoup is enabled.
  // Disable them, and warn if set (they may be set as defaults).
537
  if (params.encoder.gps.trisoup_node_size_log2 > 0) {
538
539
540
541
542
543
544
545
546
547
    if (!params.encoder.gps.geom_unique_points_flag)
      err.warn() << "TriSoup geometry does not preserve duplicated points\n";

    if (params.encoder.gps.inferred_direct_coding_mode_enabled_flag)
      err.warn() << "TriSoup geometry is incompatable with IDCM\n";

    params.encoder.gps.geom_unique_points_flag = true;
    params.encoder.gps.inferred_direct_coding_mode_enabled_flag = false;
  }

548
549
550
551
552
553
554
  // support disabling attribute coding (simplifies configuration)
  if (params.disableAttributeCoding) {
    params.encoder.attributeIdxMap.clear();
    params.encoder.sps.attributeSets.clear();
    params.encoder.aps.clear();
  }

555
  // fixup any per-attribute settings
556
  for (const auto& it : params.encoder.attributeIdxMap) {
557
    auto& attr_sps = params.encoder.sps.attributeSets[it.second];
558
559
    auto& attr_aps = params.encoder.aps[it.second];

560
561
    // Avoid wasting bits signalling chroma quant step size for reflectance
    if (it.first == "reflectance") {
562
      attr_aps.aps_chroma_qp_offset = 0;
563
564
    }

565
566
567
568
569
    bool isLifting =
      attr_aps.attr_encoding == AttributeEncoding::kPredictingTransform
      || attr_aps.attr_encoding == AttributeEncoding::kLiftingTransform;

    // derive the dist2 values based on an initial value
570
571
    if (isLifting) {
      if (attr_aps.dist2.size() > attr_aps.num_detail_levels) {
572
        attr_aps.dist2.resize(attr_aps.num_detail_levels);
573
574
575
576
577
578
579
580
581
582
583
      } else if (
        attr_aps.dist2.size() < attr_aps.num_detail_levels
        && !attr_aps.dist2.empty()) {
        if (attr_aps.dist2.size() < attr_aps.num_detail_levels) {
          attr_aps.dist2.resize(attr_aps.num_detail_levels);
          const double distRatio = 4.0;
          uint64_t d2 = attr_aps.dist2[0];
          for (int i = 0; i < attr_aps.num_detail_levels; ++i) {
            attr_aps.dist2[i] = d2;
            d2 = uint64_t(std::round(distRatio * d2));
          }
584
585
586
        }
      }
    }
587
588
589
590
591
592
593
594
595
596
597
    // In order to simplify specification of dist2 values, which are
    // depending on the scale of the coded point cloud, the following
    // adjust the dist2 values according to PQS.  The user need only
    // specify the unquantised PQS value.
    if (params.positionQuantizationScaleAdjustsDist2) {
      double pqs = params.encoder.sps.seq_source_geom_scale_factor;
      double pqs2 = pqs * pqs;
      for (auto& dist2 : attr_aps.dist2)
        dist2 = int64_t(std::round(pqs2 * dist2));
    }

598
599
600
601
602
603
604
605
    // Set default threshold based on bitdepth
    if (attr_aps.adaptive_prediction_threshold == -1) {
      attr_aps.adaptive_prediction_threshold = 1
        << (attr_sps.attr_bitdepth - 2);
    }

    if (attr_aps.attr_encoding == AttributeEncoding::kLiftingTransform) {
      attr_aps.adaptive_prediction_threshold = 0;
606
      attr_aps.intra_lod_prediction_enabled_flag = false;
607
608
609
    }

    // For RAHT, ensure that the unused lod count = 0 (prevents mishaps)
610
    if (attr_aps.attr_encoding == AttributeEncoding::kRAHTransform) {
611
      attr_aps.num_detail_levels = 0;
612
      attr_aps.adaptive_prediction_threshold = 0;
613
614
615
    }
  }

616
  // sanity checks
617
618
619
620
621
622

  if (params.encoder.gps.intra_pred_max_node_size_log2)
    if (!params.encoder.gps.neighbour_avail_boundary_log2)
      err.error() << "Geometry intra prediction requires finite"
                     "neighbour_avail_boundary_log2\n";

623
624
625
626
627
628
629
630
  for (const auto& it : params.encoder.attributeIdxMap) {
    const auto& attr_sps = params.encoder.sps.attributeSets[it.second];
    const auto& attr_aps = params.encoder.aps[it.second];

    bool isLifting =
      attr_aps.attr_encoding == AttributeEncoding::kPredictingTransform
      || attr_aps.attr_encoding == AttributeEncoding::kLiftingTransform;

631
632
633
634
635
636
637
638
639
640
641
    if (it.first == "color") {
      // todo(??): permit relaxing of the following constraint
      if (attr_sps.attr_bitdepth > 8)
        err.error() << it.first << ".bitdepth must be less than 9\n";
    }

    if (it.first == "reflectance") {
      if (attr_sps.attr_bitdepth > 16)
        err.error() << it.first << ".bitdepth must be less than 17\n";
    }

642
    if (isLifting) {
643
      int lod = attr_aps.num_detail_levels;
644
      if (lod > 255 || lod < 0) {
645
        err.error() << it.first
646
                    << ".levelOfDetailCount must be in the range [0,255]\n";
647
      }
648
649
      if (attr_aps.dist2.size() != lod) {
        err.error() << it.first << ".dist2 does not have " << lod
650
                    << " entries\n";
651
      }
652

653
654
655
656
657
      if (attr_aps.adaptive_prediction_threshold < 0) {
        err.error() << it.first
                    << ".adaptivePredictionThreshold must be positive\n";
      }

658
      if (
659
        attr_aps.num_pred_nearest_neighbours
660
        > kAttributePredictionMaxNeighbourCount) {
661
662
        err.error() << it.first
                    << ".numberOfNearestNeighborsInPrediction must be <= "
663
                    << kAttributePredictionMaxNeighbourCount << "\n";
664
      }
665
    }
666
667
668
669
670
671
672
673

    if (attr_aps.init_qp < 4)
      err.error() << it.first << ".qp must be greater than 3\n";

    if (attr_aps.init_qp + attr_aps.aps_chroma_qp_offset < 4) {
      err.error() << it.first << ".qpChromaOffset must be greater than "
                  << attr_aps.init_qp - 5 << "\n";
    }
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
674
675
  }

676
677
  // check required arguments are specified

678
  if (!params.isDecoder && params.uncompressedDataPath.empty())
679
680
    err.error() << "uncompressedDataPath not set\n";

681
  if (params.isDecoder && params.reconstructedDataPath.empty())
682
683
684
685
686
687
688
689
690
691
    err.error() << "reconstructedDataPath not set\n";

  if (params.compressedStreamPath.empty())
    err.error() << "compressedStreamPath not set\n";

  // report the current configuration (only in the absence of errors so
  // that errors/warnings are more obvious and in the same place).
  if (err.is_errored)
    return false;

692
693
  // Dump the complete derived configuration
  cout << "+ Effective configuration parameters\n";
694

695
  po::dumpCfg(cout, opts, "General", 4);
696
  if (params.isDecoder) {
697
    po::dumpCfg(cout, opts, "Decoder", 4);
698
  } else {
699
700
    po::dumpCfg(cout, opts, "Encoder", 4);
    po::dumpCfg(cout, opts, "Geometry", 4);
701
    po::dumpCfg(cout, opts, "Recolouring", 4);
702

703
    for (const auto& it : params.encoder.attributeIdxMap) {
704
      // NB: when dumping the config, opts references params_attr
705
706
      params_attr.desc = params.encoder.sps.attributeSets[it.second];
      params_attr.aps = params.encoder.aps[it.second];
707
708
709
      cout << "    " << it.first << "\n";
      po::dumpCfg(cout, opts, "Attributes", 8);
    }
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
710
711
  }

712
713
  cout << endl;

Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
714
715
  return true;
}
716

717
int
718
Compress(Parameters& params, Stopwatch& clock)
719
{
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
720
  PCCPointSet3 pointCloud;
721
722
723
  if (
    !pointCloud.read(params.uncompressedDataPath)
    || pointCloud.getPointCount() == 0) {
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
724
725
726
727
    cout << "Error: can't open input file!" << endl;
    return -1;
  }

728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
  // Sanitise the input point cloud
  // todo(df): remove the following with generic handling of properties
  bool codeColour = params.encoder.attributeIdxMap.count("color");
  if (!codeColour)
    pointCloud.removeColors();
  assert(codeColour == pointCloud.hasColors());

  bool codeReflectance = params.encoder.attributeIdxMap.count("reflectance");
  if (!codeReflectance)
    pointCloud.removeReflectances();
  assert(codeReflectance == pointCloud.hasReflectances());

  ofstream fout(params.compressedStreamPath, ios::binary);
  if (!fout.is_open()) {
    return -1;
  }

745
746
  clock.start();

Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
747
748
749
  if (params.colorTransform == COLOR_TRANSFORM_RGB_TO_YCBCR) {
    pointCloud.convertRGBToYUV();
  }
750
751
752
753
754
755
756
757
758

  if (params.reflectanceScale > 1 && pointCloud.hasReflectances()) {
    const auto pointCount = pointCloud.getPointCount();
    for (size_t i = 0; i < pointCount; ++i) {
      int val = pointCloud.getReflectance(i) / params.reflectanceScale;
      pointCloud.setReflectance(i, val);
    }
  }

Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
759
760
  PCCTMC3Encoder3 encoder;

761
762
  // The reconstructed point cloud
  std::unique_ptr<PCCPointSet3> reconPointCloud;
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
763
  if (!params.reconstructedDataPath.empty()) {
764
    reconPointCloud.reset(new PCCPointSet3);
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
765
766
  }

767
  int ret = encoder.compress(
768
769
    pointCloud, &params.encoder,
    [&](const PayloadBuffer& buf) { writeTlv(buf, fout); },
770
    reconPointCloud.get());
771
  if (ret) {
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
772
773
774
    cout << "Error: can't compress point cloud!" << endl;
    return -1;
  }
775

776
  std::cout << "Total bitstream size " << fout.tellp() << " B" << std::endl;
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
777
778
  fout.close();

779
780
  clock.stop();

Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
781
782
  if (!params.reconstructedDataPath.empty()) {
    if (params.colorTransform == COLOR_TRANSFORM_RGB_TO_YCBCR) {
783
784
785
786
787
788
789
790
791
      reconPointCloud->convertYUVToRGB();
    }

    if (params.reflectanceScale > 1 && reconPointCloud->hasReflectances()) {
      const auto pointCount = reconPointCloud->getPointCount();
      for (size_t i = 0; i < pointCount; ++i) {
        int val = reconPointCloud->getReflectance(i) * params.reflectanceScale;
        reconPointCloud->setReflectance(i, val);
      }
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
792
    }
793

794
795
    reconPointCloud->write(
      params.reconstructedDataPath, !params.outputBinaryPly);
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
796
797
798
799
  }

  return 0;
}
800
int
801
Decompress(Parameters& params, Stopwatch& clock)
802
{
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
803
804
805
806
807
  ifstream fin(params.compressedStreamPath, ios::binary);
  if (!fin.is_open()) {
    return -1;
  }

808
809
  clock.start();

810
  PayloadBuffer buf;
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
811
  PCCTMC3Decoder3 decoder;
812

813
814
815
  while (true) {
    PayloadBuffer* buf_ptr = &buf;
    readTlv(fin, &buf);
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
816

817
818
819
    // at end of file (or other error), flush decoder
    if (!fin)
      buf_ptr = nullptr;
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
820

821
822
823
    int ret = decoder.decompress(
      params.decoder, buf_ptr, [&](const PCCPointSet3& decodedPointCloud) {
        PCCPointSet3 pointCloud(decodedPointCloud);
824

825
826
827
828
        if (params.colorTransform == COLOR_TRANSFORM_RGB_TO_YCBCR) {
          pointCloud.convertYUVToRGB();
        }

829
830
831
832
833
834
835
836
        if (params.reflectanceScale > 1 && pointCloud.hasReflectances()) {
          const auto pointCount = pointCloud.getPointCount();
          for (size_t i = 0; i < pointCount; ++i) {
            int val = pointCloud.getReflectance(i) * params.reflectanceScale;
            pointCloud.setReflectance(i, val);
          }
        }

837
838
        // Dump the decoded colour using the pre inverse scaled geometry
        if (!params.preInvScalePath.empty()) {
839
          pointCloud.write(params.preInvScalePath, !params.outputBinaryPly);
840
841
        }

842
        decoder.inverseQuantization(pointCloud);
843
844
845

        clock.stop();

846
847
        if (!pointCloud.write(
              params.reconstructedDataPath, !params.outputBinaryPly)) {
848
849
850
851
852
853
854
855
856
857
858
859
860
          cout << "Error: can't open output file!" << endl;
        }

        clock.start();
      });

    if (ret) {
      cout << "Error: can't decompress point cloud!" << endl;
      return -1;
    }

    if (!buf_ptr)
      break;
Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
861
  }
862

863
864
865
866
867
868
  fin.clear();
  fin.seekg(0, ios_base::end);
  std::cout << "Total bitstream size " << fin.tellg() << " B" << std::endl;

  clock.stop();

Khaled Mammou's avatar
TMC3v0  
Khaled Mammou committed
869
870
  return 0;
}