forked from AOMediaCodec/libavif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.c
3879 lines (3388 loc) · 162 KB
/
read.c
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 Joe Drago. All rights reserved.
// SPDX-License-Identifier: BSD-2-Clause
#include "avif/internal.h"
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <string.h>
#define AUXTYPE_SIZE 64
#define CONTENTTYPE_SIZE 64
// class VisualSampleEntry(codingname) extends SampleEntry(codingname) {
// unsigned int(16) pre_defined = 0;
// const unsigned int(16) reserved = 0;
// unsigned int(32)[3] pre_defined = 0;
// unsigned int(16) width;
// unsigned int(16) height;
// template unsigned int(32) horizresolution = 0x00480000; // 72 dpi
// template unsigned int(32) vertresolution = 0x00480000; // 72 dpi
// const unsigned int(32) reserved = 0;
// template unsigned int(16) frame_count = 1;
// string[32] compressorname;
// template unsigned int(16) depth = 0x0018;
// int(16) pre_defined = -1;
// // other boxes from derived specifications
// CleanApertureBox clap; // optional
// PixelAspectRatioBox pasp; // optional
// }
static const size_t VISUALSAMPLEENTRY_SIZE = 78;
static const char xmpContentType[] = CONTENT_TYPE_XMP;
static const size_t xmpContentTypeSize = sizeof(xmpContentType);
// The only supported ipma box values for both version and flags are [0,1], so there technically
// can't be more than 4 unique tuples right now.
#define MAX_IPMA_VERSION_AND_FLAGS_SEEN 4
#define MAX_AV1_LAYER_COUNT 4
// ---------------------------------------------------------------------------
// Box data structures
// ftyp
typedef struct avifFileType
{
uint8_t majorBrand[4];
uint32_t minorVersion;
// If not null, points to a memory block of 4 * compatibleBrandsCount bytes.
const uint8_t * compatibleBrands;
int compatibleBrandsCount;
} avifFileType;
// ispe
typedef struct avifImageSpatialExtents
{
uint32_t width;
uint32_t height;
} avifImageSpatialExtents;
// auxC
typedef struct avifAuxiliaryType
{
char auxType[AUXTYPE_SIZE];
} avifAuxiliaryType;
// infe mime content_type
typedef struct avifContentType
{
char contentType[CONTENTTYPE_SIZE];
} avifContentType;
// colr
typedef struct avifColourInformationBox
{
avifBool hasICC;
const uint8_t * icc;
size_t iccSize;
avifBool hasNCLX;
avifColorPrimaries colorPrimaries;
avifTransferCharacteristics transferCharacteristics;
avifMatrixCoefficients matrixCoefficients;
avifRange range;
} avifColourInformationBox;
#define MAX_PIXI_PLANE_DEPTHS 4
typedef struct avifPixelInformationProperty
{
uint8_t planeDepths[MAX_PIXI_PLANE_DEPTHS];
uint8_t planeCount;
} avifPixelInformationProperty;
typedef struct avifOperatingPointSelectorProperty
{
uint8_t opIndex;
} avifOperatingPointSelectorProperty;
typedef struct avifLayerSelectorProperty
{
uint16_t layerID;
} avifLayerSelectorProperty;
typedef struct avifAV1LayeredImageIndexingProperty
{
uint32_t layerSize[3];
} avifAV1LayeredImageIndexingProperty;
// ---------------------------------------------------------------------------
// Top-level structures
struct avifMeta;
// Temporary storage for ipco/stsd contents until they can be associated and memcpy'd to an avifDecoderItem
typedef struct avifProperty
{
uint8_t type[4];
union
{
avifImageSpatialExtents ispe;
avifAuxiliaryType auxC;
avifColourInformationBox colr;
avifCodecConfigurationBox av1C;
avifPixelAspectRatioBox pasp;
avifCleanApertureBox clap;
avifImageRotation irot;
avifImageMirror imir;
avifPixelInformationProperty pixi;
avifOperatingPointSelectorProperty a1op;
avifLayerSelectorProperty lsel;
avifAV1LayeredImageIndexingProperty a1lx;
} u;
} avifProperty;
AVIF_ARRAY_DECLARE(avifPropertyArray, avifProperty, prop);
static const avifProperty * avifPropertyArrayFind(const avifPropertyArray * properties, const char * type)
{
for (uint32_t propertyIndex = 0; propertyIndex < properties->count; ++propertyIndex) {
avifProperty * prop = &properties->prop[propertyIndex];
if (!memcmp(prop->type, type, 4)) {
return prop;
}
}
return NULL;
}
AVIF_ARRAY_DECLARE(avifExtentArray, avifExtent, extent);
// one "item" worth for decoding (all iref, iloc, iprp, etc refer to one of these)
typedef struct avifDecoderItem
{
uint32_t id;
struct avifMeta * meta; // Unowned; A back-pointer for convenience
uint8_t type[4];
size_t size;
uint32_t idatID; // If non-zero, offset is relative to this idat box (iloc construction_method==1)
uint32_t width; // Set from this item's ispe property, if present
uint32_t height; // Set from this item's ispe property, if present
avifContentType contentType;
avifPropertyArray properties;
avifExtentArray extents; // All extent offsets/sizes
avifRWData mergedExtents; // if set, is a single contiguous block of this item's extents (unused when extents.count == 1)
avifBool ownsMergedExtents; // if true, mergedExtents must be freed when this item is destroyed
avifBool partialMergedExtents; // If true, mergedExtents doesn't have all of the item data yet
uint32_t thumbnailForID; // if non-zero, this item is a thumbnail for Item #{thumbnailForID}
uint32_t auxForID; // if non-zero, this item is an auxC plane for Item #{auxForID}
uint32_t descForID; // if non-zero, this item is a content description for Item #{descForID}
uint32_t dimgForID; // if non-zero, this item is a derived image for Item #{dimgForID}
uint32_t premByID; // if non-zero, this item is premultiplied by Item #{premByID}
avifBool hasUnsupportedEssentialProperty; // If true, this item cites a property flagged as 'essential' that libavif doesn't support (yet). Ignore the item, if so.
avifBool ipmaSeen; // if true, this item already received a property association
avifBool progressive; // if true, this item has progressive layers (a1lx), but does not select a specific layer (lsel)
} avifDecoderItem;
AVIF_ARRAY_DECLARE(avifDecoderItemArray, avifDecoderItem, item);
// idat storage
typedef struct avifDecoderItemData
{
uint32_t id;
avifRWData data;
} avifDecoderItemData;
AVIF_ARRAY_DECLARE(avifDecoderItemDataArray, avifDecoderItemData, idat);
// grid storage
typedef struct avifImageGrid
{
uint32_t rows; // Legal range: [1-256]
uint32_t columns; // Legal range: [1-256]
uint32_t outputWidth;
uint32_t outputHeight;
} avifImageGrid;
// ---------------------------------------------------------------------------
// avifTrack
typedef struct avifSampleTableChunk
{
uint64_t offset;
} avifSampleTableChunk;
AVIF_ARRAY_DECLARE(avifSampleTableChunkArray, avifSampleTableChunk, chunk);
typedef struct avifSampleTableSampleToChunk
{
uint32_t firstChunk;
uint32_t samplesPerChunk;
uint32_t sampleDescriptionIndex;
} avifSampleTableSampleToChunk;
AVIF_ARRAY_DECLARE(avifSampleTableSampleToChunkArray, avifSampleTableSampleToChunk, sampleToChunk);
typedef struct avifSampleTableSampleSize
{
uint32_t size;
} avifSampleTableSampleSize;
AVIF_ARRAY_DECLARE(avifSampleTableSampleSizeArray, avifSampleTableSampleSize, sampleSize);
typedef struct avifSampleTableTimeToSample
{
uint32_t sampleCount;
uint32_t sampleDelta;
} avifSampleTableTimeToSample;
AVIF_ARRAY_DECLARE(avifSampleTableTimeToSampleArray, avifSampleTableTimeToSample, timeToSample);
typedef struct avifSyncSample
{
uint32_t sampleNumber;
} avifSyncSample;
AVIF_ARRAY_DECLARE(avifSyncSampleArray, avifSyncSample, syncSample);
typedef struct avifSampleDescription
{
uint8_t format[4];
avifPropertyArray properties;
} avifSampleDescription;
AVIF_ARRAY_DECLARE(avifSampleDescriptionArray, avifSampleDescription, description);
typedef struct avifSampleTable
{
avifSampleTableChunkArray chunks;
avifSampleDescriptionArray sampleDescriptions;
avifSampleTableSampleToChunkArray sampleToChunks;
avifSampleTableSampleSizeArray sampleSizes;
avifSampleTableTimeToSampleArray timeToSamples;
avifSyncSampleArray syncSamples;
uint32_t allSamplesSize; // If this is non-zero, sampleSizes will be empty and all samples will be this size
} avifSampleTable;
static avifSampleTable * avifSampleTableCreate()
{
avifSampleTable * sampleTable = (avifSampleTable *)avifAlloc(sizeof(avifSampleTable));
memset(sampleTable, 0, sizeof(avifSampleTable));
avifArrayCreate(&sampleTable->chunks, sizeof(avifSampleTableChunk), 16);
avifArrayCreate(&sampleTable->sampleDescriptions, sizeof(avifSampleDescription), 2);
avifArrayCreate(&sampleTable->sampleToChunks, sizeof(avifSampleTableSampleToChunk), 16);
avifArrayCreate(&sampleTable->sampleSizes, sizeof(avifSampleTableSampleSize), 16);
avifArrayCreate(&sampleTable->timeToSamples, sizeof(avifSampleTableTimeToSample), 16);
avifArrayCreate(&sampleTable->syncSamples, sizeof(avifSyncSample), 16);
return sampleTable;
}
static void avifSampleTableDestroy(avifSampleTable * sampleTable)
{
avifArrayDestroy(&sampleTable->chunks);
for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) {
avifSampleDescription * description = &sampleTable->sampleDescriptions.description[i];
avifArrayDestroy(&description->properties);
}
avifArrayDestroy(&sampleTable->sampleDescriptions);
avifArrayDestroy(&sampleTable->sampleToChunks);
avifArrayDestroy(&sampleTable->sampleSizes);
avifArrayDestroy(&sampleTable->timeToSamples);
avifArrayDestroy(&sampleTable->syncSamples);
avifFree(sampleTable);
}
static uint32_t avifSampleTableGetImageDelta(const avifSampleTable * sampleTable, int imageIndex)
{
int maxSampleIndex = 0;
for (uint32_t i = 0; i < sampleTable->timeToSamples.count; ++i) {
const avifSampleTableTimeToSample * timeToSample = &sampleTable->timeToSamples.timeToSample[i];
maxSampleIndex += timeToSample->sampleCount;
if ((imageIndex < maxSampleIndex) || (i == (sampleTable->timeToSamples.count - 1))) {
return timeToSample->sampleDelta;
}
}
// TODO: fail here?
return 1;
}
static avifBool avifSampleTableHasFormat(const avifSampleTable * sampleTable, const char * format)
{
for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) {
if (!memcmp(sampleTable->sampleDescriptions.description[i].format, format, 4)) {
return AVIF_TRUE;
}
}
return AVIF_FALSE;
}
static uint32_t avifCodecConfigurationBoxGetDepth(const avifCodecConfigurationBox * av1C)
{
if (av1C->twelveBit) {
return 12;
} else if (av1C->highBitdepth) {
return 10;
}
return 8;
}
// This is used as a hint to validating the clap box in avifDecoderItemValidateAV1.
static avifPixelFormat avifCodecConfigurationBoxGetFormat(const avifCodecConfigurationBox * av1C)
{
if (av1C->monochrome) {
return AVIF_PIXEL_FORMAT_YUV400;
} else if (av1C->chromaSubsamplingY == 1) {
return AVIF_PIXEL_FORMAT_YUV420;
} else if (av1C->chromaSubsamplingX == 1) {
return AVIF_PIXEL_FORMAT_YUV422;
}
return AVIF_PIXEL_FORMAT_YUV444;
}
static const avifPropertyArray * avifSampleTableGetProperties(const avifSampleTable * sampleTable)
{
for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) {
const avifSampleDescription * description = &sampleTable->sampleDescriptions.description[i];
if (!memcmp(description->format, "av01", 4)) {
return &description->properties;
}
}
return NULL;
}
// one video track ("trak" contents)
typedef struct avifTrack
{
uint32_t id;
uint32_t auxForID; // if non-zero, this track is an auxC plane for Track #{auxForID}
uint32_t premByID; // if non-zero, this track is premultiplied by Track #{premByID}
uint32_t mediaTimescale;
uint64_t mediaDuration;
uint32_t width;
uint32_t height;
avifSampleTable * sampleTable;
struct avifMeta * meta;
} avifTrack;
AVIF_ARRAY_DECLARE(avifTrackArray, avifTrack, track);
// ---------------------------------------------------------------------------
// avifCodecDecodeInput
avifCodecDecodeInput * avifCodecDecodeInputCreate(void)
{
avifCodecDecodeInput * decodeInput = (avifCodecDecodeInput *)avifAlloc(sizeof(avifCodecDecodeInput));
memset(decodeInput, 0, sizeof(avifCodecDecodeInput));
avifArrayCreate(&decodeInput->samples, sizeof(avifDecodeSample), 1);
return decodeInput;
}
void avifCodecDecodeInputDestroy(avifCodecDecodeInput * decodeInput)
{
for (uint32_t sampleIndex = 0; sampleIndex < decodeInput->samples.count; ++sampleIndex) {
avifDecodeSample * sample = &decodeInput->samples.sample[sampleIndex];
if (sample->ownsData) {
avifRWDataFree((avifRWData *)&sample->data);
}
}
avifArrayDestroy(&decodeInput->samples);
avifFree(decodeInput);
}
// Returns how many samples are in the chunk.
static uint32_t avifGetSampleCountOfChunk(const avifSampleTableSampleToChunkArray * sampleToChunks, uint32_t chunkIndex)
{
uint32_t sampleCount = 0;
for (int sampleToChunkIndex = sampleToChunks->count - 1; sampleToChunkIndex >= 0; --sampleToChunkIndex) {
const avifSampleTableSampleToChunk * sampleToChunk = &sampleToChunks->sampleToChunk[sampleToChunkIndex];
if (sampleToChunk->firstChunk <= (chunkIndex + 1)) {
sampleCount = sampleToChunk->samplesPerChunk;
break;
}
}
return sampleCount;
}
static avifBool avifCodecDecodeInputFillFromSampleTable(avifCodecDecodeInput * decodeInput,
avifSampleTable * sampleTable,
const uint32_t imageCountLimit,
const uint64_t sizeHint,
avifDiagnostics * diag)
{
if (imageCountLimit) {
// Verify that the we're not about to exceed the frame count limit.
uint32_t imageCountLeft = imageCountLimit;
for (uint32_t chunkIndex = 0; chunkIndex < sampleTable->chunks.count; ++chunkIndex) {
// First, figure out how many samples are in this chunk
uint32_t sampleCount = avifGetSampleCountOfChunk(&sampleTable->sampleToChunks, chunkIndex);
if (sampleCount == 0) {
// chunks with 0 samples are invalid
avifDiagnosticsPrintf(diag, "Sample table contains a chunk with 0 samples");
return AVIF_FALSE;
}
if (sampleCount > imageCountLeft) {
// This file exceeds the imageCountLimit, bail out
avifDiagnosticsPrintf(diag, "Exceeded avifDecoder's imageCountLimit");
return AVIF_FALSE;
}
imageCountLeft -= sampleCount;
}
}
uint32_t sampleSizeIndex = 0;
for (uint32_t chunkIndex = 0; chunkIndex < sampleTable->chunks.count; ++chunkIndex) {
avifSampleTableChunk * chunk = &sampleTable->chunks.chunk[chunkIndex];
// First, figure out how many samples are in this chunk
uint32_t sampleCount = avifGetSampleCountOfChunk(&sampleTable->sampleToChunks, chunkIndex);
if (sampleCount == 0) {
// chunks with 0 samples are invalid
avifDiagnosticsPrintf(diag, "Sample table contains a chunk with 0 samples");
return AVIF_FALSE;
}
uint64_t sampleOffset = chunk->offset;
for (uint32_t sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex) {
uint32_t sampleSize = sampleTable->allSamplesSize;
if (sampleSize == 0) {
if (sampleSizeIndex >= sampleTable->sampleSizes.count) {
// We've run out of samples to sum
avifDiagnosticsPrintf(diag, "Truncated sample table");
return AVIF_FALSE;
}
avifSampleTableSampleSize * sampleSizePtr = &sampleTable->sampleSizes.sampleSize[sampleSizeIndex];
sampleSize = sampleSizePtr->size;
}
avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&decodeInput->samples);
sample->offset = sampleOffset;
sample->size = sampleSize;
sample->spatialID = AVIF_SPATIAL_ID_UNSET; // Not filtering by spatial_id
sample->sync = AVIF_FALSE; // to potentially be set to true following the outer loop
if (sampleSize > UINT64_MAX - sampleOffset) {
avifDiagnosticsPrintf(diag,
"Sample table contains an offset/size pair which overflows: [%" PRIu64 " / %u]",
sampleOffset,
sampleSize);
return AVIF_FALSE;
}
if (sizeHint && ((sampleOffset + sampleSize) > sizeHint)) {
avifDiagnosticsPrintf(diag, "Exceeded avifIO's sizeHint, possibly truncated data");
return AVIF_FALSE;
}
sampleOffset += sampleSize;
++sampleSizeIndex;
}
}
// Mark appropriate samples as sync
for (uint32_t syncSampleIndex = 0; syncSampleIndex < sampleTable->syncSamples.count; ++syncSampleIndex) {
uint32_t frameIndex = sampleTable->syncSamples.syncSample[syncSampleIndex].sampleNumber - 1; // sampleNumber is 1-based
if (frameIndex < decodeInput->samples.count) {
decodeInput->samples.sample[frameIndex].sync = AVIF_TRUE;
}
}
// Assume frame 0 is sync, just in case the stss box is absent in the BMFF. (Unnecessary?)
if (decodeInput->samples.count > 0) {
decodeInput->samples.sample[0].sync = AVIF_TRUE;
}
return AVIF_TRUE;
}
static avifBool avifCodecDecodeInputFillFromDecoderItem(avifCodecDecodeInput * decodeInput,
avifDecoderItem * item,
avifBool allowProgressive,
const uint32_t imageCountLimit,
const uint64_t sizeHint,
avifDiagnostics * diag)
{
if (sizeHint && (item->size > sizeHint)) {
avifDiagnosticsPrintf(diag, "Exceeded avifIO's sizeHint, possibly truncated data");
return AVIF_FALSE;
}
uint8_t layerCount = 0;
size_t layerSizes[4] = { 0 };
const avifProperty * a1lxProp = avifPropertyArrayFind(&item->properties, "a1lx");
if (a1lxProp) {
// Calculate layer count and all layer sizes from the a1lx box, and then validate
size_t remainingSize = item->size;
for (int i = 0; i < 3; ++i) {
++layerCount;
const size_t layerSize = (size_t)a1lxProp->u.a1lx.layerSize[i];
if (layerSize) {
if (layerSize >= remainingSize) { // >= instead of > because there must be room for the last layer
avifDiagnosticsPrintf(diag, "a1lx layer index [%d] does not fit in item size", i);
return AVIF_FALSE;
}
layerSizes[i] = layerSize;
remainingSize -= layerSize;
} else {
layerSizes[i] = remainingSize;
remainingSize = 0;
break;
}
}
if (remainingSize > 0) {
assert(layerCount == 3);
++layerCount;
layerSizes[3] = remainingSize;
}
}
const avifProperty * lselProp = avifPropertyArrayFind(&item->properties, "lsel");
item->progressive = (a1lxProp && !lselProp); // Progressive images offer layers via the a1lxProp, but don't specify a layer selection with lsel.
if (lselProp) {
// Layer selection. This requires that the underlying AV1 codec decodes all layers,
// and then only returns the requested layer as a single frame. To the user of libavif,
// this appears to be a single frame.
decodeInput->allLayers = AVIF_TRUE;
size_t sampleSize = 0;
if (layerCount > 0) {
// Optimization: If we're selecting a layer that doesn't require the entire image's payload (hinted via the a1lx box)
if (lselProp->u.lsel.layerID >= layerCount) {
avifDiagnosticsPrintf(diag,
"lsel property requests layer index [%u] which isn't present in a1lx property ([%u] layers)",
lselProp->u.lsel.layerID,
layerCount);
return AVIF_FALSE;
}
for (uint8_t i = 0; i <= lselProp->u.lsel.layerID; ++i) {
sampleSize += layerSizes[i];
}
} else {
// This layer's payload subsection is unknown, just use the whole payload
sampleSize = item->size;
}
avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&decodeInput->samples);
sample->itemID = item->id;
sample->offset = 0;
sample->size = sampleSize;
assert(lselProp->u.lsel.layerID < MAX_AV1_LAYER_COUNT);
sample->spatialID = (uint8_t)lselProp->u.lsel.layerID;
sample->sync = AVIF_TRUE;
} else if (allowProgressive && item->progressive) {
// Progressive image. Decode all layers and expose them all to the user.
if (imageCountLimit && (layerCount > imageCountLimit)) {
avifDiagnosticsPrintf(diag, "Exceeded avifDecoder's imageCountLimit (progressive)");
return AVIF_FALSE;
}
decodeInput->allLayers = AVIF_TRUE;
size_t offset = 0;
for (int i = 0; i < layerCount; ++i) {
avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&decodeInput->samples);
sample->itemID = item->id;
sample->offset = offset;
sample->size = layerSizes[i];
sample->spatialID = AVIF_SPATIAL_ID_UNSET;
sample->sync = (i == 0); // Assume all layers depend on the first layer
offset += layerSizes[i];
}
} else {
// Typical case: Use the entire item's payload for a single frame output
avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&decodeInput->samples);
sample->itemID = item->id;
sample->offset = 0;
sample->size = item->size;
sample->spatialID = AVIF_SPATIAL_ID_UNSET;
sample->sync = AVIF_TRUE;
}
return AVIF_TRUE;
}
// ---------------------------------------------------------------------------
// Helper macros / functions
#define BEGIN_STREAM(VARNAME, PTR, SIZE, DIAG, CONTEXT) \
avifROStream VARNAME; \
avifROData VARNAME##_roData; \
VARNAME##_roData.data = PTR; \
VARNAME##_roData.size = SIZE; \
avifROStreamStart(&VARNAME, &VARNAME##_roData, DIAG, CONTEXT)
// Use this to keep track of whether or not a child box that must be unique (0 or 1 present) has
// been seen yet, when parsing a parent box. If the "seen" bit is already set for a given box when
// it is encountered during parse, an error is thrown. Which bit corresponds to which box is
// dictated entirely by the calling function.
static avifBool uniqueBoxSeen(uint32_t * uniqueBoxFlags, uint32_t whichFlag, const char * parentBoxType, const char * boxType, avifDiagnostics * diagnostics)
{
const uint32_t flag = 1 << whichFlag;
if (*uniqueBoxFlags & flag) {
// This box has already been seen. Error!
avifDiagnosticsPrintf(diagnostics, "Box[%s] contains a duplicate unique box of type '%s'", parentBoxType, boxType);
return AVIF_FALSE;
}
// Mark this box as seen.
*uniqueBoxFlags |= flag;
return AVIF_TRUE;
}
// ---------------------------------------------------------------------------
// avifDecoderData
typedef struct avifTile
{
avifCodecDecodeInput * input;
struct avifCodec * codec;
avifImage * image;
uint32_t width; // Either avifTrack.width or avifDecoderItem.width
uint32_t height; // Either avifTrack.height or avifDecoderItem.height
uint8_t operatingPoint;
} avifTile;
AVIF_ARRAY_DECLARE(avifTileArray, avifTile, tile);
// This holds one "meta" box (from the BMFF and HEIF standards) worth of relevant-to-AVIF information.
// * If a meta box is parsed from the root level of the BMFF, it can contain the information about
// "items" which might be color planes, alpha planes, or EXIF or XMP metadata.
// * If a meta box is parsed from inside of a track ("trak") box, any metadata (EXIF/XMP) items inside
// of that box are implicitly associated with that track.
typedef struct avifMeta
{
// Items (from HEIF) are the generic storage for any data that does not require timed processing
// (single image color planes, alpha planes, EXIF, XMP, etc). Each item has a unique integer ID >1,
// and is defined by a series of child boxes in a meta box:
// * iloc - location: byte offset to item data, item size in bytes
// * iinf - information: type of item (color planes, alpha plane, EXIF, XMP)
// * ipco - properties: dimensions, aspect ratio, image transformations, references to other items
// * ipma - associations: Attaches an item in the properties list to a given item
//
// Items are lazily created in this array when any of the above boxes refer to one by a new (unseen) ID,
// and are then further modified/updated as new information for an item's ID is parsed.
avifDecoderItemArray items;
// Any ipco boxes explained above are populated into this array as a staging area, which are
// then duplicated into the appropriate items upon encountering an item property association
// (ipma) box.
avifPropertyArray properties;
// Filled with the contents of "idat" boxes, which are raw data that an item can directly refer to in its
// item location box (iloc) instead of just giving an offset into the overall file. If all items' iloc boxes
// simply point at an offset/length in the file itself, this array will likely be empty.
avifDecoderItemDataArray idats;
// Ever-incrementing ID for uniquely identifying which 'meta' box contains an idat (when
// multiple meta boxes exist as BMFF siblings). Each time avifParseMetaBox() is called on an
// avifMeta struct, this value is incremented. Any time an additional meta box is detected at
// the same "level" (root level, trak level, etc), this ID helps distinguish which meta box's
// "idat" is which, as items implicitly reference idat boxes that exist in the same meta
// box.
uint32_t idatID;
// Contents of a pitm box, which signal which of the items in this file is the main image. For
// AVIF, this should point at an av01 type item containing color planes, and all other items
// are ignored unless they refer to this item in some way (alpha plane, EXIF/XMP metadata).
uint32_t primaryItemID;
} avifMeta;
static avifMeta * avifMetaCreate()
{
avifMeta * meta = (avifMeta *)avifAlloc(sizeof(avifMeta));
memset(meta, 0, sizeof(avifMeta));
avifArrayCreate(&meta->items, sizeof(avifDecoderItem), 8);
avifArrayCreate(&meta->properties, sizeof(avifProperty), 16);
avifArrayCreate(&meta->idats, sizeof(avifDecoderItemData), 1);
return meta;
}
static void avifMetaDestroy(avifMeta * meta)
{
for (uint32_t i = 0; i < meta->items.count; ++i) {
avifDecoderItem * item = &meta->items.item[i];
avifArrayDestroy(&item->properties);
avifArrayDestroy(&item->extents);
if (item->ownsMergedExtents) {
avifRWDataFree(&item->mergedExtents);
}
}
avifArrayDestroy(&meta->items);
avifArrayDestroy(&meta->properties);
for (uint32_t i = 0; i < meta->idats.count; ++i) {
avifDecoderItemData * idat = &meta->idats.idat[i];
avifRWDataFree(&idat->data);
}
avifArrayDestroy(&meta->idats);
avifFree(meta);
}
static avifDecoderItem * avifMetaFindItem(avifMeta * meta, uint32_t itemID)
{
if (itemID == 0) {
return NULL;
}
for (uint32_t i = 0; i < meta->items.count; ++i) {
if (meta->items.item[i].id == itemID) {
return &meta->items.item[i];
}
}
avifDecoderItem * item = (avifDecoderItem *)avifArrayPushPtr(&meta->items);
avifArrayCreate(&item->properties, sizeof(avifProperty), 16);
avifArrayCreate(&item->extents, sizeof(avifExtent), 1);
item->id = itemID;
item->meta = meta;
return item;
}
typedef struct avifDecoderData
{
avifMeta * meta; // The root-level meta box
avifTrackArray tracks;
avifTileArray tiles;
unsigned int colorTileCount;
unsigned int alphaTileCount;
avifImageGrid colorGrid;
avifImageGrid alphaGrid;
avifDecoderSource source;
avifDiagnostics * diag; // Shallow copy; owned by avifDecoder
const avifSampleTable * sourceSampleTable; // NULL unless (source == AVIF_DECODER_SOURCE_TRACKS), owned by an avifTrack
avifBool cicpSet; // True if avifDecoder's image has had its CICP set correctly yet.
// This allows nclx colr boxes to override AV1 CICP, as specified in the MIAF
// standard (ISO/IEC 23000-22:2019), section 7.3.6.4:
//
// "The colour information property takes precedence over any colour information in the image
// bitstream, i.e. if the property is present, colour information in the bitstream shall be ignored."
} avifDecoderData;
static avifDecoderData * avifDecoderDataCreate()
{
avifDecoderData * data = (avifDecoderData *)avifAlloc(sizeof(avifDecoderData));
memset(data, 0, sizeof(avifDecoderData));
data->meta = avifMetaCreate();
avifArrayCreate(&data->tracks, sizeof(avifTrack), 2);
avifArrayCreate(&data->tiles, sizeof(avifTile), 8);
return data;
}
static void avifDecoderDataResetCodec(avifDecoderData * data)
{
for (unsigned int i = 0; i < data->tiles.count; ++i) {
avifTile * tile = &data->tiles.tile[i];
if (tile->image) {
avifImageFreePlanes(tile->image, AVIF_PLANES_ALL); // forget any pointers into codec image buffers
}
if (tile->codec) {
avifCodecDestroy(tile->codec);
tile->codec = NULL;
}
}
}
static avifTile * avifDecoderDataCreateTile(avifDecoderData * data, uint32_t width, uint32_t height, uint8_t operatingPoint)
{
avifTile * tile = (avifTile *)avifArrayPushPtr(&data->tiles);
tile->image = avifImageCreateEmpty();
tile->input = avifCodecDecodeInputCreate();
tile->width = width;
tile->height = height;
tile->operatingPoint = operatingPoint;
return tile;
}
static avifTrack * avifDecoderDataCreateTrack(avifDecoderData * data)
{
avifTrack * track = (avifTrack *)avifArrayPushPtr(&data->tracks);
track->meta = avifMetaCreate();
return track;
}
static void avifDecoderDataClearTiles(avifDecoderData * data)
{
for (unsigned int i = 0; i < data->tiles.count; ++i) {
avifTile * tile = &data->tiles.tile[i];
if (tile->input) {
avifCodecDecodeInputDestroy(tile->input);
tile->input = NULL;
}
if (tile->codec) {
avifCodecDestroy(tile->codec);
tile->codec = NULL;
}
if (tile->image) {
avifImageDestroy(tile->image);
tile->image = NULL;
}
}
data->tiles.count = 0;
data->colorTileCount = 0;
data->alphaTileCount = 0;
}
static void avifDecoderDataDestroy(avifDecoderData * data)
{
avifMetaDestroy(data->meta);
for (uint32_t i = 0; i < data->tracks.count; ++i) {
avifTrack * track = &data->tracks.track[i];
if (track->sampleTable) {
avifSampleTableDestroy(track->sampleTable);
}
if (track->meta) {
avifMetaDestroy(track->meta);
}
}
avifArrayDestroy(&data->tracks);
avifDecoderDataClearTiles(data);
avifArrayDestroy(&data->tiles);
avifFree(data);
}
// This returns the max extent that has to be read in order to decode this item. If
// the item is stored in an idat, the data has already been read during Parse() and
// this function will return AVIF_RESULT_OK with a 0-byte extent.
static avifResult avifDecoderItemMaxExtent(const avifDecoderItem * item, const avifDecodeSample * sample, avifExtent * outExtent)
{
if (item->extents.count == 0) {
return AVIF_RESULT_TRUNCATED_DATA;
}
if (item->idatID != 0) {
// construction_method: idat(1)
// Find associated idat box
for (uint32_t i = 0; i < item->meta->idats.count; ++i) {
if (item->meta->idats.idat[i].id == item->idatID) {
// Already read from a meta box during Parse()
memset(outExtent, 0, sizeof(avifExtent));
return AVIF_RESULT_OK;
}
}
// no associated idat box was found in the meta box, bail out
return AVIF_RESULT_NO_CONTENT;
}
// construction_method: file(0)
if (sample->size == 0) {
return AVIF_RESULT_TRUNCATED_DATA;
}
uint64_t remainingOffset = sample->offset;
size_t remainingBytes = sample->size; // This may be smaller than item->size if the item is progressive
// Assert that the for loop below will execute at least one iteration.
assert(item->extents.count != 0);
uint64_t minOffset = UINT64_MAX;
uint64_t maxOffset = 0;
for (uint32_t extentIter = 0; extentIter < item->extents.count; ++extentIter) {
avifExtent * extent = &item->extents.extent[extentIter];
// Make local copies of extent->offset and extent->size as they might need to be adjusted
// due to the sample's offset.
uint64_t startOffset = extent->offset;
size_t extentSize = extent->size;
if (remainingOffset) {
if (remainingOffset >= extentSize) {
remainingOffset -= extentSize;
continue;
} else {
if (remainingOffset > UINT64_MAX - startOffset) {
return AVIF_RESULT_BMFF_PARSE_FAILED;
}
startOffset += remainingOffset;
extentSize -= remainingOffset;
remainingOffset = 0;
}
}
const size_t usedExtentSize = (extentSize < remainingBytes) ? extentSize : remainingBytes;
if (usedExtentSize > UINT64_MAX - startOffset) {
return AVIF_RESULT_BMFF_PARSE_FAILED;
}
const uint64_t endOffset = startOffset + usedExtentSize;
if (minOffset > startOffset) {
minOffset = startOffset;
}
if (maxOffset < endOffset) {
maxOffset = endOffset;
}
remainingBytes -= usedExtentSize;
if (remainingBytes == 0) {
// We've got enough bytes for this sample.
break;
}
}
if (remainingBytes != 0) {
return AVIF_RESULT_TRUNCATED_DATA;
}
outExtent->offset = minOffset;
const uint64_t extentLength = maxOffset - minOffset;
if (extentLength > SIZE_MAX) {
return AVIF_RESULT_BMFF_PARSE_FAILED;
}
outExtent->size = (size_t)extentLength;
return AVIF_RESULT_OK;
}
static uint8_t avifDecoderItemOperatingPoint(const avifDecoderItem * item)
{
const avifProperty * a1opProp = avifPropertyArrayFind(&item->properties, "a1op");
if (a1opProp) {
return a1opProp->u.a1op.opIndex;
}
return 0; // default
}
static avifResult avifDecoderItemValidateAV1(const avifDecoderItem * item, avifDiagnostics * diag, const avifStrictFlags strictFlags)
{
const avifProperty * av1CProp = avifPropertyArrayFind(&item->properties, "av1C");
if (!av1CProp) {
// An av1C box is mandatory in all valid AVIF configurations. Bail out.
avifDiagnosticsPrintf(diag, "Item ID %u is missing mandatory av1C property", item->id);
return AVIF_RESULT_BMFF_PARSE_FAILED;
}
const avifProperty * pixiProp = avifPropertyArrayFind(&item->properties, "pixi");
if (!pixiProp && (strictFlags & AVIF_STRICT_PIXI_REQUIRED)) {
// A pixi box is mandatory in all valid AVIF configurations. Bail out.
avifDiagnosticsPrintf(diag, "[Strict] Item ID %u is missing mandatory pixi property", item->id);
return AVIF_RESULT_BMFF_PARSE_FAILED;
}
if (pixiProp) {
const uint32_t av1CDepth = avifCodecConfigurationBoxGetDepth(&av1CProp->u.av1C);
for (uint8_t i = 0; i < pixiProp->u.pixi.planeCount; ++i) {
if (pixiProp->u.pixi.planeDepths[i] != av1CDepth) {
// pixi depth must match av1C depth
avifDiagnosticsPrintf(diag,
"Item ID %u depth specified by pixi property [%u] does not match av1C property depth [%u]",
item->id,
pixiProp->u.pixi.planeDepths[i],
av1CDepth);
return AVIF_RESULT_BMFF_PARSE_FAILED;
}
}
}
if (strictFlags & AVIF_STRICT_CLAP_VALID) {
const avifProperty * clapProp = avifPropertyArrayFind(&item->properties, "clap");
if (clapProp) {
const avifProperty * ispeProp = avifPropertyArrayFind(&item->properties, "ispe");
if (!ispeProp) {
avifDiagnosticsPrintf(diag,
"[Strict] Item ID %u is missing an ispe property, so its clap property cannot be validated",
item->id);
return AVIF_RESULT_BMFF_PARSE_FAILED;
}
avifCropRect cropRect;
const uint32_t imageW = ispeProp->u.ispe.width;
const uint32_t imageH = ispeProp->u.ispe.height;
const avifPixelFormat av1CFormat = avifCodecConfigurationBoxGetFormat(&av1CProp->u.av1C);
avifBool validClap = avifCropRectConvertCleanApertureBox(&cropRect, &clapProp->u.clap, imageW, imageH, av1CFormat, diag);
if (!validClap) {
return AVIF_RESULT_BMFF_PARSE_FAILED;
}
}
}
return AVIF_RESULT_OK;
}
static avifResult avifDecoderItemRead(avifDecoderItem * item,
avifIO * io,
avifROData * outData,
size_t offset,
size_t partialByteCount,
avifDiagnostics * diag)
{
if (item->mergedExtents.data && !item->partialMergedExtents) {
// Multiple extents have already been concatenated for this item, just return it
if (offset >= item->mergedExtents.size) {
avifDiagnosticsPrintf(diag, "Item ID %u read has overflowing offset", item->id);
return AVIF_RESULT_TRUNCATED_DATA;
}
outData->data = item->mergedExtents.data + offset;
outData->size = item->mergedExtents.size - offset;
return AVIF_RESULT_OK;
}