io_hls.cpp 19.5 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* 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 "BitReader.h"
#include "BitWriter.h"
#include "hls.h"
#include "io_hls.h"

#include <iomanip>
#include <iterator>

namespace pcc {

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

std::ostream&
operator<<(std::ostream& os, const AttributeLabel& label)
{
  switch (KnownAttributeLabel(label.attribute_label_four_bytes)) {
  case KnownAttributeLabel::kColour: os << "color"; break;
  case KnownAttributeLabel::kReflectance: os << "reflectance"; break;
  default:
    auto iosFlags = os.flags(std::ios::hex);
    os << std::setw(8) << label.attribute_label_four_bytes;
    os.flags(iosFlags);
  }

  return os;
}

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

PayloadBuffer
write(const SequenceParameterSet& sps)
{
  PayloadBuffer buf(PayloadType::kSequenceParameterSet);
  auto bs = makeBitWriter(std::back_inserter(buf));

  bs.writeUn(24, sps.profileCompatibility.profile_compatibility_flags);
  bs.writeUn(8, sps.level);

74
  bool seq_bounding_box_present_flag = true;
75
76
  bs.write(seq_bounding_box_present_flag);
  if (seq_bounding_box_present_flag) {
77
78
79
80
81
82
83
84
    bs.writeSe(sps.seq_bounding_box_xyz0.x());
    bs.writeSe(sps.seq_bounding_box_xyz0.y());
    bs.writeSe(sps.seq_bounding_box_xyz0.z());
    int seq_bounding_box_scale = 1;
    bs.writeUe(seq_bounding_box_scale);
    bs.writeUe(sps.seq_bounding_box_whd.x());
    bs.writeUe(sps.seq_bounding_box_whd.y());
    bs.writeUe(sps.seq_bounding_box_whd.z());
85
86
87
88
89
90
91
92
93
  }
  // todo(df): determine encoding of scale factor
  bs.writeF(sps.seq_source_geom_scale_factor);

  bs.writeUe(sps.sps_seq_parameter_set_id);

  int num_attribute_sets = int(sps.attributeSets.size());
  bs.writeUe(num_attribute_sets);
  for (const auto& attr : sps.attributeSets) {
94
95
    // todo(df): should be attr_num_dimensions_minus1
    bs.writeUe(attr.attr_num_dimensions);
96
    bs.writeUe(attr.attr_instance_id);
97
    bs.writeUe(attr.attr_bitdepth);
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    bs.writeUe(attr.cicp_colour_primaries_idx);
    bs.writeUe(attr.cicp_transfer_characteristics_idx);
    bs.writeUe(attr.cicp_matrix_coefficients_idx);
    bs.write(attr.cicp_video_full_range_flag);

    const auto& label = attr.attributeLabel;

    bool known_attribute_label_flag = label.known_attribute_label_flag();
    bs.write(known_attribute_label_flag);
    if (known_attribute_label_flag) {
      bs.writeUe(int(label.known_attribute_label()));
    } else {
      bs.writeUn(32, label.attribute_label_four_bytes);
    }
  }

114
  bs.writeUn(5, sps.log2_max_frame_idx);
115
  bs.writeUn(3, sps.geometry_axis_order);
116
117
  bs.write(sps.cabac_bypass_stream_enabled_flag);

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  bool sps_extension_flag = false;
  bs.write(sps_extension_flag);
  bs.byteAlign();

  return buf;
}

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

SequenceParameterSet
parseSps(const PayloadBuffer& buf)
{
  SequenceParameterSet sps;
  assert(buf.type == PayloadType::kSequenceParameterSet);
  auto bs = makeBitReader(buf.begin(), buf.end());

  bs.readUn(24, &sps.profileCompatibility.profile_compatibility_flags);
  bs.readUn(8, &sps.level);

  bool seq_bounding_box_present_flag = bs.read();
  if (seq_bounding_box_present_flag) {
139
140
141
142
143
144
145
146
    bs.readSe(&sps.seq_bounding_box_xyz0.x());
    bs.readSe(&sps.seq_bounding_box_xyz0.y());
    bs.readSe(&sps.seq_bounding_box_xyz0.z());
    int seq_bounding_box_scale;
    bs.readUe(&seq_bounding_box_scale);
    bs.readUe(&sps.seq_bounding_box_whd.x());
    bs.readUe(&sps.seq_bounding_box_whd.y());
    bs.readUe(&sps.seq_bounding_box_whd.z());
147
148
149
150
151
152
153
154
155
  }
  bs.readF(&sps.seq_source_geom_scale_factor);

  bs.readUe(&sps.sps_seq_parameter_set_id);

  int num_attribute_sets = int(bs.readUe());
  for (int i = 0; i < num_attribute_sets; i++) {
    sps.attributeSets.emplace_back();
    auto& attr = sps.attributeSets.back();
156
    bs.readUe(&attr.attr_num_dimensions);
157
    bs.readUe(&attr.attr_instance_id);
158
    bs.readUe(&attr.attr_bitdepth);
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    bs.readUe(&attr.cicp_colour_primaries_idx);
    bs.readUe(&attr.cicp_transfer_characteristics_idx);
    bs.readUe(&attr.cicp_matrix_coefficients_idx);
    bs.read(&attr.cicp_video_full_range_flag);

    auto& label = attr.attributeLabel;

    bool known_attribute_label_flag = bs.read();
    if (known_attribute_label_flag) {
      KnownAttributeLabel known_attribute_label;
      bs.readUe(&known_attribute_label);
      label = known_attribute_label;
    } else {
      bs.readUn(32, &label.attribute_label_four_bytes);
    }
  }

176
  bs.readUn(5, &sps.log2_max_frame_idx);
177
  bs.readUn(3, &sps.geometry_axis_order);
178
179
  bs.read(&sps.cabac_bypass_stream_enabled_flag);

180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  bool sps_extension_flag = bs.read();
  if (sps_extension_flag) {
    // todo(df): sps_extension_data;
    assert(!sps_extension_flag);
  }
  bs.byteAlign();

  return sps;
}

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

PayloadBuffer
write(const GeometryParameterSet& gps)
{
  PayloadBuffer buf(PayloadType::kGeometryParameterSet);
  auto bs = makeBitWriter(std::back_inserter(buf));

  bs.writeUe(gps.gps_geom_parameter_set_id);
  bs.writeUe(gps.gps_seq_parameter_set_id);
  bs.write(gps.geom_box_present_flag);
201
202
203
204
205
  if (gps.geom_box_present_flag) {
    bs.write(gps.geom_box_log2_scale_present_flag);
    if (!gps.geom_box_log2_scale_present_flag)
      bs.writeUe(gps.gps_geom_box_log2_scale);
  }
206
207
208
  bs.write(gps.geom_unique_points_flag);
  bs.write(gps.neighbour_context_restriction_flag);
  bs.write(gps.inferred_direct_coding_mode_enabled_flag);
209
  bs.write(gps.bitwise_occupancy_coding_flag);
210
  bs.write(gps.adjacent_child_contextualization_enabled_flag);
211
  bs.writeUe(gps.geom_occupancy_ctx_reduction_factor);
212
  bs.writeUe(gps.neighbour_avail_boundary_log2);
213
  bs.writeUe(gps.intra_pred_max_node_size_log2);
214
  bs.writeUe(gps.trisoup_node_size_log2);
215

216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  bool gps_extension_flag = false;
  bs.write(gps_extension_flag);
  bs.byteAlign();

  return buf;
}

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

GeometryParameterSet
parseGps(const PayloadBuffer& buf)
{
  GeometryParameterSet gps;
  assert(buf.type == PayloadType::kGeometryParameterSet);
  auto bs = makeBitReader(buf.begin(), buf.end());

  bs.readUe(&gps.gps_geom_parameter_set_id);
  bs.readUe(&gps.gps_seq_parameter_set_id);
  bs.read(&gps.geom_box_present_flag);
235
236
237
238
239
  if (gps.geom_box_present_flag) {
    bs.read(&gps.geom_box_log2_scale_present_flag);
    if (!gps.geom_box_log2_scale_present_flag)
      bs.readUe(&gps.gps_geom_box_log2_scale);
  }
240
241
242
  bs.read(&gps.geom_unique_points_flag);
  bs.read(&gps.neighbour_context_restriction_flag);
  bs.read(&gps.inferred_direct_coding_mode_enabled_flag);
243
  bs.read(&gps.bitwise_occupancy_coding_flag);
244
  bs.read(&gps.adjacent_child_contextualization_enabled_flag);
245
  bs.readUe(&gps.geom_occupancy_ctx_reduction_factor);
246
  bs.readUe(&gps.neighbour_avail_boundary_log2);
247
  bs.readUe(&gps.intra_pred_max_node_size_log2);
248
  bs.readUe(&gps.trisoup_node_size_log2);
249

250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
  bool gps_extension_flag = bs.read();
  if (gps_extension_flag) {
    // todo(df): gps_extension_data;
    assert(!gps_extension_flag);
  }
  bs.byteAlign();

  return gps;
}

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

PayloadBuffer
write(const AttributeParameterSet& aps)
{
  PayloadBuffer buf(PayloadType::kAttributeParameterSet);
  auto bs = makeBitWriter(std::back_inserter(buf));

  bs.writeUe(aps.aps_attr_parameter_set_id);
  bs.writeUe(aps.aps_seq_parameter_set_id);
  bs.writeUe(aps.attr_encoding);

272
273
  bs.writeUe(aps.init_qp);
  bs.writeSe(aps.aps_chroma_qp_offset);
274
  bs.write(aps.aps_slice_qp_deltas_present_flag);
275

276
277
278
279
  bool isLifting = aps.attr_encoding == AttributeEncoding::kLiftingTransform
    || aps.attr_encoding == AttributeEncoding::kPredictingTransform;
  if (isLifting) {
    bs.writeUe(aps.num_pred_nearest_neighbours);
280
    bs.writeUe(aps.num_detail_levels);
281
282
283
    bs.writeUe(aps.lod_neigh_bias.x());
    bs.writeUe(aps.lod_neigh_bias.y());
    bs.writeUe(aps.lod_neigh_bias.z());
284
285
286
287
288
289
290
291
292
293
294
295

    if (aps.attr_encoding == AttributeEncoding::kLiftingTransform)
      bs.write(aps.scalable_lifting_enabled_flag);

    if (!aps.scalable_lifting_enabled_flag) {
      bs.writeUe(aps.search_range);
      bs.write(aps.lod_decimation_enabled_flag);

      for (int idx = 0; idx < aps.num_detail_levels; idx++) {
        // todo(??): is this an appropriate encoding?
        bs.writeUe64(aps.dist2[idx]);
      }
296
    }
297
298
299
300
  }

  if (aps.attr_encoding == AttributeEncoding::kPredictingTransform) {
    bs.writeUe(aps.adaptive_prediction_threshold);
301
    bs.write(aps.intra_lod_prediction_enabled_flag);
302
    bs.write(aps.inter_component_prediction_enabled_flag);
303
    bs.writeUe(aps.max_num_direct_predictors);
304
305
306
  }

  if (aps.attr_encoding == AttributeEncoding::kRAHTransform) {
307
    bs.write(aps.raht_prediction_enabled_flag);
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
    bs.writeUe(aps.raht_depth);
  }

  bool aps_extension_flag = false;
  bs.write(aps_extension_flag);
  bs.byteAlign();

  return buf;
}

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

AttributeParameterSet
parseAps(const PayloadBuffer& buf)
{
  AttributeParameterSet aps;
  assert(buf.type == PayloadType::kAttributeParameterSet);
  auto bs = makeBitReader(buf.begin(), buf.end());

  bs.readUe(&aps.aps_attr_parameter_set_id);
  bs.readUe(&aps.aps_seq_parameter_set_id);
  bs.readUe(&aps.attr_encoding);

331
332
  bs.readUe(&aps.init_qp);
  bs.readSe(&aps.aps_chroma_qp_offset);
333
  bs.read(&aps.aps_slice_qp_deltas_present_flag);
334

335
336
337
338
  bool isLifting = aps.attr_encoding == AttributeEncoding::kLiftingTransform
    || aps.attr_encoding == AttributeEncoding::kPredictingTransform;
  if (isLifting) {
    bs.readUe(&aps.num_pred_nearest_neighbours);
339
    bs.readUe(&aps.num_detail_levels);
340
341
342
    bs.readUe(&aps.lod_neigh_bias.x());
    bs.readUe(&aps.lod_neigh_bias.y());
    bs.readUe(&aps.lod_neigh_bias.z());
343
344
345
346
347
348
349
350

    aps.scalable_lifting_enabled_flag = false;
    if (aps.attr_encoding == AttributeEncoding::kLiftingTransform)
      bs.read(&aps.scalable_lifting_enabled_flag);

    if (!aps.scalable_lifting_enabled_flag) {
      bs.readUe(&aps.search_range);
      bs.read(&aps.lod_decimation_enabled_flag);
351

352
353
354
355
      aps.dist2.resize(aps.num_detail_levels);
      for (int idx = 0; idx < aps.num_detail_levels; idx++) {
        bs.readUe(&aps.dist2[idx]);
      }
356
    }
357
358
  }

359
  aps.intra_lod_prediction_enabled_flag = false;
360
361
  if (aps.attr_encoding == AttributeEncoding::kPredictingTransform) {
    bs.readUe(&aps.adaptive_prediction_threshold);
362
    bs.read(&aps.intra_lod_prediction_enabled_flag);
363
    bs.read(&aps.inter_component_prediction_enabled_flag);
364
    bs.readUe(&aps.max_num_direct_predictors);
365
366
367
  }

  if (aps.attr_encoding == AttributeEncoding::kRAHTransform) {
368
    bs.read(&aps.raht_prediction_enabled_flag);
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    bs.readUe(&aps.raht_depth);
  }

  bool aps_extension_flag = bs.read();
  if (aps_extension_flag) {
    // todo(df): aps_extension_data;
    assert(!aps_extension_flag);
  }
  bs.byteAlign();

  return aps;
}

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

void
write(
386
  const SequenceParameterSet& sps,
387
388
389
390
391
392
393
394
  const GeometryParameterSet& gps,
  const GeometryBrickHeader& gbh,
  PayloadBuffer* buf)
{
  assert(buf->type == PayloadType::kGeometryBrick);
  auto bs = makeBitWriter(std::back_inserter(*buf));

  bs.writeUe(gbh.geom_geom_parameter_set_id);
395
  bs.writeUe(gbh.geom_tile_id);
396
  bs.writeUe(gbh.geom_slice_id);
397
  bs.writeUn(sps.log2_max_frame_idx, gbh.frame_idx);
398
399

  if (gps.geom_box_present_flag) {
400
401
402
403
    int geomBoxLog2Scale = gbh.geomBoxLog2Scale(gps);
    int geom_box_origin_x = gbh.geomBoxOrigin.x() >> geomBoxLog2Scale;
    int geom_box_origin_y = gbh.geomBoxOrigin.y() >> geomBoxLog2Scale;
    int geom_box_origin_z = gbh.geomBoxOrigin.z() >> geomBoxLog2Scale;
404

405
406
    if (gps.geom_box_log2_scale_present_flag)
      bs.writeUe(gbh.geom_box_log2_scale);
407
408
409
410
411
412
413
414
415
416
417
418
419
420
    bs.writeUe(geom_box_origin_x);
    bs.writeUe(geom_box_origin_y);
    bs.writeUe(geom_box_origin_z);
  }

  bs.writeUe(gbh.geom_max_node_size_log2);
  bs.writeUe(gbh.geom_num_points);
  bs.byteAlign();
}

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

GeometryBrickHeader
parseGbh(
421
422
423
424
  const SequenceParameterSet& sps,
  const GeometryParameterSet& gps,
  const PayloadBuffer& buf,
  int* bytesRead)
425
426
427
428
429
430
{
  GeometryBrickHeader gbh;
  assert(buf.type == PayloadType::kGeometryBrick);
  auto bs = makeBitReader(buf.begin(), buf.end());

  bs.readUe(&gbh.geom_geom_parameter_set_id);
431
  bs.readUe(&gbh.geom_tile_id);
432
  bs.readUe(&gbh.geom_slice_id);
433
  bs.readUn(sps.log2_max_frame_idx, &gbh.frame_idx);
434
435

  if (gps.geom_box_present_flag) {
436
437
    if (gps.geom_box_log2_scale_present_flag)
      bs.readUe(&gbh.geom_box_log2_scale);
438
439
440
441
442
443
444

    int geom_box_origin_x;
    int geom_box_origin_y;
    int geom_box_origin_z;
    bs.readUe(&geom_box_origin_x);
    bs.readUe(&geom_box_origin_y);
    bs.readUe(&geom_box_origin_z);
445
446
447
448
    int geomBoxLog2Scale = gbh.geomBoxLog2Scale(gps);
    gbh.geomBoxOrigin.x() = geom_box_origin_x << geomBoxLog2Scale;
    gbh.geomBoxOrigin.y() = geom_box_origin_y << geomBoxLog2Scale;
    gbh.geomBoxOrigin.z() = geom_box_origin_z << geomBoxLog2Scale;
449
450
451
452
453
454
455
456
457
458
459
460
  }

  bs.readUe(&gbh.geom_max_node_size_log2);
  bs.readUe(&gbh.geom_num_points);
  bs.byteAlign();

  if (bytesRead)
    *bytesRead = int(std::distance(buf.begin(), bs.pos()));

  return gbh;
}

461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//----------------------------------------------------------------------------

GeometryBrickHeader
parseGbhIds(const PayloadBuffer& buf)
{
  GeometryBrickHeader gbh;
  assert(buf.type == PayloadType::kGeometryBrick);
  auto bs = makeBitReader(buf.begin(), buf.end());

  bs.readUe(&gbh.geom_geom_parameter_set_id);
  bs.readUe(&gbh.geom_tile_id);
  bs.readUe(&gbh.geom_slice_id);

  /* NB: this function only decodes ids at the start of the header. */
  /* NB: do not attempt to parse any further */

  return gbh;
}

480
481
482
//============================================================================

void
483
484
485
486
write(
  const AttributeParameterSet& aps,
  const AttributeBrickHeader& abh,
  PayloadBuffer* buf)
487
488
489
490
491
{
  assert(buf->type == PayloadType::kAttributeBrick);
  auto bs = makeBitWriter(std::back_inserter(*buf));

  bs.writeUe(abh.attr_attr_parameter_set_id);
492
  bs.writeUe(abh.attr_sps_attr_idx);
493
  bs.writeUe(abh.attr_geom_slice_id);
494

495
496
497
498
499
  if (aps.aps_slice_qp_deltas_present_flag) {
    bs.writeSe(abh.attr_qp_delta_luma);
    bs.writeSe(abh.attr_qp_delta_chroma);
  }

500
501
502
503
504
505
506
507
508
509
510
511
512
  bool attr_layer_qp_present_flag = !abh.attr_layer_qp_delta_luma.empty();
  bs.write(attr_layer_qp_present_flag);
  if (attr_layer_qp_present_flag) {
    int numLayers = aps.attr_encoding == AttributeEncoding::kRAHTransform
      ? aps.raht_depth + 1
      : aps.num_detail_levels + 1;

    for (int i = 0; i < numLayers; i++) {
      bs.writeSe(abh.attr_layer_qp_delta_luma[i]);
      bs.writeSe(abh.attr_layer_qp_delta_chroma[i]);
    }
  }

513
514
515
516
517
518
  bs.byteAlign();
}

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

AttributeBrickHeader
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
parseAbhIds(const PayloadBuffer& buf)
{
  AttributeBrickHeader abh;
  assert(buf.type == PayloadType::kAttributeBrick);
  auto bs = makeBitReader(buf.begin(), buf.end());

  bs.readUe(&abh.attr_attr_parameter_set_id);
  bs.readUe(&abh.attr_sps_attr_idx);
  bs.readUe(&abh.attr_geom_slice_id);

  /* NB: this function only decodes ids at the start of the header. */
  /* NB: do not attempt to parse any further */

  return abh;
}

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

AttributeBrickHeader
parseAbh(
  const AttributeParameterSet& aps, const PayloadBuffer& buf, int* bytesRead)
540
541
542
543
544
545
{
  AttributeBrickHeader abh;
  assert(buf.type == PayloadType::kAttributeBrick);
  auto bs = makeBitReader(buf.begin(), buf.end());

  bs.readUe(&abh.attr_attr_parameter_set_id);
546
  bs.readUe(&abh.attr_sps_attr_idx);
547
  bs.readUe(&abh.attr_geom_slice_id);
548

549
550
551
552
553
  if (aps.aps_slice_qp_deltas_present_flag) {
    bs.readSe(&abh.attr_qp_delta_luma);
    bs.readSe(&abh.attr_qp_delta_chroma);
  }

554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
  bool attr_layer_qp_present_flag;
  bs.read(&attr_layer_qp_present_flag);
  if (attr_layer_qp_present_flag) {
    int numLayers = aps.attr_encoding == AttributeEncoding::kRAHTransform
      ? aps.raht_depth + 1
      : aps.num_detail_levels + 1;

    abh.attr_layer_qp_delta_luma.resize(numLayers);
    abh.attr_layer_qp_delta_chroma.resize(numLayers);
    for (int i = 0; i < numLayers; i++) {
      bs.readSe(&abh.attr_layer_qp_delta_luma[i]);
      bs.readSe(&abh.attr_layer_qp_delta_chroma[i]);
    }
  }

569
570
571
572
573
574
575
576
577
578
  bs.byteAlign();

  if (bytesRead)
    *bytesRead = int(std::distance(buf.begin(), bs.pos()));

  return abh;
}

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

579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
PayloadBuffer
write(const TileInventory& inventory)
{
  PayloadBuffer buf(PayloadType::kTileInventory);
  auto bs = makeBitWriter(std::back_inserter(buf));

  int num_tiles = inventory.tiles.size();
  bs.writeUe(num_tiles);
  for (const auto& entry : inventory.tiles) {
    bs.writeSe(entry.tile_bounding_box_xyz0.x());
    bs.writeSe(entry.tile_bounding_box_xyz0.y());
    bs.writeSe(entry.tile_bounding_box_xyz0.z());
    bs.writeUe(entry.tile_bounding_box_whd.x());
    bs.writeUe(entry.tile_bounding_box_whd.y());
    bs.writeUe(entry.tile_bounding_box_whd.z());
  }

  bs.byteAlign();

  return buf;
}

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

TileInventory
parseTileInventory(const PayloadBuffer& buf)
{
  TileInventory inventory;
  assert(buf.type == PayloadType::kTileInventory);
  auto bs = makeBitReader(buf.begin(), buf.end());

  int num_tiles;
  bs.readUe(&num_tiles);
  for (int i = 0; i < num_tiles; i++) {
    TileInventory::Entry entry;
    bs.readSe(&entry.tile_bounding_box_xyz0.x());
    bs.readSe(&entry.tile_bounding_box_xyz0.y());
    bs.readSe(&entry.tile_bounding_box_xyz0.z());
    bs.readUe(&entry.tile_bounding_box_whd.x());
    bs.readUe(&entry.tile_bounding_box_whd.y());
    bs.readUe(&entry.tile_bounding_box_whd.z());
    inventory.tiles.push_back(entry);
  }

  bs.byteAlign();

  return inventory;
}

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

630
}  // namespace pcc