io_hls.cpp 19.6 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
    bs.writeUe(attr.attr_bitdepth_secondary);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    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);
    }
  }

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

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  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) {
140
141
142
143
144
145
146
147
    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());
148
149
150
151
152
153
154
155
156
  }
  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();
157
    bs.readUe(&attr.attr_num_dimensions);
158
    bs.readUe(&attr.attr_instance_id);
159
    bs.readUe(&attr.attr_bitdepth);
160
    bs.readUe(&attr.attr_bitdepth_secondary);
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    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);
    }
  }

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

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  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);
203
204
205
206
207
  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);
  }
208
209
210
  bs.write(gps.geom_unique_points_flag);
  bs.write(gps.neighbour_context_restriction_flag);
  bs.write(gps.inferred_direct_coding_mode_enabled_flag);
211
  bs.write(gps.bitwise_occupancy_coding_flag);
212
  bs.write(gps.adjacent_child_contextualization_enabled_flag);
213
  bs.writeUe(gps.geom_occupancy_ctx_reduction_factor);
214
  bs.writeUe(gps.neighbour_avail_boundary_log2);
215
  bs.writeUe(gps.intra_pred_max_node_size_log2);
216
  bs.writeUe(gps.trisoup_node_size_log2);
217

218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  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);
237
238
239
240
241
  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);
  }
242
243
244
  bs.read(&gps.geom_unique_points_flag);
  bs.read(&gps.neighbour_context_restriction_flag);
  bs.read(&gps.inferred_direct_coding_mode_enabled_flag);
245
  bs.read(&gps.bitwise_occupancy_coding_flag);
246
  bs.read(&gps.adjacent_child_contextualization_enabled_flag);
247
  bs.readUe(&gps.geom_occupancy_ctx_reduction_factor);
248
  bs.readUe(&gps.neighbour_avail_boundary_log2);
249
  bs.readUe(&gps.intra_pred_max_node_size_log2);
250
  bs.readUe(&gps.trisoup_node_size_log2);
251

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
  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);

274
275
  bs.writeUe(aps.init_qp);
  bs.writeSe(aps.aps_chroma_qp_offset);
276
  bs.write(aps.aps_slice_qp_deltas_present_flag);
277

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

    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]);
      }
298
    }
299
300
301
302
  }

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

  if (aps.attr_encoding == AttributeEncoding::kRAHTransform) {
309
    bs.write(aps.raht_prediction_enabled_flag);
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
    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);

333
334
  bs.readUe(&aps.init_qp);
  bs.readSe(&aps.aps_chroma_qp_offset);
335
  bs.read(&aps.aps_slice_qp_deltas_present_flag);
336

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

    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);
353

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

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

  if (aps.attr_encoding == AttributeEncoding::kRAHTransform) {
370
    bs.read(&aps.raht_prediction_enabled_flag);
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
    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(
388
  const SequenceParameterSet& sps,
389
390
391
392
393
394
395
396
  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);
397
  bs.writeUe(gbh.geom_tile_id);
398
  bs.writeUe(gbh.geom_slice_id);
399
  bs.writeUn(sps.log2_max_frame_idx, gbh.frame_idx);
400
401

  if (gps.geom_box_present_flag) {
402
403
404
405
    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;
406

407
408
    if (gps.geom_box_log2_scale_present_flag)
      bs.writeUe(gbh.geom_box_log2_scale);
409
410
411
412
413
414
415
416
417
418
419
420
421
422
    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(
423
424
425
426
  const SequenceParameterSet& sps,
  const GeometryParameterSet& gps,
  const PayloadBuffer& buf,
  int* bytesRead)
427
428
429
430
431
432
{
  GeometryBrickHeader gbh;
  assert(buf.type == PayloadType::kGeometryBrick);
  auto bs = makeBitReader(buf.begin(), buf.end());

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

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

    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);
447
448
449
450
    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;
451
452
453
454
455
456
457
458
459
460
461
462
  }

  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;
}

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

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;
}

482
483
484
//============================================================================

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

  bs.writeUe(abh.attr_attr_parameter_set_id);
494
  bs.writeUe(abh.attr_sps_attr_idx);
495
  bs.writeUe(abh.attr_geom_slice_id);
496

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

502
503
504
505
506
507
508
509
510
511
512
513
514
  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]);
    }
  }

515
516
517
518
519
520
  bs.byteAlign();
}

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

AttributeBrickHeader
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
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)
542
543
544
545
546
547
{
  AttributeBrickHeader abh;
  assert(buf.type == PayloadType::kAttributeBrick);
  auto bs = makeBitReader(buf.begin(), buf.end());

  bs.readUe(&abh.attr_attr_parameter_set_id);
548
  bs.readUe(&abh.attr_sps_attr_idx);
549
  bs.readUe(&abh.attr_geom_slice_id);
550

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

556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
  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]);
    }
  }

571
572
573
574
575
576
577
578
579
580
  bs.byteAlign();

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

  return abh;
}

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

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
630
631
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;
}

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

632
}  // namespace pcc