blob: 4ee762723700c95dc0e7dbf19de5e8f0ee6a14c1 [file] [log] [blame]
Joe Drago444f0512019-01-23 17:03:24 -08001// Copyright 2019 Joe Drago. All rights reserved.
2// SPDX-License-Identifier: BSD-2-Clause
3
4#include "avif/internal.h"
5
Wan-Teh Changb856dc22020-09-28 13:00:56 -07006#include <assert.h>
Wan-Teh Chang25b9c702020-10-08 12:17:17 -07007#include <limits.h>
Joe Drago444f0512019-01-23 17:03:24 -08008#include <string.h>
9
Joe Dragocd1e4c32019-02-08 11:26:31 -080010#define AUXTYPE_SIZE 64
Joe Dragof6a42272019-11-21 15:21:41 -080011#define CONTENTTYPE_SIZE 64
Joe Drago8f7a3002019-02-07 19:35:37 -080012
Joe Drago6500fd62019-10-08 17:17:34 -070013// class VisualSampleEntry(codingname) extends SampleEntry(codingname) {
14// unsigned int(16) pre_defined = 0;
15// const unsigned int(16) reserved = 0;
16// unsigned int(32)[3] pre_defined = 0;
17// unsigned int(16) width;
18// unsigned int(16) height;
19// template unsigned int(32) horizresolution = 0x00480000; // 72 dpi
20// template unsigned int(32) vertresolution = 0x00480000; // 72 dpi
21// const unsigned int(32) reserved = 0;
22// template unsigned int(16) frame_count = 1;
23// string[32] compressorname;
24// template unsigned int(16) depth = 0x0018;
25// int(16) pre_defined = -1;
26// // other boxes from derived specifications
27// CleanApertureBox clap; // optional
28// PixelAspectRatioBox pasp; // optional
29// }
30static const size_t VISUALSAMPLEENTRY_SIZE = 78;
31
Joe Dragof6a42272019-11-21 15:21:41 -080032static const char xmpContentType[] = CONTENT_TYPE_XMP;
33static const size_t xmpContentTypeSize = sizeof(xmpContentType);
34
Joe Dragob13e5722019-02-08 19:07:25 -080035// ---------------------------------------------------------------------------
36// Box data structures
37
38// ftyp
39typedef struct avifFileType
40{
41 uint8_t majorBrand[4];
42 uint32_t minorVersion;
Wan-Teh Chang6da0a882020-07-01 12:19:31 -070043 // If not null, points to a memory block of 4 * compatibleBrandsCount bytes.
44 const uint8_t * compatibleBrands;
Joe Dragob13e5722019-02-08 19:07:25 -080045 int compatibleBrandsCount;
46} avifFileType;
47
48// ispe
Joe Drago8f7a3002019-02-07 19:35:37 -080049typedef struct avifImageSpatialExtents
50{
51 uint32_t width;
52 uint32_t height;
53} avifImageSpatialExtents;
Joe Drago444f0512019-01-23 17:03:24 -080054
Joe Dragob13e5722019-02-08 19:07:25 -080055// auxC
Joe Dragocd1e4c32019-02-08 11:26:31 -080056typedef struct avifAuxiliaryType
57{
58 char auxType[AUXTYPE_SIZE];
59} avifAuxiliaryType;
60
Joe Dragof6a42272019-11-21 15:21:41 -080061// infe mime content_type
62typedef struct avifContentType
63{
64 char contentType[CONTENTTYPE_SIZE];
65} avifContentType;
66
Joe Dragob13e5722019-02-08 19:07:25 -080067// colr
Joe Drago41eb62b2019-02-08 15:38:18 -080068typedef struct avifColourInformationBox
69{
Joe Dragoa0da4a42020-05-08 14:27:40 -070070 avifBool hasICC;
Joe Drago345aaa12019-09-25 13:42:12 -070071 const uint8_t * icc;
Joe Drago41eb62b2019-02-08 15:38:18 -080072 size_t iccSize;
Joe Dragoa0da4a42020-05-08 14:27:40 -070073
74 avifBool hasNCLX;
Wan-Teh Chang559def52021-02-01 14:25:31 -080075 avifColorPrimaries colorPrimaries;
76 avifTransferCharacteristics transferCharacteristics;
77 avifMatrixCoefficients matrixCoefficients;
Joe Dragoa0da4a42020-05-08 14:27:40 -070078 avifRange range;
Joe Drago41eb62b2019-02-08 15:38:18 -080079} avifColourInformationBox;
80
Joe Drago60421562020-04-23 11:32:26 -070081#define MAX_PIXI_PLANE_DEPTHS 4
82typedef struct avifPixelInformationProperty
83{
84 uint8_t planeDepths[MAX_PIXI_PLANE_DEPTHS];
85 uint8_t planeCount;
86} avifPixelInformationProperty;
87
Joe Dragob13e5722019-02-08 19:07:25 -080088// ---------------------------------------------------------------------------
89// Top-level structures
90
Joe Drago9f2b87b2020-06-03 19:36:38 -070091struct avifMeta;
92
Joe Dragoa72da5b2020-06-15 19:40:17 -070093// Temporary storage for ipco/stsd contents until they can be associated and memcpy'd to an avifDecoderItem
Joe Drago8f7a3002019-02-07 19:35:37 -080094typedef struct avifProperty
95{
96 uint8_t type[4];
Wan-Teh Chang2031dc12020-05-22 09:24:46 -070097 union
98 {
99 avifImageSpatialExtents ispe;
100 avifAuxiliaryType auxC;
101 avifColourInformationBox colr;
102 avifCodecConfigurationBox av1C;
103 avifPixelAspectRatioBox pasp;
104 avifCleanApertureBox clap;
105 avifImageRotation irot;
106 avifImageMirror imir;
107 avifPixelInformationProperty pixi;
108 } u;
Joe Drago8f7a3002019-02-07 19:35:37 -0800109} avifProperty;
Joe Drago05559c92019-07-17 16:33:38 -0700110AVIF_ARRAY_DECLARE(avifPropertyArray, avifProperty, prop);
Joe Drago8f7a3002019-02-07 19:35:37 -0800111
Joe Dragoa72da5b2020-06-15 19:40:17 -0700112static const avifProperty * avifPropertyArrayFind(const avifPropertyArray * properties, const char * type)
113{
114 for (uint32_t propertyIndex = 0; propertyIndex < properties->count; ++propertyIndex) {
115 avifProperty * prop = &properties->prop[propertyIndex];
116 if (!memcmp(prop->type, type, 4)) {
117 return prop;
118 }
119 }
120 return NULL;
121}
122
Joe Drago4bcdfde2020-11-13 17:50:55 -0800123AVIF_ARRAY_DECLARE(avifExtentArray, avifExtent, extent);
Joe Dragoa4956902020-08-28 01:24:35 -0700124
Joe Dragoa72da5b2020-06-15 19:40:17 -0700125// one "item" worth for decoding (all iref, iloc, iprp, etc refer to one of these)
126typedef struct avifDecoderItem
127{
128 uint32_t id;
Joe Dragoba1eb492020-06-22 17:05:04 -0700129 struct avifMeta * meta; // Unowned; A back-pointer for convenience
Joe Dragoa72da5b2020-06-15 19:40:17 -0700130 uint8_t type[4];
Joe Drago217056b2020-11-13 16:19:35 -0800131 size_t size;
Joe Dragoa72da5b2020-06-15 19:40:17 -0700132 uint32_t idatID; // If non-zero, offset is relative to this idat box (iloc construction_method==1)
133 avifContentType contentType;
134 avifPropertyArray properties;
Joe Drago4bcdfde2020-11-13 17:50:55 -0800135 avifExtentArray extents; // All extent offsets/sizes
Joe Dragobe4cbb92020-09-21 12:14:05 -0700136 avifRWData mergedExtents; // if set, is a single contiguous block of this item's extents (unused when extents.count == 1)
137 avifBool ownsMergedExtents; // if true, mergedExtents must be freed when this item is destroyed
138 avifBool partialMergedExtents; // If true, mergedExtents doesn't have all of the item data yet
139 uint32_t thumbnailForID; // if non-zero, this item is a thumbnail for Item #{thumbnailForID}
140 uint32_t auxForID; // if non-zero, this item is an auxC plane for Item #{auxForID}
141 uint32_t descForID; // if non-zero, this item is a content description for Item #{descForID}
142 uint32_t dimgForID; // if non-zero, this item is a derived image for Item #{dimgForID}
Yuan Tonge4850be2021-01-22 14:21:25 +0800143 uint32_t premByID; // if non-zero, this item is premultiplied by Item #{premByID}
Joe Dragoa72da5b2020-06-15 19:40:17 -0700144 avifBool hasUnsupportedEssentialProperty; // If true, this item cites a property flagged as 'essential' that libavif doesn't support (yet). Ignore the item, if so.
Wan-Teh Changf4c65382020-11-03 14:27:45 -0800145 avifBool ipmaSeen; // if true, this item already received a property association
Joe Dragoa72da5b2020-06-15 19:40:17 -0700146} avifDecoderItem;
147AVIF_ARRAY_DECLARE(avifDecoderItemArray, avifDecoderItem, item);
148
Joe Dragof6a42272019-11-21 15:21:41 -0800149// idat storage
Joe Drago800b47f2020-03-18 16:22:37 -0700150typedef struct avifDecoderItemData
Joe Dragof6a42272019-11-21 15:21:41 -0800151{
152 uint32_t id;
Joe Dragobe4cbb92020-09-21 12:14:05 -0700153 avifRWData data;
Joe Drago800b47f2020-03-18 16:22:37 -0700154} avifDecoderItemData;
155AVIF_ARRAY_DECLARE(avifDecoderItemDataArray, avifDecoderItemData, idat);
Joe Dragof6a42272019-11-21 15:21:41 -0800156
Joe Drago060d5342020-03-03 10:53:49 -0800157// grid storage
158typedef struct avifImageGrid
159{
Joe Drago79ebcf32020-11-18 22:37:10 -0800160 uint32_t rows; // Legal range: [1-256]
161 uint32_t columns; // Legal range: [1-256]
Joe Drago060d5342020-03-03 10:53:49 -0800162 uint32_t outputWidth;
163 uint32_t outputHeight;
164} avifImageGrid;
165
Joe Dragoae7e2c32019-07-18 15:22:25 -0700166// ---------------------------------------------------------------------------
167// avifTrack
168
169typedef struct avifSampleTableChunk
170{
171 uint64_t offset;
Joe Dragoae7e2c32019-07-18 15:22:25 -0700172} avifSampleTableChunk;
173AVIF_ARRAY_DECLARE(avifSampleTableChunkArray, avifSampleTableChunk, chunk);
174
175typedef struct avifSampleTableSampleToChunk
176{
177 uint32_t firstChunk;
178 uint32_t samplesPerChunk;
179 uint32_t sampleDescriptionIndex;
180} avifSampleTableSampleToChunk;
181AVIF_ARRAY_DECLARE(avifSampleTableSampleToChunkArray, avifSampleTableSampleToChunk, sampleToChunk);
182
183typedef struct avifSampleTableSampleSize
184{
185 uint32_t size;
186} avifSampleTableSampleSize;
187AVIF_ARRAY_DECLARE(avifSampleTableSampleSizeArray, avifSampleTableSampleSize, sampleSize);
188
189typedef struct avifSampleTableTimeToSample
190{
191 uint32_t sampleCount;
192 uint32_t sampleDelta;
193} avifSampleTableTimeToSample;
194AVIF_ARRAY_DECLARE(avifSampleTableTimeToSampleArray, avifSampleTableTimeToSample, timeToSample);
195
Joe Drago22c1ad92019-09-26 12:46:50 -0700196typedef struct avifSyncSample
197{
198 uint32_t sampleNumber;
199} avifSyncSample;
200AVIF_ARRAY_DECLARE(avifSyncSampleArray, avifSyncSample, syncSample);
201
Joe Drago2c0924c2019-09-26 17:41:01 -0700202typedef struct avifSampleDescription
203{
204 uint8_t format[4];
Joe Dragoa72da5b2020-06-15 19:40:17 -0700205 avifPropertyArray properties;
Joe Drago2c0924c2019-09-26 17:41:01 -0700206} avifSampleDescription;
207AVIF_ARRAY_DECLARE(avifSampleDescriptionArray, avifSampleDescription, description);
208
Joe Dragoae7e2c32019-07-18 15:22:25 -0700209typedef struct avifSampleTable
210{
211 avifSampleTableChunkArray chunks;
Joe Drago2c0924c2019-09-26 17:41:01 -0700212 avifSampleDescriptionArray sampleDescriptions;
Joe Dragoae7e2c32019-07-18 15:22:25 -0700213 avifSampleTableSampleToChunkArray sampleToChunks;
214 avifSampleTableSampleSizeArray sampleSizes;
215 avifSampleTableTimeToSampleArray timeToSamples;
Joe Drago22c1ad92019-09-26 12:46:50 -0700216 avifSyncSampleArray syncSamples;
Joe Drago370be3f2020-02-07 15:59:42 -0800217 uint32_t allSamplesSize; // If this is non-zero, sampleSizes will be empty and all samples will be this size
Joe Dragoae7e2c32019-07-18 15:22:25 -0700218} avifSampleTable;
219
Joe Drago46ea0582019-07-22 15:55:47 -0700220static avifSampleTable * avifSampleTableCreate()
Joe Dragoae7e2c32019-07-18 15:22:25 -0700221{
222 avifSampleTable * sampleTable = (avifSampleTable *)avifAlloc(sizeof(avifSampleTable));
223 memset(sampleTable, 0, sizeof(avifSampleTable));
224 avifArrayCreate(&sampleTable->chunks, sizeof(avifSampleTableChunk), 16);
Joe Drago2c0924c2019-09-26 17:41:01 -0700225 avifArrayCreate(&sampleTable->sampleDescriptions, sizeof(avifSampleDescription), 2);
Joe Dragoae7e2c32019-07-18 15:22:25 -0700226 avifArrayCreate(&sampleTable->sampleToChunks, sizeof(avifSampleTableSampleToChunk), 16);
227 avifArrayCreate(&sampleTable->sampleSizes, sizeof(avifSampleTableSampleSize), 16);
228 avifArrayCreate(&sampleTable->timeToSamples, sizeof(avifSampleTableTimeToSample), 16);
Joe Drago759e6742019-09-26 18:07:21 -0700229 avifArrayCreate(&sampleTable->syncSamples, sizeof(avifSyncSample), 16);
Joe Dragoae7e2c32019-07-18 15:22:25 -0700230 return sampleTable;
231}
232
Joe Drago46ea0582019-07-22 15:55:47 -0700233static void avifSampleTableDestroy(avifSampleTable * sampleTable)
Joe Dragoae7e2c32019-07-18 15:22:25 -0700234{
235 avifArrayDestroy(&sampleTable->chunks);
Joe Dragoa72da5b2020-06-15 19:40:17 -0700236 for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) {
237 avifSampleDescription * description = &sampleTable->sampleDescriptions.description[i];
238 avifArrayDestroy(&description->properties);
239 }
Joe Drago2c0924c2019-09-26 17:41:01 -0700240 avifArrayDestroy(&sampleTable->sampleDescriptions);
Joe Dragoae7e2c32019-07-18 15:22:25 -0700241 avifArrayDestroy(&sampleTable->sampleToChunks);
242 avifArrayDestroy(&sampleTable->sampleSizes);
243 avifArrayDestroy(&sampleTable->timeToSamples);
Joe Drago22c1ad92019-09-26 12:46:50 -0700244 avifArrayDestroy(&sampleTable->syncSamples);
Joe Dragoae7e2c32019-07-18 15:22:25 -0700245 avifFree(sampleTable);
246}
247
Wan-Teh Chang306c3062020-04-05 12:17:33 -0700248static uint32_t avifSampleTableGetImageDelta(const avifSampleTable * sampleTable, int imageIndex)
Joe Drago46ea0582019-07-22 15:55:47 -0700249{
250 int maxSampleIndex = 0;
251 for (uint32_t i = 0; i < sampleTable->timeToSamples.count; ++i) {
Wan-Teh Chang306c3062020-04-05 12:17:33 -0700252 const avifSampleTableTimeToSample * timeToSample = &sampleTable->timeToSamples.timeToSample[i];
Joe Drago46ea0582019-07-22 15:55:47 -0700253 maxSampleIndex += timeToSample->sampleCount;
254 if ((imageIndex < maxSampleIndex) || (i == (sampleTable->timeToSamples.count - 1))) {
255 return timeToSample->sampleDelta;
256 }
257 }
258
259 // TODO: fail here?
260 return 1;
261}
262
Wan-Teh Chang306c3062020-04-05 12:17:33 -0700263static avifBool avifSampleTableHasFormat(const avifSampleTable * sampleTable, const char * format)
Joe Drago2c0924c2019-09-26 17:41:01 -0700264{
265 for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) {
266 if (!memcmp(sampleTable->sampleDescriptions.description[i].format, format, 4)) {
267 return AVIF_TRUE;
268 }
269 }
270 return AVIF_FALSE;
271}
272
Wan-Teh Chang306c3062020-04-05 12:17:33 -0700273static uint32_t avifCodecConfigurationBoxGetDepth(const avifCodecConfigurationBox * av1C)
Joe Drago6500fd62019-10-08 17:17:34 -0700274{
275 if (av1C->twelveBit) {
276 return 12;
277 } else if (av1C->highBitdepth) {
278 return 10;
279 }
280 return 8;
281}
282
Joe Dragoa72da5b2020-06-15 19:40:17 -0700283static const avifPropertyArray * avifSampleTableGetProperties(const avifSampleTable * sampleTable)
Joe Drago6500fd62019-10-08 17:17:34 -0700284{
285 for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) {
Wan-Teh Chang306c3062020-04-05 12:17:33 -0700286 const avifSampleDescription * description = &sampleTable->sampleDescriptions.description[i];
Joe Dragoa72da5b2020-06-15 19:40:17 -0700287 if (!memcmp(description->format, "av01", 4)) {
288 return &description->properties;
Joe Drago6500fd62019-10-08 17:17:34 -0700289 }
290 }
Joe Drago00bcaaf2020-06-05 15:29:38 -0700291 return NULL;
Joe Drago6500fd62019-10-08 17:17:34 -0700292}
293
Joe Dragoae7e2c32019-07-18 15:22:25 -0700294// one video track ("trak" contents)
295typedef struct avifTrack
296{
297 uint32_t id;
Wan-Teh Chang53adb6a2021-02-20 16:58:43 -0800298 uint32_t auxForID; // if non-zero, this track is an auxC plane for Track #{auxForID}
299 uint32_t premByID; // if non-zero, this track is premultiplied by Track #{premByID}
Joe Dragoae7e2c32019-07-18 15:22:25 -0700300 uint32_t mediaTimescale;
301 uint64_t mediaDuration;
Joe Dragofc4144e2019-09-27 20:35:06 -0700302 uint32_t width;
303 uint32_t height;
Joe Dragoae7e2c32019-07-18 15:22:25 -0700304 avifSampleTable * sampleTable;
Joe Dragoa72da5b2020-06-15 19:40:17 -0700305 struct avifMeta * meta;
Joe Dragoae7e2c32019-07-18 15:22:25 -0700306} avifTrack;
307AVIF_ARRAY_DECLARE(avifTrackArray, avifTrack, track);
308
309// ---------------------------------------------------------------------------
Joe Drago46ea0582019-07-22 15:55:47 -0700310// avifCodecDecodeInput
311
Joe Drago399df4f2019-07-23 16:45:14 -0700312avifCodecDecodeInput * avifCodecDecodeInputCreate(void)
Joe Drago46ea0582019-07-22 15:55:47 -0700313{
314 avifCodecDecodeInput * decodeInput = (avifCodecDecodeInput *)avifAlloc(sizeof(avifCodecDecodeInput));
315 memset(decodeInput, 0, sizeof(avifCodecDecodeInput));
Joe Dragoe3e3bfa2020-06-02 16:33:53 -0700316 avifArrayCreate(&decodeInput->samples, sizeof(avifDecodeSample), 1);
Joe Drago46ea0582019-07-22 15:55:47 -0700317 return decodeInput;
318}
319
Joe Drago8b34ad72019-07-22 16:56:32 -0700320void avifCodecDecodeInputDestroy(avifCodecDecodeInput * decodeInput)
Joe Drago46ea0582019-07-22 15:55:47 -0700321{
Joe Dragobe4cbb92020-09-21 12:14:05 -0700322 for (uint32_t sampleIndex = 0; sampleIndex < decodeInput->samples.count; ++sampleIndex) {
323 avifDecodeSample * sample = &decodeInput->samples.sample[sampleIndex];
324 if (sample->ownsData) {
325 avifRWDataFree((avifRWData *)&sample->data);
326 }
327 }
Joe Drago46ea0582019-07-22 15:55:47 -0700328 avifArrayDestroy(&decodeInput->samples);
329 avifFree(decodeInput);
330}
331
Wan-Teh Changb8c89442021-03-19 14:17:57 -0700332// Returns how many samples are in the chunk.
333static uint32_t avifGetSampleCountOfChunk(const avifSampleTableSampleToChunkArray * sampleToChunks, uint32_t chunkIndex)
334{
335 uint32_t sampleCount = 0;
336 for (int sampleToChunkIndex = sampleToChunks->count - 1; sampleToChunkIndex >= 0; --sampleToChunkIndex) {
337 const avifSampleTableSampleToChunk * sampleToChunk = &sampleToChunks->sampleToChunk[sampleToChunkIndex];
338 if (sampleToChunk->firstChunk <= (chunkIndex + 1)) {
339 sampleCount = sampleToChunk->samplesPerChunk;
340 break;
341 }
342 }
343 return sampleCount;
344}
345
Joe Dragod2340b42021-03-14 13:20:02 -0700346static avifBool avifCodecDecodeInputGetSamples(avifCodecDecodeInput * decodeInput,
347 avifSampleTable * sampleTable,
Wan-Teh Changb8c89442021-03-19 14:17:57 -0700348 const uint32_t imageCountLimit,
Joe Dragod2340b42021-03-14 13:20:02 -0700349 const uint64_t sizeHint)
Joe Drago46ea0582019-07-22 15:55:47 -0700350{
Joe Dragod2340b42021-03-14 13:20:02 -0700351 if (imageCountLimit) {
352 // Verify that the we're not about to exceed the frame count limit.
353
Wan-Teh Changb8c89442021-03-19 14:17:57 -0700354 uint32_t imageCountLeft = imageCountLimit;
Joe Dragod2340b42021-03-14 13:20:02 -0700355 for (uint32_t chunkIndex = 0; chunkIndex < sampleTable->chunks.count; ++chunkIndex) {
356 // First, figure out how many samples are in this chunk
Wan-Teh Changb8c89442021-03-19 14:17:57 -0700357 uint32_t sampleCount = avifGetSampleCountOfChunk(&sampleTable->sampleToChunks, chunkIndex);
Joe Dragod2340b42021-03-14 13:20:02 -0700358 if (sampleCount == 0) {
359 // chunks with 0 samples are invalid
360 return AVIF_FALSE;
361 }
362
Wan-Teh Changb8c89442021-03-19 14:17:57 -0700363 if (sampleCount > imageCountLeft) {
Joe Dragod2340b42021-03-14 13:20:02 -0700364 // This file exceeds the imageCountLimit, bail out
365 return AVIF_FALSE;
366 }
Wan-Teh Changb8c89442021-03-19 14:17:57 -0700367 imageCountLeft -= sampleCount;
Joe Dragod2340b42021-03-14 13:20:02 -0700368 }
369 }
370
Joe Drago46ea0582019-07-22 15:55:47 -0700371 uint32_t sampleSizeIndex = 0;
372 for (uint32_t chunkIndex = 0; chunkIndex < sampleTable->chunks.count; ++chunkIndex) {
373 avifSampleTableChunk * chunk = &sampleTable->chunks.chunk[chunkIndex];
374
375 // First, figure out how many samples are in this chunk
Wan-Teh Changb8c89442021-03-19 14:17:57 -0700376 uint32_t sampleCount = avifGetSampleCountOfChunk(&sampleTable->sampleToChunks, chunkIndex);
Joe Drago46ea0582019-07-22 15:55:47 -0700377 if (sampleCount == 0) {
378 // chunks with 0 samples are invalid
379 return AVIF_FALSE;
380 }
381
382 uint64_t sampleOffset = chunk->offset;
383 for (uint32_t sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex) {
Joe Drago370be3f2020-02-07 15:59:42 -0800384 uint32_t sampleSize = sampleTable->allSamplesSize;
385 if (sampleSize == 0) {
386 if (sampleSizeIndex >= sampleTable->sampleSizes.count) {
387 // We've run out of samples to sum
388 return AVIF_FALSE;
389 }
390 avifSampleTableSampleSize * sampleSizePtr = &sampleTable->sampleSizes.sampleSize[sampleSizeIndex];
391 sampleSize = sampleSizePtr->size;
Joe Drago46ea0582019-07-22 15:55:47 -0700392 }
393
Joe Dragoe3e3bfa2020-06-02 16:33:53 -0700394 avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&decodeInput->samples);
Joe Dragobe4cbb92020-09-21 12:14:05 -0700395 sample->offset = sampleOffset;
396 sample->size = sampleSize;
Joe Drago22c1ad92019-09-26 12:46:50 -0700397 sample->sync = AVIF_FALSE; // to potentially be set to true following the outer loop
Joe Drago46ea0582019-07-22 15:55:47 -0700398
Wan-Teh Chang3ca14242020-09-30 16:39:38 -0700399 if (sampleSize > UINT64_MAX - sampleOffset) {
400 return AVIF_FALSE;
401 }
Wan-Teh Chang136d7572020-10-08 15:13:42 -0700402 if (sizeHint && ((sampleOffset + sampleSize) > sizeHint)) {
Joe Drago34c0d312020-04-30 15:23:03 -0700403 return AVIF_FALSE;
404 }
Joe Drago46ea0582019-07-22 15:55:47 -0700405
Joe Drago370be3f2020-02-07 15:59:42 -0800406 sampleOffset += sampleSize;
Joe Drago46ea0582019-07-22 15:55:47 -0700407 ++sampleSizeIndex;
408 }
409 }
Joe Drago22c1ad92019-09-26 12:46:50 -0700410
411 // Mark appropriate samples as sync
412 for (uint32_t syncSampleIndex = 0; syncSampleIndex < sampleTable->syncSamples.count; ++syncSampleIndex) {
413 uint32_t frameIndex = sampleTable->syncSamples.syncSample[syncSampleIndex].sampleNumber - 1; // sampleNumber is 1-based
414 if (frameIndex < decodeInput->samples.count) {
415 decodeInput->samples.sample[frameIndex].sync = AVIF_TRUE;
416 }
417 }
418
419 // Assume frame 0 is sync, just in case the stss box is absent in the BMFF. (Unnecessary?)
420 if (decodeInput->samples.count > 0) {
421 decodeInput->samples.sample[0].sync = AVIF_TRUE;
422 }
Joe Drago46ea0582019-07-22 15:55:47 -0700423 return AVIF_TRUE;
424}
425
426// ---------------------------------------------------------------------------
Joe Drago8f439092020-08-28 15:05:17 -0700427// Helper macros / functions
Joe Dragoa72da5b2020-06-15 19:40:17 -0700428
429#define BEGIN_STREAM(VARNAME, PTR, SIZE) \
430 avifROStream VARNAME; \
431 avifROData VARNAME##_roData; \
432 VARNAME##_roData.data = PTR; \
433 VARNAME##_roData.size = SIZE; \
434 avifROStreamStart(&VARNAME, &VARNAME##_roData)
435
Joe Drago8f439092020-08-28 15:05:17 -0700436// Use this to keep track of whether or not a child box that must be unique (0 or 1 present) has
437// been seen yet, when parsing a parent box. If the "seen" bit is already set for a given box when
438// it is encountered during parse, an error is thrown. Which bit corresponds to which box is
439// dictated entirely by the calling function.
440static avifBool uniqueBoxSeen(uint32_t * uniqueBoxFlags, uint32_t whichFlag)
441{
442 const uint32_t flag = 1 << whichFlag;
443 if (*uniqueBoxFlags & flag) {
444 // This box has already been seen. Error!
445 return AVIF_FALSE;
446 }
447
448 // Mark this box as seen.
449 *uniqueBoxFlags |= flag;
450 return AVIF_TRUE;
451}
452
Joe Dragoa72da5b2020-06-15 19:40:17 -0700453// ---------------------------------------------------------------------------
Joe Drago800b47f2020-03-18 16:22:37 -0700454// avifDecoderData
Joe Dragoae7e2c32019-07-18 15:22:25 -0700455
Joe Drago060d5342020-03-03 10:53:49 -0800456typedef struct avifTile
457{
458 avifCodecDecodeInput * input;
459 struct avifCodec * codec;
460 avifImage * image;
461} avifTile;
462AVIF_ARRAY_DECLARE(avifTileArray, avifTile, tile);
463
Joe Dragoba1eb492020-06-22 17:05:04 -0700464// This holds one "meta" box (from the BMFF and HEIF standards) worth of relevant-to-AVIF information.
465// * If a meta box is parsed from the root level of the BMFF, it can contain the information about
466// "items" which might be color planes, alpha planes, or EXIF or XMP metadata.
467// * If a meta box is parsed from inside of a track ("trak") box, any metadata (EXIF/XMP) items inside
468// of that box are implicitly associated with that track.
Joe Drago9f2b87b2020-06-03 19:36:38 -0700469typedef struct avifMeta
Joe Drago444f0512019-01-23 17:03:24 -0800470{
Joe Dragoba1eb492020-06-22 17:05:04 -0700471 // Items (from HEIF) are the generic storage for any data that does not require timed processing
Wan-Teh Chang4b331fb2020-07-07 14:10:02 -0700472 // (single image color planes, alpha planes, EXIF, XMP, etc). Each item has a unique integer ID >1,
473 // and is defined by a series of child boxes in a meta box:
Joe Dragoba1eb492020-06-22 17:05:04 -0700474 // * iloc - location: byte offset to item data, item size in bytes
475 // * iinf - information: type of item (color planes, alpha plane, EXIF, XMP)
476 // * ipco - properties: dimensions, aspect ratio, image transformations, references to other items
477 // * ipma - associations: Attaches an item in the properties list to a given item
478 //
479 // Items are lazily created in this array when any of the above boxes refer to one by a new (unseen) ID,
480 // and are then further modified/updated as new information for an item's ID is parsed.
Joe Drago800b47f2020-03-18 16:22:37 -0700481 avifDecoderItemArray items;
Joe Dragoba1eb492020-06-22 17:05:04 -0700482
483 // Any ipco boxes explained above are populated into this array as a staging area, which are
484 // then duplicated into the appropriate items upon encountering an item property association
485 // (ipma) box.
Joe Drago05559c92019-07-17 16:33:38 -0700486 avifPropertyArray properties;
Joe Dragoba1eb492020-06-22 17:05:04 -0700487
488 // Filled with the contents of "idat" boxes, which are raw data that an item can directly refer to in its
489 // item location box (iloc) instead of just giving an offset into the overall file. If all items' iloc boxes
490 // simply point at an offset/length in the file itself, this array will likely be empty.
Joe Drago800b47f2020-03-18 16:22:37 -0700491 avifDecoderItemDataArray idats;
Joe Dragoba1eb492020-06-22 17:05:04 -0700492
493 // Ever-incrementing ID for uniquely identifying which 'meta' box contains an idat (when
Wan-Teh Chang4b331fb2020-07-07 14:10:02 -0700494 // multiple meta boxes exist as BMFF siblings). Each time avifParseMetaBox() is called on an
Joe Dragoba1eb492020-06-22 17:05:04 -0700495 // avifMeta struct, this value is incremented. Any time an additional meta box is detected at
496 // the same "level" (root level, trak level, etc), this ID helps distinguish which meta box's
Wan-Teh Chang4b331fb2020-07-07 14:10:02 -0700497 // "idat" is which, as items implicitly reference idat boxes that exist in the same meta
Joe Dragoba1eb492020-06-22 17:05:04 -0700498 // box.
499 uint32_t idatID;
500
501 // Contents of a pitm box, which signal which of the items in this file is the main image. For
502 // AVIF, this should point at an av01 type item containing color planes, and all other items
503 // are ignored unless they refer to this item in some way (alpha plane, EXIF/XMP metadata).
Joe Drago9f2b87b2020-06-03 19:36:38 -0700504 uint32_t primaryItemID;
505} avifMeta;
506
507static avifMeta * avifMetaCreate()
508{
509 avifMeta * meta = (avifMeta *)avifAlloc(sizeof(avifMeta));
510 memset(meta, 0, sizeof(avifMeta));
511 avifArrayCreate(&meta->items, sizeof(avifDecoderItem), 8);
512 avifArrayCreate(&meta->properties, sizeof(avifProperty), 16);
513 avifArrayCreate(&meta->idats, sizeof(avifDecoderItemData), 1);
514 return meta;
515}
516
517static void avifMetaDestroy(avifMeta * meta)
518{
Joe Dragoa72da5b2020-06-15 19:40:17 -0700519 for (uint32_t i = 0; i < meta->items.count; ++i) {
520 avifDecoderItem * item = &meta->items.item[i];
521 avifArrayDestroy(&item->properties);
Joe Dragoa4956902020-08-28 01:24:35 -0700522 avifArrayDestroy(&item->extents);
Joe Dragobe4cbb92020-09-21 12:14:05 -0700523 if (item->ownsMergedExtents) {
524 avifRWDataFree(&item->mergedExtents);
525 }
Joe Dragoa72da5b2020-06-15 19:40:17 -0700526 }
Joe Drago9f2b87b2020-06-03 19:36:38 -0700527 avifArrayDestroy(&meta->items);
528 avifArrayDestroy(&meta->properties);
Joe Dragobe4cbb92020-09-21 12:14:05 -0700529 for (uint32_t i = 0; i < meta->idats.count; ++i) {
530 avifDecoderItemData * idat = &meta->idats.idat[i];
531 avifRWDataFree(&idat->data);
532 }
Joe Drago9f2b87b2020-06-03 19:36:38 -0700533 avifArrayDestroy(&meta->idats);
534 avifFree(meta);
535}
536
537static avifDecoderItem * avifMetaFindItem(avifMeta * meta, uint32_t itemID)
538{
539 if (itemID == 0) {
540 return NULL;
541 }
542
543 for (uint32_t i = 0; i < meta->items.count; ++i) {
544 if (meta->items.item[i].id == itemID) {
545 return &meta->items.item[i];
546 }
547 }
548
549 avifDecoderItem * item = (avifDecoderItem *)avifArrayPushPtr(&meta->items);
Joe Dragoa72da5b2020-06-15 19:40:17 -0700550 avifArrayCreate(&item->properties, sizeof(avifProperty), 16);
Joe Drago4bcdfde2020-11-13 17:50:55 -0800551 avifArrayCreate(&item->extents, sizeof(avifExtent), 1);
Joe Drago9f2b87b2020-06-03 19:36:38 -0700552 item->id = itemID;
553 item->meta = meta;
554 return item;
555}
556
557typedef struct avifDecoderData
558{
Wan-Teh Chang6fc17582020-09-24 15:16:37 -0700559 avifMeta * meta; // The root-level meta box
Joe Dragoae7e2c32019-07-18 15:22:25 -0700560 avifTrackArray tracks;
Joe Drago060d5342020-03-03 10:53:49 -0800561 avifTileArray tiles;
562 unsigned int colorTileCount;
563 unsigned int alphaTileCount;
564 avifImageGrid colorGrid;
565 avifImageGrid alphaGrid;
Joe Drago46ea0582019-07-22 15:55:47 -0700566 avifDecoderSource source;
Wan-Teh Chang306c3062020-04-05 12:17:33 -0700567 const avifSampleTable * sourceSampleTable; // NULL unless (source == AVIF_DECODER_SOURCE_TRACKS), owned by an avifTrack
Joe Dragoc00d5832020-08-13 16:03:28 -0700568 avifBool cicpSet; // True if avifDecoder's image has had its CICP set correctly yet.
569 // This allows nclx colr boxes to override AV1 CICP, as specified in the MIAF
570 // standard (ISO/IEC 23000-22:2019), section 7.3.6.4:
571 //
572 // "The colour information property takes precedence over any colour information in the image
573 // bitstream, i.e. if the property is present, colour information in the bitstream shall be ignored."
Joe Drago800b47f2020-03-18 16:22:37 -0700574} avifDecoderData;
Joe Drago444f0512019-01-23 17:03:24 -0800575
Joe Drago800b47f2020-03-18 16:22:37 -0700576static avifDecoderData * avifDecoderDataCreate()
Joe Drago05559c92019-07-17 16:33:38 -0700577{
Joe Drago800b47f2020-03-18 16:22:37 -0700578 avifDecoderData * data = (avifDecoderData *)avifAlloc(sizeof(avifDecoderData));
579 memset(data, 0, sizeof(avifDecoderData));
Joe Drago9f2b87b2020-06-03 19:36:38 -0700580 data->meta = avifMetaCreate();
Joe Dragoae7e2c32019-07-18 15:22:25 -0700581 avifArrayCreate(&data->tracks, sizeof(avifTrack), 2);
Joe Drago060d5342020-03-03 10:53:49 -0800582 avifArrayCreate(&data->tiles, sizeof(avifTile), 8);
Joe Drago05559c92019-07-17 16:33:38 -0700583 return data;
584}
585
Joe Drago800b47f2020-03-18 16:22:37 -0700586static void avifDecoderDataResetCodec(avifDecoderData * data)
Joe Drago05559c92019-07-17 16:33:38 -0700587{
Joe Drago060d5342020-03-03 10:53:49 -0800588 for (unsigned int i = 0; i < data->tiles.count; ++i) {
589 avifTile * tile = &data->tiles.tile[i];
Joe Drago9d368782020-03-04 17:53:17 -0800590 if (tile->image) {
591 avifImageFreePlanes(tile->image, AVIF_PLANES_ALL); // forget any pointers into codec image buffers
592 }
Joe Drago060d5342020-03-03 10:53:49 -0800593 if (tile->codec) {
594 avifCodecDestroy(tile->codec);
595 tile->codec = NULL;
Joe Drago46ea0582019-07-22 15:55:47 -0700596 }
597 }
598}
599
Joe Drago800b47f2020-03-18 16:22:37 -0700600static avifTile * avifDecoderDataCreateTile(avifDecoderData * data)
Joe Drago060d5342020-03-03 10:53:49 -0800601{
602 avifTile * tile = (avifTile *)avifArrayPushPtr(&data->tiles);
603 tile->image = avifImageCreateEmpty();
604 tile->input = avifCodecDecodeInputCreate();
605 return tile;
606}
607
Joe Dragoa72da5b2020-06-15 19:40:17 -0700608static avifTrack * avifDecoderDataCreateTrack(avifDecoderData * data)
609{
610 avifTrack * track = (avifTrack *)avifArrayPushPtr(&data->tracks);
611 track->meta = avifMetaCreate();
612 return track;
613}
614
Joe Drago800b47f2020-03-18 16:22:37 -0700615static void avifDecoderDataClearTiles(avifDecoderData * data)
Joe Drago060d5342020-03-03 10:53:49 -0800616{
617 for (unsigned int i = 0; i < data->tiles.count; ++i) {
618 avifTile * tile = &data->tiles.tile[i];
619 if (tile->input) {
620 avifCodecDecodeInputDestroy(tile->input);
621 tile->input = NULL;
622 }
623 if (tile->codec) {
624 avifCodecDestroy(tile->codec);
625 tile->codec = NULL;
626 }
627 if (tile->image) {
628 avifImageDestroy(tile->image);
629 tile->image = NULL;
630 }
631 }
632 data->tiles.count = 0;
633 data->colorTileCount = 0;
634 data->alphaTileCount = 0;
635}
636
Joe Drago800b47f2020-03-18 16:22:37 -0700637static void avifDecoderDataDestroy(avifDecoderData * data)
Joe Drago46ea0582019-07-22 15:55:47 -0700638{
Joe Drago9f2b87b2020-06-03 19:36:38 -0700639 avifMetaDestroy(data->meta);
Joe Dragoae7e2c32019-07-18 15:22:25 -0700640 for (uint32_t i = 0; i < data->tracks.count; ++i) {
Joe Dragoa72da5b2020-06-15 19:40:17 -0700641 avifTrack * track = &data->tracks.track[i];
642 if (track->sampleTable) {
643 avifSampleTableDestroy(track->sampleTable);
644 }
645 if (track->meta) {
646 avifMetaDestroy(track->meta);
Joe Dragoae7e2c32019-07-18 15:22:25 -0700647 }
648 }
649 avifArrayDestroy(&data->tracks);
Joe Drago800b47f2020-03-18 16:22:37 -0700650 avifDecoderDataClearTiles(data);
Joe Drago060d5342020-03-03 10:53:49 -0800651 avifArrayDestroy(&data->tiles);
Joe Drago05559c92019-07-17 16:33:38 -0700652 avifFree(data);
653}
654
Joe Drago4bcdfde2020-11-13 17:50:55 -0800655// This returns the max extent that has to be read in order to decode this item. If
656// the item is stored in an idat, the data has already been read during Parse() and
657// this function will return AVIF_RESULT_OK with a 0-byte extent.
658static avifResult avifDecoderItemMaxExtent(const avifDecoderItem * item, avifExtent * outExtent)
659{
660 if (item->extents.count == 0) {
661 return AVIF_RESULT_TRUNCATED_DATA;
662 }
663
664 if (item->idatID != 0) {
665 // construction_method: idat(1)
666
Wan-Teh Chang80275392020-11-17 12:35:11 -0800667 // Find associated idat box
Joe Drago4bcdfde2020-11-13 17:50:55 -0800668 for (uint32_t i = 0; i < item->meta->idats.count; ++i) {
669 if (item->meta->idats.idat[i].id == item->idatID) {
670 // Already read from a meta box during Parse()
671 memset(outExtent, 0, sizeof(avifExtent));
672 return AVIF_RESULT_OK;
673 }
674 }
675
Wan-Teh Chang80275392020-11-17 12:35:11 -0800676 // no associated idat box was found in the meta box, bail out
Joe Drago4bcdfde2020-11-13 17:50:55 -0800677 return AVIF_RESULT_NO_CONTENT;
678 }
679
680 // construction_method: file(0)
681
Wan-Teh Chang15584522020-11-17 14:11:12 -0800682 // Assert that the for loop below will execute at least one iteration.
683 assert(item->extents.count != 0);
684 uint64_t minOffset = UINT64_MAX;
Joe Drago4bcdfde2020-11-13 17:50:55 -0800685 uint64_t maxOffset = 0;
686 for (uint32_t extentIter = 0; extentIter < item->extents.count; ++extentIter) {
687 avifExtent * extent = &item->extents.extent[extentIter];
688
689 if (extent->size > UINT64_MAX - extent->offset) {
690 return AVIF_RESULT_BMFF_PARSE_FAILED;
691 }
692 const uint64_t endOffset = extent->offset + extent->size;
693
Wan-Teh Chang15584522020-11-17 14:11:12 -0800694 if (minOffset > extent->offset) {
Joe Drago4bcdfde2020-11-13 17:50:55 -0800695 minOffset = extent->offset;
Wan-Teh Chang15584522020-11-17 14:11:12 -0800696 }
697 if (maxOffset < endOffset) {
Joe Drago4bcdfde2020-11-13 17:50:55 -0800698 maxOffset = endOffset;
Joe Drago4bcdfde2020-11-13 17:50:55 -0800699 }
700 }
701
702 outExtent->offset = minOffset;
Wan-Teh Changd69958e2020-11-17 12:14:27 -0800703 const uint64_t extentLength = maxOffset - minOffset;
704 if (extentLength > SIZE_MAX) {
705 return AVIF_RESULT_BMFF_PARSE_FAILED;
706 }
707 outExtent->size = (size_t)extentLength;
Joe Drago4bcdfde2020-11-13 17:50:55 -0800708 return AVIF_RESULT_OK;
709}
710
Wan-Teh Chang4548b162020-11-06 11:48:25 -0800711static avifResult avifDecoderItemRead(avifDecoderItem * item, avifIO * io, avifROData * outData, size_t partialByteCount)
Joe Dragof6a42272019-11-21 15:21:41 -0800712{
Joe Dragobe4cbb92020-09-21 12:14:05 -0700713 if (item->mergedExtents.data && !item->partialMergedExtents) {
Joe Dragoa4956902020-08-28 01:24:35 -0700714 // Multiple extents have already been concatenated for this item, just return it
Wan-Teh Chang610b0a12020-10-06 16:47:21 -0700715 memcpy(outData, &item->mergedExtents, sizeof(avifROData));
Joe Dragobe4cbb92020-09-21 12:14:05 -0700716 return AVIF_RESULT_OK;
717 }
718
719 if (item->extents.count == 0) {
720 return AVIF_RESULT_TRUNCATED_DATA;
Joe Dragoa4956902020-08-28 01:24:35 -0700721 }
722
723 // Find this item's source of all extents' data, based on the construction method
Wan-Teh Chang3c304dc2020-10-08 12:07:45 -0700724 const avifRWData * idatBuffer = NULL;
Wan-Teh Chang610b0a12020-10-06 16:47:21 -0700725 if (item->idatID != 0) {
Joe Dragof6a42272019-11-21 15:21:41 -0800726 // construction_method: idat(1)
727
Wan-Teh Chang80275392020-11-17 12:35:11 -0800728 // Find associated idat box
Joe Drago9f2b87b2020-06-03 19:36:38 -0700729 for (uint32_t i = 0; i < item->meta->idats.count; ++i) {
730 if (item->meta->idats.idat[i].id == item->idatID) {
Joe Dragobe4cbb92020-09-21 12:14:05 -0700731 idatBuffer = &item->meta->idats.idat[i].data;
Joe Dragof6a42272019-11-21 15:21:41 -0800732 break;
733 }
734 }
735
Joe Dragobe4cbb92020-09-21 12:14:05 -0700736 if (idatBuffer == NULL) {
Wan-Teh Chang80275392020-11-17 12:35:11 -0800737 // no associated idat box was found in the meta box, bail out
Joe Dragobe4cbb92020-09-21 12:14:05 -0700738 return AVIF_RESULT_NO_CONTENT;
Joe Dragof6a42272019-11-21 15:21:41 -0800739 }
740 }
741
Joe Dragobe4cbb92020-09-21 12:14:05 -0700742 // Merge extents into a single contiguous buffer
Wan-Teh Chang4548b162020-11-06 11:48:25 -0800743 if ((io->sizeHint > 0) && (item->size > io->sizeHint)) {
Wan-Teh Change5003232020-08-28 18:26:58 -0700744 // Sanity check: somehow the sum of extents for this item exceeds the entire file or idat
745 // size!
Joe Dragobe4cbb92020-09-21 12:14:05 -0700746 return AVIF_RESULT_TRUNCATED_DATA;
Joe Drago8f439092020-08-28 15:05:17 -0700747 }
Joe Dragobe4cbb92020-09-21 12:14:05 -0700748
Wan-Teh Chang610b0a12020-10-06 16:47:21 -0700749 size_t totalBytesToRead = item->size;
750 if (partialByteCount && (totalBytesToRead > partialByteCount)) {
751 totalBytesToRead = partialByteCount;
752 }
753
Joe Dragobe4cbb92020-09-21 12:14:05 -0700754 // If there is a single extent for this item and the source of the read buffer is going to be
755 // persistent for the lifetime of the avifDecoder (whether it comes from its own internal
756 // idatBuffer or from a known-persistent IO), we can avoid buffer duplication and just use the
757 // preexisting buffer.
Wan-Teh Chang4548b162020-11-06 11:48:25 -0800758 avifBool singlePersistentBuffer = ((item->extents.count == 1) && (idatBuffer || io->persistent));
Joe Dragobe4cbb92020-09-21 12:14:05 -0700759 if (!singlePersistentBuffer) {
Wan-Teh Chang610b0a12020-10-06 16:47:21 -0700760 avifRWDataRealloc(&item->mergedExtents, totalBytesToRead);
Joe Dragobe4cbb92020-09-21 12:14:05 -0700761 item->ownsMergedExtents = AVIF_TRUE;
762 }
763
764 // Set this until we manage to fill the entire mergedExtents buffer
765 item->partialMergedExtents = AVIF_TRUE;
Joe Drago8f439092020-08-28 15:05:17 -0700766
Joe Dragoa4956902020-08-28 01:24:35 -0700767 uint8_t * front = item->mergedExtents.data;
Joe Dragobe4cbb92020-09-21 12:14:05 -0700768 size_t remainingBytes = totalBytesToRead;
Joe Dragoa4956902020-08-28 01:24:35 -0700769 for (uint32_t extentIter = 0; extentIter < item->extents.count; ++extentIter) {
Joe Drago4bcdfde2020-11-13 17:50:55 -0800770 avifExtent * extent = &item->extents.extent[extentIter];
Joe Dragobe4cbb92020-09-21 12:14:05 -0700771
772 size_t bytesToRead = extent->size;
773 if (bytesToRead > remainingBytes) {
774 bytesToRead = remainingBytes;
Joe Dragoa4956902020-08-28 01:24:35 -0700775 }
776
Joe Dragobe4cbb92020-09-21 12:14:05 -0700777 avifROData offsetBuffer;
778 if (idatBuffer) {
Wan-Teh Chang610b0a12020-10-06 16:47:21 -0700779 if (extent->offset > idatBuffer->size) {
780 return AVIF_RESULT_BMFF_PARSE_FAILED;
781 }
782 if (extent->size > idatBuffer->size - extent->offset) {
783 return AVIF_RESULT_BMFF_PARSE_FAILED;
784 }
785 offsetBuffer.data = idatBuffer->data + extent->offset;
786 offsetBuffer.size = idatBuffer->size - extent->offset;
Joe Dragobe4cbb92020-09-21 12:14:05 -0700787 } else {
788 // construction_method: file(0)
789
Wan-Teh Chang4548b162020-11-06 11:48:25 -0800790 if ((io->sizeHint > 0) && (extent->offset > io->sizeHint)) {
Wan-Teh Changb856dc22020-09-28 13:00:56 -0700791 return AVIF_RESULT_BMFF_PARSE_FAILED;
792 }
Wan-Teh Chang4548b162020-11-06 11:48:25 -0800793 avifResult readResult = io->read(io, 0, extent->offset, bytesToRead, &offsetBuffer);
Joe Dragobe4cbb92020-09-21 12:14:05 -0700794 if (readResult != AVIF_RESULT_OK) {
795 return readResult;
796 }
Wan-Teh Chang3c304dc2020-10-08 12:07:45 -0700797 if (bytesToRead != offsetBuffer.size) {
798 return AVIF_RESULT_TRUNCATED_DATA;
799 }
Joe Dragobe4cbb92020-09-21 12:14:05 -0700800 }
801
802 if (singlePersistentBuffer) {
803 memcpy(&item->mergedExtents, &offsetBuffer, sizeof(avifRWData));
804 item->mergedExtents.size = bytesToRead;
805 } else {
Joe Dragodb1009a2021-03-16 15:26:14 -0700806 assert(item->ownsMergedExtents);
807 assert(front);
Joe Dragobe4cbb92020-09-21 12:14:05 -0700808 memcpy(front, offsetBuffer.data, bytesToRead);
809 front += bytesToRead;
810 }
811
812 remainingBytes -= bytesToRead;
813 if (remainingBytes == 0) {
814 // This happens when partialByteCount is set
815 break;
816 }
Joe Dragoa4956902020-08-28 01:24:35 -0700817 }
818 if (remainingBytes != 0) {
819 // This should be impossible?
Joe Dragobe4cbb92020-09-21 12:14:05 -0700820 return AVIF_RESULT_TRUNCATED_DATA;
Joe Dragof6a42272019-11-21 15:21:41 -0800821 }
Joe Dragobe4cbb92020-09-21 12:14:05 -0700822
823 outData->data = item->mergedExtents.data;
824 outData->size = totalBytesToRead;
825 item->partialMergedExtents = (item->size != totalBytesToRead);
826 return AVIF_RESULT_OK;
Joe Dragof6a42272019-11-21 15:21:41 -0800827}
828
Joe Drago800b47f2020-03-18 16:22:37 -0700829static avifBool avifDecoderDataGenerateImageGridTiles(avifDecoderData * data, avifImageGrid * grid, avifDecoderItem * gridItem, avifBool alpha)
Joe Drago060d5342020-03-03 10:53:49 -0800830{
Wan-Teh Chang7ca3dd92020-11-20 12:50:44 -0800831 unsigned int tilesRequested = grid->rows * grid->columns;
Joe Drago060d5342020-03-03 10:53:49 -0800832
833 // Count number of dimg for this item, bail out if it doesn't match perfectly
834 unsigned int tilesAvailable = 0;
Joe Drago9f2b87b2020-06-03 19:36:38 -0700835 for (uint32_t i = 0; i < gridItem->meta->items.count; ++i) {
836 avifDecoderItem * item = &gridItem->meta->items.item[i];
Joe Drago060d5342020-03-03 10:53:49 -0800837 if (item->dimgForID == gridItem->id) {
838 if (memcmp(item->type, "av01", 4)) {
839 continue;
840 }
Joe Drago3320e5f2020-04-21 17:36:27 -0700841 if (item->hasUnsupportedEssentialProperty) {
Wan-Teh Chang29aaade2020-08-10 16:14:16 -0700842 // An essential property isn't supported by libavif; can't
843 // decode a grid image if any tile in the grid isn't supported.
844 return AVIF_FALSE;
Joe Drago3320e5f2020-04-21 17:36:27 -0700845 }
Joe Drago060d5342020-03-03 10:53:49 -0800846
847 ++tilesAvailable;
848 }
849 }
850
851 if (tilesRequested != tilesAvailable) {
852 return AVIF_FALSE;
853 }
854
Joe Drago9c5f5652020-06-29 15:13:44 -0700855 avifBool firstTile = AVIF_TRUE;
Joe Drago9f2b87b2020-06-03 19:36:38 -0700856 for (uint32_t i = 0; i < gridItem->meta->items.count; ++i) {
857 avifDecoderItem * item = &gridItem->meta->items.item[i];
Joe Drago060d5342020-03-03 10:53:49 -0800858 if (item->dimgForID == gridItem->id) {
859 if (memcmp(item->type, "av01", 4)) {
860 continue;
861 }
862
Joe Drago800b47f2020-03-18 16:22:37 -0700863 avifTile * tile = avifDecoderDataCreateTile(data);
Joe Dragoe3e3bfa2020-06-02 16:33:53 -0700864 avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&tile->input->samples);
Joe Dragobe4cbb92020-09-21 12:14:05 -0700865 sample->itemID = item->id;
866 sample->offset = 0;
867 sample->size = item->size;
Joe Drago060d5342020-03-03 10:53:49 -0800868 sample->sync = AVIF_TRUE;
869 tile->input->alpha = alpha;
Joe Drago9c5f5652020-06-29 15:13:44 -0700870
871 if (firstTile) {
872 firstTile = AVIF_FALSE;
873
874 // Adopt the av1C property of the first av01 tile, so that it can be queried from
875 // the top-level color/alpha item during avifDecoderReset().
876 const avifProperty * srcProp = avifPropertyArrayFind(&item->properties, "av1C");
877 if (!srcProp) {
878 return AVIF_FALSE;
879 }
880 avifProperty * dstProp = (avifProperty *)avifArrayPushPtr(&gridItem->properties);
881 memcpy(dstProp, srcProp, sizeof(avifProperty));
882 }
Joe Drago060d5342020-03-03 10:53:49 -0800883 }
884 }
885 return AVIF_TRUE;
886}
887
Joe Drago800b47f2020-03-18 16:22:37 -0700888static avifBool avifDecoderDataFillImageGrid(avifDecoderData * data,
889 avifImageGrid * grid,
890 avifImage * dstImage,
891 unsigned int firstTileIndex,
892 unsigned int tileCount,
893 avifBool alpha)
Joe Drago060d5342020-03-03 10:53:49 -0800894{
895 if (tileCount == 0) {
896 return AVIF_FALSE;
897 }
898
899 avifTile * firstTile = &data->tiles.tile[firstTileIndex];
Joe Dragoa0da4a42020-05-08 14:27:40 -0700900 avifBool firstTileUVPresent = (firstTile->image->yuvPlanes[AVIF_CHAN_U] && firstTile->image->yuvPlanes[AVIF_CHAN_V]);
psi / Ryo Hirafuji922d8a12020-03-10 03:24:57 +0900901
Joe Dragoa0da4a42020-05-08 14:27:40 -0700902 // Check for tile consistency: All tiles in a grid image should match in the properties checked below.
Joe Drago060d5342020-03-03 10:53:49 -0800903 for (unsigned int i = 1; i < tileCount; ++i) {
904 avifTile * tile = &data->tiles.tile[firstTileIndex + i];
Joe Drago951a0022020-03-09 16:19:44 -0700905 avifBool uvPresent = (tile->image->yuvPlanes[AVIF_CHAN_U] && tile->image->yuvPlanes[AVIF_CHAN_V]);
Joe Dragoa0da4a42020-05-08 14:27:40 -0700906 if ((tile->image->width != firstTile->image->width) || (tile->image->height != firstTile->image->height) ||
907 (tile->image->depth != firstTile->image->depth) || (tile->image->yuvFormat != firstTile->image->yuvFormat) ||
908 (tile->image->yuvRange != firstTile->image->yuvRange) || (uvPresent != firstTileUVPresent) ||
wantehchangbc35a5f2020-08-12 15:27:39 -0700909 (tile->image->colorPrimaries != firstTile->image->colorPrimaries) ||
910 (tile->image->transferCharacteristics != firstTile->image->transferCharacteristics) ||
Wan-Teh Chang5bdead52020-08-12 17:02:22 -0700911 (tile->image->matrixCoefficients != firstTile->image->matrixCoefficients) ||
912 (tile->image->alphaRange != firstTile->image->alphaRange)) {
Joe Drago060d5342020-03-03 10:53:49 -0800913 return AVIF_FALSE;
914 }
915 }
916
wantehchangdf586a82020-08-12 13:06:08 -0700917 // Validate grid image size and tile size.
918 //
919 // HEIF (ISO/IEC 23008-12:2017), Section 6.6.2.3.1:
TYTYb587c592020-12-08 17:42:18 +0800920 // The tiled input images shall completely "cover" the reconstructed image grid canvas, ...
wantehchangbc35a5f2020-08-12 15:27:39 -0700921 if (((firstTile->image->width * grid->columns) < grid->outputWidth) ||
922 ((firstTile->image->height * grid->rows) < grid->outputHeight)) {
wantehchangdf586a82020-08-12 13:06:08 -0700923 return AVIF_FALSE;
924 }
925 // Tiles in the rightmost column and bottommost row must overlap the reconstructed image grid canvas. See MIAF (ISO/IEC 23000-22:2019), Section 7.3.11.4.2, Figure 2.
wantehchangbc35a5f2020-08-12 15:27:39 -0700926 if (((firstTile->image->width * (grid->columns - 1)) >= grid->outputWidth) ||
927 ((firstTile->image->height * (grid->rows - 1)) >= grid->outputHeight)) {
wantehchangdf586a82020-08-12 13:06:08 -0700928 return AVIF_FALSE;
929 }
930 // Check the restrictions in MIAF (ISO/IEC 23000-22:2019), Section 7.3.11.4.2.
931 //
932 // The tile_width shall be greater than or equal to 64, and the tile_height shall be greater than or equal to 64.
wantehchangbc35a5f2020-08-12 15:27:39 -0700933 if ((firstTile->image->width < 64) || (firstTile->image->height < 64)) {
wantehchangdf586a82020-08-12 13:06:08 -0700934 return AVIF_FALSE;
935 }
936 if (!alpha) {
wantehchangbc35a5f2020-08-12 15:27:39 -0700937 if ((firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV422) || (firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV420)) {
wantehchangdf586a82020-08-12 13:06:08 -0700938 // The horizontal tile offsets and widths, and the output width, shall be even numbers.
wantehchangbc35a5f2020-08-12 15:27:39 -0700939 if (((firstTile->image->width & 1) != 0) || ((grid->outputWidth & 1) != 0)) {
wantehchangdf586a82020-08-12 13:06:08 -0700940 return AVIF_FALSE;
941 }
942 }
943 if (firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV420) {
944 // The vertical tile offsets and heights, and the output height, shall be even numbers.
wantehchangbc35a5f2020-08-12 15:27:39 -0700945 if (((firstTile->image->height & 1) != 0) || ((grid->outputHeight & 1) != 0)) {
wantehchangdf586a82020-08-12 13:06:08 -0700946 return AVIF_FALSE;
947 }
948 }
949 }
950
Joe Dragoa0da4a42020-05-08 14:27:40 -0700951 // Lazily populate dstImage with the new frame's properties. If we're decoding alpha,
952 // these values must already match.
953 if ((dstImage->width != grid->outputWidth) || (dstImage->height != grid->outputHeight) ||
Wan-Teh Chang5bdead52020-08-12 17:02:22 -0700954 (dstImage->depth != firstTile->image->depth) || (!alpha && (dstImage->yuvFormat != firstTile->image->yuvFormat))) {
Joe Drago060d5342020-03-03 10:53:49 -0800955 if (alpha) {
956 // Alpha doesn't match size, just bail out
957 return AVIF_FALSE;
958 }
959
960 avifImageFreePlanes(dstImage, AVIF_PLANES_ALL);
961 dstImage->width = grid->outputWidth;
962 dstImage->height = grid->outputHeight;
Joe Dragoa0da4a42020-05-08 14:27:40 -0700963 dstImage->depth = firstTile->image->depth;
964 dstImage->yuvFormat = firstTile->image->yuvFormat;
Joe Dragoc00d5832020-08-13 16:03:28 -0700965 dstImage->yuvRange = firstTile->image->yuvRange;
Joe Dragoa0da4a42020-05-08 14:27:40 -0700966 if (!data->cicpSet) {
967 data->cicpSet = AVIF_TRUE;
Joe Dragoc00d5832020-08-13 16:03:28 -0700968 dstImage->colorPrimaries = firstTile->image->colorPrimaries;
969 dstImage->transferCharacteristics = firstTile->image->transferCharacteristics;
970 dstImage->matrixCoefficients = firstTile->image->matrixCoefficients;
psi / Ryo Hirafuji922d8a12020-03-10 03:24:57 +0900971 }
Joe Drago060d5342020-03-03 10:53:49 -0800972 }
Joe Dragod0eeb182020-05-18 17:23:48 -0700973 if (alpha) {
974 dstImage->alphaRange = firstTile->image->alphaRange;
975 }
Joe Drago060d5342020-03-03 10:53:49 -0800976
977 avifImageAllocatePlanes(dstImage, alpha ? AVIF_PLANES_A : AVIF_PLANES_YUV);
978
979 avifPixelFormatInfo formatInfo;
Joe Drago7a249f52020-08-13 12:58:03 -0700980 avifGetPixelFormatInfo(firstTile->image->yuvFormat, &formatInfo);
Joe Drago060d5342020-03-03 10:53:49 -0800981
982 unsigned int tileIndex = firstTileIndex;
983 size_t pixelBytes = avifImageUsesU16(dstImage) ? 2 : 1;
984 for (unsigned int rowIndex = 0; rowIndex < grid->rows; ++rowIndex) {
985 for (unsigned int colIndex = 0; colIndex < grid->columns; ++colIndex, ++tileIndex) {
986 avifTile * tile = &data->tiles.tile[tileIndex];
987
Joe Dragoa0da4a42020-05-08 14:27:40 -0700988 unsigned int widthToCopy = firstTile->image->width;
989 unsigned int maxX = firstTile->image->width * (colIndex + 1);
Joe Drago060d5342020-03-03 10:53:49 -0800990 if (maxX > grid->outputWidth) {
991 widthToCopy -= maxX - grid->outputWidth;
992 }
993
Joe Dragoa0da4a42020-05-08 14:27:40 -0700994 unsigned int heightToCopy = firstTile->image->height;
995 unsigned int maxY = firstTile->image->height * (rowIndex + 1);
Joe Drago060d5342020-03-03 10:53:49 -0800996 if (maxY > grid->outputHeight) {
997 heightToCopy -= maxY - grid->outputHeight;
998 }
999
1000 // Y and A channels
Joe Dragodb1009a2021-03-16 15:26:14 -07001001 size_t yaColOffset = (size_t)colIndex * firstTile->image->width;
1002 size_t yaRowOffset = (size_t)rowIndex * firstTile->image->height;
Joe Drago060d5342020-03-03 10:53:49 -08001003 size_t yaRowBytes = widthToCopy * pixelBytes;
1004
1005 if (alpha) {
1006 // A
1007 for (unsigned int j = 0; j < heightToCopy; ++j) {
1008 uint8_t * src = &tile->image->alphaPlane[j * tile->image->alphaRowBytes];
1009 uint8_t * dst = &dstImage->alphaPlane[(yaColOffset * pixelBytes) + ((yaRowOffset + j) * dstImage->alphaRowBytes)];
1010 memcpy(dst, src, yaRowBytes);
1011 }
1012 } else {
1013 // Y
1014 for (unsigned int j = 0; j < heightToCopy; ++j) {
1015 uint8_t * src = &tile->image->yuvPlanes[AVIF_CHAN_Y][j * tile->image->yuvRowBytes[AVIF_CHAN_Y]];
1016 uint8_t * dst =
1017 &dstImage->yuvPlanes[AVIF_CHAN_Y][(yaColOffset * pixelBytes) + ((yaRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_Y])];
1018 memcpy(dst, src, yaRowBytes);
1019 }
1020
Joe Dragoa0da4a42020-05-08 14:27:40 -07001021 if (!firstTileUVPresent) {
Joe Drago060d5342020-03-03 10:53:49 -08001022 continue;
1023 }
1024
1025 // UV
Joe Drago060d5342020-03-03 10:53:49 -08001026 heightToCopy >>= formatInfo.chromaShiftY;
1027 size_t uvColOffset = yaColOffset >> formatInfo.chromaShiftX;
1028 size_t uvRowOffset = yaRowOffset >> formatInfo.chromaShiftY;
1029 size_t uvRowBytes = yaRowBytes >> formatInfo.chromaShiftX;
1030 for (unsigned int j = 0; j < heightToCopy; ++j) {
1031 uint8_t * srcU = &tile->image->yuvPlanes[AVIF_CHAN_U][j * tile->image->yuvRowBytes[AVIF_CHAN_U]];
1032 uint8_t * dstU =
1033 &dstImage->yuvPlanes[AVIF_CHAN_U][(uvColOffset * pixelBytes) + ((uvRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_U])];
1034 memcpy(dstU, srcU, uvRowBytes);
1035
1036 uint8_t * srcV = &tile->image->yuvPlanes[AVIF_CHAN_V][j * tile->image->yuvRowBytes[AVIF_CHAN_V]];
1037 uint8_t * dstV =
1038 &dstImage->yuvPlanes[AVIF_CHAN_V][(uvColOffset * pixelBytes) + ((uvRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_V])];
1039 memcpy(dstV, srcV, uvRowBytes);
1040 }
1041 }
1042 }
1043 }
1044
1045 return AVIF_TRUE;
1046}
1047
Joe Drago39267fd2020-06-19 15:21:14 -07001048// If colorId == 0 (a sentinel value as item IDs must be nonzero), accept any found EXIF/XMP metadata. Passing in 0
1049// is used when finding metadata in a meta box embedded in a trak box, as any items inside of a meta box that is
Joe Dragofa990822020-06-19 15:23:55 -07001050// inside of a trak box are implicitly associated to the track.
Joe Dragobe4cbb92020-09-21 12:14:05 -07001051static avifResult avifDecoderFindMetadata(avifDecoder * decoder, avifMeta * meta, avifImage * image, uint32_t colorId)
Joe Dragoa72da5b2020-06-15 19:40:17 -07001052{
Joe Dragobe4cbb92020-09-21 12:14:05 -07001053 if (decoder->ignoreExif && decoder->ignoreXMP) {
1054 // Nothing to do!
1055 return AVIF_RESULT_OK;
1056 }
Joe Dragoa72da5b2020-06-15 19:40:17 -07001057
1058 for (uint32_t itemIndex = 0; itemIndex < meta->items.count; ++itemIndex) {
1059 avifDecoderItem * item = &meta->items.item[itemIndex];
Joe Dragoba1eb492020-06-22 17:05:04 -07001060 if (!item->size) {
1061 continue;
Joe Dragoa72da5b2020-06-15 19:40:17 -07001062 }
1063 if (item->hasUnsupportedEssentialProperty) {
1064 // An essential property isn't supported by libavif; ignore the item.
1065 continue;
1066 }
1067
1068 if ((colorId > 0) && (item->descForID != colorId)) {
1069 // Not a content description (metadata) for the colorOBU, skip it
1070 continue;
1071 }
1072
Joe Dragobe4cbb92020-09-21 12:14:05 -07001073 if (!decoder->ignoreExif && !memcmp(item->type, "Exif", 4)) {
1074 avifROData exifContents;
Wan-Teh Chang4548b162020-11-06 11:48:25 -08001075 avifResult readResult = avifDecoderItemRead(item, decoder->io, &exifContents, 0);
Joe Dragobe4cbb92020-09-21 12:14:05 -07001076 if (readResult != AVIF_RESULT_OK) {
1077 return readResult;
1078 }
1079
Joe Dragoa72da5b2020-06-15 19:40:17 -07001080 // Advance past Annex A.2.1's header
Joe Dragobe4cbb92020-09-21 12:14:05 -07001081 BEGIN_STREAM(exifBoxStream, exifContents.data, exifContents.size);
Joe Dragoa72da5b2020-06-15 19:40:17 -07001082 uint32_t exifTiffHeaderOffset;
Wan-Teh Changf1d60192020-10-07 16:23:08 -07001083 CHECKERR(avifROStreamReadU32(&exifBoxStream, &exifTiffHeaderOffset), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) exif_tiff_header_offset;
Joe Dragoa72da5b2020-06-15 19:40:17 -07001084
Joe Dragobe4cbb92020-09-21 12:14:05 -07001085 avifImageSetMetadataExif(image, avifROStreamCurrent(&exifBoxStream), avifROStreamRemainingBytes(&exifBoxStream));
1086 } else if (!decoder->ignoreXMP && !memcmp(item->type, "mime", 4) &&
1087 !memcmp(item->contentType.contentType, xmpContentType, xmpContentTypeSize)) {
1088 avifROData xmpContents;
Wan-Teh Chang4548b162020-11-06 11:48:25 -08001089 avifResult readResult = avifDecoderItemRead(item, decoder->io, &xmpContents, 0);
Joe Dragobe4cbb92020-09-21 12:14:05 -07001090 if (readResult != AVIF_RESULT_OK) {
1091 return readResult;
1092 }
1093
1094 avifImageSetMetadataXMP(image, xmpContents.data, xmpContents.size);
Joe Dragoa72da5b2020-06-15 19:40:17 -07001095 }
1096 }
Joe Dragobe4cbb92020-09-21 12:14:05 -07001097 return AVIF_RESULT_OK;
Joe Dragoa72da5b2020-06-15 19:40:17 -07001098}
1099
Joe Drago8f7a3002019-02-07 19:35:37 -08001100// ---------------------------------------------------------------------------
Joe Dragocd1e4c32019-02-08 11:26:31 -08001101// URN
1102
Joe Dragoa72da5b2020-06-15 19:40:17 -07001103static avifBool isAlphaURN(const char * urn)
Joe Dragocd1e4c32019-02-08 11:26:31 -08001104{
wantehchang3fde0d02020-03-10 23:58:32 -07001105 return !strcmp(urn, URN_ALPHA0) || !strcmp(urn, URN_ALPHA1);
Joe Dragocd1e4c32019-02-08 11:26:31 -08001106}
1107
1108// ---------------------------------------------------------------------------
Joe Drago8f7a3002019-02-07 19:35:37 -08001109// BMFF Parsing
1110
Joe Drago9f2b87b2020-06-03 19:36:38 -07001111static avifBool avifParseItemLocationBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago444f0512019-01-23 17:03:24 -08001112{
Joe Drago8f7a3002019-02-07 19:35:37 -08001113 BEGIN_STREAM(s, raw, rawLen);
Joe Drago444f0512019-01-23 17:03:24 -08001114
Joe Dragof6a42272019-11-21 15:21:41 -08001115 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -07001116 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Dragof6a42272019-11-21 15:21:41 -08001117 if (version > 2) {
1118 return AVIF_FALSE;
1119 }
Joe Drago444f0512019-01-23 17:03:24 -08001120
Joe Drago8f7a3002019-02-07 19:35:37 -08001121 uint8_t offsetSizeAndLengthSize;
Joe Drago345aaa12019-09-25 13:42:12 -07001122 CHECK(avifROStreamRead(&s, &offsetSizeAndLengthSize, 1));
Joe Drago8f7a3002019-02-07 19:35:37 -08001123 uint8_t offsetSize = (offsetSizeAndLengthSize >> 4) & 0xf; // unsigned int(4) offset_size;
1124 uint8_t lengthSize = (offsetSizeAndLengthSize >> 0) & 0xf; // unsigned int(4) length_size;
Joe Drago444f0512019-01-23 17:03:24 -08001125
Joe Dragof6a42272019-11-21 15:21:41 -08001126 uint8_t baseOffsetSizeAndIndexSize;
1127 CHECK(avifROStreamRead(&s, &baseOffsetSizeAndIndexSize, 1));
1128 uint8_t baseOffsetSize = (baseOffsetSizeAndIndexSize >> 4) & 0xf; // unsigned int(4) base_offset_size;
1129 uint8_t indexSize = 0;
1130 if ((version == 1) || (version == 2)) {
1131 indexSize = baseOffsetSizeAndIndexSize & 0xf; // unsigned int(4) index_size;
1132 if (indexSize != 0) {
1133 // extent_index unsupported
1134 return AVIF_FALSE;
1135 }
1136 }
Joe Drago444f0512019-01-23 17:03:24 -08001137
Joe Dragof6a42272019-11-21 15:21:41 -08001138 uint16_t tmp16;
1139 uint32_t itemCount;
1140 if (version < 2) {
1141 CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_count;
1142 itemCount = tmp16;
1143 } else {
1144 CHECK(avifROStreamReadU32(&s, &itemCount)); // unsigned int(32) item_count;
1145 }
1146 for (uint32_t i = 0; i < itemCount; ++i) {
1147 uint32_t itemID;
1148 uint32_t idatID = 0;
1149 if (version < 2) {
1150 CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID;
1151 itemID = tmp16;
1152 } else {
1153 CHECK(avifROStreamReadU32(&s, &itemID)); // unsigned int(32) item_ID;
1154 }
1155
1156 if ((version == 1) || (version == 2)) {
1157 uint8_t ignored;
1158 uint8_t constructionMethod;
1159 CHECK(avifROStreamRead(&s, &ignored, 1)); // unsigned int(12) reserved = 0;
1160 CHECK(avifROStreamRead(&s, &constructionMethod, 1)); // unsigned int(4) construction_method;
1161 constructionMethod = constructionMethod & 0xf;
1162 if ((constructionMethod != 0 /* file */) && (constructionMethod != 1 /* idat */)) {
1163 // construction method item(2) unsupported
1164 return AVIF_FALSE;
1165 }
1166 if (constructionMethod == 1) {
Joe Dragoba1eb492020-06-22 17:05:04 -07001167 idatID = meta->idatID;
Joe Dragof6a42272019-11-21 15:21:41 -08001168 }
1169 }
1170
Joe Dragoa4956902020-08-28 01:24:35 -07001171 avifDecoderItem * item = avifMetaFindItem(meta, itemID);
1172 if (!item) {
1173 return AVIF_FALSE;
1174 }
Joe Drago8f439092020-08-28 15:05:17 -07001175 if (item->extents.count > 0) {
1176 // This item has already been given extents via this iloc box. This is invalid.
1177 return AVIF_FALSE;
1178 }
Joe Dragoa4956902020-08-28 01:24:35 -07001179 item->idatID = idatID;
1180
Joe Drago345aaa12019-09-25 13:42:12 -07001181 uint16_t dataReferenceIndex; // unsigned int(16) data_ref rence_index;
1182 CHECK(avifROStreamReadU16(&s, &dataReferenceIndex)); //
1183 uint64_t baseOffset; // unsigned int(base_offset_size*8) base_offset;
1184 CHECK(avifROStreamReadUX8(&s, &baseOffset, baseOffsetSize)); //
1185 uint16_t extentCount; // unsigned int(16) extent_count;
1186 CHECK(avifROStreamReadU16(&s, &extentCount)); //
Joe Dragoa4956902020-08-28 01:24:35 -07001187 for (int extentIter = 0; extentIter < extentCount; ++extentIter) {
Joe Dragof6a42272019-11-21 15:21:41 -08001188 // If extent_index is ever supported, this spec must be implemented here:
1189 // :: if (((version == 1) || (version == 2)) && (index_size > 0)) {
1190 // :: unsigned int(index_size*8) extent_index;
1191 // :: }
1192
Joe Drago8f7a3002019-02-07 19:35:37 -08001193 uint64_t extentOffset; // unsigned int(offset_size*8) extent_offset;
Joe Drago345aaa12019-09-25 13:42:12 -07001194 CHECK(avifROStreamReadUX8(&s, &extentOffset, offsetSize));
Joe Drago8f7a3002019-02-07 19:35:37 -08001195 uint64_t extentLength; // unsigned int(offset_size*8) extent_length;
Joe Drago345aaa12019-09-25 13:42:12 -07001196 CHECK(avifROStreamReadUX8(&s, &extentLength, lengthSize));
Joe Drago444f0512019-01-23 17:03:24 -08001197
Joe Drago4bcdfde2020-11-13 17:50:55 -08001198 avifExtent * extent = (avifExtent *)avifArrayPushPtr(&item->extents);
Wan-Teh Changb9853692020-08-28 15:32:36 -07001199 if (extentOffset > UINT64_MAX - baseOffset) {
1200 return AVIF_FALSE;
1201 }
1202 uint64_t offset = baseOffset + extentOffset;
Joe Drago217056b2020-11-13 16:19:35 -08001203 extent->offset = offset;
1204 if (extentLength > SIZE_MAX) {
Wan-Teh Changb9853692020-08-28 15:32:36 -07001205 return AVIF_FALSE;
1206 }
Joe Drago217056b2020-11-13 16:19:35 -08001207 extent->size = (size_t)extentLength;
1208 if (extent->size > SIZE_MAX - item->size) {
Wan-Teh Changb9853692020-08-28 15:32:36 -07001209 return AVIF_FALSE;
1210 }
Joe Dragoa4956902020-08-28 01:24:35 -07001211 item->size += extent->size;
Joe Drago444f0512019-01-23 17:03:24 -08001212 }
1213 }
1214 return AVIF_TRUE;
1215}
1216
Joe Drago060d5342020-03-03 10:53:49 -08001217static avifBool avifParseImageGridBox(avifImageGrid * grid, const uint8_t * raw, size_t rawLen)
1218{
1219 BEGIN_STREAM(s, raw, rawLen);
1220
1221 uint8_t version, flags;
1222 CHECK(avifROStreamRead(&s, &version, 1)); // unsigned int(8) version = 0;
1223 if (version != 0) {
1224 return AVIF_FALSE;
1225 }
Joe Drago79ebcf32020-11-18 22:37:10 -08001226 uint8_t rowsMinusOne, columnsMinusOne;
1227 CHECK(avifROStreamRead(&s, &flags, 1)); // unsigned int(8) flags;
1228 CHECK(avifROStreamRead(&s, &rowsMinusOne, 1)); // unsigned int(8) rows_minus_one;
1229 CHECK(avifROStreamRead(&s, &columnsMinusOne, 1)); // unsigned int(8) columns_minus_one;
1230 grid->rows = (uint32_t)rowsMinusOne + 1;
1231 grid->columns = (uint32_t)columnsMinusOne + 1;
Joe Drago060d5342020-03-03 10:53:49 -08001232
1233 uint32_t fieldLength = ((flags & 1) + 1) * 16;
1234 if (fieldLength == 16) {
1235 uint16_t outputWidth16, outputHeight16;
1236 CHECK(avifROStreamReadU16(&s, &outputWidth16)); // unsigned int(FieldLength) output_width;
1237 CHECK(avifROStreamReadU16(&s, &outputHeight16)); // unsigned int(FieldLength) output_height;
1238 grid->outputWidth = outputWidth16;
1239 grid->outputHeight = outputHeight16;
1240 } else {
1241 if (fieldLength != 32) {
1242 // This should be impossible
1243 return AVIF_FALSE;
1244 }
1245 CHECK(avifROStreamReadU32(&s, &grid->outputWidth)); // unsigned int(FieldLength) output_width;
1246 CHECK(avifROStreamReadU32(&s, &grid->outputHeight)); // unsigned int(FieldLength) output_height;
1247 }
wantehchangbc35a5f2020-08-12 15:27:39 -07001248 if ((grid->outputWidth == 0) || (grid->outputHeight == 0) || (grid->outputWidth > (AVIF_MAX_IMAGE_SIZE / grid->outputHeight))) {
Wan-Teh Chang0a8e7242020-08-10 13:24:59 -07001249 return AVIF_FALSE;
1250 }
wantehchangae2074b2020-08-12 13:02:27 -07001251 return avifROStreamRemainingBytes(&s) == 0;
Joe Drago060d5342020-03-03 10:53:49 -08001252}
1253
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001254static avifBool avifParseImageSpatialExtentsProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001255{
1256 BEGIN_STREAM(s, raw, rawLen);
Joe Drago345aaa12019-09-25 13:42:12 -07001257 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Drago8f7a3002019-02-07 19:35:37 -08001258
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001259 avifImageSpatialExtents * ispe = &prop->u.ispe;
1260 CHECK(avifROStreamReadU32(&s, &ispe->width));
1261 CHECK(avifROStreamReadU32(&s, &ispe->height));
Joe Drago8f7a3002019-02-07 19:35:37 -08001262 return AVIF_TRUE;
1263}
1264
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001265static avifBool avifParseAuxiliaryTypeProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Dragocd1e4c32019-02-08 11:26:31 -08001266{
1267 BEGIN_STREAM(s, raw, rawLen);
Joe Drago345aaa12019-09-25 13:42:12 -07001268 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragocd1e4c32019-02-08 11:26:31 -08001269
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001270 CHECK(avifROStreamReadString(&s, prop->u.auxC.auxType, AUXTYPE_SIZE));
Joe Dragocd1e4c32019-02-08 11:26:31 -08001271 return AVIF_TRUE;
1272}
1273
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001274static avifBool avifParseColourInformationBox(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago41eb62b2019-02-08 15:38:18 -08001275{
1276 BEGIN_STREAM(s, raw, rawLen);
Joe Dragoe7ce20d2019-02-11 16:37:38 -08001277
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001278 avifColourInformationBox * colr = &prop->u.colr;
1279 colr->hasICC = AVIF_FALSE;
1280 colr->hasNCLX = AVIF_FALSE;
Joe Dragoe7ce20d2019-02-11 16:37:38 -08001281
Joe Dragoa0da4a42020-05-08 14:27:40 -07001282 uint8_t colorType[4]; // unsigned int(32) colour_type;
1283 CHECK(avifROStreamRead(&s, colorType, 4));
1284 if (!memcmp(colorType, "rICC", 4) || !memcmp(colorType, "prof", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001285 colr->hasICC = AVIF_TRUE;
1286 colr->icc = avifROStreamCurrent(&s);
1287 colr->iccSize = avifROStreamRemainingBytes(&s);
Joe Dragoa0da4a42020-05-08 14:27:40 -07001288 } else if (!memcmp(colorType, "nclx", 4)) {
Joe Dragofb5a5f02021-01-31 20:43:57 -08001289 CHECK(avifROStreamReadU16(&s, &colr->colorPrimaries)); // unsigned int(16) colour_primaries;
1290 CHECK(avifROStreamReadU16(&s, &colr->transferCharacteristics)); // unsigned int(16) transfer_characteristics;
1291 CHECK(avifROStreamReadU16(&s, &colr->matrixCoefficients)); // unsigned int(16) matrix_coefficients;
Joe Drago97b071c2019-07-17 14:24:56 -07001292 // unsigned int(1) full_range_flag;
1293 // unsigned int(7) reserved = 0;
Joe Drago74cd1c92020-04-16 12:17:11 -07001294 uint8_t tmp8;
1295 CHECK(avifROStreamRead(&s, &tmp8, 1));
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001296 colr->range = (tmp8 & 0x80) ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED;
1297 colr->hasNCLX = AVIF_TRUE;
Joe Drago41eb62b2019-02-08 15:38:18 -08001298 }
1299 return AVIF_TRUE;
1300}
1301
Joe Drago6500fd62019-10-08 17:17:34 -07001302static avifBool avifParseAV1CodecConfigurationBox(const uint8_t * raw, size_t rawLen, avifCodecConfigurationBox * av1C)
1303{
1304 BEGIN_STREAM(s, raw, rawLen);
1305
1306 uint8_t markerAndVersion = 0;
1307 CHECK(avifROStreamRead(&s, &markerAndVersion, 1));
1308 uint8_t seqProfileAndIndex = 0;
1309 CHECK(avifROStreamRead(&s, &seqProfileAndIndex, 1));
1310 uint8_t rawFlags = 0;
1311 CHECK(avifROStreamRead(&s, &rawFlags, 1));
1312
1313 if (markerAndVersion != 0x81) {
1314 // Marker and version must both == 1
1315 return AVIF_FALSE;
1316 }
1317
1318 av1C->seqProfile = (seqProfileAndIndex >> 5) & 0x7; // unsigned int (3) seq_profile;
1319 av1C->seqLevelIdx0 = (seqProfileAndIndex >> 0) & 0x1f; // unsigned int (5) seq_level_idx_0;
1320 av1C->seqTier0 = (rawFlags >> 7) & 0x1; // unsigned int (1) seq_tier_0;
1321 av1C->highBitdepth = (rawFlags >> 6) & 0x1; // unsigned int (1) high_bitdepth;
1322 av1C->twelveBit = (rawFlags >> 5) & 0x1; // unsigned int (1) twelve_bit;
1323 av1C->monochrome = (rawFlags >> 4) & 0x1; // unsigned int (1) monochrome;
1324 av1C->chromaSubsamplingX = (rawFlags >> 3) & 0x1; // unsigned int (1) chroma_subsampling_x;
1325 av1C->chromaSubsamplingY = (rawFlags >> 2) & 0x1; // unsigned int (1) chroma_subsampling_y;
1326 av1C->chromaSamplePosition = (rawFlags >> 0) & 0x3; // unsigned int (2) chroma_sample_position;
1327 return AVIF_TRUE;
1328}
1329
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001330static avifBool avifParseAV1CodecConfigurationBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago6500fd62019-10-08 17:17:34 -07001331{
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001332 return avifParseAV1CodecConfigurationBox(raw, rawLen, &prop->u.av1C);
Joe Drago6500fd62019-10-08 17:17:34 -07001333}
1334
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001335static avifBool avifParsePixelAspectRatioBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001336{
1337 BEGIN_STREAM(s, raw, rawLen);
1338
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001339 avifPixelAspectRatioBox * pasp = &prop->u.pasp;
Joe Drago89f0cc82020-03-09 16:13:27 -07001340 CHECK(avifROStreamReadU32(&s, &pasp->hSpacing)); // unsigned int(32) hSpacing;
1341 CHECK(avifROStreamReadU32(&s, &pasp->vSpacing)); // unsigned int(32) vSpacing;
1342 return AVIF_TRUE;
1343}
1344
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001345static avifBool avifParseCleanApertureBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001346{
1347 BEGIN_STREAM(s, raw, rawLen);
1348
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001349 avifCleanApertureBox * clap = &prop->u.clap;
Joe Drago89f0cc82020-03-09 16:13:27 -07001350 CHECK(avifROStreamReadU32(&s, &clap->widthN)); // unsigned int(32) cleanApertureWidthN;
1351 CHECK(avifROStreamReadU32(&s, &clap->widthD)); // unsigned int(32) cleanApertureWidthD;
1352 CHECK(avifROStreamReadU32(&s, &clap->heightN)); // unsigned int(32) cleanApertureHeightN;
1353 CHECK(avifROStreamReadU32(&s, &clap->heightD)); // unsigned int(32) cleanApertureHeightD;
1354 CHECK(avifROStreamReadU32(&s, &clap->horizOffN)); // unsigned int(32) horizOffN;
1355 CHECK(avifROStreamReadU32(&s, &clap->horizOffD)); // unsigned int(32) horizOffD;
1356 CHECK(avifROStreamReadU32(&s, &clap->vertOffN)); // unsigned int(32) vertOffN;
1357 CHECK(avifROStreamReadU32(&s, &clap->vertOffD)); // unsigned int(32) vertOffD;
1358 return AVIF_TRUE;
1359}
1360
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001361static avifBool avifParseImageRotationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001362{
1363 BEGIN_STREAM(s, raw, rawLen);
1364
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001365 avifImageRotation * irot = &prop->u.irot;
Joe Drago89f0cc82020-03-09 16:13:27 -07001366 CHECK(avifROStreamRead(&s, &irot->angle, 1)); // unsigned int (6) reserved = 0; unsigned int (2) angle;
1367 if ((irot->angle & 0xfc) != 0) {
1368 // reserved bits must be 0
1369 return AVIF_FALSE;
1370 }
1371 return AVIF_TRUE;
1372}
1373
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001374static avifBool avifParseImageMirrorProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001375{
1376 BEGIN_STREAM(s, raw, rawLen);
1377
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001378 avifImageMirror * imir = &prop->u.imir;
Joe Drago89f0cc82020-03-09 16:13:27 -07001379 CHECK(avifROStreamRead(&s, &imir->axis, 1)); // unsigned int (7) reserved = 0; unsigned int (1) axis;
1380 if ((imir->axis & 0xfe) != 0) {
1381 // reserved bits must be 0
1382 return AVIF_FALSE;
1383 }
1384 return AVIF_TRUE;
1385}
1386
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001387static avifBool avifParsePixelInformationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago60421562020-04-23 11:32:26 -07001388{
1389 BEGIN_STREAM(s, raw, rawLen);
1390 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
1391
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001392 avifPixelInformationProperty * pixi = &prop->u.pixi;
Joe Drago60421562020-04-23 11:32:26 -07001393 CHECK(avifROStreamRead(&s, &pixi->planeCount, 1)); // unsigned int (8) num_channels;
1394 if (pixi->planeCount > MAX_PIXI_PLANE_DEPTHS) {
1395 return AVIF_FALSE;
1396 }
1397 for (uint8_t i = 0; i < pixi->planeCount; ++i) {
1398 CHECK(avifROStreamRead(&s, &pixi->planeDepths[i], 1)); // unsigned int (8) bits_per_channel;
1399 }
1400 return AVIF_TRUE;
1401}
1402
Joe Dragoa72da5b2020-06-15 19:40:17 -07001403static avifBool avifParseItemPropertyContainerBox(avifPropertyArray * properties, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001404{
1405 BEGIN_STREAM(s, raw, rawLen);
1406
Joe Drago345aaa12019-09-25 13:42:12 -07001407 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001408 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001409 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago8f7a3002019-02-07 19:35:37 -08001410
Joe Dragoa72da5b2020-06-15 19:40:17 -07001411 int propertyIndex = avifArrayPushIndex(properties);
1412 avifProperty * prop = &properties->prop[propertyIndex];
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001413 memcpy(prop->type, header.type, 4);
Joe Drago8f7a3002019-02-07 19:35:37 -08001414 if (!memcmp(header.type, "ispe", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001415 CHECK(avifParseImageSpatialExtentsProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001416 } else if (!memcmp(header.type, "auxC", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001417 CHECK(avifParseAuxiliaryTypeProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001418 } else if (!memcmp(header.type, "colr", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001419 CHECK(avifParseColourInformationBox(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001420 } else if (!memcmp(header.type, "av1C", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001421 CHECK(avifParseAV1CodecConfigurationBoxProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001422 } else if (!memcmp(header.type, "pasp", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001423 CHECK(avifParsePixelAspectRatioBoxProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001424 } else if (!memcmp(header.type, "clap", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001425 CHECK(avifParseCleanApertureBoxProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001426 } else if (!memcmp(header.type, "irot", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001427 CHECK(avifParseImageRotationProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001428 } else if (!memcmp(header.type, "imir", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001429 CHECK(avifParseImageMirrorProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001430 } else if (!memcmp(header.type, "pixi", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001431 CHECK(avifParsePixelInformationProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Drago60421562020-04-23 11:32:26 -07001432 }
Joe Drago8f7a3002019-02-07 19:35:37 -08001433
Joe Drago345aaa12019-09-25 13:42:12 -07001434 CHECK(avifROStreamSkip(&s, header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001435 }
1436 return AVIF_TRUE;
1437}
1438
Joe Drago9f2b87b2020-06-03 19:36:38 -07001439static avifBool avifParseItemPropertyAssociation(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001440{
1441 BEGIN_STREAM(s, raw, rawLen);
1442
1443 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -07001444 uint32_t flags;
1445 CHECK(avifROStreamReadVersionAndFlags(&s, &version, &flags));
1446 avifBool propertyIndexIsU16 = ((flags & 0x1) != 0);
Joe Drago8f7a3002019-02-07 19:35:37 -08001447
1448 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001449 CHECK(avifROStreamReadU32(&s, &entryCount));
Wan-Teh Changf4c65382020-11-03 14:27:45 -08001450 unsigned int prevItemID = 0;
Joe Drago8f7a3002019-02-07 19:35:37 -08001451 for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) {
Wan-Teh Changf4c65382020-11-03 14:27:45 -08001452 // ISO/IEC 23008-12, First edition, 2017-12, Section 9.3.1:
1453 // Each ItemPropertyAssociation box shall be ordered by increasing item_ID, and there shall
1454 // be at most one association box for each item_ID, in any ItemPropertyAssociation box.
Joe Drago8f7a3002019-02-07 19:35:37 -08001455 unsigned int itemID;
1456 if (version < 1) {
1457 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001458 CHECK(avifROStreamReadU16(&s, &tmp));
Joe Drago8f7a3002019-02-07 19:35:37 -08001459 itemID = tmp;
1460 } else {
Joe Drago345aaa12019-09-25 13:42:12 -07001461 CHECK(avifROStreamReadU32(&s, &itemID));
Joe Drago8f7a3002019-02-07 19:35:37 -08001462 }
Wan-Teh Changf4c65382020-11-03 14:27:45 -08001463 if (itemID <= prevItemID) {
1464 return AVIF_FALSE;
1465 }
1466 prevItemID = itemID;
Wan-Teh Chang750a1922020-10-28 17:54:35 -07001467
1468 avifDecoderItem * item = avifMetaFindItem(meta, itemID);
1469 if (!item) {
1470 return AVIF_FALSE;
1471 }
Wan-Teh Changf4c65382020-11-03 14:27:45 -08001472 if (item->ipmaSeen) {
1473 return AVIF_FALSE;
1474 }
1475 item->ipmaSeen = AVIF_TRUE;
Wan-Teh Chang750a1922020-10-28 17:54:35 -07001476
Joe Drago8f7a3002019-02-07 19:35:37 -08001477 uint8_t associationCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001478 CHECK(avifROStreamRead(&s, &associationCount, 1));
Joe Drago8f7a3002019-02-07 19:35:37 -08001479 for (uint8_t associationIndex = 0; associationIndex < associationCount; ++associationIndex) {
Joe Drago3320e5f2020-04-21 17:36:27 -07001480 avifBool essential = AVIF_FALSE;
Joe Drago8f7a3002019-02-07 19:35:37 -08001481 uint16_t propertyIndex = 0;
1482 if (propertyIndexIsU16) {
Joe Drago345aaa12019-09-25 13:42:12 -07001483 CHECK(avifROStreamReadU16(&s, &propertyIndex));
Joe Drago3320e5f2020-04-21 17:36:27 -07001484 essential = ((propertyIndex & 0x8000) != 0);
Joe Drago8f7a3002019-02-07 19:35:37 -08001485 propertyIndex &= 0x7fff;
1486 } else {
1487 uint8_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001488 CHECK(avifROStreamRead(&s, &tmp, 1));
Joe Drago3320e5f2020-04-21 17:36:27 -07001489 essential = ((tmp & 0x80) != 0);
Joe Drago8f7a3002019-02-07 19:35:37 -08001490 propertyIndex = tmp & 0x7f;
1491 }
1492
1493 if (propertyIndex == 0) {
1494 // Not associated with any item
1495 continue;
1496 }
1497 --propertyIndex; // 1-indexed
1498
Joe Drago9f2b87b2020-06-03 19:36:38 -07001499 if (propertyIndex >= meta->properties.count) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001500 return AVIF_FALSE;
1501 }
1502
Joe Dragoa72da5b2020-06-15 19:40:17 -07001503 // Copy property to item
1504 avifProperty * srcProp = &meta->properties.prop[propertyIndex];
1505
1506 static const char * supportedTypes[] = { "ispe", "auxC", "colr", "av1C", "pasp", "clap", "irot", "imir", "pixi" };
1507 size_t supportedTypesCount = sizeof(supportedTypes) / sizeof(supportedTypes[0]);
1508 avifBool supportedType = AVIF_FALSE;
1509 for (size_t i = 0; i < supportedTypesCount; ++i) {
1510 if (!memcmp(srcProp->type, supportedTypes[i], 4)) {
1511 supportedType = AVIF_TRUE;
1512 break;
1513 }
1514 }
1515 if (supportedType) {
1516 avifProperty * dstProp = (avifProperty *)avifArrayPushPtr(&item->properties);
1517 memcpy(dstProp, srcProp, sizeof(avifProperty));
Joe Drago3320e5f2020-04-21 17:36:27 -07001518 } else {
1519 if (essential) {
1520 // Discovered an essential item property that libavif doesn't support!
1521 // Make a note to ignore this item later.
1522 item->hasUnsupportedEssentialProperty = AVIF_TRUE;
1523 }
Joe Drago8f7a3002019-02-07 19:35:37 -08001524 }
1525 }
1526 }
1527
1528 return AVIF_TRUE;
1529}
1530
Joe Drago9f2b87b2020-06-03 19:36:38 -07001531static avifBool avifParsePrimaryItemBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Dragof6a42272019-11-21 15:21:41 -08001532{
Joe Drago9f2b87b2020-06-03 19:36:38 -07001533 if (meta->primaryItemID > 0) {
Joe Dragof6a42272019-11-21 15:21:41 -08001534 // Illegal to have multiple pitm boxes, bail out
1535 return AVIF_FALSE;
1536 }
1537
1538 BEGIN_STREAM(s, raw, rawLen);
1539
1540 uint8_t version;
1541 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
1542
1543 if (version == 0) {
1544 uint16_t tmp16;
1545 CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID;
Joe Drago9f2b87b2020-06-03 19:36:38 -07001546 meta->primaryItemID = tmp16;
Joe Dragof6a42272019-11-21 15:21:41 -08001547 } else {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001548 CHECK(avifROStreamReadU32(&s, &meta->primaryItemID)); // unsigned int(32) item_ID;
Joe Dragof6a42272019-11-21 15:21:41 -08001549 }
1550 return AVIF_TRUE;
1551}
1552
Joe Drago9f2b87b2020-06-03 19:36:38 -07001553static avifBool avifParseItemDataBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Dragof6a42272019-11-21 15:21:41 -08001554{
Joe Dragof6a42272019-11-21 15:21:41 -08001555 // Check to see if we've already seen an idat box for this meta box. If so, bail out
Joe Drago9f2b87b2020-06-03 19:36:38 -07001556 for (uint32_t i = 0; i < meta->idats.count; ++i) {
Joe Dragoba1eb492020-06-22 17:05:04 -07001557 if (meta->idats.idat[i].id == meta->idatID) {
Joe Dragof6a42272019-11-21 15:21:41 -08001558 return AVIF_FALSE;
1559 }
1560 }
1561
Joe Drago9f2b87b2020-06-03 19:36:38 -07001562 int index = avifArrayPushIndex(&meta->idats);
1563 avifDecoderItemData * idat = &meta->idats.idat[index];
Joe Dragoba1eb492020-06-22 17:05:04 -07001564 idat->id = meta->idatID;
Joe Dragobe4cbb92020-09-21 12:14:05 -07001565 avifRWDataSet(&idat->data, raw, rawLen);
Joe Dragof6a42272019-11-21 15:21:41 -08001566 return AVIF_TRUE;
1567}
1568
Joe Drago9f2b87b2020-06-03 19:36:38 -07001569static avifBool avifParseItemPropertiesBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001570{
1571 BEGIN_STREAM(s, raw, rawLen);
1572
1573 avifBoxHeader ipcoHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001574 CHECK(avifROStreamReadBoxHeader(&s, &ipcoHeader));
wantehchangbc35a5f2020-08-12 15:27:39 -07001575 if (memcmp(ipcoHeader.type, "ipco", 4)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001576 return AVIF_FALSE;
1577 }
1578
1579 // Read all item properties inside of ItemPropertyContainerBox
Joe Dragoa72da5b2020-06-15 19:40:17 -07001580 CHECK(avifParseItemPropertyContainerBox(&meta->properties, avifROStreamCurrent(&s), ipcoHeader.size));
Joe Drago345aaa12019-09-25 13:42:12 -07001581 CHECK(avifROStreamSkip(&s, ipcoHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001582
1583 // Now read all ItemPropertyAssociation until the end of the box, and make associations
Joe Drago345aaa12019-09-25 13:42:12 -07001584 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001585 avifBoxHeader ipmaHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001586 CHECK(avifROStreamReadBoxHeader(&s, &ipmaHeader));
Joe Drago8f7a3002019-02-07 19:35:37 -08001587
1588 if (!memcmp(ipmaHeader.type, "ipma", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001589 CHECK(avifParseItemPropertyAssociation(meta, avifROStreamCurrent(&s), ipmaHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001590 } else {
1591 // These must all be type ipma
1592 return AVIF_FALSE;
1593 }
1594
Joe Drago345aaa12019-09-25 13:42:12 -07001595 CHECK(avifROStreamSkip(&s, ipmaHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001596 }
1597 return AVIF_TRUE;
1598}
1599
Joe Drago9f2b87b2020-06-03 19:36:38 -07001600static avifBool avifParseItemInfoEntry(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001601{
1602 BEGIN_STREAM(s, raw, rawLen);
1603
Joe Drago345aaa12019-09-25 13:42:12 -07001604 CHECK(avifROStreamReadAndEnforceVersion(&s, 2)); // TODO: support version > 2? 2+ is required for item_type
Joe Drago8f7a3002019-02-07 19:35:37 -08001605
Joe Drago345aaa12019-09-25 13:42:12 -07001606 uint16_t itemID; // unsigned int(16) item_ID;
1607 CHECK(avifROStreamReadU16(&s, &itemID)); //
1608 uint16_t itemProtectionIndex; // unsigned int(16) item_protection_index;
1609 CHECK(avifROStreamReadU16(&s, &itemProtectionIndex)); //
1610 uint8_t itemType[4]; // unsigned int(32) item_type;
1611 CHECK(avifROStreamRead(&s, itemType, 4)); //
Joe Drago8f7a3002019-02-07 19:35:37 -08001612
Joe Dragof6a42272019-11-21 15:21:41 -08001613 avifContentType contentType;
1614 if (!memcmp(itemType, "mime", 4)) {
1615 CHECK(avifROStreamReadString(&s, NULL, 0)); // string item_name; (skipped)
1616 CHECK(avifROStreamReadString(&s, contentType.contentType, CONTENTTYPE_SIZE)); // string content_type;
1617 } else {
1618 memset(&contentType, 0, sizeof(contentType));
1619 }
1620
Joe Drago9f2b87b2020-06-03 19:36:38 -07001621 avifDecoderItem * item = avifMetaFindItem(meta, itemID);
Joe Drago46ea0582019-07-22 15:55:47 -07001622 if (!item) {
1623 return AVIF_FALSE;
1624 }
1625
Joe Drago05559c92019-07-17 16:33:38 -07001626 memcpy(item->type, itemType, sizeof(itemType));
Joe Dragof6a42272019-11-21 15:21:41 -08001627 memcpy(&item->contentType, &contentType, sizeof(contentType));
Joe Drago8f7a3002019-02-07 19:35:37 -08001628 return AVIF_TRUE;
1629}
1630
Joe Drago9f2b87b2020-06-03 19:36:38 -07001631static avifBool avifParseItemInfoBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001632{
1633 BEGIN_STREAM(s, raw, rawLen);
1634
1635 uint8_t version;
Joe Drago345aaa12019-09-25 13:42:12 -07001636 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Drago8f7a3002019-02-07 19:35:37 -08001637 uint32_t entryCount;
1638 if (version == 0) {
1639 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001640 CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) entry_count;
Joe Drago8f7a3002019-02-07 19:35:37 -08001641 entryCount = tmp;
1642 } else if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001643 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Drago8f7a3002019-02-07 19:35:37 -08001644 } else {
1645 return AVIF_FALSE;
1646 }
1647
Joe Drago678b9382019-02-09 03:17:47 -08001648 for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001649 avifBoxHeader infeHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001650 CHECK(avifROStreamReadBoxHeader(&s, &infeHeader));
Joe Drago8f7a3002019-02-07 19:35:37 -08001651
1652 if (!memcmp(infeHeader.type, "infe", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001653 CHECK(avifParseItemInfoEntry(meta, avifROStreamCurrent(&s), infeHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001654 } else {
1655 // These must all be type ipma
1656 return AVIF_FALSE;
1657 }
1658
Joe Drago345aaa12019-09-25 13:42:12 -07001659 CHECK(avifROStreamSkip(&s, infeHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001660 }
1661
1662 return AVIF_TRUE;
1663}
1664
Joe Drago9f2b87b2020-06-03 19:36:38 -07001665static avifBool avifParseItemReferenceBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001666{
1667 BEGIN_STREAM(s, raw, rawLen);
1668
1669 uint8_t version;
Joe Drago345aaa12019-09-25 13:42:12 -07001670 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Drago8f7a3002019-02-07 19:35:37 -08001671
Joe Drago345aaa12019-09-25 13:42:12 -07001672 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001673 avifBoxHeader irefHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001674 CHECK(avifROStreamReadBoxHeader(&s, &irefHeader));
Joe Drago8f7a3002019-02-07 19:35:37 -08001675
1676 uint32_t fromID = 0;
1677 if (version == 0) {
1678 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001679 CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) from_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001680 fromID = tmp;
1681 } else if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001682 CHECK(avifROStreamReadU32(&s, &fromID)); // unsigned int(32) from_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001683 } else {
1684 // unsupported iref version, skip it
1685 break;
1686 }
1687
1688 uint16_t referenceCount = 0;
Joe Drago345aaa12019-09-25 13:42:12 -07001689 CHECK(avifROStreamReadU16(&s, &referenceCount)); // unsigned int(16) reference_count;
Joe Drago8f7a3002019-02-07 19:35:37 -08001690
1691 for (uint16_t refIndex = 0; refIndex < referenceCount; ++refIndex) {
1692 uint32_t toID = 0;
1693 if (version == 0) {
1694 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001695 CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) to_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001696 toID = tmp;
1697 } else if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001698 CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) to_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001699 } else {
1700 // unsupported iref version, skip it
1701 break;
1702 }
1703
1704 // Read this reference as "{fromID} is a {irefType} for {toID}"
1705 if (fromID && toID) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001706 avifDecoderItem * item = avifMetaFindItem(meta, fromID);
Joe Drago46ea0582019-07-22 15:55:47 -07001707 if (!item) {
1708 return AVIF_FALSE;
1709 }
1710
Joe Drago8f7a3002019-02-07 19:35:37 -08001711 if (!memcmp(irefHeader.type, "thmb", 4)) {
Joe Drago05559c92019-07-17 16:33:38 -07001712 item->thumbnailForID = toID;
Wan-Teh Changb309e982021-02-20 16:59:36 -08001713 } else if (!memcmp(irefHeader.type, "auxl", 4)) {
Joe Drago05559c92019-07-17 16:33:38 -07001714 item->auxForID = toID;
Wan-Teh Changb309e982021-02-20 16:59:36 -08001715 } else if (!memcmp(irefHeader.type, "cdsc", 4)) {
Joe Dragof6a42272019-11-21 15:21:41 -08001716 item->descForID = toID;
Wan-Teh Changb309e982021-02-20 16:59:36 -08001717 } else if (!memcmp(irefHeader.type, "dimg", 4)) {
Joe Drago060d5342020-03-03 10:53:49 -08001718 // derived images refer in the opposite direction
Joe Drago9f2b87b2020-06-03 19:36:38 -07001719 avifDecoderItem * dimg = avifMetaFindItem(meta, toID);
Joe Drago060d5342020-03-03 10:53:49 -08001720 if (!dimg) {
1721 return AVIF_FALSE;
1722 }
1723
1724 dimg->dimgForID = fromID;
Wan-Teh Changb309e982021-02-20 16:59:36 -08001725 } else if (!memcmp(irefHeader.type, "prem", 4)) {
Yuan Tonge4850be2021-01-22 14:21:25 +08001726 item->premByID = toID;
1727 }
Joe Drago8f7a3002019-02-07 19:35:37 -08001728 }
1729 }
1730 }
1731
1732 return AVIF_TRUE;
1733}
1734
Joe Drago9f2b87b2020-06-03 19:36:38 -07001735static avifBool avifParseMetaBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001736{
1737 BEGIN_STREAM(s, raw, rawLen);
1738
Joe Drago345aaa12019-09-25 13:42:12 -07001739 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Drago8f7a3002019-02-07 19:35:37 -08001740
Joe Dragoba1eb492020-06-22 17:05:04 -07001741 ++meta->idatID; // for tracking idat
Joe Dragof6a42272019-11-21 15:21:41 -08001742
Joe Drago8f439092020-08-28 15:05:17 -07001743 uint32_t uniqueBoxFlags = 0;
Joe Drago345aaa12019-09-25 13:42:12 -07001744 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001745 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001746 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago8f7a3002019-02-07 19:35:37 -08001747
1748 if (!memcmp(header.type, "iloc", 4)) {
Joe Drago8f439092020-08-28 15:05:17 -07001749 CHECK(uniqueBoxSeen(&uniqueBoxFlags, 0));
Joe Drago9f2b87b2020-06-03 19:36:38 -07001750 CHECK(avifParseItemLocationBox(meta, avifROStreamCurrent(&s), header.size));
Joe Dragof6a42272019-11-21 15:21:41 -08001751 } else if (!memcmp(header.type, "pitm", 4)) {
Joe Drago8f439092020-08-28 15:05:17 -07001752 CHECK(uniqueBoxSeen(&uniqueBoxFlags, 1));
Joe Drago9f2b87b2020-06-03 19:36:38 -07001753 CHECK(avifParsePrimaryItemBox(meta, avifROStreamCurrent(&s), header.size));
Joe Dragof6a42272019-11-21 15:21:41 -08001754 } else if (!memcmp(header.type, "idat", 4)) {
Joe Drago8f439092020-08-28 15:05:17 -07001755 CHECK(uniqueBoxSeen(&uniqueBoxFlags, 2));
Joe Drago9f2b87b2020-06-03 19:36:38 -07001756 CHECK(avifParseItemDataBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001757 } else if (!memcmp(header.type, "iprp", 4)) {
Joe Drago8f439092020-08-28 15:05:17 -07001758 CHECK(uniqueBoxSeen(&uniqueBoxFlags, 3));
Joe Drago9f2b87b2020-06-03 19:36:38 -07001759 CHECK(avifParseItemPropertiesBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001760 } else if (!memcmp(header.type, "iinf", 4)) {
Joe Drago8f439092020-08-28 15:05:17 -07001761 CHECK(uniqueBoxSeen(&uniqueBoxFlags, 4));
Joe Drago9f2b87b2020-06-03 19:36:38 -07001762 CHECK(avifParseItemInfoBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001763 } else if (!memcmp(header.type, "iref", 4)) {
Joe Drago8f439092020-08-28 15:05:17 -07001764 CHECK(uniqueBoxSeen(&uniqueBoxFlags, 5));
Joe Drago9f2b87b2020-06-03 19:36:38 -07001765 CHECK(avifParseItemReferenceBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001766 }
1767
Joe Drago345aaa12019-09-25 13:42:12 -07001768 CHECK(avifROStreamSkip(&s, header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001769 }
1770 return AVIF_TRUE;
1771}
1772
Joe Drago9f2b87b2020-06-03 19:36:38 -07001773static avifBool avifParseTrackHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001774{
1775 BEGIN_STREAM(s, raw, rawLen);
1776
1777 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -07001778 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001779
1780 uint32_t ignored32, trackID;
1781 uint64_t ignored64;
1782 if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001783 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time;
1784 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time;
1785 CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID;
Joe Dragofc4144e2019-09-27 20:35:06 -07001786 CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0;
1787 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001788 } else if (version == 0) {
Joe Drago345aaa12019-09-25 13:42:12 -07001789 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time;
1790 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time;
1791 CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID;
Joe Dragofc4144e2019-09-27 20:35:06 -07001792 CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0;
1793 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001794 } else {
1795 // Unsupported version
1796 return AVIF_FALSE;
1797 }
1798
Joe Dragofc4144e2019-09-27 20:35:06 -07001799 // Skipping the following 52 bytes here:
1800 // ------------------------------------
1801 // const unsigned int(32)[2] reserved = 0;
1802 // template int(16) layer = 0;
1803 // template int(16) alternate_group = 0;
1804 // template int(16) volume = {if track_is_audio 0x0100 else 0};
1805 // const unsigned int(16) reserved = 0;
1806 // template int(32)[9] matrix= { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 }; // unity matrix
1807 CHECK(avifROStreamSkip(&s, 52));
1808
1809 uint32_t width, height;
1810 CHECK(avifROStreamReadU32(&s, &width)); // unsigned int(32) width;
1811 CHECK(avifROStreamReadU32(&s, &height)); // unsigned int(32) height;
1812 track->width = width >> 16;
1813 track->height = height >> 16;
1814
Joe Dragoae7e2c32019-07-18 15:22:25 -07001815 // TODO: support scaling based on width/height track header info?
1816
1817 track->id = trackID;
1818 return AVIF_TRUE;
1819}
1820
Joe Drago9f2b87b2020-06-03 19:36:38 -07001821static avifBool avifParseMediaHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001822{
1823 BEGIN_STREAM(s, raw, rawLen);
1824
1825 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -07001826 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001827
1828 uint32_t ignored32, mediaTimescale, mediaDuration32;
1829 uint64_t ignored64, mediaDuration64;
1830 if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001831 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time;
1832 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time;
1833 CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale;
1834 CHECK(avifROStreamReadU64(&s, &mediaDuration64)); // unsigned int(64) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001835 track->mediaDuration = mediaDuration64;
1836 } else if (version == 0) {
Joe Drago345aaa12019-09-25 13:42:12 -07001837 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time;
1838 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time;
1839 CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale;
1840 CHECK(avifROStreamReadU32(&s, &mediaDuration32)); // unsigned int(32) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001841 track->mediaDuration = (uint64_t)mediaDuration32;
1842 } else {
1843 // Unsupported version
1844 return AVIF_FALSE;
1845 }
1846
1847 track->mediaTimescale = mediaTimescale;
1848 return AVIF_TRUE;
1849}
1850
Joe Drago9f2b87b2020-06-03 19:36:38 -07001851static avifBool avifParseChunkOffsetBox(avifSampleTable * sampleTable, avifBool largeOffsets, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001852{
1853 BEGIN_STREAM(s, raw, rawLen);
1854
Joe Drago345aaa12019-09-25 13:42:12 -07001855 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001856
1857 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001858 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001859 for (uint32_t i = 0; i < entryCount; ++i) {
1860 uint64_t offset;
1861 if (largeOffsets) {
Joe Drago345aaa12019-09-25 13:42:12 -07001862 CHECK(avifROStreamReadU64(&s, &offset)); // unsigned int(32) chunk_offset;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001863 } else {
1864 uint32_t offset32;
Joe Drago345aaa12019-09-25 13:42:12 -07001865 CHECK(avifROStreamReadU32(&s, &offset32)); // unsigned int(32) chunk_offset;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001866 offset = (uint64_t)offset32;
1867 }
1868
1869 avifSampleTableChunk * chunk = (avifSampleTableChunk *)avifArrayPushPtr(&sampleTable->chunks);
1870 chunk->offset = offset;
1871 }
1872 return AVIF_TRUE;
1873}
1874
Joe Drago9f2b87b2020-06-03 19:36:38 -07001875static avifBool avifParseSampleToChunkBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001876{
1877 BEGIN_STREAM(s, raw, rawLen);
1878
Joe Drago345aaa12019-09-25 13:42:12 -07001879 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001880
1881 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001882 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Drago859c9102021-03-11 18:06:40 -08001883 uint32_t prevFirstChunk = 0;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001884 for (uint32_t i = 0; i < entryCount; ++i) {
1885 avifSampleTableSampleToChunk * sampleToChunk = (avifSampleTableSampleToChunk *)avifArrayPushPtr(&sampleTable->sampleToChunks);
Joe Drago345aaa12019-09-25 13:42:12 -07001886 CHECK(avifROStreamReadU32(&s, &sampleToChunk->firstChunk)); // unsigned int(32) first_chunk;
1887 CHECK(avifROStreamReadU32(&s, &sampleToChunk->samplesPerChunk)); // unsigned int(32) samples_per_chunk;
1888 CHECK(avifROStreamReadU32(&s, &sampleToChunk->sampleDescriptionIndex)); // unsigned int(32) sample_description_index;
Wan-Teh Chang2456d2f2021-03-11 17:31:03 -08001889 // The first_chunk fields should start with 1 and be strictly increasing.
1890 if (i == 0) {
1891 if (sampleToChunk->firstChunk != 1) {
1892 return AVIF_FALSE;
1893 }
1894 } else {
1895 if (sampleToChunk->firstChunk <= prevFirstChunk) {
1896 return AVIF_FALSE;
1897 }
1898 }
1899 prevFirstChunk = sampleToChunk->firstChunk;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001900 }
1901 return AVIF_TRUE;
1902}
1903
Joe Drago9f2b87b2020-06-03 19:36:38 -07001904static avifBool avifParseSampleSizeBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001905{
1906 BEGIN_STREAM(s, raw, rawLen);
1907
Joe Drago345aaa12019-09-25 13:42:12 -07001908 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001909
Joe Drago370be3f2020-02-07 15:59:42 -08001910 uint32_t allSamplesSize, sampleCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001911 CHECK(avifROStreamReadU32(&s, &allSamplesSize)); // unsigned int(32) sample_size;
Joe Drago370be3f2020-02-07 15:59:42 -08001912 CHECK(avifROStreamReadU32(&s, &sampleCount)); // unsigned int(32) sample_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001913
Joe Drago370be3f2020-02-07 15:59:42 -08001914 if (allSamplesSize > 0) {
1915 sampleTable->allSamplesSize = allSamplesSize;
1916 } else {
1917 for (uint32_t i = 0; i < sampleCount; ++i) {
1918 avifSampleTableSampleSize * sampleSize = (avifSampleTableSampleSize *)avifArrayPushPtr(&sampleTable->sampleSizes);
Joe Drago345aaa12019-09-25 13:42:12 -07001919 CHECK(avifROStreamReadU32(&s, &sampleSize->size)); // unsigned int(32) entry_size;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001920 }
1921 }
1922 return AVIF_TRUE;
1923}
1924
Joe Drago9f2b87b2020-06-03 19:36:38 -07001925static avifBool avifParseSyncSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Drago22c1ad92019-09-26 12:46:50 -07001926{
1927 BEGIN_STREAM(s, raw, rawLen);
Joe Drago22c1ad92019-09-26 12:46:50 -07001928
1929 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
1930
1931 uint32_t entryCount;
1932 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
1933
1934 for (uint32_t i = 0; i < entryCount; ++i) {
1935 uint32_t sampleNumber = 0;
1936 CHECK(avifROStreamReadU32(&s, &sampleNumber)); // unsigned int(32) sample_number;
1937 avifSyncSample * syncSample = (avifSyncSample *)avifArrayPushPtr(&sampleTable->syncSamples);
1938 syncSample->sampleNumber = sampleNumber;
1939 }
1940 return AVIF_TRUE;
1941}
1942
Joe Drago9f2b87b2020-06-03 19:36:38 -07001943static avifBool avifParseTimeToSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001944{
1945 BEGIN_STREAM(s, raw, rawLen);
1946
Joe Drago345aaa12019-09-25 13:42:12 -07001947 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001948
1949 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001950 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001951
1952 for (uint32_t i = 0; i < entryCount; ++i) {
1953 avifSampleTableTimeToSample * timeToSample = (avifSampleTableTimeToSample *)avifArrayPushPtr(&sampleTable->timeToSamples);
Joe Drago345aaa12019-09-25 13:42:12 -07001954 CHECK(avifROStreamReadU32(&s, &timeToSample->sampleCount)); // unsigned int(32) sample_count;
1955 CHECK(avifROStreamReadU32(&s, &timeToSample->sampleDelta)); // unsigned int(32) sample_delta;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001956 }
1957 return AVIF_TRUE;
1958}
1959
Joe Drago9f2b87b2020-06-03 19:36:38 -07001960static avifBool avifParseSampleDescriptionBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Drago2c0924c2019-09-26 17:41:01 -07001961{
1962 BEGIN_STREAM(s, raw, rawLen);
Joe Drago2c0924c2019-09-26 17:41:01 -07001963
1964 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
1965
1966 uint32_t entryCount;
1967 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
1968
1969 for (uint32_t i = 0; i < entryCount; ++i) {
1970 avifBoxHeader sampleEntryHeader;
1971 CHECK(avifROStreamReadBoxHeader(&s, &sampleEntryHeader));
1972
1973 avifSampleDescription * description = (avifSampleDescription *)avifArrayPushPtr(&sampleTable->sampleDescriptions);
Joe Dragoa72da5b2020-06-15 19:40:17 -07001974 avifArrayCreate(&description->properties, sizeof(avifProperty), 16);
Joe Drago2c0924c2019-09-26 17:41:01 -07001975 memcpy(description->format, sampleEntryHeader.type, sizeof(description->format));
Joe Drago6500fd62019-10-08 17:17:34 -07001976 size_t remainingBytes = avifROStreamRemainingBytes(&s);
1977 if (!memcmp(description->format, "av01", 4) && (remainingBytes > VISUALSAMPLEENTRY_SIZE)) {
Joe Drago11d23592021-01-05 14:18:57 -08001978 CHECK(avifParseItemPropertyContainerBox(&description->properties,
1979 avifROStreamCurrent(&s) + VISUALSAMPLEENTRY_SIZE,
1980 remainingBytes - VISUALSAMPLEENTRY_SIZE));
Joe Drago6500fd62019-10-08 17:17:34 -07001981 }
Joe Drago2c0924c2019-09-26 17:41:01 -07001982
1983 CHECK(avifROStreamSkip(&s, sampleEntryHeader.size));
1984 }
1985 return AVIF_TRUE;
1986}
1987
Joe Drago9f2b87b2020-06-03 19:36:38 -07001988static avifBool avifParseSampleTableBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001989{
1990 if (track->sampleTable) {
1991 // A TrackBox may only have one SampleTable
1992 return AVIF_FALSE;
1993 }
1994 track->sampleTable = avifSampleTableCreate();
1995
1996 BEGIN_STREAM(s, raw, rawLen);
1997
Joe Drago345aaa12019-09-25 13:42:12 -07001998 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07001999 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07002000 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002001
2002 if (!memcmp(header.type, "stco", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002003 CHECK(avifParseChunkOffsetBox(track->sampleTable, AVIF_FALSE, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002004 } else if (!memcmp(header.type, "co64", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002005 CHECK(avifParseChunkOffsetBox(track->sampleTable, AVIF_TRUE, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002006 } else if (!memcmp(header.type, "stsc", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002007 CHECK(avifParseSampleToChunkBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002008 } else if (!memcmp(header.type, "stsz", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002009 CHECK(avifParseSampleSizeBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Drago22c1ad92019-09-26 12:46:50 -07002010 } else if (!memcmp(header.type, "stss", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002011 CHECK(avifParseSyncSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002012 } else if (!memcmp(header.type, "stts", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002013 CHECK(avifParseTimeToSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Drago2c0924c2019-09-26 17:41:01 -07002014 } else if (!memcmp(header.type, "stsd", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002015 CHECK(avifParseSampleDescriptionBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002016 }
2017
Joe Drago345aaa12019-09-25 13:42:12 -07002018 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002019 }
Joe Dragoae7e2c32019-07-18 15:22:25 -07002020 return AVIF_TRUE;
2021}
2022
Joe Drago9f2b87b2020-06-03 19:36:38 -07002023static avifBool avifParseMediaInformationBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07002024{
2025 BEGIN_STREAM(s, raw, rawLen);
2026
Joe Drago345aaa12019-09-25 13:42:12 -07002027 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07002028 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07002029 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002030
2031 if (!memcmp(header.type, "stbl", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002032 CHECK(avifParseSampleTableBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002033 }
2034
Joe Drago345aaa12019-09-25 13:42:12 -07002035 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002036 }
2037 return AVIF_TRUE;
2038}
2039
Joe Drago9f2b87b2020-06-03 19:36:38 -07002040static avifBool avifParseMediaBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07002041{
2042 BEGIN_STREAM(s, raw, rawLen);
2043
Joe Drago345aaa12019-09-25 13:42:12 -07002044 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07002045 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07002046 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002047
2048 if (!memcmp(header.type, "mdhd", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002049 CHECK(avifParseMediaHeaderBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002050 } else if (!memcmp(header.type, "minf", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002051 CHECK(avifParseMediaInformationBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002052 }
2053
Joe Drago345aaa12019-09-25 13:42:12 -07002054 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002055 }
2056 return AVIF_TRUE;
2057}
2058
Joe Drago9f2b87b2020-06-03 19:36:38 -07002059static avifBool avifTrackReferenceBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Drago46ea0582019-07-22 15:55:47 -07002060{
2061 BEGIN_STREAM(s, raw, rawLen);
2062
Joe Drago345aaa12019-09-25 13:42:12 -07002063 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago46ea0582019-07-22 15:55:47 -07002064 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07002065 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago46ea0582019-07-22 15:55:47 -07002066
2067 if (!memcmp(header.type, "auxl", 4)) {
2068 uint32_t toID;
Joe Dragoceb2fa02021-01-29 18:14:55 -08002069 CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) track_IDs[];
Joe Drago345aaa12019-09-25 13:42:12 -07002070 CHECK(avifROStreamSkip(&s, header.size - sizeof(uint32_t))); // just take the first one
Joe Drago46ea0582019-07-22 15:55:47 -07002071 track->auxForID = toID;
Yuan Tonge4850be2021-01-22 14:21:25 +08002072 } else if (!memcmp(header.type, "prem", 4)) {
2073 uint32_t byID;
Joe Dragoceb2fa02021-01-29 18:14:55 -08002074 CHECK(avifROStreamReadU32(&s, &byID)); // unsigned int(32) track_IDs[];
2075 CHECK(avifROStreamSkip(&s, header.size - sizeof(uint32_t))); // just take the first one
Yuan Tonge4850be2021-01-22 14:21:25 +08002076 track->premByID = byID;
Joe Drago46ea0582019-07-22 15:55:47 -07002077 } else {
Joe Drago345aaa12019-09-25 13:42:12 -07002078 CHECK(avifROStreamSkip(&s, header.size));
Joe Drago46ea0582019-07-22 15:55:47 -07002079 }
2080 }
2081 return AVIF_TRUE;
2082}
2083
Joe Drago800b47f2020-03-18 16:22:37 -07002084static avifBool avifParseTrackBox(avifDecoderData * data, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07002085{
2086 BEGIN_STREAM(s, raw, rawLen);
2087
Joe Dragoa72da5b2020-06-15 19:40:17 -07002088 avifTrack * track = avifDecoderDataCreateTrack(data);
Joe Dragoae7e2c32019-07-18 15:22:25 -07002089
Joe Drago345aaa12019-09-25 13:42:12 -07002090 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07002091 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07002092 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002093
2094 if (!memcmp(header.type, "tkhd", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002095 CHECK(avifParseTrackHeaderBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07002096 } else if (!memcmp(header.type, "meta", 4)) {
2097 CHECK(avifParseMetaBox(track->meta, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002098 } else if (!memcmp(header.type, "mdia", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002099 CHECK(avifParseMediaBox(track, avifROStreamCurrent(&s), header.size));
Joe Drago46ea0582019-07-22 15:55:47 -07002100 } else if (!memcmp(header.type, "tref", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002101 CHECK(avifTrackReferenceBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002102 }
2103
Joe Drago345aaa12019-09-25 13:42:12 -07002104 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002105 }
2106 return AVIF_TRUE;
2107}
2108
Joe Drago800b47f2020-03-18 16:22:37 -07002109static avifBool avifParseMoovBox(avifDecoderData * data, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07002110{
2111 BEGIN_STREAM(s, raw, rawLen);
2112
Joe Drago345aaa12019-09-25 13:42:12 -07002113 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07002114 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07002115 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002116
2117 if (!memcmp(header.type, "trak", 4)) {
Joe Drago345aaa12019-09-25 13:42:12 -07002118 CHECK(avifParseTrackBox(data, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002119 }
2120
Joe Drago345aaa12019-09-25 13:42:12 -07002121 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002122 }
2123 return AVIF_TRUE;
2124}
2125
Joe Drago345aaa12019-09-25 13:42:12 -07002126static avifBool avifParseFileTypeBox(avifFileType * ftyp, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08002127{
2128 BEGIN_STREAM(s, raw, rawLen);
2129
Joe Drago345aaa12019-09-25 13:42:12 -07002130 CHECK(avifROStreamRead(&s, ftyp->majorBrand, 4));
2131 CHECK(avifROStreamReadU32(&s, &ftyp->minorVersion));
Joe Drago8f7a3002019-02-07 19:35:37 -08002132
Joe Drago345aaa12019-09-25 13:42:12 -07002133 size_t compatibleBrandsBytes = avifROStreamRemainingBytes(&s);
Joe Drago8f7a3002019-02-07 19:35:37 -08002134 if ((compatibleBrandsBytes % 4) != 0) {
2135 return AVIF_FALSE;
2136 }
Wan-Teh Chang6da0a882020-07-01 12:19:31 -07002137 ftyp->compatibleBrands = avifROStreamCurrent(&s);
2138 CHECK(avifROStreamSkip(&s, compatibleBrandsBytes));
Joe Drago7e37b972019-07-24 12:44:47 -07002139 ftyp->compatibleBrandsCount = (int)compatibleBrandsBytes / 4;
Joe Drago8f7a3002019-02-07 19:35:37 -08002140
2141 return AVIF_TRUE;
2142}
2143
Joe Dragobb39aab2020-11-03 19:23:40 -08002144static avifBool avifFileTypeHasBrand(avifFileType * ftyp, const char * brand);
Wan-Teh Chang6fc17582020-09-24 15:16:37 -07002145static avifBool avifFileTypeIsCompatible(avifFileType * ftyp);
2146
Joe Dragobe4cbb92020-09-21 12:14:05 -07002147static avifResult avifParse(avifDecoder * decoder)
Joe Drago8f7a3002019-02-07 19:35:37 -08002148{
Joe Drago9aa931f2020-09-24 13:10:11 -07002149 // Note: this top-level function is the only avifParse*() function that returns avifResult instead of avifBool.
2150 // Be sure to use CHECKERR() in this function with an explicit error result instead of simply using CHECK().
2151
Joe Dragobe4cbb92020-09-21 12:14:05 -07002152 avifResult readResult;
Wan-Teh Chang3b8b81c2020-09-28 10:35:08 -07002153 uint64_t parseOffset = 0;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002154 avifDecoderData * data = decoder->data;
Joe Dragobb39aab2020-11-03 19:23:40 -08002155 avifBool ftypSeen = AVIF_FALSE;
2156 avifBool metaSeen = AVIF_FALSE;
2157 avifBool moovSeen = AVIF_FALSE;
2158 avifBool needsMeta = AVIF_FALSE;
2159 avifBool needsMoov = AVIF_FALSE;
Joe Drago8f7a3002019-02-07 19:35:37 -08002160
Joe Dragobe4cbb92020-09-21 12:14:05 -07002161 for (;;) {
2162 // Read just enough to get the next box header (a max of 32 bytes)
2163 avifROData headerContents;
Wan-Teh Changb856dc22020-09-28 13:00:56 -07002164 if ((decoder->io->sizeHint > 0) && (parseOffset > decoder->io->sizeHint)) {
2165 return AVIF_RESULT_BMFF_PARSE_FAILED;
2166 }
Joe Dragobe4cbb92020-09-21 12:14:05 -07002167 readResult = decoder->io->read(decoder->io, 0, parseOffset, 32, &headerContents);
2168 if (readResult != AVIF_RESULT_OK) {
2169 return readResult;
2170 }
2171 if (!headerContents.size) {
2172 // If we got AVIF_RESULT_OK from the reader but received 0 bytes,
Wan-Teh Chang277d0d72020-10-08 10:24:41 -07002173 // we've reached the end of the file with no errors. Hooray!
Joe Dragobe4cbb92020-09-21 12:14:05 -07002174 break;
Joe Drago8f7a3002019-02-07 19:35:37 -08002175 }
2176
Joe Dragobe4cbb92020-09-21 12:14:05 -07002177 // Parse the header, and find out how many bytes it actually was
2178 BEGIN_STREAM(headerStream, headerContents.data, headerContents.size);
2179 avifBoxHeader header;
Joe Drago468ded82020-09-24 12:52:51 -07002180 CHECKERR(avifROStreamReadBoxHeaderPartial(&headerStream, &header), AVIF_RESULT_BMFF_PARSE_FAILED);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002181 parseOffset += headerStream.offset;
Wan-Teh Changb856dc22020-09-28 13:00:56 -07002182 assert((decoder->io->sizeHint == 0) || (parseOffset <= decoder->io->sizeHint));
Joe Dragobe4cbb92020-09-21 12:14:05 -07002183
2184 // Try to get the remainder of the box, if necessary
2185 avifROData boxContents = AVIF_DATA_EMPTY;
2186
2187 // TODO: reorg this code to only do these memcmps once each
Wan-Teh Changb309e982021-02-20 16:59:36 -08002188 if (!memcmp(header.type, "ftyp", 4) || !memcmp(header.type, "meta", 4) || !memcmp(header.type, "moov", 4)) {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002189 readResult = decoder->io->read(decoder->io, 0, parseOffset, header.size, &boxContents);
2190 if (readResult != AVIF_RESULT_OK) {
2191 return readResult;
2192 }
2193 if (boxContents.size != header.size) {
2194 // A truncated box, bail out
Joe Dragofe5d5e42020-09-24 13:07:58 -07002195 return AVIF_RESULT_TRUNCATED_DATA;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002196 }
Wan-Teh Chang3b8b81c2020-09-28 10:35:08 -07002197 } else if (header.size > (UINT64_MAX - parseOffset)) {
2198 return AVIF_RESULT_BMFF_PARSE_FAILED;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002199 }
Wan-Teh Chang3b8b81c2020-09-28 10:35:08 -07002200 parseOffset += header.size;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002201
2202 if (!memcmp(header.type, "ftyp", 4)) {
Joe Dragobb39aab2020-11-03 19:23:40 -08002203 CHECKERR(!ftypSeen, AVIF_RESULT_BMFF_PARSE_FAILED);
Wan-Teh Chang6fc17582020-09-24 15:16:37 -07002204 avifFileType ftyp;
2205 CHECKERR(avifParseFileTypeBox(&ftyp, boxContents.data, boxContents.size), AVIF_RESULT_BMFF_PARSE_FAILED);
Joe Dragobb39aab2020-11-03 19:23:40 -08002206 if (!avifFileTypeIsCompatible(&ftyp)) {
Wan-Teh Chang6fc17582020-09-24 15:16:37 -07002207 return AVIF_RESULT_INVALID_FTYP;
2208 }
Joe Dragobb39aab2020-11-03 19:23:40 -08002209 ftypSeen = AVIF_TRUE;
2210 needsMeta = avifFileTypeHasBrand(&ftyp, "avif");
2211 needsMoov = avifFileTypeHasBrand(&ftyp, "avis");
Joe Dragobe4cbb92020-09-21 12:14:05 -07002212 } else if (!memcmp(header.type, "meta", 4)) {
Joe Dragobb39aab2020-11-03 19:23:40 -08002213 CHECKERR(!metaSeen, AVIF_RESULT_BMFF_PARSE_FAILED);
Joe Drago468ded82020-09-24 12:52:51 -07002214 CHECKERR(avifParseMetaBox(data->meta, boxContents.data, boxContents.size), AVIF_RESULT_BMFF_PARSE_FAILED);
Joe Dragobb39aab2020-11-03 19:23:40 -08002215 metaSeen = AVIF_TRUE;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002216 } else if (!memcmp(header.type, "moov", 4)) {
Joe Dragobb39aab2020-11-03 19:23:40 -08002217 CHECKERR(!moovSeen, AVIF_RESULT_BMFF_PARSE_FAILED);
Joe Drago468ded82020-09-24 12:52:51 -07002218 CHECKERR(avifParseMoovBox(data, boxContents.data, boxContents.size), AVIF_RESULT_BMFF_PARSE_FAILED);
Joe Dragobb39aab2020-11-03 19:23:40 -08002219 moovSeen = AVIF_TRUE;
2220 }
2221
2222 // See if there is enough information to consider Parse() a success and early-out:
2223 // * If the brand 'avif' is present, require a meta box
2224 // * If the brand 'avis' is present, require a moov box
2225 if (ftypSeen && (!needsMeta || metaSeen) && (!needsMoov || moovSeen)) {
2226 return AVIF_RESULT_OK;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002227 }
Joe Drago8f7a3002019-02-07 19:35:37 -08002228 }
Joe Dragobe4cbb92020-09-21 12:14:05 -07002229 return AVIF_RESULT_OK;
Joe Drago8f7a3002019-02-07 19:35:37 -08002230}
2231
2232// ---------------------------------------------------------------------------
Joe Drago8f7a3002019-02-07 19:35:37 -08002233
Joe Dragobb39aab2020-11-03 19:23:40 -08002234static avifBool avifFileTypeHasBrand(avifFileType * ftyp, const char * brand)
Joe Drago7e37b972019-07-24 12:44:47 -07002235{
Joe Dragobb39aab2020-11-03 19:23:40 -08002236 if (!memcmp(ftyp->majorBrand, brand, 4)) {
2237 return AVIF_TRUE;
2238 }
2239
2240 for (int compatibleBrandIndex = 0; compatibleBrandIndex < ftyp->compatibleBrandsCount; ++compatibleBrandIndex) {
2241 const uint8_t * compatibleBrand = &ftyp->compatibleBrands[4 * compatibleBrandIndex];
2242 if (!memcmp(compatibleBrand, brand, 4)) {
2243 return AVIF_TRUE;
Joe Drago7e37b972019-07-24 12:44:47 -07002244 }
2245 }
Joe Dragobb39aab2020-11-03 19:23:40 -08002246 return AVIF_FALSE;
2247}
2248
2249static avifBool avifFileTypeIsCompatible(avifFileType * ftyp)
2250{
2251 return avifFileTypeHasBrand(ftyp, "avif") || avifFileTypeHasBrand(ftyp, "avis");
Joe Drago7e37b972019-07-24 12:44:47 -07002252}
2253
Wan-Teh Change184dc12020-05-11 12:47:21 -07002254avifBool avifPeekCompatibleFileType(const avifROData * input)
Joe Drago7e37b972019-07-24 12:44:47 -07002255{
2256 BEGIN_STREAM(s, input->data, input->size);
2257
2258 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07002259 CHECK(avifROStreamReadBoxHeader(&s, &header));
wantehchangbc35a5f2020-08-12 15:27:39 -07002260 if (memcmp(header.type, "ftyp", 4)) {
Joe Drago7e37b972019-07-24 12:44:47 -07002261 return AVIF_FALSE;
2262 }
2263
2264 avifFileType ftyp;
2265 memset(&ftyp, 0, sizeof(avifFileType));
Joe Drago345aaa12019-09-25 13:42:12 -07002266 avifBool parsed = avifParseFileTypeBox(&ftyp, avifROStreamCurrent(&s), header.size);
Joe Drago7e37b972019-07-24 12:44:47 -07002267 if (!parsed) {
2268 return AVIF_FALSE;
2269 }
2270 return avifFileTypeIsCompatible(&ftyp);
2271}
2272
2273// ---------------------------------------------------------------------------
2274
Joe Drago0b05eee2019-06-12 13:24:39 -07002275avifDecoder * avifDecoderCreate(void)
2276{
2277 avifDecoder * decoder = (avifDecoder *)avifAlloc(sizeof(avifDecoder));
2278 memset(decoder, 0, sizeof(avifDecoder));
Joe Dragoede5c202020-11-11 09:42:57 -08002279 decoder->maxThreads = 1;
Joe Dragod2340b42021-03-14 13:20:02 -07002280 decoder->imageCountLimit = AVIF_DEFAULT_IMAGE_COUNT_LIMIT;
Joe Drago0b05eee2019-06-12 13:24:39 -07002281 return decoder;
2282}
2283
Joe Drago46ea0582019-07-22 15:55:47 -07002284static void avifDecoderCleanup(avifDecoder * decoder)
2285{
2286 if (decoder->data) {
Joe Drago800b47f2020-03-18 16:22:37 -07002287 avifDecoderDataDestroy(decoder->data);
Joe Drago46ea0582019-07-22 15:55:47 -07002288 decoder->data = NULL;
2289 }
2290
2291 if (decoder->image) {
2292 avifImageDestroy(decoder->image);
2293 decoder->image = NULL;
2294 }
2295}
2296
Joe Drago0b05eee2019-06-12 13:24:39 -07002297void avifDecoderDestroy(avifDecoder * decoder)
2298{
Joe Drago46ea0582019-07-22 15:55:47 -07002299 avifDecoderCleanup(decoder);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002300 avifIODestroy(decoder->io);
Joe Drago0b05eee2019-06-12 13:24:39 -07002301 avifFree(decoder);
2302}
2303
Joe Drago46ea0582019-07-22 15:55:47 -07002304avifResult avifDecoderSetSource(avifDecoder * decoder, avifDecoderSource source)
Joe Drago444f0512019-01-23 17:03:24 -08002305{
Joe Drago46ea0582019-07-22 15:55:47 -07002306 decoder->requestedSource = source;
2307 return avifDecoderReset(decoder);
2308}
Joe Drago33f1d362019-02-13 16:46:22 -08002309
Wan-Teh Change67f9362020-10-12 16:07:57 -07002310void avifDecoderSetIO(avifDecoder * decoder, avifIO * io)
Joe Drago46ea0582019-07-22 15:55:47 -07002311{
Joe Dragobe4cbb92020-09-21 12:14:05 -07002312 avifIODestroy(decoder->io);
2313 decoder->io = io;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002314}
2315
Wan-Teh Chang7de44452020-10-08 11:43:18 -07002316avifResult avifDecoderSetIOMemory(avifDecoder * decoder, const uint8_t * data, size_t size)
Joe Dragobe4cbb92020-09-21 12:14:05 -07002317{
Wan-Teh Chang7de44452020-10-08 11:43:18 -07002318 avifIO * io = avifIOCreateMemoryReader(data, size);
Wan-Teh Chang31c7c1a2020-10-13 16:45:41 -07002319 assert(io);
Wan-Teh Change67f9362020-10-12 16:07:57 -07002320 avifDecoderSetIO(decoder, io);
2321 return AVIF_RESULT_OK;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002322}
2323
Joe Dragobe4cbb92020-09-21 12:14:05 -07002324avifResult avifDecoderSetIOFile(avifDecoder * decoder, const char * filename)
2325{
Wan-Teh Chang7de44452020-10-08 11:43:18 -07002326 avifIO * io = avifIOCreateFileReader(filename);
2327 if (!io) {
Wan-Teh Chang31c7c1a2020-10-13 16:45:41 -07002328 return AVIF_RESULT_IO_ERROR;
Wan-Teh Chang7de44452020-10-08 11:43:18 -07002329 }
Wan-Teh Change67f9362020-10-12 16:07:57 -07002330 avifDecoderSetIO(decoder, io);
2331 return AVIF_RESULT_OK;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002332}
2333
Joe Drago4bcdfde2020-11-13 17:50:55 -08002334// 0-byte extents are ignored/overwritten during the merge, as they are the signal from helper
2335// functions that no extent was necessary for this given sample. If both provided extents are
2336// >0 bytes, this will set dst to be an extent that bounds both supplied extents.
Wan-Teh Changd69958e2020-11-17 12:14:27 -08002337static avifResult avifExtentMerge(avifExtent * dst, const avifExtent * src)
Joe Drago4bcdfde2020-11-13 17:50:55 -08002338{
2339 if (!dst->size) {
2340 memcpy(dst, src, sizeof(avifExtent));
Wan-Teh Changd69958e2020-11-17 12:14:27 -08002341 return AVIF_RESULT_OK;
Joe Drago4bcdfde2020-11-13 17:50:55 -08002342 }
2343 if (!src->size) {
Wan-Teh Changd69958e2020-11-17 12:14:27 -08002344 return AVIF_RESULT_OK;
Joe Drago4bcdfde2020-11-13 17:50:55 -08002345 }
2346
2347 const uint64_t minExtent1 = dst->offset;
2348 const uint64_t maxExtent1 = dst->offset + dst->size;
2349 const uint64_t minExtent2 = src->offset;
2350 const uint64_t maxExtent2 = src->offset + src->size;
2351 dst->offset = AVIF_MIN(minExtent1, minExtent2);
Wan-Teh Changd69958e2020-11-17 12:14:27 -08002352 const uint64_t extentLength = AVIF_MAX(maxExtent1, maxExtent2) - dst->offset;
2353 if (extentLength > SIZE_MAX) {
2354 return AVIF_RESULT_BMFF_PARSE_FAILED;
2355 }
2356 dst->size = (size_t)extentLength;
2357 return AVIF_RESULT_OK;
Joe Drago4bcdfde2020-11-13 17:50:55 -08002358}
2359
Joe Drago93d5bf92020-11-17 14:31:57 -08002360avifResult avifDecoderNthImageMaxExtent(const avifDecoder * decoder, uint32_t frameIndex, avifExtent * outExtent)
Joe Drago4bcdfde2020-11-13 17:50:55 -08002361{
2362 if (!decoder->data) {
2363 // Nothing has been parsed yet
2364 return AVIF_RESULT_NO_CONTENT;
2365 }
2366
2367 memset(outExtent, 0, sizeof(avifExtent));
2368
Joe Drago93d5bf92020-11-17 14:31:57 -08002369 uint32_t startFrameIndex = avifDecoderNearestKeyframe(decoder, frameIndex);
Joe Drago4bcdfde2020-11-13 17:50:55 -08002370 uint32_t endFrameIndex = frameIndex;
2371 for (uint32_t currentFrameIndex = startFrameIndex; currentFrameIndex <= endFrameIndex; ++currentFrameIndex) {
2372 for (unsigned int tileIndex = 0; tileIndex < decoder->data->tiles.count; ++tileIndex) {
2373 avifTile * tile = &decoder->data->tiles.tile[tileIndex];
2374 if (currentFrameIndex >= tile->input->samples.count) {
2375 return AVIF_RESULT_NO_IMAGES_REMAINING;
2376 }
2377
2378 avifDecodeSample * sample = &tile->input->samples.sample[currentFrameIndex];
2379 avifExtent sampleExtent;
2380 if (sample->itemID) {
2381 // The data comes from an item. Let avifDecoderItemMaxExtent() do the heavy lifting.
2382
2383 avifDecoderItem * item = avifMetaFindItem(decoder->data->meta, sample->itemID);
2384 avifResult maxExtentResult = avifDecoderItemMaxExtent(item, &sampleExtent);
2385 if (maxExtentResult != AVIF_RESULT_OK) {
2386 return maxExtentResult;
2387 }
2388 } else {
2389 // The data likely comes from a sample table. Use the sample position directly.
2390
2391 sampleExtent.offset = sample->offset;
2392 sampleExtent.size = sample->size;
2393 }
2394
2395 if (sampleExtent.size > UINT64_MAX - sampleExtent.offset) {
2396 return AVIF_RESULT_BMFF_PARSE_FAILED;
2397 }
2398
Wan-Teh Changd69958e2020-11-17 12:14:27 -08002399 avifResult extentMergeResult = avifExtentMerge(outExtent, &sampleExtent);
2400 if (extentMergeResult != AVIF_RESULT_OK) {
2401 return extentMergeResult;
2402 }
Joe Drago4bcdfde2020-11-13 17:50:55 -08002403 }
2404 }
2405 return AVIF_RESULT_OK;
2406}
2407
Joe Dragobe4cbb92020-09-21 12:14:05 -07002408static avifResult avifDecoderPrepareSample(avifDecoder * decoder, avifDecodeSample * sample, size_t partialByteCount)
2409{
2410 if (!sample->data.size || sample->partialData) {
2411 // This sample hasn't been read from IO or had its extents fully merged yet.
2412
2413 if (sample->itemID) {
Wan-Teh Chang4548b162020-11-06 11:48:25 -08002414 // The data comes from an item. Let avifDecoderItemRead() do the heavy lifting.
Joe Dragobe4cbb92020-09-21 12:14:05 -07002415
2416 avifDecoderItem * item = avifMetaFindItem(decoder->data->meta, sample->itemID);
2417 avifROData itemContents;
Wan-Teh Chang4548b162020-11-06 11:48:25 -08002418 avifResult readResult = avifDecoderItemRead(item, decoder->io, &itemContents, partialByteCount);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002419 if (readResult != AVIF_RESULT_OK) {
2420 return readResult;
2421 }
2422
Wan-Teh Chang4548b162020-11-06 11:48:25 -08002423 // avifDecoderItemRead is guaranteed to already be persisted by either the underlying IO
Wan-Teh Chang277d0d72020-10-08 10:24:41 -07002424 // or by mergedExtents; just reuse the buffer here.
Joe Dragobe4cbb92020-09-21 12:14:05 -07002425 memcpy(&sample->data, &itemContents, sizeof(avifROData));
2426 sample->ownsData = AVIF_FALSE;
2427 sample->partialData = item->partialMergedExtents;
2428 } else {
2429 // The data likely comes from a sample table. Pull the sample and make a copy if necessary.
2430
2431 size_t bytesToRead = sample->size;
2432 if (partialByteCount && (bytesToRead > partialByteCount)) {
2433 bytesToRead = partialByteCount;
2434 }
2435
2436 avifROData sampleContents;
Wan-Teh Changb856dc22020-09-28 13:00:56 -07002437 if ((decoder->io->sizeHint > 0) && (sample->offset > decoder->io->sizeHint)) {
2438 return AVIF_RESULT_BMFF_PARSE_FAILED;
2439 }
Joe Dragobe4cbb92020-09-21 12:14:05 -07002440 avifResult readResult = decoder->io->read(decoder->io, 0, sample->offset, bytesToRead, &sampleContents);
2441 if (readResult != AVIF_RESULT_OK) {
2442 return readResult;
2443 }
2444 if (sampleContents.size != bytesToRead) {
2445 return AVIF_RESULT_TRUNCATED_DATA;
2446 }
2447
2448 sample->ownsData = !decoder->io->persistent;
2449 sample->partialData = (bytesToRead != sample->size);
2450 if (decoder->io->persistent) {
2451 memcpy(&sample->data, &sampleContents, sizeof(avifROData));
2452 } else {
2453 avifRWDataSet((avifRWData *)&sample->data, sampleContents.data, sampleContents.size);
2454 }
2455 }
2456 }
2457 return AVIF_RESULT_OK;
2458}
2459
2460avifResult avifDecoderParse(avifDecoder * decoder)
2461{
2462 if (!decoder->io || !decoder->io->read) {
Wan-Teh Chang31c7c1a2020-10-13 16:45:41 -07002463 return AVIF_RESULT_IO_NOT_SET;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002464 }
2465
Joe Drago46ea0582019-07-22 15:55:47 -07002466 // Cleanup anything lingering in the decoder
2467 avifDecoderCleanup(decoder);
2468
Joe Drago444f0512019-01-23 17:03:24 -08002469 // -----------------------------------------------------------------------
2470 // Parse BMFF boxes
2471
Joe Drago800b47f2020-03-18 16:22:37 -07002472 decoder->data = avifDecoderDataCreate();
Joe Drago46ea0582019-07-22 15:55:47 -07002473
Joe Dragobe4cbb92020-09-21 12:14:05 -07002474 avifResult parseResult = avifParse(decoder);
2475 if (parseResult != AVIF_RESULT_OK) {
2476 return parseResult;
Joe Drago444f0512019-01-23 17:03:24 -08002477 }
2478
Joe Drago46ea0582019-07-22 15:55:47 -07002479 return avifDecoderReset(decoder);
2480}
2481
Joe Dragobe4cbb92020-09-21 12:14:05 -07002482static avifCodec * avifCodecCreateInternal(avifCodecChoice choice)
Joe Drago46ea0582019-07-22 15:55:47 -07002483{
Joe Dragobe4cbb92020-09-21 12:14:05 -07002484 return avifCodecCreate(choice, AVIF_CODEC_FLAG_CAN_DECODE);
Joe Drago46ea0582019-07-22 15:55:47 -07002485}
2486
Joe Drago22c1ad92019-09-26 12:46:50 -07002487static avifResult avifDecoderFlush(avifDecoder * decoder)
2488{
Joe Drago800b47f2020-03-18 16:22:37 -07002489 avifDecoderDataResetCodec(decoder->data);
Joe Drago22c1ad92019-09-26 12:46:50 -07002490
Joe Drago060d5342020-03-03 10:53:49 -08002491 for (unsigned int i = 0; i < decoder->data->tiles.count; ++i) {
2492 avifTile * tile = &decoder->data->tiles.tile[i];
Joe Dragobe4cbb92020-09-21 12:14:05 -07002493 tile->codec = avifCodecCreateInternal(decoder->codecChoice);
Joe Drago060d5342020-03-03 10:53:49 -08002494 if (!tile->codec) {
Joe Drago53355352019-10-28 19:04:51 -07002495 return AVIF_RESULT_NO_CODEC_AVAILABLE;
2496 }
Joe Dragoede5c202020-11-11 09:42:57 -08002497 if (!tile->codec->open(tile->codec, decoder)) {
Joe Drago060d5342020-03-03 10:53:49 -08002498 return AVIF_RESULT_DECODE_COLOR_FAILED;
Joe Drago22c1ad92019-09-26 12:46:50 -07002499 }
2500 }
2501 return AVIF_RESULT_OK;
2502}
2503
Joe Drago46ea0582019-07-22 15:55:47 -07002504avifResult avifDecoderReset(avifDecoder * decoder)
2505{
Joe Drago800b47f2020-03-18 16:22:37 -07002506 avifDecoderData * data = decoder->data;
Joe Drago46ea0582019-07-22 15:55:47 -07002507 if (!data) {
2508 // Nothing to reset.
2509 return AVIF_RESULT_OK;
2510 }
2511
Joe Drago060d5342020-03-03 10:53:49 -08002512 memset(&data->colorGrid, 0, sizeof(data->colorGrid));
2513 memset(&data->alphaGrid, 0, sizeof(data->alphaGrid));
Joe Drago800b47f2020-03-18 16:22:37 -07002514 avifDecoderDataClearTiles(data);
Joe Drago89f0cc82020-03-09 16:13:27 -07002515
2516 // Prepare / cleanup decoded image state
Joe Dragoa0da4a42020-05-08 14:27:40 -07002517 if (decoder->image) {
2518 avifImageDestroy(decoder->image);
Joe Drago8f7a3002019-02-07 19:35:37 -08002519 }
Joe Dragoa0da4a42020-05-08 14:27:40 -07002520 decoder->image = avifImageCreateEmpty();
2521 data->cicpSet = AVIF_FALSE;
Joe Drago8f7a3002019-02-07 19:35:37 -08002522
Joe Drago70cbf602019-07-24 15:30:55 -07002523 memset(&decoder->ioStats, 0, sizeof(decoder->ioStats));
2524
Joe Drago444f0512019-01-23 17:03:24 -08002525 // -----------------------------------------------------------------------
Joe Drago46ea0582019-07-22 15:55:47 -07002526 // Build decode input
Joe Drago444f0512019-01-23 17:03:24 -08002527
Joe Drago46ea0582019-07-22 15:55:47 -07002528 data->sourceSampleTable = NULL; // Reset
2529 if (decoder->requestedSource == AVIF_DECODER_SOURCE_AUTO) {
2530 if (data->tracks.count > 0) {
2531 data->source = AVIF_DECODER_SOURCE_TRACKS;
2532 } else {
2533 data->source = AVIF_DECODER_SOURCE_PRIMARY_ITEM;
Joe Drago76370232019-07-16 11:00:52 -07002534 }
Joe Drago46ea0582019-07-22 15:55:47 -07002535 } else {
2536 data->source = decoder->requestedSource;
Joe Drago76370232019-07-16 11:00:52 -07002537 }
2538
Joe Dragoa72da5b2020-06-15 19:40:17 -07002539 const avifPropertyArray * colorProperties = NULL;
Joe Drago46ea0582019-07-22 15:55:47 -07002540 if (data->source == AVIF_DECODER_SOURCE_TRACKS) {
2541 avifTrack * colorTrack = NULL;
2542 avifTrack * alphaTrack = NULL;
2543
2544 // Find primary track - this probably needs some better detection
2545 uint32_t colorTrackIndex = 0;
wantehchangb207b4d2020-08-11 17:50:22 -07002546 for (; colorTrackIndex < data->tracks.count; ++colorTrackIndex) {
2547 avifTrack * track = &data->tracks.track[colorTrackIndex];
Joe Drago46ea0582019-07-22 15:55:47 -07002548 if (!track->sampleTable) {
2549 continue;
2550 }
Joe Dragoba1eb492020-06-22 17:05:04 -07002551 if (!track->id) { // trak box might be missing a tkhd box inside, skip it
Joe Drago4a25c192020-06-03 16:29:58 -07002552 continue;
2553 }
Joe Drago46ea0582019-07-22 15:55:47 -07002554 if (!track->sampleTable->chunks.count) {
2555 continue;
2556 }
Joe Drago2c0924c2019-09-26 17:41:01 -07002557 if (!avifSampleTableHasFormat(track->sampleTable, "av01")) {
2558 continue;
2559 }
Joe Drago46ea0582019-07-22 15:55:47 -07002560 if (track->auxForID != 0) {
2561 continue;
2562 }
2563
2564 // Found one!
Joe Drago444f0512019-01-23 17:03:24 -08002565 break;
2566 }
wantehchangb207b4d2020-08-11 17:50:22 -07002567 if (colorTrackIndex == data->tracks.count) {
Joe Drago46ea0582019-07-22 15:55:47 -07002568 return AVIF_RESULT_NO_CONTENT;
Joe Drago444f0512019-01-23 17:03:24 -08002569 }
wantehchangb207b4d2020-08-11 17:50:22 -07002570 colorTrack = &data->tracks.track[colorTrackIndex];
Joe Drago46ea0582019-07-22 15:55:47 -07002571
Joe Dragoa72da5b2020-06-15 19:40:17 -07002572 colorProperties = avifSampleTableGetProperties(colorTrack->sampleTable);
2573 if (!colorProperties) {
2574 return AVIF_RESULT_BMFF_PARSE_FAILED;
2575 }
2576
2577 // Find Exif and/or XMP metadata, if any
2578 if (colorTrack->meta) {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002579 // See the comment above avifDecoderFindMetadata() for the explanation of using 0 here
2580 avifResult findResult = avifDecoderFindMetadata(decoder, colorTrack->meta, decoder->image, 0);
2581 if (findResult != AVIF_RESULT_OK) {
2582 return findResult;
Joe Dragoa72da5b2020-06-15 19:40:17 -07002583 }
2584 }
2585
Joe Drago46ea0582019-07-22 15:55:47 -07002586 uint32_t alphaTrackIndex = 0;
wantehchangb207b4d2020-08-11 17:50:22 -07002587 for (; alphaTrackIndex < data->tracks.count; ++alphaTrackIndex) {
2588 avifTrack * track = &data->tracks.track[alphaTrackIndex];
Joe Drago46ea0582019-07-22 15:55:47 -07002589 if (!track->sampleTable) {
2590 continue;
2591 }
Joe Drago4a25c192020-06-03 16:29:58 -07002592 if (!track->id) {
2593 continue;
2594 }
Joe Drago46ea0582019-07-22 15:55:47 -07002595 if (!track->sampleTable->chunks.count) {
2596 continue;
2597 }
Joe Drago2c0924c2019-09-26 17:41:01 -07002598 if (!avifSampleTableHasFormat(track->sampleTable, "av01")) {
2599 continue;
2600 }
Joe Drago46ea0582019-07-22 15:55:47 -07002601 if (track->auxForID == colorTrack->id) {
2602 // Found it!
2603 break;
2604 }
2605 }
wantehchangb207b4d2020-08-11 17:50:22 -07002606 if (alphaTrackIndex != data->tracks.count) {
2607 alphaTrack = &data->tracks.track[alphaTrackIndex];
Joe Drago8f7a3002019-02-07 19:35:37 -08002608 }
Joe Drago444f0512019-01-23 17:03:24 -08002609
wantehchangb207b4d2020-08-11 17:50:22 -07002610 avifTile * colorTile = avifDecoderDataCreateTile(data);
Joe Dragod2340b42021-03-14 13:20:02 -07002611 if (!avifCodecDecodeInputGetSamples(colorTile->input, colorTrack->sampleTable, decoder->imageCountLimit, decoder->io->sizeHint)) {
Joe Drago46ea0582019-07-22 15:55:47 -07002612 return AVIF_RESULT_BMFF_PARSE_FAILED;
2613 }
wantehchangb207b4d2020-08-11 17:50:22 -07002614 data->colorTileCount = 1;
Joe Drago46ea0582019-07-22 15:55:47 -07002615
2616 if (alphaTrack) {
wantehchang76e16bf2020-08-12 13:01:31 -07002617 avifTile * alphaTile = avifDecoderDataCreateTile(data);
Joe Dragod2340b42021-03-14 13:20:02 -07002618 if (!avifCodecDecodeInputGetSamples(alphaTile->input, alphaTrack->sampleTable, decoder->imageCountLimit, decoder->io->sizeHint)) {
Joe Drago46ea0582019-07-22 15:55:47 -07002619 return AVIF_RESULT_BMFF_PARSE_FAILED;
2620 }
Joe Drago060d5342020-03-03 10:53:49 -08002621 alphaTile->input->alpha = AVIF_TRUE;
wantehchangb207b4d2020-08-11 17:50:22 -07002622 data->alphaTileCount = 1;
Joe Drago46ea0582019-07-22 15:55:47 -07002623 }
2624
2625 // Stash off sample table for future timing information
2626 data->sourceSampleTable = colorTrack->sampleTable;
2627
2628 // Image sequence timing
2629 decoder->imageIndex = -1;
Joe Drago060d5342020-03-03 10:53:49 -08002630 decoder->imageCount = colorTile->input->samples.count;
Joe Drago46ea0582019-07-22 15:55:47 -07002631 decoder->timescale = colorTrack->mediaTimescale;
2632 decoder->durationInTimescales = colorTrack->mediaDuration;
2633 if (colorTrack->mediaTimescale) {
2634 decoder->duration = (double)decoder->durationInTimescales / (double)colorTrack->mediaTimescale;
2635 } else {
2636 decoder->duration = 0;
2637 }
2638 memset(&decoder->imageTiming, 0, sizeof(decoder->imageTiming)); // to be set in avifDecoderNextImage()
Joe Drago41700852019-09-26 17:01:43 -07002639
Joe Dragoc554f5f2020-06-09 18:59:51 -07002640 decoder->image->width = colorTrack->width;
2641 decoder->image->height = colorTrack->height;
Joe Dragoc554f5f2020-06-09 18:59:51 -07002642 decoder->alphaPresent = (alphaTrack != NULL);
Joe Dragoceb2fa02021-01-29 18:14:55 -08002643 decoder->image->alphaPremultiplied = decoder->alphaPresent && (colorTrack->premByID == alphaTrack->id);
Joe Drago46ea0582019-07-22 15:55:47 -07002644 } else {
2645 // Create from items
2646
Joe Dragobe4cbb92020-09-21 12:14:05 -07002647 avifDecoderItem * colorItem = NULL;
2648 avifDecoderItem * alphaItem = NULL;
Joe Drago46ea0582019-07-22 15:55:47 -07002649
Joe Dragof6a42272019-11-21 15:21:41 -08002650 // Find the colorOBU (primary) item
Joe Drago9f2b87b2020-06-03 19:36:38 -07002651 for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) {
2652 avifDecoderItem * item = &data->meta->items.item[itemIndex];
Joe Dragoba1eb492020-06-22 17:05:04 -07002653 if (!item->size) {
2654 continue;
Joe Drago8f7a3002019-02-07 19:35:37 -08002655 }
Joe Drago3320e5f2020-04-21 17:36:27 -07002656 if (item->hasUnsupportedEssentialProperty) {
2657 // An essential property isn't supported by libavif; ignore the item.
2658 continue;
2659 }
Joe Drago951a0022020-03-09 16:19:44 -07002660 avifBool isGrid = (memcmp(item->type, "grid", 4) == 0);
Joe Drago060d5342020-03-03 10:53:49 -08002661 if (memcmp(item->type, "av01", 4) && !isGrid) {
Joe Drago8f7a3002019-02-07 19:35:37 -08002662 // probably exif or some other data
2663 continue;
2664 }
2665 if (item->thumbnailForID != 0) {
2666 // It's a thumbnail, skip it
2667 continue;
2668 }
Joe Drago9f2b87b2020-06-03 19:36:38 -07002669 if ((data->meta->primaryItemID > 0) && (item->id != data->meta->primaryItemID)) {
Joe Dragof6a42272019-11-21 15:21:41 -08002670 // a primary item ID was specified, require it
2671 continue;
2672 }
Joe Drago8f7a3002019-02-07 19:35:37 -08002673
Joe Drago060d5342020-03-03 10:53:49 -08002674 if (isGrid) {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002675 avifROData readData;
Wan-Teh Chang4548b162020-11-06 11:48:25 -08002676 avifResult readResult = avifDecoderItemRead(item, decoder->io, &readData, 0);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002677 if (readResult != AVIF_RESULT_OK) {
2678 return readResult;
2679 }
Wan-Teh Changc443f142020-10-08 14:58:42 -07002680 if (!avifParseImageGridBox(&data->colorGrid, readData.data, readData.size)) {
Joe Drago060d5342020-03-03 10:53:49 -08002681 return AVIF_RESULT_INVALID_IMAGE_GRID;
2682 }
Joe Drago060d5342020-03-03 10:53:49 -08002683 }
2684
Joe Dragobe4cbb92020-09-21 12:14:05 -07002685 colorItem = item;
Joe Drago46ea0582019-07-22 15:55:47 -07002686 break;
2687 }
2688
Joe Dragobe4cbb92020-09-21 12:14:05 -07002689 if (!colorItem) {
Joe Drago060d5342020-03-03 10:53:49 -08002690 return AVIF_RESULT_NO_AV1_ITEMS_FOUND;
2691 }
Joe Dragobe4cbb92020-09-21 12:14:05 -07002692 colorProperties = &colorItem->properties;
Joe Drago46ea0582019-07-22 15:55:47 -07002693
Joe Drago060d5342020-03-03 10:53:49 -08002694 // Find the alphaOBU item, if any
Joe Drago9f2b87b2020-06-03 19:36:38 -07002695 for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) {
2696 avifDecoderItem * item = &data->meta->items.item[itemIndex];
Joe Dragoba1eb492020-06-22 17:05:04 -07002697 if (!item->size) {
2698 continue;
Joe Drago060d5342020-03-03 10:53:49 -08002699 }
Joe Drago3320e5f2020-04-21 17:36:27 -07002700 if (item->hasUnsupportedEssentialProperty) {
2701 // An essential property isn't supported by libavif; ignore the item.
2702 continue;
2703 }
Joe Drago951a0022020-03-09 16:19:44 -07002704 avifBool isGrid = (memcmp(item->type, "grid", 4) == 0);
Joe Drago060d5342020-03-03 10:53:49 -08002705 if (memcmp(item->type, "av01", 4) && !isGrid) {
2706 // probably exif or some other data
2707 continue;
2708 }
Joe Dragof6a42272019-11-21 15:21:41 -08002709
Joe Dragobe4cbb92020-09-21 12:14:05 -07002710 // Is this an alpha auxiliary item of whatever we chose for colorItem?
Joe Dragoa72da5b2020-06-15 19:40:17 -07002711 const avifProperty * auxCProp = avifPropertyArrayFind(&item->properties, "auxC");
Joe Dragobe4cbb92020-09-21 12:14:05 -07002712 if (auxCProp && isAlphaURN(auxCProp->u.auxC.auxType) && (item->auxForID == colorItem->id)) {
Joe Drago060d5342020-03-03 10:53:49 -08002713 if (isGrid) {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002714 avifROData readData;
Wan-Teh Chang4548b162020-11-06 11:48:25 -08002715 avifResult readResult = avifDecoderItemRead(item, decoder->io, &readData, 0);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002716 if (readResult != AVIF_RESULT_OK) {
2717 return readResult;
2718 }
Wan-Teh Changc443f142020-10-08 14:58:42 -07002719 if (!avifParseImageGridBox(&data->alphaGrid, readData.data, readData.size)) {
Joe Drago060d5342020-03-03 10:53:49 -08002720 return AVIF_RESULT_INVALID_IMAGE_GRID;
2721 }
Joe Dragof6a42272019-11-21 15:21:41 -08002722 }
2723
Joe Dragobe4cbb92020-09-21 12:14:05 -07002724 alphaItem = item;
Joe Drago060d5342020-03-03 10:53:49 -08002725 break;
Joe Dragof6a42272019-11-21 15:21:41 -08002726 }
Joe Drago444f0512019-01-23 17:03:24 -08002727 }
Joe Drago444f0512019-01-23 17:03:24 -08002728
Joe Drago060d5342020-03-03 10:53:49 -08002729 // Find Exif and/or XMP metadata, if any
Joe Dragobe4cbb92020-09-21 12:14:05 -07002730 avifResult findResult = avifDecoderFindMetadata(decoder, data->meta, decoder->image, colorItem->id);
2731 if (findResult != AVIF_RESULT_OK) {
2732 return findResult;
Joe Drago060d5342020-03-03 10:53:49 -08002733 }
2734
Wan-Teh Chang4295bcb2020-04-05 15:41:05 -07002735 if ((data->colorGrid.rows > 0) && (data->colorGrid.columns > 0)) {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002736 if (!avifDecoderDataGenerateImageGridTiles(data, &data->colorGrid, colorItem, AVIF_FALSE)) {
Joe Drago060d5342020-03-03 10:53:49 -08002737 return AVIF_RESULT_INVALID_IMAGE_GRID;
2738 }
2739 data->colorTileCount = data->tiles.count;
2740 } else {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002741 if (colorItem->size == 0) {
Joe Drago060d5342020-03-03 10:53:49 -08002742 return AVIF_RESULT_NO_AV1_ITEMS_FOUND;
2743 }
2744
wantehchangb207b4d2020-08-11 17:50:22 -07002745 avifTile * colorTile = avifDecoderDataCreateTile(data);
Joe Dragoe3e3bfa2020-06-02 16:33:53 -07002746 avifDecodeSample * colorSample = (avifDecodeSample *)avifArrayPushPtr(&colorTile->input->samples);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002747 colorSample->itemID = colorItem->id;
2748 colorSample->offset = 0;
2749 colorSample->size = colorItem->size;
Joe Drago060d5342020-03-03 10:53:49 -08002750 colorSample->sync = AVIF_TRUE;
wantehchangb207b4d2020-08-11 17:50:22 -07002751 data->colorTileCount = 1;
Joe Drago060d5342020-03-03 10:53:49 -08002752 }
2753
Joe Dragobe4cbb92020-09-21 12:14:05 -07002754 if (alphaItem) {
Wan-Teh Chang272aafe2020-08-12 17:31:38 -07002755 if ((data->alphaGrid.rows > 0) && (data->alphaGrid.columns > 0)) {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002756 if (!avifDecoderDataGenerateImageGridTiles(data, &data->alphaGrid, alphaItem, AVIF_TRUE)) {
Wan-Teh Chang272aafe2020-08-12 17:31:38 -07002757 return AVIF_RESULT_INVALID_IMAGE_GRID;
2758 }
2759 data->alphaTileCount = data->tiles.count - data->colorTileCount;
2760 } else {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002761 if (alphaItem->size == 0) {
Wan-Teh Chang272aafe2020-08-12 17:31:38 -07002762 return AVIF_RESULT_NO_AV1_ITEMS_FOUND;
2763 }
2764
wantehchang76e16bf2020-08-12 13:01:31 -07002765 avifTile * alphaTile = avifDecoderDataCreateTile(data);
Joe Dragoe3e3bfa2020-06-02 16:33:53 -07002766 avifDecodeSample * alphaSample = (avifDecodeSample *)avifArrayPushPtr(&alphaTile->input->samples);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002767 alphaSample->itemID = alphaItem->id;
2768 alphaSample->offset = 0;
2769 alphaSample->size = alphaItem->size;
Joe Drago060d5342020-03-03 10:53:49 -08002770 alphaSample->sync = AVIF_TRUE;
2771 alphaTile->input->alpha = AVIF_TRUE;
wantehchangb207b4d2020-08-11 17:50:22 -07002772 data->alphaTileCount = 1;
Joe Drago060d5342020-03-03 10:53:49 -08002773 }
Joe Drago444f0512019-01-23 17:03:24 -08002774 }
Joe Drago33f1d362019-02-13 16:46:22 -08002775
Joe Drago46ea0582019-07-22 15:55:47 -07002776 // Set all counts and timing to safe-but-uninteresting values
2777 decoder->imageIndex = -1;
2778 decoder->imageCount = 1;
2779 decoder->imageTiming.timescale = 1;
2780 decoder->imageTiming.pts = 0;
2781 decoder->imageTiming.ptsInTimescales = 0;
2782 decoder->imageTiming.duration = 1;
2783 decoder->imageTiming.durationInTimescales = 1;
2784 decoder->timescale = 1;
2785 decoder->duration = 1;
2786 decoder->durationInTimescales = 1;
Joe Drago70cbf602019-07-24 15:30:55 -07002787
Joe Dragobe4cbb92020-09-21 12:14:05 -07002788 decoder->ioStats.colorOBUSize = colorItem->size;
2789 decoder->ioStats.alphaOBUSize = alphaItem ? alphaItem->size : 0;
Joe Drago41700852019-09-26 17:01:43 -07002790
Joe Dragoa72da5b2020-06-15 19:40:17 -07002791 const avifProperty * ispeProp = avifPropertyArrayFind(colorProperties, "ispe");
2792 if (ispeProp) {
2793 decoder->image->width = ispeProp->u.ispe.width;
2794 decoder->image->height = ispeProp->u.ispe.height;
Joe Drago41700852019-09-26 17:01:43 -07002795 } else {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002796 decoder->image->width = 0;
2797 decoder->image->height = 0;
Joe Drago41700852019-09-26 17:01:43 -07002798 }
Joe Dragobe4cbb92020-09-21 12:14:05 -07002799 decoder->alphaPresent = (alphaItem != NULL);
Joe Dragoceb2fa02021-01-29 18:14:55 -08002800 decoder->image->alphaPremultiplied = decoder->alphaPresent && (colorItem->premByID == alphaItem->id);
Joe Drago00bcaaf2020-06-05 15:29:38 -07002801 }
2802
Joe Drago11f2a5e2020-07-06 10:49:00 -07002803 // Sanity check tiles
2804 for (uint32_t tileIndex = 0; tileIndex < data->tiles.count; ++tileIndex) {
2805 avifTile * tile = &data->tiles.tile[tileIndex];
2806 for (uint32_t sampleIndex = 0; sampleIndex < tile->input->samples.count; ++sampleIndex) {
Joe Drago043311b2020-07-06 16:48:41 -07002807 avifDecodeSample * sample = &tile->input->samples.sample[sampleIndex];
Joe Dragobe4cbb92020-09-21 12:14:05 -07002808 if (!sample->size) {
Joe Drago11f2a5e2020-07-06 10:49:00 -07002809 // Every sample must have some data
2810 return AVIF_RESULT_BMFF_PARSE_FAILED;
2811 }
2812 }
2813 }
2814
Joe Dragobf58fe72020-11-05 13:25:14 -08002815 // Find and adopt all colr boxes "at most one for a given value of colour type" (HEIF 6.5.5.1, from Amendment 3)
2816 // Accept one of each type, and bail out if more than one of a given type is provided.
2817 avifBool colrICCSeen = AVIF_FALSE;
2818 avifBool colrNCLXSeen = AVIF_FALSE;
2819 for (uint32_t propertyIndex = 0; propertyIndex < colorProperties->count; ++propertyIndex) {
2820 avifProperty * prop = &colorProperties->prop[propertyIndex];
2821
2822 if (!memcmp(prop->type, "colr", 4)) {
2823 if (prop->u.colr.hasICC) {
2824 if (colrICCSeen) {
2825 return AVIF_RESULT_BMFF_PARSE_FAILED;
2826 }
2827 colrICCSeen = AVIF_TRUE;
2828 avifImageSetProfileICC(decoder->image, prop->u.colr.icc, prop->u.colr.iccSize);
2829 }
2830 if (prop->u.colr.hasNCLX) {
2831 if (colrNCLXSeen) {
2832 return AVIF_RESULT_BMFF_PARSE_FAILED;
2833 }
2834 colrNCLXSeen = AVIF_TRUE;
2835 data->cicpSet = AVIF_TRUE;
2836 decoder->image->colorPrimaries = prop->u.colr.colorPrimaries;
2837 decoder->image->transferCharacteristics = prop->u.colr.transferCharacteristics;
2838 decoder->image->matrixCoefficients = prop->u.colr.matrixCoefficients;
2839 decoder->image->yuvRange = prop->u.colr.range;
2840 }
Joe Dragoa72da5b2020-06-15 19:40:17 -07002841 }
2842 }
2843
2844 // Transformations
2845 const avifProperty * paspProp = avifPropertyArrayFind(colorProperties, "pasp");
2846 if (paspProp) {
2847 decoder->image->transformFlags |= AVIF_TRANSFORM_PASP;
2848 memcpy(&decoder->image->pasp, &paspProp->u.pasp, sizeof(avifPixelAspectRatioBox));
2849 }
2850 const avifProperty * clapProp = avifPropertyArrayFind(colorProperties, "clap");
2851 if (clapProp) {
2852 decoder->image->transformFlags |= AVIF_TRANSFORM_CLAP;
2853 memcpy(&decoder->image->clap, &clapProp->u.clap, sizeof(avifCleanApertureBox));
2854 }
2855 const avifProperty * irotProp = avifPropertyArrayFind(colorProperties, "irot");
2856 if (irotProp) {
2857 decoder->image->transformFlags |= AVIF_TRANSFORM_IROT;
2858 memcpy(&decoder->image->irot, &irotProp->u.irot, sizeof(avifImageRotation));
2859 }
2860 const avifProperty * imirProp = avifPropertyArrayFind(colorProperties, "imir");
2861 if (imirProp) {
2862 decoder->image->transformFlags |= AVIF_TRANSFORM_IMIR;
2863 memcpy(&decoder->image->imir, &imirProp->u.imir, sizeof(avifImageMirror));
2864 }
2865
wantehchangb207b4d2020-08-11 17:50:22 -07002866 if (!data->cicpSet && (data->tiles.count > 0)) {
Joe Dragoda92a382020-06-09 17:08:45 -07002867 avifTile * firstTile = &data->tiles.tile[0];
2868 if (firstTile->input->samples.count > 0) {
2869 avifDecodeSample * sample = &firstTile->input->samples.sample[0];
Joe Dragobe4cbb92020-09-21 12:14:05 -07002870
2871 // Harvest CICP from the AV1's sequence header, which should be very close to the front
2872 // of the first sample. Read in successively larger chunks until we successfully parse the sequence.
2873 static const size_t searchSampleChunkIncrement = 64;
Wan-Teh Changac145712021-02-08 15:53:44 -08002874 static const size_t searchSampleSizeMax = 4096;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002875 size_t searchSampleSize = 0;
Wan-Teh Changc6c2fe82020-10-08 10:44:04 -07002876 do {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002877 searchSampleSize += searchSampleChunkIncrement;
2878 if (searchSampleSize > sample->size) {
2879 searchSampleSize = sample->size;
2880 }
2881
2882 avifResult prepareResult = avifDecoderPrepareSample(decoder, sample, searchSampleSize);
2883 if (prepareResult != AVIF_RESULT_OK) {
2884 return prepareResult;
2885 }
2886
2887 avifSequenceHeader sequenceHeader;
2888 if (avifSequenceHeaderParse(&sequenceHeader, &sample->data)) {
2889 data->cicpSet = AVIF_TRUE;
2890 decoder->image->colorPrimaries = sequenceHeader.colorPrimaries;
2891 decoder->image->transferCharacteristics = sequenceHeader.transferCharacteristics;
2892 decoder->image->matrixCoefficients = sequenceHeader.matrixCoefficients;
2893 decoder->image->yuvRange = sequenceHeader.range;
2894 break;
2895 }
Wan-Teh Changac145712021-02-08 15:53:44 -08002896 } while (searchSampleSize != sample->size && searchSampleSize < searchSampleSizeMax);
Joe Dragoda92a382020-06-09 17:08:45 -07002897 }
2898 }
2899
Joe Dragoa72da5b2020-06-15 19:40:17 -07002900 const avifProperty * av1CProp = avifPropertyArrayFind(colorProperties, "av1C");
2901 if (av1CProp) {
Joe Dragob8401122020-06-19 11:45:49 -07002902 decoder->image->depth = avifCodecConfigurationBoxGetDepth(&av1CProp->u.av1C);
2903 if (av1CProp->u.av1C.monochrome) {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002904 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV400;
Joe Drago7b2cf802020-06-09 17:57:23 -07002905 } else {
Joe Dragob8401122020-06-19 11:45:49 -07002906 if (av1CProp->u.av1C.chromaSubsamplingX && av1CProp->u.av1C.chromaSubsamplingY) {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002907 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV420;
Joe Dragob8401122020-06-19 11:45:49 -07002908 } else if (av1CProp->u.av1C.chromaSubsamplingX) {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002909 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV422;
Joe Drago7b2cf802020-06-09 17:57:23 -07002910
2911 } else {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002912 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV444;
Joe Drago7b2cf802020-06-09 17:57:23 -07002913 }
2914 }
Joe Dragob8401122020-06-19 11:45:49 -07002915 decoder->image->yuvChromaSamplePosition = (avifChromaSamplePosition)av1CProp->u.av1C.chromaSamplePosition;
Joe Drago00bcaaf2020-06-05 15:29:38 -07002916 } else {
Joe Dragof48a3382020-06-19 14:13:44 -07002917 // An av1C box is mandatory in all valid AVIF configurations. Bail out.
2918 return AVIF_RESULT_BMFF_PARSE_FAILED;
Joe Drago46ea0582019-07-22 15:55:47 -07002919 }
2920
Joe Drago22c1ad92019-09-26 12:46:50 -07002921 return avifDecoderFlush(decoder);
Joe Drago46ea0582019-07-22 15:55:47 -07002922}
Joe Drago444f0512019-01-23 17:03:24 -08002923
Joe Drago5998f592020-11-13 15:38:20 -08002924avifResult avifDecoderNextImage(avifDecoder * decoder)
Joe Drago46ea0582019-07-22 15:55:47 -07002925{
Wan-Teh Changaf0d4842020-11-17 12:27:34 -08002926 if (!decoder->data) {
2927 // Nothing has been parsed yet
2928 return AVIF_RESULT_NO_CONTENT;
2929 }
2930
Joe Dragobe4cbb92020-09-21 12:14:05 -07002931 if (!decoder->io || !decoder->io->read) {
Wan-Teh Chang31c7c1a2020-10-13 16:45:41 -07002932 return AVIF_RESULT_IO_NOT_SET;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002933 }
2934
Wan-Teh Changaf0d4842020-11-17 12:27:34 -08002935 const uint32_t nextImageIndex = (uint32_t)(decoder->imageIndex + 1);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002936
2937 // Acquire all sample data for the current image first, allowing for any read call to bail out
2938 // with AVIF_RESULT_WAITING_ON_IO harmlessly / idempotently.
2939 for (unsigned int tileIndex = 0; tileIndex < decoder->data->tiles.count; ++tileIndex) {
2940 avifTile * tile = &decoder->data->tiles.tile[tileIndex];
Joe Drago5998f592020-11-13 15:38:20 -08002941 if (nextImageIndex >= tile->input->samples.count) {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002942 return AVIF_RESULT_NO_IMAGES_REMAINING;
2943 }
2944
Joe Drago5998f592020-11-13 15:38:20 -08002945 avifDecodeSample * sample = &tile->input->samples.sample[nextImageIndex];
Joe Dragobe4cbb92020-09-21 12:14:05 -07002946 avifResult prepareResult = avifDecoderPrepareSample(decoder, sample, 0);
2947 if (prepareResult != AVIF_RESULT_OK) {
2948 return prepareResult;
2949 }
2950 }
2951
Wan-Teh Changaf0d4842020-11-17 12:27:34 -08002952 // Decode all tiles now that the sample data is ready.
Joe Drago060d5342020-03-03 10:53:49 -08002953 for (unsigned int tileIndex = 0; tileIndex < decoder->data->tiles.count; ++tileIndex) {
2954 avifTile * tile = &decoder->data->tiles.tile[tileIndex];
Joe Drago41eb62b2019-02-08 15:38:18 -08002955
Wan-Teh Changb4977b32020-10-05 18:04:54 -07002956 const avifDecodeSample * sample = &tile->input->samples.sample[nextImageIndex];
Joe Dragobe4cbb92020-09-21 12:14:05 -07002957
2958 if (!tile->codec->getNextImage(tile->codec, sample, tile->input->alpha, tile->image)) {
Joe Drago060d5342020-03-03 10:53:49 -08002959 if (tile->input->alpha) {
2960 return AVIF_RESULT_DECODE_ALPHA_FAILED;
2961 } else {
2962 if (tile->image->width) {
2963 // We've sent at least one image, but we've run out now.
2964 return AVIF_RESULT_NO_IMAGES_REMAINING;
2965 }
2966 return AVIF_RESULT_DECODE_COLOR_FAILED;
2967 }
Joe Drago46ea0582019-07-22 15:55:47 -07002968 }
Joe Drago060d5342020-03-03 10:53:49 -08002969 }
2970
2971 if (decoder->data->tiles.count != (decoder->data->colorTileCount + decoder->data->alphaTileCount)) {
2972 // TODO: assert here? This should be impossible.
2973 return AVIF_RESULT_UNKNOWN_ERROR;
2974 }
2975
Wan-Teh Changab8d9a52020-08-12 17:17:37 -07002976 if ((decoder->data->colorGrid.rows > 0) && (decoder->data->colorGrid.columns > 0)) {
Joe Drago11d23592021-01-05 14:18:57 -08002977 if (!avifDecoderDataFillImageGrid(decoder->data, &decoder->data->colorGrid, decoder->image, 0, decoder->data->colorTileCount, AVIF_FALSE)) {
Joe Drago060d5342020-03-03 10:53:49 -08002978 return AVIF_RESULT_INVALID_IMAGE_GRID;
2979 }
2980 } else {
2981 // Normal (most common) non-grid path. Just steal the planes from the only "tile".
2982
2983 if (decoder->data->colorTileCount != 1) {
2984 return AVIF_RESULT_DECODE_COLOR_FAILED;
2985 }
2986
2987 avifImage * srcColor = decoder->data->tiles.tile[0].image;
2988
2989 if ((decoder->image->width != srcColor->width) || (decoder->image->height != srcColor->height) ||
2990 (decoder->image->depth != srcColor->depth)) {
2991 avifImageFreePlanes(decoder->image, AVIF_PLANES_ALL);
2992
2993 decoder->image->width = srcColor->width;
2994 decoder->image->height = srcColor->height;
2995 decoder->image->depth = srcColor->depth;
Joe Dragoc554f5f2020-06-09 18:59:51 -07002996 }
psi / Ryo Hirafuji922d8a12020-03-10 03:24:57 +09002997
Joe Dragoc00d5832020-08-13 16:03:28 -07002998#if 0
2999 // This code is currently unnecessary as the CICP is always set by the end of avifDecoderParse().
3000 if (!decoder->data->cicpSet) {
3001 decoder->data->cicpSet = AVIF_TRUE;
3002 decoder->image->colorPrimaries = srcColor->colorPrimaries;
3003 decoder->image->transferCharacteristics = srcColor->transferCharacteristics;
3004 decoder->image->matrixCoefficients = srcColor->matrixCoefficients;
3005 }
3006#endif
3007
Joe Drago060d5342020-03-03 10:53:49 -08003008 avifImageStealPlanes(decoder->image, srcColor, AVIF_PLANES_YUV);
3009 }
3010
Wan-Teh Changab8d9a52020-08-12 17:17:37 -07003011 if ((decoder->data->alphaGrid.rows > 0) && (decoder->data->alphaGrid.columns > 0)) {
Joe Drago11d23592021-01-05 14:18:57 -08003012 if (!avifDecoderDataFillImageGrid(decoder->data,
3013 &decoder->data->alphaGrid,
3014 decoder->image,
3015 decoder->data->colorTileCount,
3016 decoder->data->alphaTileCount,
3017 AVIF_TRUE)) {
Joe Drago060d5342020-03-03 10:53:49 -08003018 return AVIF_RESULT_INVALID_IMAGE_GRID;
3019 }
3020 } else {
3021 // Normal (most common) non-grid path. Just steal the planes from the only "tile".
3022
3023 if (decoder->data->alphaTileCount == 0) {
3024 avifImageFreePlanes(decoder->image, AVIF_PLANES_A); // no alpha
3025 } else {
3026 if (decoder->data->alphaTileCount != 1) {
3027 return AVIF_RESULT_DECODE_ALPHA_FAILED;
3028 }
3029
3030 avifImage * srcAlpha = decoder->data->tiles.tile[decoder->data->colorTileCount].image;
3031 if ((decoder->image->width != srcAlpha->width) || (decoder->image->height != srcAlpha->height) ||
3032 (decoder->image->depth != srcAlpha->depth)) {
3033 return AVIF_RESULT_DECODE_ALPHA_FAILED;
3034 }
3035
3036 avifImageStealPlanes(decoder->image, srcAlpha, AVIF_PLANES_A);
Joe Drago3fd2db22020-08-13 16:07:24 -07003037 decoder->image->alphaRange = srcAlpha->alphaRange;
Joe Drago060d5342020-03-03 10:53:49 -08003038 }
3039 }
Joe Drago7ad3ad62019-02-07 11:17:34 -08003040
Joe Dragobe4cbb92020-09-21 12:14:05 -07003041 decoder->imageIndex = nextImageIndex;
Joe Drago46ea0582019-07-22 15:55:47 -07003042 if (decoder->data->sourceSampleTable) {
3043 // Decoding from a track! Provide timing information.
Joe Drago05559c92019-07-17 16:33:38 -07003044
Joe Dragoe9c58602020-04-13 17:23:13 -07003045 avifResult timingResult = avifDecoderNthImageTiming(decoder, decoder->imageIndex, &decoder->imageTiming);
3046 if (timingResult != AVIF_RESULT_OK) {
3047 return timingResult;
Joe Drago22c1ad92019-09-26 12:46:50 -07003048 }
Joe Dragoe9c58602020-04-13 17:23:13 -07003049 }
3050 return AVIF_RESULT_OK;
3051}
Joe Drago46ea0582019-07-22 15:55:47 -07003052
Wan-Teh Change184dc12020-05-11 12:47:21 -07003053avifResult avifDecoderNthImageTiming(const avifDecoder * decoder, uint32_t frameIndex, avifImageTiming * outTiming)
Joe Dragoe9c58602020-04-13 17:23:13 -07003054{
3055 if (!decoder->data) {
3056 // Nothing has been parsed yet
3057 return AVIF_RESULT_NO_CONTENT;
3058 }
3059
Wan-Teh Chang25b9c702020-10-08 12:17:17 -07003060 if ((frameIndex > INT_MAX) || ((int)frameIndex >= decoder->imageCount)) {
Joe Dragoe9c58602020-04-13 17:23:13 -07003061 // Impossible index
3062 return AVIF_RESULT_NO_IMAGES_REMAINING;
3063 }
3064
3065 if (!decoder->data->sourceSampleTable) {
3066 // There isn't any real timing associated with this decode, so
3067 // just hand back the defaults chosen in avifDecoderReset().
3068 memcpy(outTiming, &decoder->imageTiming, sizeof(avifImageTiming));
3069 return AVIF_RESULT_OK;
3070 }
3071
Wan-Teh Chang167e4062020-04-14 16:06:12 -07003072 outTiming->timescale = decoder->timescale;
3073 outTiming->ptsInTimescales = 0;
Joe Dragoe9c58602020-04-13 17:23:13 -07003074 for (int imageIndex = 0; imageIndex < (int)frameIndex; ++imageIndex) {
Wan-Teh Chang167e4062020-04-14 16:06:12 -07003075 outTiming->ptsInTimescales += avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, imageIndex);
Joe Dragoe9c58602020-04-13 17:23:13 -07003076 }
Wan-Teh Chang167e4062020-04-14 16:06:12 -07003077 outTiming->durationInTimescales = avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, frameIndex);
Joe Dragoe9c58602020-04-13 17:23:13 -07003078
Wan-Teh Chang167e4062020-04-14 16:06:12 -07003079 if (outTiming->timescale > 0) {
3080 outTiming->pts = (double)outTiming->ptsInTimescales / (double)outTiming->timescale;
3081 outTiming->duration = (double)outTiming->durationInTimescales / (double)outTiming->timescale;
Joe Dragoe9c58602020-04-13 17:23:13 -07003082 } else {
Wan-Teh Chang167e4062020-04-14 16:06:12 -07003083 outTiming->pts = 0.0;
3084 outTiming->duration = 0.0;
Joe Drago444f0512019-01-23 17:03:24 -08003085 }
Joe Drago46ea0582019-07-22 15:55:47 -07003086 return AVIF_RESULT_OK;
3087}
3088
Joe Drago22c1ad92019-09-26 12:46:50 -07003089avifResult avifDecoderNthImage(avifDecoder * decoder, uint32_t frameIndex)
3090{
Wan-Teh Chang25b9c702020-10-08 12:17:17 -07003091 if (frameIndex > INT_MAX) {
3092 // Impossible index
3093 return AVIF_RESULT_NO_IMAGES_REMAINING;
3094 }
3095
Joe Drago22c1ad92019-09-26 12:46:50 -07003096 int requestedIndex = (int)frameIndex;
3097 if (requestedIndex == decoder->imageIndex) {
3098 // We're here already, nothing to do
3099 return AVIF_RESULT_OK;
3100 }
3101
3102 if (requestedIndex == (decoder->imageIndex + 1)) {
3103 // it's just the next image, nothing special here
3104 return avifDecoderNextImage(decoder);
3105 }
3106
3107 if (requestedIndex >= decoder->imageCount) {
3108 // Impossible index
3109 return AVIF_RESULT_NO_IMAGES_REMAINING;
3110 }
3111
Wan-Teh Chang722fb7e2020-11-06 12:39:58 -08003112 int nearestKeyFrame = (int)avifDecoderNearestKeyframe(decoder, frameIndex);
3113 if ((nearestKeyFrame > (decoder->imageIndex + 1)) || (requestedIndex < decoder->imageIndex)) {
3114 // If we get here, a decoder flush is necessary
3115 decoder->imageIndex = nearestKeyFrame - 1; // prepare to read nearest keyframe
3116 avifDecoderFlush(decoder);
3117 }
Joe Drago22c1ad92019-09-26 12:46:50 -07003118 for (;;) {
3119 avifResult result = avifDecoderNextImage(decoder);
3120 if (result != AVIF_RESULT_OK) {
3121 return result;
3122 }
3123
3124 if (requestedIndex == decoder->imageIndex) {
3125 break;
3126 }
Joe Dragofc4144e2019-09-27 20:35:06 -07003127 }
Joe Drago22c1ad92019-09-26 12:46:50 -07003128 return AVIF_RESULT_OK;
3129}
3130
Wan-Teh Change184dc12020-05-11 12:47:21 -07003131avifBool avifDecoderIsKeyframe(const avifDecoder * decoder, uint32_t frameIndex)
Joe Drago22c1ad92019-09-26 12:46:50 -07003132{
Wan-Teh Changaf0d4842020-11-17 12:27:34 -08003133 if (!decoder->data) {
3134 // Nothing has been parsed yet
3135 return AVIF_FALSE;
3136 }
3137
Joe Drago060d5342020-03-03 10:53:49 -08003138 if ((decoder->data->tiles.count > 0) && decoder->data->tiles.tile[0].input) {
3139 if (frameIndex < decoder->data->tiles.tile[0].input->samples.count) {
3140 return decoder->data->tiles.tile[0].input->samples.sample[frameIndex].sync;
Joe Drago22c1ad92019-09-26 12:46:50 -07003141 }
3142 }
3143 return AVIF_FALSE;
3144}
3145
Wan-Teh Change184dc12020-05-11 12:47:21 -07003146uint32_t avifDecoderNearestKeyframe(const avifDecoder * decoder, uint32_t frameIndex)
Joe Drago22c1ad92019-09-26 12:46:50 -07003147{
Wan-Teh Changaf0d4842020-11-17 12:27:34 -08003148 if (!decoder->data) {
3149 // Nothing has been parsed yet
3150 return 0;
3151 }
3152
Joe Drago22c1ad92019-09-26 12:46:50 -07003153 for (; frameIndex != 0; --frameIndex) {
3154 if (avifDecoderIsKeyframe(decoder, frameIndex)) {
3155 break;
3156 }
3157 }
3158 return frameIndex;
3159}
3160
Joe Dragobe4cbb92020-09-21 12:14:05 -07003161avifResult avifDecoderRead(avifDecoder * decoder, avifImage * image)
Joe Drago46ea0582019-07-22 15:55:47 -07003162{
Joe Dragobe4cbb92020-09-21 12:14:05 -07003163 avifResult result = avifDecoderParse(decoder);
Joe Drago46ea0582019-07-22 15:55:47 -07003164 if (result != AVIF_RESULT_OK) {
3165 return result;
Joe Drago05559c92019-07-17 16:33:38 -07003166 }
Joe Drago46ea0582019-07-22 15:55:47 -07003167 result = avifDecoderNextImage(decoder);
3168 if (result != AVIF_RESULT_OK) {
3169 return result;
3170 }
Joe Drago250221a2020-06-01 11:11:06 -07003171 avifImageCopy(image, decoder->image, AVIF_PLANES_ALL);
Joe Drago46ea0582019-07-22 15:55:47 -07003172 return AVIF_RESULT_OK;
Joe Drago444f0512019-01-23 17:03:24 -08003173}
Joe Dragobe4cbb92020-09-21 12:14:05 -07003174
Wan-Teh Chang7de44452020-10-08 11:43:18 -07003175avifResult avifDecoderReadMemory(avifDecoder * decoder, avifImage * image, const uint8_t * data, size_t size)
Joe Dragobe4cbb92020-09-21 12:14:05 -07003176{
Wan-Teh Chang7de44452020-10-08 11:43:18 -07003177 avifResult result = avifDecoderSetIOMemory(decoder, data, size);
Joe Dragobe4cbb92020-09-21 12:14:05 -07003178 if (result != AVIF_RESULT_OK) {
3179 return result;
3180 }
3181 return avifDecoderRead(decoder, image);
3182}
3183
3184avifResult avifDecoderReadFile(avifDecoder * decoder, avifImage * image, const char * filename)
3185{
3186 avifResult result = avifDecoderSetIOFile(decoder, filename);
3187 if (result != AVIF_RESULT_OK) {
3188 return result;
3189 }
3190 return avifDecoderRead(decoder, image);
3191}