/
Parser.cpp
683 lines (642 loc) · 21.9 KB
/
Parser.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
//
// Created by psi on 2019/11/23.
//
#include <cstdio>
#include <string>
#include <memory>
#include "../../external/tinyformat/tinyformat.h"
#include "util/Logger.hpp"
#include "Parser.hpp"
#include "FileBox.hpp"
#include "util/FourCC.hpp"
#include "ColourInformationBox.hpp"
using avif::util::str2uint;
using avif::util::uint2str;
namespace avif {
constexpr uint32_t boxType(const char *str) {
return str2uint(str);
}
//-----------------------------------------------------------------------------
Parser::Parser(util::Logger& log, std::vector<uint8_t> buff)
:log_(log)
,buffer_(std::move(buff))
,reader_(log, buffer_)
,fileBox_()
{
}
std::shared_ptr<Parser::Result> Parser::parse() {
if(this->result_) {
return this->result_;
}
try {
this->parseFile();
this->result_ = std::make_shared<Parser::Result>(std::move(this->buffer_), std::move(fileBox_));
} catch(Parser::Error& err) {
this->result_ = std::make_shared<Parser::Result>(std::move(this->buffer_), std::move(err));
} catch(std::exception& err) {
this->result_ = std::make_shared<Parser::Result>(std::move(this->buffer_), Parser::Error(err));
} catch(...) {
this->result_ = std::make_shared<Parser::Result>(std::move(this->buffer_), Parser::Error("Unknwon error"));
}
return this->result_;
}
void Parser::parseFile() {
while(!this->consumed()) {
this->parseBoxInFile();
}
}
void Parser::parseBoxInFile() {
FileBox& box = this->fileBox_;
Box::Header hdr = readBoxHeader();
switch(hdr.type) {
case boxType("ftyp"):
// ISO/IEC 14496-12:2015(E)
// 4.3 File Type Box
// Quantity: Exactly one (but see below)
box.fileTypeBox.hdr = hdr;
this->parseFileTypeBox(box.fileTypeBox, hdr.end());
break;
case boxType("free"):
case boxType("skip"):
// ISO/IEC 14496-12:2015(E)
// 8.1.2
// free_type may be ‘free’ or ‘skip’.
break;
case boxType("meta"): {
// ISO/IEC 14496-12:2015(E)
// 8.11.1
// Quantity: Zero or one (in File, ‘moov’, and ‘trak’), One or more (in ‘meco’)
box.metaBox.hdr = hdr;
this->parseMetaBox(box.metaBox, hdr.end());
break;
}
case boxType("mdat"): {
// ISO/IEC 14496-12:2015(E)
// 8.1.1. Media Data Box
// Quantity: Zero or more
MediaDataBox mediaDataBox{};
mediaDataBox.hdr = hdr;
this->parseMediaDataBox(mediaDataBox, hdr.end());
box.mediaDataBoxes.emplace_back(mediaDataBox);
break;
}
default:
warningUnknownBox(hdr);
break;
}
this->seek(hdr.end());
}
void Parser::parseFileTypeBox(FileTypeBox& box, size_t const end) {
uint32_t const majorBrand = readU32();
uint32_t const minorVersion = readU32();
if(str2uint("avif") != majorBrand) {
throw Error("Unsupported brand: %s", uint2str(majorBrand));
}
if(minorVersion != 0) {
throw Error("We currently just support version 0(!=%d)", minorVersion);
}
std::vector<std::string> compatibleBrands;
while(this->pos() < end) {
uint32_t const compatBrand = readU32();
compatibleBrands.emplace_back(uint2str(compatBrand));
}
box.majorBrand = uint2str(majorBrand);
box.minorVersion = minorVersion;
box.compatibleBrands = std::move(compatibleBrands);
}
// See ISOBMFF p.90
void Parser::parseMetaBox(MetaBox& box, size_t const end) {
// MetaBox extends FullBox.
// See: ISOBMFF p.21
this->parseFullBoxHeader(box);
while(this->pos() < end) {
this->parseBoxInMeta(box, end);
}
}
void Parser::parseBoxInMeta(MetaBox& box, size_t const endOfBox) {
Box::Header const hdr = readBoxHeader();
switch(hdr.type) {
case boxType("hdlr"):
box.handlerBox.hdr = hdr;
this->parseHandlerBox(box.handlerBox, hdr.end());
break;
case boxType("iprp"):
// ISO/IEC 23008-12:2017 p.28
// 9.3 Item Properties Box
box.itemPropertiesBox.hdr = hdr;
this->parseItemPropertiesBox(box.itemPropertiesBox, hdr.end());
break;
case boxType("iinf"):
// 8.11.6 Item Information Box
// See: ISOBMFF p.95
box.itemInfoBox.hdr = hdr;
this->parseItemInfoBox(box.itemInfoBox, hdr.end());
break;
case boxType("iloc"):
// 8.11.3 The Item Location Box
// See: ISOBMFF p.91
box.itemLocationBox.hdr = hdr;
this->parseItemLocationBox(box.itemLocationBox, hdr.end());
break;
case boxType("pitm"): {
// 8.11.3 The Item Location Box
// See: ISOBMFF p.80
PrimaryItemBox pitm{};
this->parsePrimaryItemBox(pitm, hdr.end());
box.primaryItemBox = pitm;
break;
}
default:
warningUnknownBox(hdr);
break;
}
// ISO/IEC 23008-12:2017 p.34
// 10.2 Image and image collection brands
// Note particularly that the brand 'mif1' does not mandate a MovieBox ('moov').
this->seek(hdr.end());
}
void Parser::parseHandlerBox(HandlerBox& box, size_t const end) {
// HandlerBox extends FullBox.
// See: ISOBMFF p.43
this->parseFullBoxHeader(box);
// ISOBMFF p.44 8.4.3.2
uint32_t reserved;
reserved = readU32(); // pre_defined = 0
uint32_t const handlerType = readU32();
reserved = readU32(); // reserved
reserved = readU32(); // reserved
reserved = readU32(); // reserved
box.name = readString();
switch(handlerType) {
case str2uint("pict"):
// よくわからんが、これで正しいらしい
// たぶんHEIFの仕様書に書いてある…
// https://github.com/AOMediaCodec/libavif/blob/c673f2e884e635ff8e9b2c2951a2dddd2a00ffc3/src/write.c#L185
// https://github.com/Kagami/avif.js/blob/500e3357e5d56750dbd4e40b3aee74b13207721e/mov.js#L49
box.handler = "pict";
break;
case str2uint("null"):
log().warn("NULL header type in HeaderBox");
break;
default:
log().warn("Unknown header type=%s in HeaderBox", uint2str(handlerType));
break;
}
}
void Parser::parseItemPropertiesBox(ItemPropertiesBox& box, size_t const end) {
// ISO/IEC 23008-12:2017 p.28
// 9.3 Item Properties Box
{
Box::Header const hdr = readBoxHeader();
if(hdr.type != str2uint("ipco")) {
throw Error("ItemPropertyContainer expected, got %s", uint2str(hdr.type));
}
box.propertyContainers.hdr = hdr;
// https://github.com/nokiatech/heif/blob/master/srcs/common/itempropertycontainer.cpp
while(this->pos() < hdr.end()) {
this->parseBoxInItemPropertyContainer(box.propertyContainers);
}
this->seek(hdr.end());
}
// read "ipma"s
while(this->pos() < end) {
Box::Header const hdr = readBoxHeader();
if(hdr.type != str2uint("ipma")) {
throw Error("ItemPropertyAssociation(ipma) expected, got %s", uint2str(hdr.type));
}
ItemPropertyAssociation itemPropertyAssociation{};
itemPropertyAssociation.hdr = hdr;
this->parseItemPropertyAssociation(itemPropertyAssociation);
box.associations.emplace_back(itemPropertyAssociation);
this->seek(hdr.end());
}
}
void Parser::parseBoxInItemPropertyContainer(ItemPropertyContainer& container) {
// https://github.com/nokiatech/heif/blob/master/srcs/common/itempropertycontainer.cpp
Box::Header const hdr = readBoxHeader();
switch(hdr.type) {
case boxType("pasp"): {
PixelAspectRatioBox box{};
box.hdr = hdr;
this->parsePixelAspectRatioBox(box, hdr.end());
container.properties.emplace_back(box);
break;
}
case boxType("ispe"): {
ImageSpatialExtentsProperty box{};
box.hdr = hdr;
this->parseImageSpatialExtentsProperty(box, hdr.end());
container.properties.emplace_back(box);
break;
}
case boxType("pixi"): {
// ISO/IEC 23008-12:2017(E)
// p.13
// 6.5.6.1 Pixel information
PixelInformationProperty box{};
box.hdr = hdr;
this->parsePixelInformationProperty(box, hdr.end());
container.properties.emplace_back(box);
break;
}
case boxType("clap"): {
// ISO/IEC 14496-12:2015(E)
// p.158
CleanApertureBox clap{};
clap.hdr = hdr;
this->parseCleanApertureBox(clap, hdr.end());
container.properties.emplace_back(clap);
break;
}
case boxType("irot"): {
ImageRotationBox irot;
irot.hdr = hdr;
this->parseImageRotationBox(irot, hdr.end());
container.properties.emplace_back(irot);
break;
}
case boxType("imir"): {
ImageMirrorBox imir;
imir.hdr = hdr;
this->parseImageMirrorBox(imir, hdr.end());
container.properties.emplace_back(imir);
break;
}
case boxType("colr"): {
ColourInformationBox colr;
colr.hdr = hdr;
this->parseColourInformationBox(colr, hdr.end());
container.properties.emplace_back(colr);
break;
}
case boxType("clli"): {
ContentLightLevelBox clli;
clli.hdr = hdr;
this->parseContentLightLevelBox(clli, hdr.end());
container.properties.emplace_back(clli);
break;
}
case boxType("mdcv"): {
MasteringDisplayColourVolumeBox mdcv;
mdcv.hdr = hdr;
this->parseMasteringDisplayColourVolumeBox(mdcv, hdr.end());
container.properties.emplace_back(mdcv);
break;
}
case boxType("av1C"): {
AV1CodecConfigurationRecordBox box{};
box.hdr = hdr;
this->parseAV1CodecConfigurationRecordBox(box, hdr.end());
container.properties.emplace_back(box);
break;
}
case boxType("free"):
case boxType("skip"):
// ISO/IEC 14496-12:2015(E)
// 8.1.2
// free_type may be ‘free’ or ‘skip’.
break;
default:
warningUnknownBox(hdr);
break;
}
this->seek(hdr.end());
}
void Parser::parsePixelAspectRatioBox(PixelAspectRatioBox& box, size_t const end) {
// 12.1.4 Pixel Aspect Ratio and Clean Aperture
// See: ISOBMFF p.170
box.hSpacing = readU32();
box.vSpacing = readU32();
}
void Parser::parseImageSpatialExtentsProperty(ImageSpatialExtentsProperty& prop, size_t const end) {
// ISO/IEC 23008-12:2017(E)
// p.11
// 6.5.3 Image spatial extents
parseFullBoxHeader(prop);
prop.imageWidth = readU32();
prop.imageHeight = readU32();
}
void Parser::parsePixelInformationProperty(PixelInformationProperty& prop, size_t const end) {
// ISO/IEC 23008-12:2017(E)
// p.13
// 6.5.6.1 Pixel information
parseFullBoxHeader(prop);
uint8_t const numChannels = readU8();
for(uint8_t i =0; i < numChannels; ++i) {
prop.bitsPerChannel.emplace_back(readU8());
}
}
void Parser::parseCleanApertureBox(CleanApertureBox& box, size_t const end) {
// ISO/IEC 14496-12:2015(E)
// p.158
// a fractional number which defines the exact
// clean aperture width, in counted pixels, of the video image
box.cleanApertureWidthN = static_cast<int32_t>(readU32());
box.cleanApertureWidthD = readU32();
// a fractional number which defines the
// exact clean aperture height, in counted pixels, of the video image
box.cleanApertureHeightN = static_cast<int32_t>(readU32());
box.cleanApertureHeightD = readU32();
// a fractional number which defines the horizontal offset of clean
// aperture centre minus (width‐1)/2. Typically 0.
box.horizOffN = static_cast<int32_t>(readU32());
box.horizOffD = readU32();
// a fractional number which defines the vertical offset of clean aperture
// centre minus (height‐1)/2. Typically 0.
box.vertOffN = static_cast<int32_t>(readU32());
box.vertOffD = readU32();
}
void Parser::parseImageRotationBox(ImageRotationBox& box, size_t const end) {
// ISO/IEC 23008-12:2017(E)
// p.15
// 6.5.10 Image rotation
box.angle = static_cast<ImageRotationBox::Rotation>(readU8() & 0x3u); // angle * 90
// angle * 90 specifies the angle (in anti-clockwise direction) in units of degrees.
}
void Parser::parseImageMirrorBox(ImageMirrorBox& box, size_t const end) {
// ISO/IEC 23008-12:2017(E)
// p.16
// 6.5.12 Image rotation
box.axis = static_cast<ImageMirrorBox::Axis>(readU8() & 0x1u);
// angle * 90 specifies the angle (in anti-clockwise direction) in units of degrees.
}
void Parser::parseColourInformationBox(ColourInformationBox& box, uint32_t end) {
// ISO/IEC 14496-12:2015(E)
// p/159
// 12.1.5 Colour information
uint32_t const colourType = readU32();
switch(colourType) {
case str2uint("nclx"): {
ColourInformationBox::NCLX nclx{};
nclx.colourPrimaries = readU16();
nclx.transferCharacteristics = readU16();
nclx.matrixCoefficients = readU16();
nclx.fullRangeFlag = 0x80u == (readU8() & 0x80u);
box.profile = nclx;
break;
}
case str2uint("rICC"): {
std::vector<uint8_t> data(std::next(buffer_.begin(), pos()), std::next(buffer_.begin(), end));
box.profile = ColourInformationBox::RestrictedICC {
.payload = std::move(data),
};
break;
}
case str2uint("prof"): {
std::vector<uint8_t> data(std::next(buffer_.begin(), pos()), std::next(buffer_.begin(), end));
box.profile = ColourInformationBox::UnrestrictedICC {
.payload = std::move(data),
};
break;
}
default:
throw Error("Unknown profile type: %s", uint2str(colourType));
}
}
void Parser::parseContentLightLevelBox(ContentLightLevelBox& box, uint32_t end) {
box.maxContentLightLevel = readU16();
box.maxPicAverageLightLevel = readU16();
}
void Parser::parseMasteringDisplayColourVolumeBox(MasteringDisplayColourVolumeBox& box, uint32_t end) {
for(int c = 0; c < 3; ++c) {
box.displayPrimariesX[c] = readU16();
box.displayPrimariesY[c] = readU16();
}
box.whitePointX = readU16();
box.whitePointY = readU16();
box.maxDisplayMasteringLuminance = readU32();
box.minDisplayMasteringLuminance = readU32();
}
void Parser::parseAV1CodecConfigurationRecordBox(AV1CodecConfigurationRecordBox& box, size_t const end) {
// https://aomediacodec.github.io/av1-isobmff/#av1codecconfigurationbox-section
AV1CodecConfigurationRecord& conf = box.av1Config;
uint8_t tmp = readU8();
conf.marker = (tmp & 0x80u) == 0x80u;
conf.version = tmp & 0x7fu;
tmp = readU8();
conf.seqProfile = static_cast<uint8_t>(tmp >> 5u) & 0x7u;
conf.seqLevelIdx0 = tmp & 31u;
tmp = readU8();
conf.seqTier0 = static_cast<uint8_t>(tmp >> 7u) & 1u;
conf.highBitDepth = (tmp & (1u << 6u)) == (1u << 6u);
conf.twelveBit = (tmp & (1u << 5u)) == (1u << 5u);
conf.monochrome = (tmp & (1u << 4u)) == (1u << 4u);
conf.chromaSubsamplingX = static_cast<uint8_t>(tmp >> 3u) & 1u;
conf.chromaSubsamplingY = static_cast<uint8_t>(tmp >> 2u) & 1u;
conf.chromaSamplePosition = tmp & 3u;
tmp = readU8();
tmp = tmp & 31u; // unsigned int (3) reserved = 0;
conf.initialPresentationDelayPresent = (tmp & (1u << 4u)) == (1u << 4u);
if(conf.initialPresentationDelayPresent) {
conf.initialPresentationDelay = (tmp & 15u) + 1u;
} else {
conf.initialPresentationDelay = 0;
}
conf.configOBUs = std::vector<uint8_t>(std::next(this->buffer_.begin(), this->pos()), std::next(this->buffer_.begin(), end));
}
void Parser::parseItemPropertyAssociation(ItemPropertyAssociation& assoc) {
// https://github.com/nokiatech/heif/blob/master/srcs/common/itempropertyassociation.cpp
parseFullBoxHeader(assoc);
uint32_t const itemCount = readU32();
for(uint32_t i = 0; i < itemCount; ++i) {
ItemPropertyAssociation::Item item;
if(assoc.version() < 1) {
item.itemID = readU16();
} else {
item.itemID = readU32();
}
uint8_t entryCount = readU8();
for(uint8_t j = 0; j < entryCount; ++j) {
ItemPropertyAssociation::Item::Entry entry{};
if((assoc.flags() & 1u) == 1u) {
uint16_t const v = readU16();
entry.essential = (v & 0x8000u) == 0x8000u;
entry.propertyIndex = v & 0x7fffu;
} else {
uint8_t const v = readU8();
entry.essential = (v & 0x80u) == 0x80u;
entry.propertyIndex = v & 0x7fu;
}
item.entries.emplace_back(entry);
}
assoc.items.emplace_back(item);
}
}
void Parser::parseItemInfoBox(ItemInfoBox& box, size_t const end) {
// 8.11.6 Item Information Box
// See: ISOBMFF p.95
this->parseFullBoxHeader(box);
uint32_t entryCount;
if(box.version() == 0){
entryCount = readU16();
} else {
entryCount = readU32();
}
for(uint32_t i = 0; i < entryCount; ++i) {
Box::Header hdr = readBoxHeader();
if(hdr.type != str2uint("infe")) {
throw Error("'infe' expected in ItemInfoBox, got %s", uint2str(hdr.type));
}
ItemInfoEntry entry;
entry.hdr = hdr;
this->parseItemInfoEntry(entry, hdr.end());
box.itemInfos.emplace_back(std::move(entry));
this->seek(hdr.end());
}
}
void Parser::parseItemInfoEntry(ItemInfoEntry& box, size_t const end) {
parseFullBoxHeader(box);
if(box.version() == 0 || box.version() == 1) {
box.itemID = readU16();
box.itemProtectionIndex = readU16();
box.itemName = readString();
box.contentType = readString();
box.contentEncoding = readString();
}
if(box.version() == 1) {
uint32_t const extensionType = readU32();
switch (extensionType) {
case str2uint("fdel"): {
FDItemInfoExtension ext;
ext.extensionType = extensionType;
ext.contentLocation = readString();
ext.contentLength = readU64();
ext.transferLength = readU64();
uint8_t entryCount = readU8();
for(uint8_t i = 0; i < entryCount; ++i) {
ext.groupIDs.emplace_back(readU8());
}
std::get<FDItemInfoExtension>(box.itemInfoExtension) = std::move(ext);
break;
}
default:
throw Error("Unknwon ItemInfoExtension with type = %s", uint2str(extensionType));
}
}
if(box.version() >= 2) {
if (box.version() == 2) {
box.itemID = readU16();
} else if(box.version() == 3) {
box.itemID = readU32();
} else {
throw Error("ItemInfoEntry with version=%d not supported.", box.version());
}
box.itemProtectionIndex = readU16();
uint32_t const itemType = readU32();
box.itemType = std::make_optional<std::string>(uint2str(itemType));
box.itemName = readString();
switch(itemType) { // There may be additional data in particular cases.
case str2uint("mime"):
box.contentType = readString();
box.contentEncoding = readString();
break;
case str2uint("uri "):
box.itemURIType = std::make_optional<std::string>(readString());
break;
default:
break;
}
}
}
void Parser::parseItemLocationBox(ItemLocationBox& box, size_t const end) {
// 8.11.3 The Item Location Box
// See: ISOBMFF p.91
this->parseFullBoxHeader(box);
uint32_t v = readU8();
box.offsetSize = static_cast<uint8_t>(v >> 4u);
if(!(box.offsetSize == 0 || box.offsetSize == 4 || box.offsetSize == 8)) {
throw Error("Invalid ItemLocationBox::offsetSize=%d", box.offsetSize);
}
box.lengthSize = static_cast<uint8_t>(v & 0xfu);
if(!(box.lengthSize == 0 || box.lengthSize == 4 || box.lengthSize == 8)) {
throw Error("Invalid ItemLocationBox::lengthSize=%d", box.offsetSize);
}
v = readU8();
box.baseOffsetSize = static_cast<uint8_t>(v >> 4u);
if(!(box.baseOffsetSize == 0 || box.baseOffsetSize == 4 || box.baseOffsetSize == 8)) {
throw Error("Invalid ItemLocationBox::baseOffsetSize=%d", box.offsetSize);
}
if(box.version() == 1 || box.version() == 2) {
box.indexSize = static_cast<uint8_t>(v & 0xfu);
if(!(box.indexSize == 0 || box.indexSize == 4 || box.indexSize == 8)) {
throw Error("Invalid ItemLocationBox::baseOffsetSize=%d", box.offsetSize);
}
}
uint32_t itemCount = 0;
if(box.version() < 2) {
itemCount = readU16();
}else if(box.version() == 2){
itemCount = readU32();
}else{
throw Error("Unknwon ItemLocationBox version=%d", box.version());
}
for(uint32_t i = 0; i < itemCount; ++i) {
ItemLocationBox::Item item{};
if (box.version() < 2) {
item.itemID = readU16();
} else if (box.version() == 2) {
item.itemID = readU32();
} else {
throw Error("Unknwon ItemLocationBox version=%d", box.version());
}
if (box.version() == 1 || box.version() == 2) {
item.constructionMethod = readU16() & 0x7u;
}
item.dataReferenceIndex = readU16();
item.baseOffset = readUint(box.baseOffsetSize).value();
uint16_t const extentCount = readU16();
for (uint32_t j = 0; j < extentCount; ++j) {
ItemLocationBox::Item::Extent extent{};
if((box.version() == 1 || box.version() == 2) && (box.indexSize > 0)) {
extent.extentIndex = readUint(box.indexSize).value();
}
extent.extentOffset = readUint(box.offsetSize).value();
extent.extentLength = readUint(box.lengthSize).value();
item.extents.emplace_back(extent);
}
box.items.emplace_back(item);
}
}
void Parser::parsePrimaryItemBox(avif::PrimaryItemBox& box, size_t const end) {
parseFullBoxHeader(box);
if(box.version() == 0) {
box.itemID = readU16();
} else {
box.itemID = readU32();
}
}
void Parser::parseMediaDataBox(MediaDataBox& box, size_t const end) {
box.offset = this->pos();
box.size = end - box.offset;
}
//-----------------------------------------------------------------------------
// util
//-----------------------------------------------------------------------------
Box::Header Parser::readBoxHeader() {
Box::Header hdr{};
hdr.offset = this->pos();
hdr.size = readU32();
hdr.type = readU32();
if(hdr.size == 1) {
throw Error("LargeSize box is not supported.");
}
if(hdr.size == 0) {
hdr.size = this->buffer_.size() - hdr.offset;
}
if((hdr.end()) > this->buffer_.size()) {
throw Error("File corrupted. Detected at %s box, from %d to %d, but buffer.size = %d.", uint2str(hdr.type), hdr.offset, hdr.end(), this->buffer_.size());
}
return hdr;
}
void Parser::parseFullBoxHeader(FullBox& fullBox) {
// See: ISOBMFF p.22
uint32_t v = readU32();
auto const version = static_cast<uint8_t>(v >> 24u);
auto const flags = (v & 0x00ffffffu);
fullBox.setFullBoxHeader(version, flags);
}
void Parser::warningUnknownBox(Box::Header const& hdr) {
std::string typeStr = uint2str(hdr.type);
log().warn("Unknown box type=%s(=0x%0lx) with size=%ld(%ld~%ld/%ld)", typeStr.c_str(), hdr.type, hdr.size, hdr.offset, hdr.end(), this->buffer_.size());
}
}