blob: e6ec361fbbdbd87bef810fa9fd9c5bd1fc7e4e6d [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;
298 uint32_t auxForID; // if non-zero, this item is an auxC plane for Track #{auxForID}
Yuan Tonge4850be2021-01-22 14:21:25 +0800299 uint32_t premByID; // if non-zero, this item is premultiplied by Item #{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 Chang136d7572020-10-08 15:13:42 -0700332static avifBool avifCodecDecodeInputGetSamples(avifCodecDecodeInput * decodeInput, avifSampleTable * sampleTable, uint64_t sizeHint)
Joe Drago46ea0582019-07-22 15:55:47 -0700333{
334 uint32_t sampleSizeIndex = 0;
335 for (uint32_t chunkIndex = 0; chunkIndex < sampleTable->chunks.count; ++chunkIndex) {
336 avifSampleTableChunk * chunk = &sampleTable->chunks.chunk[chunkIndex];
337
338 // First, figure out how many samples are in this chunk
339 uint32_t sampleCount = 0;
340 for (int sampleToChunkIndex = sampleTable->sampleToChunks.count - 1; sampleToChunkIndex >= 0; --sampleToChunkIndex) {
341 avifSampleTableSampleToChunk * sampleToChunk = &sampleTable->sampleToChunks.sampleToChunk[sampleToChunkIndex];
342 if (sampleToChunk->firstChunk <= (chunkIndex + 1)) {
343 sampleCount = sampleToChunk->samplesPerChunk;
344 break;
345 }
346 }
347 if (sampleCount == 0) {
348 // chunks with 0 samples are invalid
349 return AVIF_FALSE;
350 }
351
352 uint64_t sampleOffset = chunk->offset;
353 for (uint32_t sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex) {
Joe Drago370be3f2020-02-07 15:59:42 -0800354 uint32_t sampleSize = sampleTable->allSamplesSize;
355 if (sampleSize == 0) {
356 if (sampleSizeIndex >= sampleTable->sampleSizes.count) {
357 // We've run out of samples to sum
358 return AVIF_FALSE;
359 }
360 avifSampleTableSampleSize * sampleSizePtr = &sampleTable->sampleSizes.sampleSize[sampleSizeIndex];
361 sampleSize = sampleSizePtr->size;
Joe Drago46ea0582019-07-22 15:55:47 -0700362 }
363
Joe Dragoe3e3bfa2020-06-02 16:33:53 -0700364 avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&decodeInput->samples);
Joe Dragobe4cbb92020-09-21 12:14:05 -0700365 sample->offset = sampleOffset;
366 sample->size = sampleSize;
Joe Drago22c1ad92019-09-26 12:46:50 -0700367 sample->sync = AVIF_FALSE; // to potentially be set to true following the outer loop
Joe Drago46ea0582019-07-22 15:55:47 -0700368
Wan-Teh Chang3ca14242020-09-30 16:39:38 -0700369 if (sampleSize > UINT64_MAX - sampleOffset) {
370 return AVIF_FALSE;
371 }
Wan-Teh Chang136d7572020-10-08 15:13:42 -0700372 if (sizeHint && ((sampleOffset + sampleSize) > sizeHint)) {
Joe Drago34c0d312020-04-30 15:23:03 -0700373 return AVIF_FALSE;
374 }
Joe Drago46ea0582019-07-22 15:55:47 -0700375
Joe Drago370be3f2020-02-07 15:59:42 -0800376 sampleOffset += sampleSize;
Joe Drago46ea0582019-07-22 15:55:47 -0700377 ++sampleSizeIndex;
378 }
379 }
Joe Drago22c1ad92019-09-26 12:46:50 -0700380
381 // Mark appropriate samples as sync
382 for (uint32_t syncSampleIndex = 0; syncSampleIndex < sampleTable->syncSamples.count; ++syncSampleIndex) {
383 uint32_t frameIndex = sampleTable->syncSamples.syncSample[syncSampleIndex].sampleNumber - 1; // sampleNumber is 1-based
384 if (frameIndex < decodeInput->samples.count) {
385 decodeInput->samples.sample[frameIndex].sync = AVIF_TRUE;
386 }
387 }
388
389 // Assume frame 0 is sync, just in case the stss box is absent in the BMFF. (Unnecessary?)
390 if (decodeInput->samples.count > 0) {
391 decodeInput->samples.sample[0].sync = AVIF_TRUE;
392 }
Joe Drago46ea0582019-07-22 15:55:47 -0700393 return AVIF_TRUE;
394}
395
396// ---------------------------------------------------------------------------
Joe Drago8f439092020-08-28 15:05:17 -0700397// Helper macros / functions
Joe Dragoa72da5b2020-06-15 19:40:17 -0700398
399#define BEGIN_STREAM(VARNAME, PTR, SIZE) \
400 avifROStream VARNAME; \
401 avifROData VARNAME##_roData; \
402 VARNAME##_roData.data = PTR; \
403 VARNAME##_roData.size = SIZE; \
404 avifROStreamStart(&VARNAME, &VARNAME##_roData)
405
Joe Drago8f439092020-08-28 15:05:17 -0700406// Use this to keep track of whether or not a child box that must be unique (0 or 1 present) has
407// been seen yet, when parsing a parent box. If the "seen" bit is already set for a given box when
408// it is encountered during parse, an error is thrown. Which bit corresponds to which box is
409// dictated entirely by the calling function.
410static avifBool uniqueBoxSeen(uint32_t * uniqueBoxFlags, uint32_t whichFlag)
411{
412 const uint32_t flag = 1 << whichFlag;
413 if (*uniqueBoxFlags & flag) {
414 // This box has already been seen. Error!
415 return AVIF_FALSE;
416 }
417
418 // Mark this box as seen.
419 *uniqueBoxFlags |= flag;
420 return AVIF_TRUE;
421}
422
Joe Dragoa72da5b2020-06-15 19:40:17 -0700423// ---------------------------------------------------------------------------
Joe Drago800b47f2020-03-18 16:22:37 -0700424// avifDecoderData
Joe Dragoae7e2c32019-07-18 15:22:25 -0700425
Joe Drago060d5342020-03-03 10:53:49 -0800426typedef struct avifTile
427{
428 avifCodecDecodeInput * input;
429 struct avifCodec * codec;
430 avifImage * image;
431} avifTile;
432AVIF_ARRAY_DECLARE(avifTileArray, avifTile, tile);
433
Joe Dragoba1eb492020-06-22 17:05:04 -0700434// This holds one "meta" box (from the BMFF and HEIF standards) worth of relevant-to-AVIF information.
435// * If a meta box is parsed from the root level of the BMFF, it can contain the information about
436// "items" which might be color planes, alpha planes, or EXIF or XMP metadata.
437// * If a meta box is parsed from inside of a track ("trak") box, any metadata (EXIF/XMP) items inside
438// of that box are implicitly associated with that track.
Joe Drago9f2b87b2020-06-03 19:36:38 -0700439typedef struct avifMeta
Joe Drago444f0512019-01-23 17:03:24 -0800440{
Joe Dragoba1eb492020-06-22 17:05:04 -0700441 // Items (from HEIF) are the generic storage for any data that does not require timed processing
Wan-Teh Chang4b331fb2020-07-07 14:10:02 -0700442 // (single image color planes, alpha planes, EXIF, XMP, etc). Each item has a unique integer ID >1,
443 // and is defined by a series of child boxes in a meta box:
Joe Dragoba1eb492020-06-22 17:05:04 -0700444 // * iloc - location: byte offset to item data, item size in bytes
445 // * iinf - information: type of item (color planes, alpha plane, EXIF, XMP)
446 // * ipco - properties: dimensions, aspect ratio, image transformations, references to other items
447 // * ipma - associations: Attaches an item in the properties list to a given item
448 //
449 // Items are lazily created in this array when any of the above boxes refer to one by a new (unseen) ID,
450 // and are then further modified/updated as new information for an item's ID is parsed.
Joe Drago800b47f2020-03-18 16:22:37 -0700451 avifDecoderItemArray items;
Joe Dragoba1eb492020-06-22 17:05:04 -0700452
453 // Any ipco boxes explained above are populated into this array as a staging area, which are
454 // then duplicated into the appropriate items upon encountering an item property association
455 // (ipma) box.
Joe Drago05559c92019-07-17 16:33:38 -0700456 avifPropertyArray properties;
Joe Dragoba1eb492020-06-22 17:05:04 -0700457
458 // Filled with the contents of "idat" boxes, which are raw data that an item can directly refer to in its
459 // item location box (iloc) instead of just giving an offset into the overall file. If all items' iloc boxes
460 // simply point at an offset/length in the file itself, this array will likely be empty.
Joe Drago800b47f2020-03-18 16:22:37 -0700461 avifDecoderItemDataArray idats;
Joe Dragoba1eb492020-06-22 17:05:04 -0700462
463 // Ever-incrementing ID for uniquely identifying which 'meta' box contains an idat (when
Wan-Teh Chang4b331fb2020-07-07 14:10:02 -0700464 // multiple meta boxes exist as BMFF siblings). Each time avifParseMetaBox() is called on an
Joe Dragoba1eb492020-06-22 17:05:04 -0700465 // avifMeta struct, this value is incremented. Any time an additional meta box is detected at
466 // the same "level" (root level, trak level, etc), this ID helps distinguish which meta box's
Wan-Teh Chang4b331fb2020-07-07 14:10:02 -0700467 // "idat" is which, as items implicitly reference idat boxes that exist in the same meta
Joe Dragoba1eb492020-06-22 17:05:04 -0700468 // box.
469 uint32_t idatID;
470
471 // Contents of a pitm box, which signal which of the items in this file is the main image. For
472 // AVIF, this should point at an av01 type item containing color planes, and all other items
473 // are ignored unless they refer to this item in some way (alpha plane, EXIF/XMP metadata).
Joe Drago9f2b87b2020-06-03 19:36:38 -0700474 uint32_t primaryItemID;
475} avifMeta;
476
477static avifMeta * avifMetaCreate()
478{
479 avifMeta * meta = (avifMeta *)avifAlloc(sizeof(avifMeta));
480 memset(meta, 0, sizeof(avifMeta));
481 avifArrayCreate(&meta->items, sizeof(avifDecoderItem), 8);
482 avifArrayCreate(&meta->properties, sizeof(avifProperty), 16);
483 avifArrayCreate(&meta->idats, sizeof(avifDecoderItemData), 1);
484 return meta;
485}
486
487static void avifMetaDestroy(avifMeta * meta)
488{
Joe Dragoa72da5b2020-06-15 19:40:17 -0700489 for (uint32_t i = 0; i < meta->items.count; ++i) {
490 avifDecoderItem * item = &meta->items.item[i];
491 avifArrayDestroy(&item->properties);
Joe Dragoa4956902020-08-28 01:24:35 -0700492 avifArrayDestroy(&item->extents);
Joe Dragobe4cbb92020-09-21 12:14:05 -0700493 if (item->ownsMergedExtents) {
494 avifRWDataFree(&item->mergedExtents);
495 }
Joe Dragoa72da5b2020-06-15 19:40:17 -0700496 }
Joe Drago9f2b87b2020-06-03 19:36:38 -0700497 avifArrayDestroy(&meta->items);
498 avifArrayDestroy(&meta->properties);
Joe Dragobe4cbb92020-09-21 12:14:05 -0700499 for (uint32_t i = 0; i < meta->idats.count; ++i) {
500 avifDecoderItemData * idat = &meta->idats.idat[i];
501 avifRWDataFree(&idat->data);
502 }
Joe Drago9f2b87b2020-06-03 19:36:38 -0700503 avifArrayDestroy(&meta->idats);
504 avifFree(meta);
505}
506
507static avifDecoderItem * avifMetaFindItem(avifMeta * meta, uint32_t itemID)
508{
509 if (itemID == 0) {
510 return NULL;
511 }
512
513 for (uint32_t i = 0; i < meta->items.count; ++i) {
514 if (meta->items.item[i].id == itemID) {
515 return &meta->items.item[i];
516 }
517 }
518
519 avifDecoderItem * item = (avifDecoderItem *)avifArrayPushPtr(&meta->items);
Joe Dragoa72da5b2020-06-15 19:40:17 -0700520 avifArrayCreate(&item->properties, sizeof(avifProperty), 16);
Joe Drago4bcdfde2020-11-13 17:50:55 -0800521 avifArrayCreate(&item->extents, sizeof(avifExtent), 1);
Joe Drago9f2b87b2020-06-03 19:36:38 -0700522 item->id = itemID;
523 item->meta = meta;
524 return item;
525}
526
527typedef struct avifDecoderData
528{
Wan-Teh Chang6fc17582020-09-24 15:16:37 -0700529 avifMeta * meta; // The root-level meta box
Joe Dragoae7e2c32019-07-18 15:22:25 -0700530 avifTrackArray tracks;
Joe Drago060d5342020-03-03 10:53:49 -0800531 avifTileArray tiles;
532 unsigned int colorTileCount;
533 unsigned int alphaTileCount;
534 avifImageGrid colorGrid;
535 avifImageGrid alphaGrid;
Joe Drago46ea0582019-07-22 15:55:47 -0700536 avifDecoderSource source;
Wan-Teh Chang306c3062020-04-05 12:17:33 -0700537 const avifSampleTable * sourceSampleTable; // NULL unless (source == AVIF_DECODER_SOURCE_TRACKS), owned by an avifTrack
Joe Dragoc00d5832020-08-13 16:03:28 -0700538 avifBool cicpSet; // True if avifDecoder's image has had its CICP set correctly yet.
539 // This allows nclx colr boxes to override AV1 CICP, as specified in the MIAF
540 // standard (ISO/IEC 23000-22:2019), section 7.3.6.4:
541 //
542 // "The colour information property takes precedence over any colour information in the image
543 // bitstream, i.e. if the property is present, colour information in the bitstream shall be ignored."
Joe Drago800b47f2020-03-18 16:22:37 -0700544} avifDecoderData;
Joe Drago444f0512019-01-23 17:03:24 -0800545
Joe Drago800b47f2020-03-18 16:22:37 -0700546static avifDecoderData * avifDecoderDataCreate()
Joe Drago05559c92019-07-17 16:33:38 -0700547{
Joe Drago800b47f2020-03-18 16:22:37 -0700548 avifDecoderData * data = (avifDecoderData *)avifAlloc(sizeof(avifDecoderData));
549 memset(data, 0, sizeof(avifDecoderData));
Joe Drago9f2b87b2020-06-03 19:36:38 -0700550 data->meta = avifMetaCreate();
Joe Dragoae7e2c32019-07-18 15:22:25 -0700551 avifArrayCreate(&data->tracks, sizeof(avifTrack), 2);
Joe Drago060d5342020-03-03 10:53:49 -0800552 avifArrayCreate(&data->tiles, sizeof(avifTile), 8);
Joe Drago05559c92019-07-17 16:33:38 -0700553 return data;
554}
555
Joe Drago800b47f2020-03-18 16:22:37 -0700556static void avifDecoderDataResetCodec(avifDecoderData * data)
Joe Drago05559c92019-07-17 16:33:38 -0700557{
Joe Drago060d5342020-03-03 10:53:49 -0800558 for (unsigned int i = 0; i < data->tiles.count; ++i) {
559 avifTile * tile = &data->tiles.tile[i];
Joe Drago9d368782020-03-04 17:53:17 -0800560 if (tile->image) {
561 avifImageFreePlanes(tile->image, AVIF_PLANES_ALL); // forget any pointers into codec image buffers
562 }
Joe Drago060d5342020-03-03 10:53:49 -0800563 if (tile->codec) {
564 avifCodecDestroy(tile->codec);
565 tile->codec = NULL;
Joe Drago46ea0582019-07-22 15:55:47 -0700566 }
567 }
568}
569
Joe Drago800b47f2020-03-18 16:22:37 -0700570static avifTile * avifDecoderDataCreateTile(avifDecoderData * data)
Joe Drago060d5342020-03-03 10:53:49 -0800571{
572 avifTile * tile = (avifTile *)avifArrayPushPtr(&data->tiles);
573 tile->image = avifImageCreateEmpty();
574 tile->input = avifCodecDecodeInputCreate();
575 return tile;
576}
577
Joe Dragoa72da5b2020-06-15 19:40:17 -0700578static avifTrack * avifDecoderDataCreateTrack(avifDecoderData * data)
579{
580 avifTrack * track = (avifTrack *)avifArrayPushPtr(&data->tracks);
581 track->meta = avifMetaCreate();
582 return track;
583}
584
Joe Drago800b47f2020-03-18 16:22:37 -0700585static void avifDecoderDataClearTiles(avifDecoderData * data)
Joe Drago060d5342020-03-03 10:53:49 -0800586{
587 for (unsigned int i = 0; i < data->tiles.count; ++i) {
588 avifTile * tile = &data->tiles.tile[i];
589 if (tile->input) {
590 avifCodecDecodeInputDestroy(tile->input);
591 tile->input = NULL;
592 }
593 if (tile->codec) {
594 avifCodecDestroy(tile->codec);
595 tile->codec = NULL;
596 }
597 if (tile->image) {
598 avifImageDestroy(tile->image);
599 tile->image = NULL;
600 }
601 }
602 data->tiles.count = 0;
603 data->colorTileCount = 0;
604 data->alphaTileCount = 0;
605}
606
Joe Drago800b47f2020-03-18 16:22:37 -0700607static void avifDecoderDataDestroy(avifDecoderData * data)
Joe Drago46ea0582019-07-22 15:55:47 -0700608{
Joe Drago9f2b87b2020-06-03 19:36:38 -0700609 avifMetaDestroy(data->meta);
Joe Dragoae7e2c32019-07-18 15:22:25 -0700610 for (uint32_t i = 0; i < data->tracks.count; ++i) {
Joe Dragoa72da5b2020-06-15 19:40:17 -0700611 avifTrack * track = &data->tracks.track[i];
612 if (track->sampleTable) {
613 avifSampleTableDestroy(track->sampleTable);
614 }
615 if (track->meta) {
616 avifMetaDestroy(track->meta);
Joe Dragoae7e2c32019-07-18 15:22:25 -0700617 }
618 }
619 avifArrayDestroy(&data->tracks);
Joe Drago800b47f2020-03-18 16:22:37 -0700620 avifDecoderDataClearTiles(data);
Joe Drago060d5342020-03-03 10:53:49 -0800621 avifArrayDestroy(&data->tiles);
Joe Drago05559c92019-07-17 16:33:38 -0700622 avifFree(data);
623}
624
Joe Drago4bcdfde2020-11-13 17:50:55 -0800625// This returns the max extent that has to be read in order to decode this item. If
626// the item is stored in an idat, the data has already been read during Parse() and
627// this function will return AVIF_RESULT_OK with a 0-byte extent.
628static avifResult avifDecoderItemMaxExtent(const avifDecoderItem * item, avifExtent * outExtent)
629{
630 if (item->extents.count == 0) {
631 return AVIF_RESULT_TRUNCATED_DATA;
632 }
633
634 if (item->idatID != 0) {
635 // construction_method: idat(1)
636
Wan-Teh Chang80275392020-11-17 12:35:11 -0800637 // Find associated idat box
Joe Drago4bcdfde2020-11-13 17:50:55 -0800638 for (uint32_t i = 0; i < item->meta->idats.count; ++i) {
639 if (item->meta->idats.idat[i].id == item->idatID) {
640 // Already read from a meta box during Parse()
641 memset(outExtent, 0, sizeof(avifExtent));
642 return AVIF_RESULT_OK;
643 }
644 }
645
Wan-Teh Chang80275392020-11-17 12:35:11 -0800646 // no associated idat box was found in the meta box, bail out
Joe Drago4bcdfde2020-11-13 17:50:55 -0800647 return AVIF_RESULT_NO_CONTENT;
648 }
649
650 // construction_method: file(0)
651
Wan-Teh Chang15584522020-11-17 14:11:12 -0800652 // Assert that the for loop below will execute at least one iteration.
653 assert(item->extents.count != 0);
654 uint64_t minOffset = UINT64_MAX;
Joe Drago4bcdfde2020-11-13 17:50:55 -0800655 uint64_t maxOffset = 0;
656 for (uint32_t extentIter = 0; extentIter < item->extents.count; ++extentIter) {
657 avifExtent * extent = &item->extents.extent[extentIter];
658
659 if (extent->size > UINT64_MAX - extent->offset) {
660 return AVIF_RESULT_BMFF_PARSE_FAILED;
661 }
662 const uint64_t endOffset = extent->offset + extent->size;
663
Wan-Teh Chang15584522020-11-17 14:11:12 -0800664 if (minOffset > extent->offset) {
Joe Drago4bcdfde2020-11-13 17:50:55 -0800665 minOffset = extent->offset;
Wan-Teh Chang15584522020-11-17 14:11:12 -0800666 }
667 if (maxOffset < endOffset) {
Joe Drago4bcdfde2020-11-13 17:50:55 -0800668 maxOffset = endOffset;
Joe Drago4bcdfde2020-11-13 17:50:55 -0800669 }
670 }
671
672 outExtent->offset = minOffset;
Wan-Teh Changd69958e2020-11-17 12:14:27 -0800673 const uint64_t extentLength = maxOffset - minOffset;
674 if (extentLength > SIZE_MAX) {
675 return AVIF_RESULT_BMFF_PARSE_FAILED;
676 }
677 outExtent->size = (size_t)extentLength;
Joe Drago4bcdfde2020-11-13 17:50:55 -0800678 return AVIF_RESULT_OK;
679}
680
Wan-Teh Chang4548b162020-11-06 11:48:25 -0800681static avifResult avifDecoderItemRead(avifDecoderItem * item, avifIO * io, avifROData * outData, size_t partialByteCount)
Joe Dragof6a42272019-11-21 15:21:41 -0800682{
Joe Dragobe4cbb92020-09-21 12:14:05 -0700683 if (item->mergedExtents.data && !item->partialMergedExtents) {
Joe Dragoa4956902020-08-28 01:24:35 -0700684 // Multiple extents have already been concatenated for this item, just return it
Wan-Teh Chang610b0a12020-10-06 16:47:21 -0700685 memcpy(outData, &item->mergedExtents, sizeof(avifROData));
Joe Dragobe4cbb92020-09-21 12:14:05 -0700686 return AVIF_RESULT_OK;
687 }
688
689 if (item->extents.count == 0) {
690 return AVIF_RESULT_TRUNCATED_DATA;
Joe Dragoa4956902020-08-28 01:24:35 -0700691 }
692
693 // Find this item's source of all extents' data, based on the construction method
Wan-Teh Chang3c304dc2020-10-08 12:07:45 -0700694 const avifRWData * idatBuffer = NULL;
Wan-Teh Chang610b0a12020-10-06 16:47:21 -0700695 if (item->idatID != 0) {
Joe Dragof6a42272019-11-21 15:21:41 -0800696 // construction_method: idat(1)
697
Wan-Teh Chang80275392020-11-17 12:35:11 -0800698 // Find associated idat box
Joe Drago9f2b87b2020-06-03 19:36:38 -0700699 for (uint32_t i = 0; i < item->meta->idats.count; ++i) {
700 if (item->meta->idats.idat[i].id == item->idatID) {
Joe Dragobe4cbb92020-09-21 12:14:05 -0700701 idatBuffer = &item->meta->idats.idat[i].data;
Joe Dragof6a42272019-11-21 15:21:41 -0800702 break;
703 }
704 }
705
Joe Dragobe4cbb92020-09-21 12:14:05 -0700706 if (idatBuffer == NULL) {
Wan-Teh Chang80275392020-11-17 12:35:11 -0800707 // no associated idat box was found in the meta box, bail out
Joe Dragobe4cbb92020-09-21 12:14:05 -0700708 return AVIF_RESULT_NO_CONTENT;
Joe Dragof6a42272019-11-21 15:21:41 -0800709 }
710 }
711
Joe Dragobe4cbb92020-09-21 12:14:05 -0700712 // Merge extents into a single contiguous buffer
Wan-Teh Chang4548b162020-11-06 11:48:25 -0800713 if ((io->sizeHint > 0) && (item->size > io->sizeHint)) {
Wan-Teh Change5003232020-08-28 18:26:58 -0700714 // Sanity check: somehow the sum of extents for this item exceeds the entire file or idat
715 // size!
Joe Dragobe4cbb92020-09-21 12:14:05 -0700716 return AVIF_RESULT_TRUNCATED_DATA;
Joe Drago8f439092020-08-28 15:05:17 -0700717 }
Joe Dragobe4cbb92020-09-21 12:14:05 -0700718
Wan-Teh Chang610b0a12020-10-06 16:47:21 -0700719 size_t totalBytesToRead = item->size;
720 if (partialByteCount && (totalBytesToRead > partialByteCount)) {
721 totalBytesToRead = partialByteCount;
722 }
723
Joe Dragobe4cbb92020-09-21 12:14:05 -0700724 // If there is a single extent for this item and the source of the read buffer is going to be
725 // persistent for the lifetime of the avifDecoder (whether it comes from its own internal
726 // idatBuffer or from a known-persistent IO), we can avoid buffer duplication and just use the
727 // preexisting buffer.
Wan-Teh Chang4548b162020-11-06 11:48:25 -0800728 avifBool singlePersistentBuffer = ((item->extents.count == 1) && (idatBuffer || io->persistent));
Joe Dragobe4cbb92020-09-21 12:14:05 -0700729 if (!singlePersistentBuffer) {
Wan-Teh Chang610b0a12020-10-06 16:47:21 -0700730 avifRWDataRealloc(&item->mergedExtents, totalBytesToRead);
Joe Dragobe4cbb92020-09-21 12:14:05 -0700731 item->ownsMergedExtents = AVIF_TRUE;
732 }
733
734 // Set this until we manage to fill the entire mergedExtents buffer
735 item->partialMergedExtents = AVIF_TRUE;
Joe Drago8f439092020-08-28 15:05:17 -0700736
Joe Dragoa4956902020-08-28 01:24:35 -0700737 uint8_t * front = item->mergedExtents.data;
Joe Dragobe4cbb92020-09-21 12:14:05 -0700738 size_t remainingBytes = totalBytesToRead;
Joe Dragoa4956902020-08-28 01:24:35 -0700739 for (uint32_t extentIter = 0; extentIter < item->extents.count; ++extentIter) {
Joe Drago4bcdfde2020-11-13 17:50:55 -0800740 avifExtent * extent = &item->extents.extent[extentIter];
Joe Dragobe4cbb92020-09-21 12:14:05 -0700741
742 size_t bytesToRead = extent->size;
743 if (bytesToRead > remainingBytes) {
744 bytesToRead = remainingBytes;
Joe Dragoa4956902020-08-28 01:24:35 -0700745 }
746
Joe Dragobe4cbb92020-09-21 12:14:05 -0700747 avifROData offsetBuffer;
748 if (idatBuffer) {
Wan-Teh Chang610b0a12020-10-06 16:47:21 -0700749 if (extent->offset > idatBuffer->size) {
750 return AVIF_RESULT_BMFF_PARSE_FAILED;
751 }
752 if (extent->size > idatBuffer->size - extent->offset) {
753 return AVIF_RESULT_BMFF_PARSE_FAILED;
754 }
755 offsetBuffer.data = idatBuffer->data + extent->offset;
756 offsetBuffer.size = idatBuffer->size - extent->offset;
Joe Dragobe4cbb92020-09-21 12:14:05 -0700757 } else {
758 // construction_method: file(0)
759
Wan-Teh Chang4548b162020-11-06 11:48:25 -0800760 if ((io->sizeHint > 0) && (extent->offset > io->sizeHint)) {
Wan-Teh Changb856dc22020-09-28 13:00:56 -0700761 return AVIF_RESULT_BMFF_PARSE_FAILED;
762 }
Wan-Teh Chang4548b162020-11-06 11:48:25 -0800763 avifResult readResult = io->read(io, 0, extent->offset, bytesToRead, &offsetBuffer);
Joe Dragobe4cbb92020-09-21 12:14:05 -0700764 if (readResult != AVIF_RESULT_OK) {
765 return readResult;
766 }
Wan-Teh Chang3c304dc2020-10-08 12:07:45 -0700767 if (bytesToRead != offsetBuffer.size) {
768 return AVIF_RESULT_TRUNCATED_DATA;
769 }
Joe Dragobe4cbb92020-09-21 12:14:05 -0700770 }
771
772 if (singlePersistentBuffer) {
773 memcpy(&item->mergedExtents, &offsetBuffer, sizeof(avifRWData));
774 item->mergedExtents.size = bytesToRead;
775 } else {
776 memcpy(front, offsetBuffer.data, bytesToRead);
777 front += bytesToRead;
778 }
779
780 remainingBytes -= bytesToRead;
781 if (remainingBytes == 0) {
782 // This happens when partialByteCount is set
783 break;
784 }
Joe Dragoa4956902020-08-28 01:24:35 -0700785 }
786 if (remainingBytes != 0) {
787 // This should be impossible?
Joe Dragobe4cbb92020-09-21 12:14:05 -0700788 return AVIF_RESULT_TRUNCATED_DATA;
Joe Dragof6a42272019-11-21 15:21:41 -0800789 }
Joe Dragobe4cbb92020-09-21 12:14:05 -0700790
791 outData->data = item->mergedExtents.data;
792 outData->size = totalBytesToRead;
793 item->partialMergedExtents = (item->size != totalBytesToRead);
794 return AVIF_RESULT_OK;
Joe Dragof6a42272019-11-21 15:21:41 -0800795}
796
Joe Drago800b47f2020-03-18 16:22:37 -0700797static avifBool avifDecoderDataGenerateImageGridTiles(avifDecoderData * data, avifImageGrid * grid, avifDecoderItem * gridItem, avifBool alpha)
Joe Drago060d5342020-03-03 10:53:49 -0800798{
Wan-Teh Chang7ca3dd92020-11-20 12:50:44 -0800799 unsigned int tilesRequested = grid->rows * grid->columns;
Joe Drago060d5342020-03-03 10:53:49 -0800800
801 // Count number of dimg for this item, bail out if it doesn't match perfectly
802 unsigned int tilesAvailable = 0;
Joe Drago9f2b87b2020-06-03 19:36:38 -0700803 for (uint32_t i = 0; i < gridItem->meta->items.count; ++i) {
804 avifDecoderItem * item = &gridItem->meta->items.item[i];
Joe Drago060d5342020-03-03 10:53:49 -0800805 if (item->dimgForID == gridItem->id) {
806 if (memcmp(item->type, "av01", 4)) {
807 continue;
808 }
Joe Drago3320e5f2020-04-21 17:36:27 -0700809 if (item->hasUnsupportedEssentialProperty) {
Wan-Teh Chang29aaade2020-08-10 16:14:16 -0700810 // An essential property isn't supported by libavif; can't
811 // decode a grid image if any tile in the grid isn't supported.
812 return AVIF_FALSE;
Joe Drago3320e5f2020-04-21 17:36:27 -0700813 }
Joe Drago060d5342020-03-03 10:53:49 -0800814
815 ++tilesAvailable;
816 }
817 }
818
819 if (tilesRequested != tilesAvailable) {
820 return AVIF_FALSE;
821 }
822
Joe Drago9c5f5652020-06-29 15:13:44 -0700823 avifBool firstTile = AVIF_TRUE;
Joe Drago9f2b87b2020-06-03 19:36:38 -0700824 for (uint32_t i = 0; i < gridItem->meta->items.count; ++i) {
825 avifDecoderItem * item = &gridItem->meta->items.item[i];
Joe Drago060d5342020-03-03 10:53:49 -0800826 if (item->dimgForID == gridItem->id) {
827 if (memcmp(item->type, "av01", 4)) {
828 continue;
829 }
830
Joe Drago800b47f2020-03-18 16:22:37 -0700831 avifTile * tile = avifDecoderDataCreateTile(data);
Joe Dragoe3e3bfa2020-06-02 16:33:53 -0700832 avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&tile->input->samples);
Joe Dragobe4cbb92020-09-21 12:14:05 -0700833 sample->itemID = item->id;
834 sample->offset = 0;
835 sample->size = item->size;
Joe Drago060d5342020-03-03 10:53:49 -0800836 sample->sync = AVIF_TRUE;
837 tile->input->alpha = alpha;
Joe Drago9c5f5652020-06-29 15:13:44 -0700838
839 if (firstTile) {
840 firstTile = AVIF_FALSE;
841
842 // Adopt the av1C property of the first av01 tile, so that it can be queried from
843 // the top-level color/alpha item during avifDecoderReset().
844 const avifProperty * srcProp = avifPropertyArrayFind(&item->properties, "av1C");
845 if (!srcProp) {
846 return AVIF_FALSE;
847 }
848 avifProperty * dstProp = (avifProperty *)avifArrayPushPtr(&gridItem->properties);
849 memcpy(dstProp, srcProp, sizeof(avifProperty));
850 }
Joe Drago060d5342020-03-03 10:53:49 -0800851 }
852 }
853 return AVIF_TRUE;
854}
855
Joe Drago800b47f2020-03-18 16:22:37 -0700856static avifBool avifDecoderDataFillImageGrid(avifDecoderData * data,
857 avifImageGrid * grid,
858 avifImage * dstImage,
859 unsigned int firstTileIndex,
860 unsigned int tileCount,
861 avifBool alpha)
Joe Drago060d5342020-03-03 10:53:49 -0800862{
863 if (tileCount == 0) {
864 return AVIF_FALSE;
865 }
866
867 avifTile * firstTile = &data->tiles.tile[firstTileIndex];
Joe Dragoa0da4a42020-05-08 14:27:40 -0700868 avifBool firstTileUVPresent = (firstTile->image->yuvPlanes[AVIF_CHAN_U] && firstTile->image->yuvPlanes[AVIF_CHAN_V]);
psi / Ryo Hirafuji922d8a12020-03-10 03:24:57 +0900869
Joe Dragoa0da4a42020-05-08 14:27:40 -0700870 // Check for tile consistency: All tiles in a grid image should match in the properties checked below.
Joe Drago060d5342020-03-03 10:53:49 -0800871 for (unsigned int i = 1; i < tileCount; ++i) {
872 avifTile * tile = &data->tiles.tile[firstTileIndex + i];
Joe Drago951a0022020-03-09 16:19:44 -0700873 avifBool uvPresent = (tile->image->yuvPlanes[AVIF_CHAN_U] && tile->image->yuvPlanes[AVIF_CHAN_V]);
Joe Dragoa0da4a42020-05-08 14:27:40 -0700874 if ((tile->image->width != firstTile->image->width) || (tile->image->height != firstTile->image->height) ||
875 (tile->image->depth != firstTile->image->depth) || (tile->image->yuvFormat != firstTile->image->yuvFormat) ||
876 (tile->image->yuvRange != firstTile->image->yuvRange) || (uvPresent != firstTileUVPresent) ||
wantehchangbc35a5f2020-08-12 15:27:39 -0700877 (tile->image->colorPrimaries != firstTile->image->colorPrimaries) ||
878 (tile->image->transferCharacteristics != firstTile->image->transferCharacteristics) ||
Wan-Teh Chang5bdead52020-08-12 17:02:22 -0700879 (tile->image->matrixCoefficients != firstTile->image->matrixCoefficients) ||
880 (tile->image->alphaRange != firstTile->image->alphaRange)) {
Joe Drago060d5342020-03-03 10:53:49 -0800881 return AVIF_FALSE;
882 }
883 }
884
wantehchangdf586a82020-08-12 13:06:08 -0700885 // Validate grid image size and tile size.
886 //
887 // HEIF (ISO/IEC 23008-12:2017), Section 6.6.2.3.1:
TYTYb587c592020-12-08 17:42:18 +0800888 // The tiled input images shall completely "cover" the reconstructed image grid canvas, ...
wantehchangbc35a5f2020-08-12 15:27:39 -0700889 if (((firstTile->image->width * grid->columns) < grid->outputWidth) ||
890 ((firstTile->image->height * grid->rows) < grid->outputHeight)) {
wantehchangdf586a82020-08-12 13:06:08 -0700891 return AVIF_FALSE;
892 }
893 // 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 -0700894 if (((firstTile->image->width * (grid->columns - 1)) >= grid->outputWidth) ||
895 ((firstTile->image->height * (grid->rows - 1)) >= grid->outputHeight)) {
wantehchangdf586a82020-08-12 13:06:08 -0700896 return AVIF_FALSE;
897 }
898 // Check the restrictions in MIAF (ISO/IEC 23000-22:2019), Section 7.3.11.4.2.
899 //
900 // 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 -0700901 if ((firstTile->image->width < 64) || (firstTile->image->height < 64)) {
wantehchangdf586a82020-08-12 13:06:08 -0700902 return AVIF_FALSE;
903 }
904 if (!alpha) {
wantehchangbc35a5f2020-08-12 15:27:39 -0700905 if ((firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV422) || (firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV420)) {
wantehchangdf586a82020-08-12 13:06:08 -0700906 // The horizontal tile offsets and widths, and the output width, shall be even numbers.
wantehchangbc35a5f2020-08-12 15:27:39 -0700907 if (((firstTile->image->width & 1) != 0) || ((grid->outputWidth & 1) != 0)) {
wantehchangdf586a82020-08-12 13:06:08 -0700908 return AVIF_FALSE;
909 }
910 }
911 if (firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV420) {
912 // The vertical tile offsets and heights, and the output height, shall be even numbers.
wantehchangbc35a5f2020-08-12 15:27:39 -0700913 if (((firstTile->image->height & 1) != 0) || ((grid->outputHeight & 1) != 0)) {
wantehchangdf586a82020-08-12 13:06:08 -0700914 return AVIF_FALSE;
915 }
916 }
917 }
918
Joe Dragoa0da4a42020-05-08 14:27:40 -0700919 // Lazily populate dstImage with the new frame's properties. If we're decoding alpha,
920 // these values must already match.
921 if ((dstImage->width != grid->outputWidth) || (dstImage->height != grid->outputHeight) ||
Wan-Teh Chang5bdead52020-08-12 17:02:22 -0700922 (dstImage->depth != firstTile->image->depth) || (!alpha && (dstImage->yuvFormat != firstTile->image->yuvFormat))) {
Joe Drago060d5342020-03-03 10:53:49 -0800923 if (alpha) {
924 // Alpha doesn't match size, just bail out
925 return AVIF_FALSE;
926 }
927
928 avifImageFreePlanes(dstImage, AVIF_PLANES_ALL);
929 dstImage->width = grid->outputWidth;
930 dstImage->height = grid->outputHeight;
Joe Dragoa0da4a42020-05-08 14:27:40 -0700931 dstImage->depth = firstTile->image->depth;
932 dstImage->yuvFormat = firstTile->image->yuvFormat;
Joe Dragoc00d5832020-08-13 16:03:28 -0700933 dstImage->yuvRange = firstTile->image->yuvRange;
Joe Dragoa0da4a42020-05-08 14:27:40 -0700934 if (!data->cicpSet) {
935 data->cicpSet = AVIF_TRUE;
Joe Dragoc00d5832020-08-13 16:03:28 -0700936 dstImage->colorPrimaries = firstTile->image->colorPrimaries;
937 dstImage->transferCharacteristics = firstTile->image->transferCharacteristics;
938 dstImage->matrixCoefficients = firstTile->image->matrixCoefficients;
psi / Ryo Hirafuji922d8a12020-03-10 03:24:57 +0900939 }
Joe Drago060d5342020-03-03 10:53:49 -0800940 }
Joe Dragod0eeb182020-05-18 17:23:48 -0700941 if (alpha) {
942 dstImage->alphaRange = firstTile->image->alphaRange;
943 }
Joe Drago060d5342020-03-03 10:53:49 -0800944
945 avifImageAllocatePlanes(dstImage, alpha ? AVIF_PLANES_A : AVIF_PLANES_YUV);
946
947 avifPixelFormatInfo formatInfo;
Joe Drago7a249f52020-08-13 12:58:03 -0700948 avifGetPixelFormatInfo(firstTile->image->yuvFormat, &formatInfo);
Joe Drago060d5342020-03-03 10:53:49 -0800949
950 unsigned int tileIndex = firstTileIndex;
951 size_t pixelBytes = avifImageUsesU16(dstImage) ? 2 : 1;
952 for (unsigned int rowIndex = 0; rowIndex < grid->rows; ++rowIndex) {
953 for (unsigned int colIndex = 0; colIndex < grid->columns; ++colIndex, ++tileIndex) {
954 avifTile * tile = &data->tiles.tile[tileIndex];
955
Joe Dragoa0da4a42020-05-08 14:27:40 -0700956 unsigned int widthToCopy = firstTile->image->width;
957 unsigned int maxX = firstTile->image->width * (colIndex + 1);
Joe Drago060d5342020-03-03 10:53:49 -0800958 if (maxX > grid->outputWidth) {
959 widthToCopy -= maxX - grid->outputWidth;
960 }
961
Joe Dragoa0da4a42020-05-08 14:27:40 -0700962 unsigned int heightToCopy = firstTile->image->height;
963 unsigned int maxY = firstTile->image->height * (rowIndex + 1);
Joe Drago060d5342020-03-03 10:53:49 -0800964 if (maxY > grid->outputHeight) {
965 heightToCopy -= maxY - grid->outputHeight;
966 }
967
968 // Y and A channels
Joe Dragoa0da4a42020-05-08 14:27:40 -0700969 size_t yaColOffset = colIndex * firstTile->image->width;
970 size_t yaRowOffset = rowIndex * firstTile->image->height;
Joe Drago060d5342020-03-03 10:53:49 -0800971 size_t yaRowBytes = widthToCopy * pixelBytes;
972
973 if (alpha) {
974 // A
975 for (unsigned int j = 0; j < heightToCopy; ++j) {
976 uint8_t * src = &tile->image->alphaPlane[j * tile->image->alphaRowBytes];
977 uint8_t * dst = &dstImage->alphaPlane[(yaColOffset * pixelBytes) + ((yaRowOffset + j) * dstImage->alphaRowBytes)];
978 memcpy(dst, src, yaRowBytes);
979 }
980 } else {
981 // Y
982 for (unsigned int j = 0; j < heightToCopy; ++j) {
983 uint8_t * src = &tile->image->yuvPlanes[AVIF_CHAN_Y][j * tile->image->yuvRowBytes[AVIF_CHAN_Y]];
984 uint8_t * dst =
985 &dstImage->yuvPlanes[AVIF_CHAN_Y][(yaColOffset * pixelBytes) + ((yaRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_Y])];
986 memcpy(dst, src, yaRowBytes);
987 }
988
Joe Dragoa0da4a42020-05-08 14:27:40 -0700989 if (!firstTileUVPresent) {
Joe Drago060d5342020-03-03 10:53:49 -0800990 continue;
991 }
992
993 // UV
Joe Drago060d5342020-03-03 10:53:49 -0800994 heightToCopy >>= formatInfo.chromaShiftY;
995 size_t uvColOffset = yaColOffset >> formatInfo.chromaShiftX;
996 size_t uvRowOffset = yaRowOffset >> formatInfo.chromaShiftY;
997 size_t uvRowBytes = yaRowBytes >> formatInfo.chromaShiftX;
998 for (unsigned int j = 0; j < heightToCopy; ++j) {
999 uint8_t * srcU = &tile->image->yuvPlanes[AVIF_CHAN_U][j * tile->image->yuvRowBytes[AVIF_CHAN_U]];
1000 uint8_t * dstU =
1001 &dstImage->yuvPlanes[AVIF_CHAN_U][(uvColOffset * pixelBytes) + ((uvRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_U])];
1002 memcpy(dstU, srcU, uvRowBytes);
1003
1004 uint8_t * srcV = &tile->image->yuvPlanes[AVIF_CHAN_V][j * tile->image->yuvRowBytes[AVIF_CHAN_V]];
1005 uint8_t * dstV =
1006 &dstImage->yuvPlanes[AVIF_CHAN_V][(uvColOffset * pixelBytes) + ((uvRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_V])];
1007 memcpy(dstV, srcV, uvRowBytes);
1008 }
1009 }
1010 }
1011 }
1012
1013 return AVIF_TRUE;
1014}
1015
Joe Drago39267fd2020-06-19 15:21:14 -07001016// If colorId == 0 (a sentinel value as item IDs must be nonzero), accept any found EXIF/XMP metadata. Passing in 0
1017// 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 -07001018// inside of a trak box are implicitly associated to the track.
Joe Dragobe4cbb92020-09-21 12:14:05 -07001019static avifResult avifDecoderFindMetadata(avifDecoder * decoder, avifMeta * meta, avifImage * image, uint32_t colorId)
Joe Dragoa72da5b2020-06-15 19:40:17 -07001020{
Joe Dragobe4cbb92020-09-21 12:14:05 -07001021 if (decoder->ignoreExif && decoder->ignoreXMP) {
1022 // Nothing to do!
1023 return AVIF_RESULT_OK;
1024 }
Joe Dragoa72da5b2020-06-15 19:40:17 -07001025
1026 for (uint32_t itemIndex = 0; itemIndex < meta->items.count; ++itemIndex) {
1027 avifDecoderItem * item = &meta->items.item[itemIndex];
Joe Dragoba1eb492020-06-22 17:05:04 -07001028 if (!item->size) {
1029 continue;
Joe Dragoa72da5b2020-06-15 19:40:17 -07001030 }
1031 if (item->hasUnsupportedEssentialProperty) {
1032 // An essential property isn't supported by libavif; ignore the item.
1033 continue;
1034 }
1035
1036 if ((colorId > 0) && (item->descForID != colorId)) {
1037 // Not a content description (metadata) for the colorOBU, skip it
1038 continue;
1039 }
1040
Joe Dragobe4cbb92020-09-21 12:14:05 -07001041 if (!decoder->ignoreExif && !memcmp(item->type, "Exif", 4)) {
1042 avifROData exifContents;
Wan-Teh Chang4548b162020-11-06 11:48:25 -08001043 avifResult readResult = avifDecoderItemRead(item, decoder->io, &exifContents, 0);
Joe Dragobe4cbb92020-09-21 12:14:05 -07001044 if (readResult != AVIF_RESULT_OK) {
1045 return readResult;
1046 }
1047
Joe Dragoa72da5b2020-06-15 19:40:17 -07001048 // Advance past Annex A.2.1's header
Joe Dragobe4cbb92020-09-21 12:14:05 -07001049 BEGIN_STREAM(exifBoxStream, exifContents.data, exifContents.size);
Joe Dragoa72da5b2020-06-15 19:40:17 -07001050 uint32_t exifTiffHeaderOffset;
Wan-Teh Changf1d60192020-10-07 16:23:08 -07001051 CHECKERR(avifROStreamReadU32(&exifBoxStream, &exifTiffHeaderOffset), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) exif_tiff_header_offset;
Joe Dragoa72da5b2020-06-15 19:40:17 -07001052
Joe Dragobe4cbb92020-09-21 12:14:05 -07001053 avifImageSetMetadataExif(image, avifROStreamCurrent(&exifBoxStream), avifROStreamRemainingBytes(&exifBoxStream));
1054 } else if (!decoder->ignoreXMP && !memcmp(item->type, "mime", 4) &&
1055 !memcmp(item->contentType.contentType, xmpContentType, xmpContentTypeSize)) {
1056 avifROData xmpContents;
Wan-Teh Chang4548b162020-11-06 11:48:25 -08001057 avifResult readResult = avifDecoderItemRead(item, decoder->io, &xmpContents, 0);
Joe Dragobe4cbb92020-09-21 12:14:05 -07001058 if (readResult != AVIF_RESULT_OK) {
1059 return readResult;
1060 }
1061
1062 avifImageSetMetadataXMP(image, xmpContents.data, xmpContents.size);
Joe Dragoa72da5b2020-06-15 19:40:17 -07001063 }
1064 }
Joe Dragobe4cbb92020-09-21 12:14:05 -07001065 return AVIF_RESULT_OK;
Joe Dragoa72da5b2020-06-15 19:40:17 -07001066}
1067
Joe Drago8f7a3002019-02-07 19:35:37 -08001068// ---------------------------------------------------------------------------
Joe Dragocd1e4c32019-02-08 11:26:31 -08001069// URN
1070
Joe Dragoa72da5b2020-06-15 19:40:17 -07001071static avifBool isAlphaURN(const char * urn)
Joe Dragocd1e4c32019-02-08 11:26:31 -08001072{
wantehchang3fde0d02020-03-10 23:58:32 -07001073 return !strcmp(urn, URN_ALPHA0) || !strcmp(urn, URN_ALPHA1);
Joe Dragocd1e4c32019-02-08 11:26:31 -08001074}
1075
1076// ---------------------------------------------------------------------------
Joe Drago8f7a3002019-02-07 19:35:37 -08001077// BMFF Parsing
1078
Joe Drago9f2b87b2020-06-03 19:36:38 -07001079static avifBool avifParseItemLocationBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago444f0512019-01-23 17:03:24 -08001080{
Joe Drago8f7a3002019-02-07 19:35:37 -08001081 BEGIN_STREAM(s, raw, rawLen);
Joe Drago444f0512019-01-23 17:03:24 -08001082
Joe Dragof6a42272019-11-21 15:21:41 -08001083 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -07001084 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Dragof6a42272019-11-21 15:21:41 -08001085 if (version > 2) {
1086 return AVIF_FALSE;
1087 }
Joe Drago444f0512019-01-23 17:03:24 -08001088
Joe Drago8f7a3002019-02-07 19:35:37 -08001089 uint8_t offsetSizeAndLengthSize;
Joe Drago345aaa12019-09-25 13:42:12 -07001090 CHECK(avifROStreamRead(&s, &offsetSizeAndLengthSize, 1));
Joe Drago8f7a3002019-02-07 19:35:37 -08001091 uint8_t offsetSize = (offsetSizeAndLengthSize >> 4) & 0xf; // unsigned int(4) offset_size;
1092 uint8_t lengthSize = (offsetSizeAndLengthSize >> 0) & 0xf; // unsigned int(4) length_size;
Joe Drago444f0512019-01-23 17:03:24 -08001093
Joe Dragof6a42272019-11-21 15:21:41 -08001094 uint8_t baseOffsetSizeAndIndexSize;
1095 CHECK(avifROStreamRead(&s, &baseOffsetSizeAndIndexSize, 1));
1096 uint8_t baseOffsetSize = (baseOffsetSizeAndIndexSize >> 4) & 0xf; // unsigned int(4) base_offset_size;
1097 uint8_t indexSize = 0;
1098 if ((version == 1) || (version == 2)) {
1099 indexSize = baseOffsetSizeAndIndexSize & 0xf; // unsigned int(4) index_size;
1100 if (indexSize != 0) {
1101 // extent_index unsupported
1102 return AVIF_FALSE;
1103 }
1104 }
Joe Drago444f0512019-01-23 17:03:24 -08001105
Joe Dragof6a42272019-11-21 15:21:41 -08001106 uint16_t tmp16;
1107 uint32_t itemCount;
1108 if (version < 2) {
1109 CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_count;
1110 itemCount = tmp16;
1111 } else {
1112 CHECK(avifROStreamReadU32(&s, &itemCount)); // unsigned int(32) item_count;
1113 }
1114 for (uint32_t i = 0; i < itemCount; ++i) {
1115 uint32_t itemID;
1116 uint32_t idatID = 0;
1117 if (version < 2) {
1118 CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID;
1119 itemID = tmp16;
1120 } else {
1121 CHECK(avifROStreamReadU32(&s, &itemID)); // unsigned int(32) item_ID;
1122 }
1123
1124 if ((version == 1) || (version == 2)) {
1125 uint8_t ignored;
1126 uint8_t constructionMethod;
1127 CHECK(avifROStreamRead(&s, &ignored, 1)); // unsigned int(12) reserved = 0;
1128 CHECK(avifROStreamRead(&s, &constructionMethod, 1)); // unsigned int(4) construction_method;
1129 constructionMethod = constructionMethod & 0xf;
1130 if ((constructionMethod != 0 /* file */) && (constructionMethod != 1 /* idat */)) {
1131 // construction method item(2) unsupported
1132 return AVIF_FALSE;
1133 }
1134 if (constructionMethod == 1) {
Joe Dragoba1eb492020-06-22 17:05:04 -07001135 idatID = meta->idatID;
Joe Dragof6a42272019-11-21 15:21:41 -08001136 }
1137 }
1138
Joe Dragoa4956902020-08-28 01:24:35 -07001139 avifDecoderItem * item = avifMetaFindItem(meta, itemID);
1140 if (!item) {
1141 return AVIF_FALSE;
1142 }
Joe Drago8f439092020-08-28 15:05:17 -07001143 if (item->extents.count > 0) {
1144 // This item has already been given extents via this iloc box. This is invalid.
1145 return AVIF_FALSE;
1146 }
Joe Dragoa4956902020-08-28 01:24:35 -07001147 item->idatID = idatID;
1148
Joe Drago345aaa12019-09-25 13:42:12 -07001149 uint16_t dataReferenceIndex; // unsigned int(16) data_ref rence_index;
1150 CHECK(avifROStreamReadU16(&s, &dataReferenceIndex)); //
1151 uint64_t baseOffset; // unsigned int(base_offset_size*8) base_offset;
1152 CHECK(avifROStreamReadUX8(&s, &baseOffset, baseOffsetSize)); //
1153 uint16_t extentCount; // unsigned int(16) extent_count;
1154 CHECK(avifROStreamReadU16(&s, &extentCount)); //
Joe Dragoa4956902020-08-28 01:24:35 -07001155 for (int extentIter = 0; extentIter < extentCount; ++extentIter) {
Joe Dragof6a42272019-11-21 15:21:41 -08001156 // If extent_index is ever supported, this spec must be implemented here:
1157 // :: if (((version == 1) || (version == 2)) && (index_size > 0)) {
1158 // :: unsigned int(index_size*8) extent_index;
1159 // :: }
1160
Joe Drago8f7a3002019-02-07 19:35:37 -08001161 uint64_t extentOffset; // unsigned int(offset_size*8) extent_offset;
Joe Drago345aaa12019-09-25 13:42:12 -07001162 CHECK(avifROStreamReadUX8(&s, &extentOffset, offsetSize));
Joe Drago8f7a3002019-02-07 19:35:37 -08001163 uint64_t extentLength; // unsigned int(offset_size*8) extent_length;
Joe Drago345aaa12019-09-25 13:42:12 -07001164 CHECK(avifROStreamReadUX8(&s, &extentLength, lengthSize));
Joe Drago444f0512019-01-23 17:03:24 -08001165
Joe Drago4bcdfde2020-11-13 17:50:55 -08001166 avifExtent * extent = (avifExtent *)avifArrayPushPtr(&item->extents);
Wan-Teh Changb9853692020-08-28 15:32:36 -07001167 if (extentOffset > UINT64_MAX - baseOffset) {
1168 return AVIF_FALSE;
1169 }
1170 uint64_t offset = baseOffset + extentOffset;
Joe Drago217056b2020-11-13 16:19:35 -08001171 extent->offset = offset;
1172 if (extentLength > SIZE_MAX) {
Wan-Teh Changb9853692020-08-28 15:32:36 -07001173 return AVIF_FALSE;
1174 }
Joe Drago217056b2020-11-13 16:19:35 -08001175 extent->size = (size_t)extentLength;
1176 if (extent->size > SIZE_MAX - item->size) {
Wan-Teh Changb9853692020-08-28 15:32:36 -07001177 return AVIF_FALSE;
1178 }
Joe Dragoa4956902020-08-28 01:24:35 -07001179 item->size += extent->size;
Joe Drago444f0512019-01-23 17:03:24 -08001180 }
1181 }
1182 return AVIF_TRUE;
1183}
1184
Joe Drago060d5342020-03-03 10:53:49 -08001185static avifBool avifParseImageGridBox(avifImageGrid * grid, const uint8_t * raw, size_t rawLen)
1186{
1187 BEGIN_STREAM(s, raw, rawLen);
1188
1189 uint8_t version, flags;
1190 CHECK(avifROStreamRead(&s, &version, 1)); // unsigned int(8) version = 0;
1191 if (version != 0) {
1192 return AVIF_FALSE;
1193 }
Joe Drago79ebcf32020-11-18 22:37:10 -08001194 uint8_t rowsMinusOne, columnsMinusOne;
1195 CHECK(avifROStreamRead(&s, &flags, 1)); // unsigned int(8) flags;
1196 CHECK(avifROStreamRead(&s, &rowsMinusOne, 1)); // unsigned int(8) rows_minus_one;
1197 CHECK(avifROStreamRead(&s, &columnsMinusOne, 1)); // unsigned int(8) columns_minus_one;
1198 grid->rows = (uint32_t)rowsMinusOne + 1;
1199 grid->columns = (uint32_t)columnsMinusOne + 1;
Joe Drago060d5342020-03-03 10:53:49 -08001200
1201 uint32_t fieldLength = ((flags & 1) + 1) * 16;
1202 if (fieldLength == 16) {
1203 uint16_t outputWidth16, outputHeight16;
1204 CHECK(avifROStreamReadU16(&s, &outputWidth16)); // unsigned int(FieldLength) output_width;
1205 CHECK(avifROStreamReadU16(&s, &outputHeight16)); // unsigned int(FieldLength) output_height;
1206 grid->outputWidth = outputWidth16;
1207 grid->outputHeight = outputHeight16;
1208 } else {
1209 if (fieldLength != 32) {
1210 // This should be impossible
1211 return AVIF_FALSE;
1212 }
1213 CHECK(avifROStreamReadU32(&s, &grid->outputWidth)); // unsigned int(FieldLength) output_width;
1214 CHECK(avifROStreamReadU32(&s, &grid->outputHeight)); // unsigned int(FieldLength) output_height;
1215 }
wantehchangbc35a5f2020-08-12 15:27:39 -07001216 if ((grid->outputWidth == 0) || (grid->outputHeight == 0) || (grid->outputWidth > (AVIF_MAX_IMAGE_SIZE / grid->outputHeight))) {
Wan-Teh Chang0a8e7242020-08-10 13:24:59 -07001217 return AVIF_FALSE;
1218 }
wantehchangae2074b2020-08-12 13:02:27 -07001219 return avifROStreamRemainingBytes(&s) == 0;
Joe Drago060d5342020-03-03 10:53:49 -08001220}
1221
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001222static avifBool avifParseImageSpatialExtentsProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001223{
1224 BEGIN_STREAM(s, raw, rawLen);
Joe Drago345aaa12019-09-25 13:42:12 -07001225 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Drago8f7a3002019-02-07 19:35:37 -08001226
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001227 avifImageSpatialExtents * ispe = &prop->u.ispe;
1228 CHECK(avifROStreamReadU32(&s, &ispe->width));
1229 CHECK(avifROStreamReadU32(&s, &ispe->height));
Joe Drago8f7a3002019-02-07 19:35:37 -08001230 return AVIF_TRUE;
1231}
1232
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001233static avifBool avifParseAuxiliaryTypeProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Dragocd1e4c32019-02-08 11:26:31 -08001234{
1235 BEGIN_STREAM(s, raw, rawLen);
Joe Drago345aaa12019-09-25 13:42:12 -07001236 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragocd1e4c32019-02-08 11:26:31 -08001237
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001238 CHECK(avifROStreamReadString(&s, prop->u.auxC.auxType, AUXTYPE_SIZE));
Joe Dragocd1e4c32019-02-08 11:26:31 -08001239 return AVIF_TRUE;
1240}
1241
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001242static avifBool avifParseColourInformationBox(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago41eb62b2019-02-08 15:38:18 -08001243{
1244 BEGIN_STREAM(s, raw, rawLen);
Joe Dragoe7ce20d2019-02-11 16:37:38 -08001245
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001246 avifColourInformationBox * colr = &prop->u.colr;
1247 colr->hasICC = AVIF_FALSE;
1248 colr->hasNCLX = AVIF_FALSE;
Joe Dragoe7ce20d2019-02-11 16:37:38 -08001249
Joe Dragoa0da4a42020-05-08 14:27:40 -07001250 uint8_t colorType[4]; // unsigned int(32) colour_type;
1251 CHECK(avifROStreamRead(&s, colorType, 4));
1252 if (!memcmp(colorType, "rICC", 4) || !memcmp(colorType, "prof", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001253 colr->hasICC = AVIF_TRUE;
1254 colr->icc = avifROStreamCurrent(&s);
1255 colr->iccSize = avifROStreamRemainingBytes(&s);
Joe Dragoa0da4a42020-05-08 14:27:40 -07001256 } else if (!memcmp(colorType, "nclx", 4)) {
Joe Dragofb5a5f02021-01-31 20:43:57 -08001257 CHECK(avifROStreamReadU16(&s, &colr->colorPrimaries)); // unsigned int(16) colour_primaries;
1258 CHECK(avifROStreamReadU16(&s, &colr->transferCharacteristics)); // unsigned int(16) transfer_characteristics;
1259 CHECK(avifROStreamReadU16(&s, &colr->matrixCoefficients)); // unsigned int(16) matrix_coefficients;
Joe Drago97b071c2019-07-17 14:24:56 -07001260 // unsigned int(1) full_range_flag;
1261 // unsigned int(7) reserved = 0;
Joe Drago74cd1c92020-04-16 12:17:11 -07001262 uint8_t tmp8;
1263 CHECK(avifROStreamRead(&s, &tmp8, 1));
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001264 colr->range = (tmp8 & 0x80) ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED;
1265 colr->hasNCLX = AVIF_TRUE;
Joe Drago41eb62b2019-02-08 15:38:18 -08001266 }
1267 return AVIF_TRUE;
1268}
1269
Joe Drago6500fd62019-10-08 17:17:34 -07001270static avifBool avifParseAV1CodecConfigurationBox(const uint8_t * raw, size_t rawLen, avifCodecConfigurationBox * av1C)
1271{
1272 BEGIN_STREAM(s, raw, rawLen);
1273
1274 uint8_t markerAndVersion = 0;
1275 CHECK(avifROStreamRead(&s, &markerAndVersion, 1));
1276 uint8_t seqProfileAndIndex = 0;
1277 CHECK(avifROStreamRead(&s, &seqProfileAndIndex, 1));
1278 uint8_t rawFlags = 0;
1279 CHECK(avifROStreamRead(&s, &rawFlags, 1));
1280
1281 if (markerAndVersion != 0x81) {
1282 // Marker and version must both == 1
1283 return AVIF_FALSE;
1284 }
1285
1286 av1C->seqProfile = (seqProfileAndIndex >> 5) & 0x7; // unsigned int (3) seq_profile;
1287 av1C->seqLevelIdx0 = (seqProfileAndIndex >> 0) & 0x1f; // unsigned int (5) seq_level_idx_0;
1288 av1C->seqTier0 = (rawFlags >> 7) & 0x1; // unsigned int (1) seq_tier_0;
1289 av1C->highBitdepth = (rawFlags >> 6) & 0x1; // unsigned int (1) high_bitdepth;
1290 av1C->twelveBit = (rawFlags >> 5) & 0x1; // unsigned int (1) twelve_bit;
1291 av1C->monochrome = (rawFlags >> 4) & 0x1; // unsigned int (1) monochrome;
1292 av1C->chromaSubsamplingX = (rawFlags >> 3) & 0x1; // unsigned int (1) chroma_subsampling_x;
1293 av1C->chromaSubsamplingY = (rawFlags >> 2) & 0x1; // unsigned int (1) chroma_subsampling_y;
1294 av1C->chromaSamplePosition = (rawFlags >> 0) & 0x3; // unsigned int (2) chroma_sample_position;
1295 return AVIF_TRUE;
1296}
1297
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001298static avifBool avifParseAV1CodecConfigurationBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago6500fd62019-10-08 17:17:34 -07001299{
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001300 return avifParseAV1CodecConfigurationBox(raw, rawLen, &prop->u.av1C);
Joe Drago6500fd62019-10-08 17:17:34 -07001301}
1302
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001303static avifBool avifParsePixelAspectRatioBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001304{
1305 BEGIN_STREAM(s, raw, rawLen);
1306
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001307 avifPixelAspectRatioBox * pasp = &prop->u.pasp;
Joe Drago89f0cc82020-03-09 16:13:27 -07001308 CHECK(avifROStreamReadU32(&s, &pasp->hSpacing)); // unsigned int(32) hSpacing;
1309 CHECK(avifROStreamReadU32(&s, &pasp->vSpacing)); // unsigned int(32) vSpacing;
1310 return AVIF_TRUE;
1311}
1312
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001313static avifBool avifParseCleanApertureBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001314{
1315 BEGIN_STREAM(s, raw, rawLen);
1316
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001317 avifCleanApertureBox * clap = &prop->u.clap;
Joe Drago89f0cc82020-03-09 16:13:27 -07001318 CHECK(avifROStreamReadU32(&s, &clap->widthN)); // unsigned int(32) cleanApertureWidthN;
1319 CHECK(avifROStreamReadU32(&s, &clap->widthD)); // unsigned int(32) cleanApertureWidthD;
1320 CHECK(avifROStreamReadU32(&s, &clap->heightN)); // unsigned int(32) cleanApertureHeightN;
1321 CHECK(avifROStreamReadU32(&s, &clap->heightD)); // unsigned int(32) cleanApertureHeightD;
1322 CHECK(avifROStreamReadU32(&s, &clap->horizOffN)); // unsigned int(32) horizOffN;
1323 CHECK(avifROStreamReadU32(&s, &clap->horizOffD)); // unsigned int(32) horizOffD;
1324 CHECK(avifROStreamReadU32(&s, &clap->vertOffN)); // unsigned int(32) vertOffN;
1325 CHECK(avifROStreamReadU32(&s, &clap->vertOffD)); // unsigned int(32) vertOffD;
1326 return AVIF_TRUE;
1327}
1328
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001329static avifBool avifParseImageRotationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001330{
1331 BEGIN_STREAM(s, raw, rawLen);
1332
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001333 avifImageRotation * irot = &prop->u.irot;
Joe Drago89f0cc82020-03-09 16:13:27 -07001334 CHECK(avifROStreamRead(&s, &irot->angle, 1)); // unsigned int (6) reserved = 0; unsigned int (2) angle;
1335 if ((irot->angle & 0xfc) != 0) {
1336 // reserved bits must be 0
1337 return AVIF_FALSE;
1338 }
1339 return AVIF_TRUE;
1340}
1341
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001342static avifBool avifParseImageMirrorProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001343{
1344 BEGIN_STREAM(s, raw, rawLen);
1345
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001346 avifImageMirror * imir = &prop->u.imir;
Joe Drago89f0cc82020-03-09 16:13:27 -07001347 CHECK(avifROStreamRead(&s, &imir->axis, 1)); // unsigned int (7) reserved = 0; unsigned int (1) axis;
1348 if ((imir->axis & 0xfe) != 0) {
1349 // reserved bits must be 0
1350 return AVIF_FALSE;
1351 }
1352 return AVIF_TRUE;
1353}
1354
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001355static avifBool avifParsePixelInformationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago60421562020-04-23 11:32:26 -07001356{
1357 BEGIN_STREAM(s, raw, rawLen);
1358 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
1359
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001360 avifPixelInformationProperty * pixi = &prop->u.pixi;
Joe Drago60421562020-04-23 11:32:26 -07001361 CHECK(avifROStreamRead(&s, &pixi->planeCount, 1)); // unsigned int (8) num_channels;
1362 if (pixi->planeCount > MAX_PIXI_PLANE_DEPTHS) {
1363 return AVIF_FALSE;
1364 }
1365 for (uint8_t i = 0; i < pixi->planeCount; ++i) {
1366 CHECK(avifROStreamRead(&s, &pixi->planeDepths[i], 1)); // unsigned int (8) bits_per_channel;
1367 }
1368 return AVIF_TRUE;
1369}
1370
Joe Dragoa72da5b2020-06-15 19:40:17 -07001371static avifBool avifParseItemPropertyContainerBox(avifPropertyArray * properties, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001372{
1373 BEGIN_STREAM(s, raw, rawLen);
1374
Joe Drago345aaa12019-09-25 13:42:12 -07001375 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001376 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001377 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago8f7a3002019-02-07 19:35:37 -08001378
Joe Dragoa72da5b2020-06-15 19:40:17 -07001379 int propertyIndex = avifArrayPushIndex(properties);
1380 avifProperty * prop = &properties->prop[propertyIndex];
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001381 memcpy(prop->type, header.type, 4);
Joe Drago8f7a3002019-02-07 19:35:37 -08001382 if (!memcmp(header.type, "ispe", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001383 CHECK(avifParseImageSpatialExtentsProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001384 } else if (!memcmp(header.type, "auxC", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001385 CHECK(avifParseAuxiliaryTypeProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001386 } else if (!memcmp(header.type, "colr", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001387 CHECK(avifParseColourInformationBox(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001388 } else if (!memcmp(header.type, "av1C", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001389 CHECK(avifParseAV1CodecConfigurationBoxProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001390 } else if (!memcmp(header.type, "pasp", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001391 CHECK(avifParsePixelAspectRatioBoxProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001392 } else if (!memcmp(header.type, "clap", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001393 CHECK(avifParseCleanApertureBoxProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001394 } else if (!memcmp(header.type, "irot", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001395 CHECK(avifParseImageRotationProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001396 } else if (!memcmp(header.type, "imir", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001397 CHECK(avifParseImageMirrorProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001398 } else if (!memcmp(header.type, "pixi", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001399 CHECK(avifParsePixelInformationProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Drago60421562020-04-23 11:32:26 -07001400 }
Joe Drago8f7a3002019-02-07 19:35:37 -08001401
Joe Drago345aaa12019-09-25 13:42:12 -07001402 CHECK(avifROStreamSkip(&s, header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001403 }
1404 return AVIF_TRUE;
1405}
1406
Joe Drago9f2b87b2020-06-03 19:36:38 -07001407static avifBool avifParseItemPropertyAssociation(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001408{
1409 BEGIN_STREAM(s, raw, rawLen);
1410
1411 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -07001412 uint32_t flags;
1413 CHECK(avifROStreamReadVersionAndFlags(&s, &version, &flags));
1414 avifBool propertyIndexIsU16 = ((flags & 0x1) != 0);
Joe Drago8f7a3002019-02-07 19:35:37 -08001415
1416 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001417 CHECK(avifROStreamReadU32(&s, &entryCount));
Wan-Teh Changf4c65382020-11-03 14:27:45 -08001418 unsigned int prevItemID = 0;
Joe Drago8f7a3002019-02-07 19:35:37 -08001419 for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) {
Wan-Teh Changf4c65382020-11-03 14:27:45 -08001420 // ISO/IEC 23008-12, First edition, 2017-12, Section 9.3.1:
1421 // Each ItemPropertyAssociation box shall be ordered by increasing item_ID, and there shall
1422 // be at most one association box for each item_ID, in any ItemPropertyAssociation box.
Joe Drago8f7a3002019-02-07 19:35:37 -08001423 unsigned int itemID;
1424 if (version < 1) {
1425 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001426 CHECK(avifROStreamReadU16(&s, &tmp));
Joe Drago8f7a3002019-02-07 19:35:37 -08001427 itemID = tmp;
1428 } else {
Joe Drago345aaa12019-09-25 13:42:12 -07001429 CHECK(avifROStreamReadU32(&s, &itemID));
Joe Drago8f7a3002019-02-07 19:35:37 -08001430 }
Wan-Teh Changf4c65382020-11-03 14:27:45 -08001431 if (itemID <= prevItemID) {
1432 return AVIF_FALSE;
1433 }
1434 prevItemID = itemID;
Wan-Teh Chang750a1922020-10-28 17:54:35 -07001435
1436 avifDecoderItem * item = avifMetaFindItem(meta, itemID);
1437 if (!item) {
1438 return AVIF_FALSE;
1439 }
Wan-Teh Changf4c65382020-11-03 14:27:45 -08001440 if (item->ipmaSeen) {
1441 return AVIF_FALSE;
1442 }
1443 item->ipmaSeen = AVIF_TRUE;
Wan-Teh Chang750a1922020-10-28 17:54:35 -07001444
Joe Drago8f7a3002019-02-07 19:35:37 -08001445 uint8_t associationCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001446 CHECK(avifROStreamRead(&s, &associationCount, 1));
Joe Drago8f7a3002019-02-07 19:35:37 -08001447 for (uint8_t associationIndex = 0; associationIndex < associationCount; ++associationIndex) {
Joe Drago3320e5f2020-04-21 17:36:27 -07001448 avifBool essential = AVIF_FALSE;
Joe Drago8f7a3002019-02-07 19:35:37 -08001449 uint16_t propertyIndex = 0;
1450 if (propertyIndexIsU16) {
Joe Drago345aaa12019-09-25 13:42:12 -07001451 CHECK(avifROStreamReadU16(&s, &propertyIndex));
Joe Drago3320e5f2020-04-21 17:36:27 -07001452 essential = ((propertyIndex & 0x8000) != 0);
Joe Drago8f7a3002019-02-07 19:35:37 -08001453 propertyIndex &= 0x7fff;
1454 } else {
1455 uint8_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001456 CHECK(avifROStreamRead(&s, &tmp, 1));
Joe Drago3320e5f2020-04-21 17:36:27 -07001457 essential = ((tmp & 0x80) != 0);
Joe Drago8f7a3002019-02-07 19:35:37 -08001458 propertyIndex = tmp & 0x7f;
1459 }
1460
1461 if (propertyIndex == 0) {
1462 // Not associated with any item
1463 continue;
1464 }
1465 --propertyIndex; // 1-indexed
1466
Joe Drago9f2b87b2020-06-03 19:36:38 -07001467 if (propertyIndex >= meta->properties.count) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001468 return AVIF_FALSE;
1469 }
1470
Joe Dragoa72da5b2020-06-15 19:40:17 -07001471 // Copy property to item
1472 avifProperty * srcProp = &meta->properties.prop[propertyIndex];
1473
1474 static const char * supportedTypes[] = { "ispe", "auxC", "colr", "av1C", "pasp", "clap", "irot", "imir", "pixi" };
1475 size_t supportedTypesCount = sizeof(supportedTypes) / sizeof(supportedTypes[0]);
1476 avifBool supportedType = AVIF_FALSE;
1477 for (size_t i = 0; i < supportedTypesCount; ++i) {
1478 if (!memcmp(srcProp->type, supportedTypes[i], 4)) {
1479 supportedType = AVIF_TRUE;
1480 break;
1481 }
1482 }
1483 if (supportedType) {
1484 avifProperty * dstProp = (avifProperty *)avifArrayPushPtr(&item->properties);
1485 memcpy(dstProp, srcProp, sizeof(avifProperty));
Joe Drago3320e5f2020-04-21 17:36:27 -07001486 } else {
1487 if (essential) {
1488 // Discovered an essential item property that libavif doesn't support!
1489 // Make a note to ignore this item later.
1490 item->hasUnsupportedEssentialProperty = AVIF_TRUE;
1491 }
Joe Drago8f7a3002019-02-07 19:35:37 -08001492 }
1493 }
1494 }
1495
1496 return AVIF_TRUE;
1497}
1498
Joe Drago9f2b87b2020-06-03 19:36:38 -07001499static avifBool avifParsePrimaryItemBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Dragof6a42272019-11-21 15:21:41 -08001500{
Joe Drago9f2b87b2020-06-03 19:36:38 -07001501 if (meta->primaryItemID > 0) {
Joe Dragof6a42272019-11-21 15:21:41 -08001502 // Illegal to have multiple pitm boxes, bail out
1503 return AVIF_FALSE;
1504 }
1505
1506 BEGIN_STREAM(s, raw, rawLen);
1507
1508 uint8_t version;
1509 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
1510
1511 if (version == 0) {
1512 uint16_t tmp16;
1513 CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID;
Joe Drago9f2b87b2020-06-03 19:36:38 -07001514 meta->primaryItemID = tmp16;
Joe Dragof6a42272019-11-21 15:21:41 -08001515 } else {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001516 CHECK(avifROStreamReadU32(&s, &meta->primaryItemID)); // unsigned int(32) item_ID;
Joe Dragof6a42272019-11-21 15:21:41 -08001517 }
1518 return AVIF_TRUE;
1519}
1520
Joe Drago9f2b87b2020-06-03 19:36:38 -07001521static avifBool avifParseItemDataBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Dragof6a42272019-11-21 15:21:41 -08001522{
Joe Dragof6a42272019-11-21 15:21:41 -08001523 // 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 -07001524 for (uint32_t i = 0; i < meta->idats.count; ++i) {
Joe Dragoba1eb492020-06-22 17:05:04 -07001525 if (meta->idats.idat[i].id == meta->idatID) {
Joe Dragof6a42272019-11-21 15:21:41 -08001526 return AVIF_FALSE;
1527 }
1528 }
1529
Joe Drago9f2b87b2020-06-03 19:36:38 -07001530 int index = avifArrayPushIndex(&meta->idats);
1531 avifDecoderItemData * idat = &meta->idats.idat[index];
Joe Dragoba1eb492020-06-22 17:05:04 -07001532 idat->id = meta->idatID;
Joe Dragobe4cbb92020-09-21 12:14:05 -07001533 avifRWDataSet(&idat->data, raw, rawLen);
Joe Dragof6a42272019-11-21 15:21:41 -08001534 return AVIF_TRUE;
1535}
1536
Joe Drago9f2b87b2020-06-03 19:36:38 -07001537static avifBool avifParseItemPropertiesBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001538{
1539 BEGIN_STREAM(s, raw, rawLen);
1540
1541 avifBoxHeader ipcoHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001542 CHECK(avifROStreamReadBoxHeader(&s, &ipcoHeader));
wantehchangbc35a5f2020-08-12 15:27:39 -07001543 if (memcmp(ipcoHeader.type, "ipco", 4)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001544 return AVIF_FALSE;
1545 }
1546
1547 // Read all item properties inside of ItemPropertyContainerBox
Joe Dragoa72da5b2020-06-15 19:40:17 -07001548 CHECK(avifParseItemPropertyContainerBox(&meta->properties, avifROStreamCurrent(&s), ipcoHeader.size));
Joe Drago345aaa12019-09-25 13:42:12 -07001549 CHECK(avifROStreamSkip(&s, ipcoHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001550
1551 // Now read all ItemPropertyAssociation until the end of the box, and make associations
Joe Drago345aaa12019-09-25 13:42:12 -07001552 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001553 avifBoxHeader ipmaHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001554 CHECK(avifROStreamReadBoxHeader(&s, &ipmaHeader));
Joe Drago8f7a3002019-02-07 19:35:37 -08001555
1556 if (!memcmp(ipmaHeader.type, "ipma", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001557 CHECK(avifParseItemPropertyAssociation(meta, avifROStreamCurrent(&s), ipmaHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001558 } else {
1559 // These must all be type ipma
1560 return AVIF_FALSE;
1561 }
1562
Joe Drago345aaa12019-09-25 13:42:12 -07001563 CHECK(avifROStreamSkip(&s, ipmaHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001564 }
1565 return AVIF_TRUE;
1566}
1567
Joe Drago9f2b87b2020-06-03 19:36:38 -07001568static avifBool avifParseItemInfoEntry(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001569{
1570 BEGIN_STREAM(s, raw, rawLen);
1571
Joe Drago345aaa12019-09-25 13:42:12 -07001572 CHECK(avifROStreamReadAndEnforceVersion(&s, 2)); // TODO: support version > 2? 2+ is required for item_type
Joe Drago8f7a3002019-02-07 19:35:37 -08001573
Joe Drago345aaa12019-09-25 13:42:12 -07001574 uint16_t itemID; // unsigned int(16) item_ID;
1575 CHECK(avifROStreamReadU16(&s, &itemID)); //
1576 uint16_t itemProtectionIndex; // unsigned int(16) item_protection_index;
1577 CHECK(avifROStreamReadU16(&s, &itemProtectionIndex)); //
1578 uint8_t itemType[4]; // unsigned int(32) item_type;
1579 CHECK(avifROStreamRead(&s, itemType, 4)); //
Joe Drago8f7a3002019-02-07 19:35:37 -08001580
Joe Dragof6a42272019-11-21 15:21:41 -08001581 avifContentType contentType;
1582 if (!memcmp(itemType, "mime", 4)) {
1583 CHECK(avifROStreamReadString(&s, NULL, 0)); // string item_name; (skipped)
1584 CHECK(avifROStreamReadString(&s, contentType.contentType, CONTENTTYPE_SIZE)); // string content_type;
1585 } else {
1586 memset(&contentType, 0, sizeof(contentType));
1587 }
1588
Joe Drago9f2b87b2020-06-03 19:36:38 -07001589 avifDecoderItem * item = avifMetaFindItem(meta, itemID);
Joe Drago46ea0582019-07-22 15:55:47 -07001590 if (!item) {
1591 return AVIF_FALSE;
1592 }
1593
Joe Drago05559c92019-07-17 16:33:38 -07001594 memcpy(item->type, itemType, sizeof(itemType));
Joe Dragof6a42272019-11-21 15:21:41 -08001595 memcpy(&item->contentType, &contentType, sizeof(contentType));
Joe Drago8f7a3002019-02-07 19:35:37 -08001596 return AVIF_TRUE;
1597}
1598
Joe Drago9f2b87b2020-06-03 19:36:38 -07001599static avifBool avifParseItemInfoBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001600{
1601 BEGIN_STREAM(s, raw, rawLen);
1602
1603 uint8_t version;
Joe Drago345aaa12019-09-25 13:42:12 -07001604 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Drago8f7a3002019-02-07 19:35:37 -08001605 uint32_t entryCount;
1606 if (version == 0) {
1607 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001608 CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) entry_count;
Joe Drago8f7a3002019-02-07 19:35:37 -08001609 entryCount = tmp;
1610 } else if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001611 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Drago8f7a3002019-02-07 19:35:37 -08001612 } else {
1613 return AVIF_FALSE;
1614 }
1615
Joe Drago678b9382019-02-09 03:17:47 -08001616 for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001617 avifBoxHeader infeHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001618 CHECK(avifROStreamReadBoxHeader(&s, &infeHeader));
Joe Drago8f7a3002019-02-07 19:35:37 -08001619
1620 if (!memcmp(infeHeader.type, "infe", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001621 CHECK(avifParseItemInfoEntry(meta, avifROStreamCurrent(&s), infeHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001622 } else {
1623 // These must all be type ipma
1624 return AVIF_FALSE;
1625 }
1626
Joe Drago345aaa12019-09-25 13:42:12 -07001627 CHECK(avifROStreamSkip(&s, infeHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001628 }
1629
1630 return AVIF_TRUE;
1631}
1632
Joe Drago9f2b87b2020-06-03 19:36:38 -07001633static avifBool avifParseItemReferenceBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001634{
1635 BEGIN_STREAM(s, raw, rawLen);
1636
1637 uint8_t version;
Joe Drago345aaa12019-09-25 13:42:12 -07001638 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Drago8f7a3002019-02-07 19:35:37 -08001639
Joe Drago345aaa12019-09-25 13:42:12 -07001640 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001641 avifBoxHeader irefHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001642 CHECK(avifROStreamReadBoxHeader(&s, &irefHeader));
Joe Drago8f7a3002019-02-07 19:35:37 -08001643
1644 uint32_t fromID = 0;
1645 if (version == 0) {
1646 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001647 CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) from_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001648 fromID = tmp;
1649 } else if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001650 CHECK(avifROStreamReadU32(&s, &fromID)); // unsigned int(32) from_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001651 } else {
1652 // unsupported iref version, skip it
1653 break;
1654 }
1655
1656 uint16_t referenceCount = 0;
Joe Drago345aaa12019-09-25 13:42:12 -07001657 CHECK(avifROStreamReadU16(&s, &referenceCount)); // unsigned int(16) reference_count;
Joe Drago8f7a3002019-02-07 19:35:37 -08001658
1659 for (uint16_t refIndex = 0; refIndex < referenceCount; ++refIndex) {
1660 uint32_t toID = 0;
1661 if (version == 0) {
1662 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001663 CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) to_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001664 toID = tmp;
1665 } else if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001666 CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) to_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001667 } else {
1668 // unsupported iref version, skip it
1669 break;
1670 }
1671
1672 // Read this reference as "{fromID} is a {irefType} for {toID}"
1673 if (fromID && toID) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001674 avifDecoderItem * item = avifMetaFindItem(meta, fromID);
Joe Drago46ea0582019-07-22 15:55:47 -07001675 if (!item) {
1676 return AVIF_FALSE;
1677 }
1678
Joe Drago8f7a3002019-02-07 19:35:37 -08001679 if (!memcmp(irefHeader.type, "thmb", 4)) {
Joe Drago05559c92019-07-17 16:33:38 -07001680 item->thumbnailForID = toID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001681 }
1682 if (!memcmp(irefHeader.type, "auxl", 4)) {
Joe Drago05559c92019-07-17 16:33:38 -07001683 item->auxForID = toID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001684 }
Joe Dragof6a42272019-11-21 15:21:41 -08001685 if (!memcmp(irefHeader.type, "cdsc", 4)) {
1686 item->descForID = toID;
1687 }
Joe Drago060d5342020-03-03 10:53:49 -08001688 if (!memcmp(irefHeader.type, "dimg", 4)) {
1689 // derived images refer in the opposite direction
Joe Drago9f2b87b2020-06-03 19:36:38 -07001690 avifDecoderItem * dimg = avifMetaFindItem(meta, toID);
Joe Drago060d5342020-03-03 10:53:49 -08001691 if (!dimg) {
1692 return AVIF_FALSE;
1693 }
1694
1695 dimg->dimgForID = fromID;
1696 }
Yuan Tonge4850be2021-01-22 14:21:25 +08001697 if (!memcmp(irefHeader.type, "prem", 4)) {
1698 item->premByID = toID;
1699 }
Joe Drago8f7a3002019-02-07 19:35:37 -08001700 }
1701 }
1702 }
1703
1704 return AVIF_TRUE;
1705}
1706
Joe Drago9f2b87b2020-06-03 19:36:38 -07001707static avifBool avifParseMetaBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001708{
1709 BEGIN_STREAM(s, raw, rawLen);
1710
Joe Drago345aaa12019-09-25 13:42:12 -07001711 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Drago8f7a3002019-02-07 19:35:37 -08001712
Joe Dragoba1eb492020-06-22 17:05:04 -07001713 ++meta->idatID; // for tracking idat
Joe Dragof6a42272019-11-21 15:21:41 -08001714
Joe Drago8f439092020-08-28 15:05:17 -07001715 uint32_t uniqueBoxFlags = 0;
Joe Drago345aaa12019-09-25 13:42:12 -07001716 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001717 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001718 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago8f7a3002019-02-07 19:35:37 -08001719
1720 if (!memcmp(header.type, "iloc", 4)) {
Joe Drago8f439092020-08-28 15:05:17 -07001721 CHECK(uniqueBoxSeen(&uniqueBoxFlags, 0));
Joe Drago9f2b87b2020-06-03 19:36:38 -07001722 CHECK(avifParseItemLocationBox(meta, avifROStreamCurrent(&s), header.size));
Joe Dragof6a42272019-11-21 15:21:41 -08001723 } else if (!memcmp(header.type, "pitm", 4)) {
Joe Drago8f439092020-08-28 15:05:17 -07001724 CHECK(uniqueBoxSeen(&uniqueBoxFlags, 1));
Joe Drago9f2b87b2020-06-03 19:36:38 -07001725 CHECK(avifParsePrimaryItemBox(meta, avifROStreamCurrent(&s), header.size));
Joe Dragof6a42272019-11-21 15:21:41 -08001726 } else if (!memcmp(header.type, "idat", 4)) {
Joe Drago8f439092020-08-28 15:05:17 -07001727 CHECK(uniqueBoxSeen(&uniqueBoxFlags, 2));
Joe Drago9f2b87b2020-06-03 19:36:38 -07001728 CHECK(avifParseItemDataBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001729 } else if (!memcmp(header.type, "iprp", 4)) {
Joe Drago8f439092020-08-28 15:05:17 -07001730 CHECK(uniqueBoxSeen(&uniqueBoxFlags, 3));
Joe Drago9f2b87b2020-06-03 19:36:38 -07001731 CHECK(avifParseItemPropertiesBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001732 } else if (!memcmp(header.type, "iinf", 4)) {
Joe Drago8f439092020-08-28 15:05:17 -07001733 CHECK(uniqueBoxSeen(&uniqueBoxFlags, 4));
Joe Drago9f2b87b2020-06-03 19:36:38 -07001734 CHECK(avifParseItemInfoBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001735 } else if (!memcmp(header.type, "iref", 4)) {
Joe Drago8f439092020-08-28 15:05:17 -07001736 CHECK(uniqueBoxSeen(&uniqueBoxFlags, 5));
Joe Drago9f2b87b2020-06-03 19:36:38 -07001737 CHECK(avifParseItemReferenceBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001738 }
1739
Joe Drago345aaa12019-09-25 13:42:12 -07001740 CHECK(avifROStreamSkip(&s, header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001741 }
1742 return AVIF_TRUE;
1743}
1744
Joe Drago9f2b87b2020-06-03 19:36:38 -07001745static avifBool avifParseTrackHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001746{
1747 BEGIN_STREAM(s, raw, rawLen);
1748
1749 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -07001750 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001751
1752 uint32_t ignored32, trackID;
1753 uint64_t ignored64;
1754 if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001755 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time;
1756 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time;
1757 CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID;
Joe Dragofc4144e2019-09-27 20:35:06 -07001758 CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0;
1759 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001760 } else if (version == 0) {
Joe Drago345aaa12019-09-25 13:42:12 -07001761 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time;
1762 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time;
1763 CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID;
Joe Dragofc4144e2019-09-27 20:35:06 -07001764 CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0;
1765 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001766 } else {
1767 // Unsupported version
1768 return AVIF_FALSE;
1769 }
1770
Joe Dragofc4144e2019-09-27 20:35:06 -07001771 // Skipping the following 52 bytes here:
1772 // ------------------------------------
1773 // const unsigned int(32)[2] reserved = 0;
1774 // template int(16) layer = 0;
1775 // template int(16) alternate_group = 0;
1776 // template int(16) volume = {if track_is_audio 0x0100 else 0};
1777 // const unsigned int(16) reserved = 0;
1778 // template int(32)[9] matrix= { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 }; // unity matrix
1779 CHECK(avifROStreamSkip(&s, 52));
1780
1781 uint32_t width, height;
1782 CHECK(avifROStreamReadU32(&s, &width)); // unsigned int(32) width;
1783 CHECK(avifROStreamReadU32(&s, &height)); // unsigned int(32) height;
1784 track->width = width >> 16;
1785 track->height = height >> 16;
1786
Joe Dragoae7e2c32019-07-18 15:22:25 -07001787 // TODO: support scaling based on width/height track header info?
1788
1789 track->id = trackID;
1790 return AVIF_TRUE;
1791}
1792
Joe Drago9f2b87b2020-06-03 19:36:38 -07001793static avifBool avifParseMediaHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001794{
1795 BEGIN_STREAM(s, raw, rawLen);
1796
1797 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -07001798 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001799
1800 uint32_t ignored32, mediaTimescale, mediaDuration32;
1801 uint64_t ignored64, mediaDuration64;
1802 if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001803 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time;
1804 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time;
1805 CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale;
1806 CHECK(avifROStreamReadU64(&s, &mediaDuration64)); // unsigned int(64) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001807 track->mediaDuration = mediaDuration64;
1808 } else if (version == 0) {
Joe Drago345aaa12019-09-25 13:42:12 -07001809 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time;
1810 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time;
1811 CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale;
1812 CHECK(avifROStreamReadU32(&s, &mediaDuration32)); // unsigned int(32) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001813 track->mediaDuration = (uint64_t)mediaDuration32;
1814 } else {
1815 // Unsupported version
1816 return AVIF_FALSE;
1817 }
1818
1819 track->mediaTimescale = mediaTimescale;
1820 return AVIF_TRUE;
1821}
1822
Joe Drago9f2b87b2020-06-03 19:36:38 -07001823static avifBool avifParseChunkOffsetBox(avifSampleTable * sampleTable, avifBool largeOffsets, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001824{
1825 BEGIN_STREAM(s, raw, rawLen);
1826
Joe Drago345aaa12019-09-25 13:42:12 -07001827 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001828
1829 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001830 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001831 for (uint32_t i = 0; i < entryCount; ++i) {
1832 uint64_t offset;
1833 if (largeOffsets) {
Joe Drago345aaa12019-09-25 13:42:12 -07001834 CHECK(avifROStreamReadU64(&s, &offset)); // unsigned int(32) chunk_offset;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001835 } else {
1836 uint32_t offset32;
Joe Drago345aaa12019-09-25 13:42:12 -07001837 CHECK(avifROStreamReadU32(&s, &offset32)); // unsigned int(32) chunk_offset;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001838 offset = (uint64_t)offset32;
1839 }
1840
1841 avifSampleTableChunk * chunk = (avifSampleTableChunk *)avifArrayPushPtr(&sampleTable->chunks);
1842 chunk->offset = offset;
1843 }
1844 return AVIF_TRUE;
1845}
1846
Joe Drago9f2b87b2020-06-03 19:36:38 -07001847static avifBool avifParseSampleToChunkBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001848{
1849 BEGIN_STREAM(s, raw, rawLen);
1850
Joe Drago345aaa12019-09-25 13:42:12 -07001851 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001852
1853 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001854 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001855 for (uint32_t i = 0; i < entryCount; ++i) {
1856 avifSampleTableSampleToChunk * sampleToChunk = (avifSampleTableSampleToChunk *)avifArrayPushPtr(&sampleTable->sampleToChunks);
Joe Drago345aaa12019-09-25 13:42:12 -07001857 CHECK(avifROStreamReadU32(&s, &sampleToChunk->firstChunk)); // unsigned int(32) first_chunk;
1858 CHECK(avifROStreamReadU32(&s, &sampleToChunk->samplesPerChunk)); // unsigned int(32) samples_per_chunk;
1859 CHECK(avifROStreamReadU32(&s, &sampleToChunk->sampleDescriptionIndex)); // unsigned int(32) sample_description_index;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001860 }
1861 return AVIF_TRUE;
1862}
1863
Joe Drago9f2b87b2020-06-03 19:36:38 -07001864static avifBool avifParseSampleSizeBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001865{
1866 BEGIN_STREAM(s, raw, rawLen);
1867
Joe Drago345aaa12019-09-25 13:42:12 -07001868 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001869
Joe Drago370be3f2020-02-07 15:59:42 -08001870 uint32_t allSamplesSize, sampleCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001871 CHECK(avifROStreamReadU32(&s, &allSamplesSize)); // unsigned int(32) sample_size;
Joe Drago370be3f2020-02-07 15:59:42 -08001872 CHECK(avifROStreamReadU32(&s, &sampleCount)); // unsigned int(32) sample_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001873
Joe Drago370be3f2020-02-07 15:59:42 -08001874 if (allSamplesSize > 0) {
1875 sampleTable->allSamplesSize = allSamplesSize;
1876 } else {
1877 for (uint32_t i = 0; i < sampleCount; ++i) {
1878 avifSampleTableSampleSize * sampleSize = (avifSampleTableSampleSize *)avifArrayPushPtr(&sampleTable->sampleSizes);
Joe Drago345aaa12019-09-25 13:42:12 -07001879 CHECK(avifROStreamReadU32(&s, &sampleSize->size)); // unsigned int(32) entry_size;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001880 }
1881 }
1882 return AVIF_TRUE;
1883}
1884
Joe Drago9f2b87b2020-06-03 19:36:38 -07001885static avifBool avifParseSyncSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Drago22c1ad92019-09-26 12:46:50 -07001886{
1887 BEGIN_STREAM(s, raw, rawLen);
Joe Drago22c1ad92019-09-26 12:46:50 -07001888
1889 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
1890
1891 uint32_t entryCount;
1892 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
1893
1894 for (uint32_t i = 0; i < entryCount; ++i) {
1895 uint32_t sampleNumber = 0;
1896 CHECK(avifROStreamReadU32(&s, &sampleNumber)); // unsigned int(32) sample_number;
1897 avifSyncSample * syncSample = (avifSyncSample *)avifArrayPushPtr(&sampleTable->syncSamples);
1898 syncSample->sampleNumber = sampleNumber;
1899 }
1900 return AVIF_TRUE;
1901}
1902
Joe Drago9f2b87b2020-06-03 19:36:38 -07001903static avifBool avifParseTimeToSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001904{
1905 BEGIN_STREAM(s, raw, rawLen);
1906
Joe Drago345aaa12019-09-25 13:42:12 -07001907 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001908
1909 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001910 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001911
1912 for (uint32_t i = 0; i < entryCount; ++i) {
1913 avifSampleTableTimeToSample * timeToSample = (avifSampleTableTimeToSample *)avifArrayPushPtr(&sampleTable->timeToSamples);
Joe Drago345aaa12019-09-25 13:42:12 -07001914 CHECK(avifROStreamReadU32(&s, &timeToSample->sampleCount)); // unsigned int(32) sample_count;
1915 CHECK(avifROStreamReadU32(&s, &timeToSample->sampleDelta)); // unsigned int(32) sample_delta;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001916 }
1917 return AVIF_TRUE;
1918}
1919
Joe Drago9f2b87b2020-06-03 19:36:38 -07001920static avifBool avifParseSampleDescriptionBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Drago2c0924c2019-09-26 17:41:01 -07001921{
1922 BEGIN_STREAM(s, raw, rawLen);
Joe Drago2c0924c2019-09-26 17:41:01 -07001923
1924 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
1925
1926 uint32_t entryCount;
1927 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
1928
1929 for (uint32_t i = 0; i < entryCount; ++i) {
1930 avifBoxHeader sampleEntryHeader;
1931 CHECK(avifROStreamReadBoxHeader(&s, &sampleEntryHeader));
1932
1933 avifSampleDescription * description = (avifSampleDescription *)avifArrayPushPtr(&sampleTable->sampleDescriptions);
Joe Dragoa72da5b2020-06-15 19:40:17 -07001934 avifArrayCreate(&description->properties, sizeof(avifProperty), 16);
Joe Drago2c0924c2019-09-26 17:41:01 -07001935 memcpy(description->format, sampleEntryHeader.type, sizeof(description->format));
Joe Drago6500fd62019-10-08 17:17:34 -07001936 size_t remainingBytes = avifROStreamRemainingBytes(&s);
1937 if (!memcmp(description->format, "av01", 4) && (remainingBytes > VISUALSAMPLEENTRY_SIZE)) {
Joe Drago11d23592021-01-05 14:18:57 -08001938 CHECK(avifParseItemPropertyContainerBox(&description->properties,
1939 avifROStreamCurrent(&s) + VISUALSAMPLEENTRY_SIZE,
1940 remainingBytes - VISUALSAMPLEENTRY_SIZE));
Joe Drago6500fd62019-10-08 17:17:34 -07001941 }
Joe Drago2c0924c2019-09-26 17:41:01 -07001942
1943 CHECK(avifROStreamSkip(&s, sampleEntryHeader.size));
1944 }
1945 return AVIF_TRUE;
1946}
1947
Joe Drago9f2b87b2020-06-03 19:36:38 -07001948static avifBool avifParseSampleTableBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001949{
1950 if (track->sampleTable) {
1951 // A TrackBox may only have one SampleTable
1952 return AVIF_FALSE;
1953 }
1954 track->sampleTable = avifSampleTableCreate();
1955
1956 BEGIN_STREAM(s, raw, rawLen);
1957
Joe Drago345aaa12019-09-25 13:42:12 -07001958 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07001959 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001960 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001961
1962 if (!memcmp(header.type, "stco", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001963 CHECK(avifParseChunkOffsetBox(track->sampleTable, AVIF_FALSE, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001964 } else if (!memcmp(header.type, "co64", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001965 CHECK(avifParseChunkOffsetBox(track->sampleTable, AVIF_TRUE, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001966 } else if (!memcmp(header.type, "stsc", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001967 CHECK(avifParseSampleToChunkBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001968 } else if (!memcmp(header.type, "stsz", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001969 CHECK(avifParseSampleSizeBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Drago22c1ad92019-09-26 12:46:50 -07001970 } else if (!memcmp(header.type, "stss", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001971 CHECK(avifParseSyncSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001972 } else if (!memcmp(header.type, "stts", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001973 CHECK(avifParseTimeToSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Drago2c0924c2019-09-26 17:41:01 -07001974 } else if (!memcmp(header.type, "stsd", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001975 CHECK(avifParseSampleDescriptionBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001976 }
1977
Joe Drago345aaa12019-09-25 13:42:12 -07001978 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001979 }
Joe Dragoae7e2c32019-07-18 15:22:25 -07001980 return AVIF_TRUE;
1981}
1982
Joe Drago9f2b87b2020-06-03 19:36:38 -07001983static avifBool avifParseMediaInformationBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001984{
1985 BEGIN_STREAM(s, raw, rawLen);
1986
Joe Drago345aaa12019-09-25 13:42:12 -07001987 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07001988 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001989 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001990
1991 if (!memcmp(header.type, "stbl", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001992 CHECK(avifParseSampleTableBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001993 }
1994
Joe Drago345aaa12019-09-25 13:42:12 -07001995 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001996 }
1997 return AVIF_TRUE;
1998}
1999
Joe Drago9f2b87b2020-06-03 19:36:38 -07002000static avifBool avifParseMediaBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07002001{
2002 BEGIN_STREAM(s, raw, rawLen);
2003
Joe Drago345aaa12019-09-25 13:42:12 -07002004 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07002005 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07002006 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002007
2008 if (!memcmp(header.type, "mdhd", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002009 CHECK(avifParseMediaHeaderBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002010 } else if (!memcmp(header.type, "minf", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002011 CHECK(avifParseMediaInformationBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002012 }
2013
Joe Drago345aaa12019-09-25 13:42:12 -07002014 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002015 }
2016 return AVIF_TRUE;
2017}
2018
Joe Drago9f2b87b2020-06-03 19:36:38 -07002019static avifBool avifTrackReferenceBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Drago46ea0582019-07-22 15:55:47 -07002020{
2021 BEGIN_STREAM(s, raw, rawLen);
2022
Joe Drago345aaa12019-09-25 13:42:12 -07002023 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago46ea0582019-07-22 15:55:47 -07002024 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07002025 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago46ea0582019-07-22 15:55:47 -07002026
2027 if (!memcmp(header.type, "auxl", 4)) {
2028 uint32_t toID;
Joe Dragoceb2fa02021-01-29 18:14:55 -08002029 CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) track_IDs[];
Joe Drago345aaa12019-09-25 13:42:12 -07002030 CHECK(avifROStreamSkip(&s, header.size - sizeof(uint32_t))); // just take the first one
Joe Drago46ea0582019-07-22 15:55:47 -07002031 track->auxForID = toID;
Yuan Tonge4850be2021-01-22 14:21:25 +08002032 } else if (!memcmp(header.type, "prem", 4)) {
2033 uint32_t byID;
Joe Dragoceb2fa02021-01-29 18:14:55 -08002034 CHECK(avifROStreamReadU32(&s, &byID)); // unsigned int(32) track_IDs[];
2035 CHECK(avifROStreamSkip(&s, header.size - sizeof(uint32_t))); // just take the first one
Yuan Tonge4850be2021-01-22 14:21:25 +08002036 track->premByID = byID;
Joe Drago46ea0582019-07-22 15:55:47 -07002037 } else {
Joe Drago345aaa12019-09-25 13:42:12 -07002038 CHECK(avifROStreamSkip(&s, header.size));
Joe Drago46ea0582019-07-22 15:55:47 -07002039 }
2040 }
2041 return AVIF_TRUE;
2042}
2043
Joe Drago800b47f2020-03-18 16:22:37 -07002044static avifBool avifParseTrackBox(avifDecoderData * data, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07002045{
2046 BEGIN_STREAM(s, raw, rawLen);
2047
Joe Dragoa72da5b2020-06-15 19:40:17 -07002048 avifTrack * track = avifDecoderDataCreateTrack(data);
Joe Dragoae7e2c32019-07-18 15:22:25 -07002049
Joe Drago345aaa12019-09-25 13:42:12 -07002050 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07002051 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07002052 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002053
2054 if (!memcmp(header.type, "tkhd", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002055 CHECK(avifParseTrackHeaderBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07002056 } else if (!memcmp(header.type, "meta", 4)) {
2057 CHECK(avifParseMetaBox(track->meta, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002058 } else if (!memcmp(header.type, "mdia", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002059 CHECK(avifParseMediaBox(track, avifROStreamCurrent(&s), header.size));
Joe Drago46ea0582019-07-22 15:55:47 -07002060 } else if (!memcmp(header.type, "tref", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07002061 CHECK(avifTrackReferenceBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002062 }
2063
Joe Drago345aaa12019-09-25 13:42:12 -07002064 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002065 }
2066 return AVIF_TRUE;
2067}
2068
Joe Drago800b47f2020-03-18 16:22:37 -07002069static avifBool avifParseMoovBox(avifDecoderData * data, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07002070{
2071 BEGIN_STREAM(s, raw, rawLen);
2072
Joe Drago345aaa12019-09-25 13:42:12 -07002073 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07002074 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07002075 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002076
2077 if (!memcmp(header.type, "trak", 4)) {
Joe Drago345aaa12019-09-25 13:42:12 -07002078 CHECK(avifParseTrackBox(data, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002079 }
2080
Joe Drago345aaa12019-09-25 13:42:12 -07002081 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07002082 }
2083 return AVIF_TRUE;
2084}
2085
Joe Drago345aaa12019-09-25 13:42:12 -07002086static avifBool avifParseFileTypeBox(avifFileType * ftyp, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08002087{
2088 BEGIN_STREAM(s, raw, rawLen);
2089
Joe Drago345aaa12019-09-25 13:42:12 -07002090 CHECK(avifROStreamRead(&s, ftyp->majorBrand, 4));
2091 CHECK(avifROStreamReadU32(&s, &ftyp->minorVersion));
Joe Drago8f7a3002019-02-07 19:35:37 -08002092
Joe Drago345aaa12019-09-25 13:42:12 -07002093 size_t compatibleBrandsBytes = avifROStreamRemainingBytes(&s);
Joe Drago8f7a3002019-02-07 19:35:37 -08002094 if ((compatibleBrandsBytes % 4) != 0) {
2095 return AVIF_FALSE;
2096 }
Wan-Teh Chang6da0a882020-07-01 12:19:31 -07002097 ftyp->compatibleBrands = avifROStreamCurrent(&s);
2098 CHECK(avifROStreamSkip(&s, compatibleBrandsBytes));
Joe Drago7e37b972019-07-24 12:44:47 -07002099 ftyp->compatibleBrandsCount = (int)compatibleBrandsBytes / 4;
Joe Drago8f7a3002019-02-07 19:35:37 -08002100
2101 return AVIF_TRUE;
2102}
2103
Joe Dragobb39aab2020-11-03 19:23:40 -08002104static avifBool avifFileTypeHasBrand(avifFileType * ftyp, const char * brand);
Wan-Teh Chang6fc17582020-09-24 15:16:37 -07002105static avifBool avifFileTypeIsCompatible(avifFileType * ftyp);
2106
Joe Dragobe4cbb92020-09-21 12:14:05 -07002107static avifResult avifParse(avifDecoder * decoder)
Joe Drago8f7a3002019-02-07 19:35:37 -08002108{
Joe Drago9aa931f2020-09-24 13:10:11 -07002109 // Note: this top-level function is the only avifParse*() function that returns avifResult instead of avifBool.
2110 // Be sure to use CHECKERR() in this function with an explicit error result instead of simply using CHECK().
2111
Joe Dragobe4cbb92020-09-21 12:14:05 -07002112 avifResult readResult;
Wan-Teh Chang3b8b81c2020-09-28 10:35:08 -07002113 uint64_t parseOffset = 0;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002114 avifDecoderData * data = decoder->data;
Joe Dragobb39aab2020-11-03 19:23:40 -08002115 avifBool ftypSeen = AVIF_FALSE;
2116 avifBool metaSeen = AVIF_FALSE;
2117 avifBool moovSeen = AVIF_FALSE;
2118 avifBool needsMeta = AVIF_FALSE;
2119 avifBool needsMoov = AVIF_FALSE;
Joe Drago8f7a3002019-02-07 19:35:37 -08002120
Joe Dragobe4cbb92020-09-21 12:14:05 -07002121 for (;;) {
2122 // Read just enough to get the next box header (a max of 32 bytes)
2123 avifROData headerContents;
Wan-Teh Changb856dc22020-09-28 13:00:56 -07002124 if ((decoder->io->sizeHint > 0) && (parseOffset > decoder->io->sizeHint)) {
2125 return AVIF_RESULT_BMFF_PARSE_FAILED;
2126 }
Joe Dragobe4cbb92020-09-21 12:14:05 -07002127 readResult = decoder->io->read(decoder->io, 0, parseOffset, 32, &headerContents);
2128 if (readResult != AVIF_RESULT_OK) {
2129 return readResult;
2130 }
2131 if (!headerContents.size) {
2132 // If we got AVIF_RESULT_OK from the reader but received 0 bytes,
Wan-Teh Chang277d0d72020-10-08 10:24:41 -07002133 // we've reached the end of the file with no errors. Hooray!
Joe Dragobe4cbb92020-09-21 12:14:05 -07002134 break;
Joe Drago8f7a3002019-02-07 19:35:37 -08002135 }
2136
Joe Dragobe4cbb92020-09-21 12:14:05 -07002137 // Parse the header, and find out how many bytes it actually was
2138 BEGIN_STREAM(headerStream, headerContents.data, headerContents.size);
2139 avifBoxHeader header;
Joe Drago468ded82020-09-24 12:52:51 -07002140 CHECKERR(avifROStreamReadBoxHeaderPartial(&headerStream, &header), AVIF_RESULT_BMFF_PARSE_FAILED);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002141 parseOffset += headerStream.offset;
Wan-Teh Changb856dc22020-09-28 13:00:56 -07002142 assert((decoder->io->sizeHint == 0) || (parseOffset <= decoder->io->sizeHint));
Joe Dragobe4cbb92020-09-21 12:14:05 -07002143
2144 // Try to get the remainder of the box, if necessary
2145 avifROData boxContents = AVIF_DATA_EMPTY;
2146
2147 // TODO: reorg this code to only do these memcmps once each
2148 if (!memcmp(header.type, "ftyp", 4) || (!memcmp(header.type, "meta", 4) || !memcmp(header.type, "moov", 4))) {
2149 readResult = decoder->io->read(decoder->io, 0, parseOffset, header.size, &boxContents);
2150 if (readResult != AVIF_RESULT_OK) {
2151 return readResult;
2152 }
2153 if (boxContents.size != header.size) {
2154 // A truncated box, bail out
Joe Dragofe5d5e42020-09-24 13:07:58 -07002155 return AVIF_RESULT_TRUNCATED_DATA;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002156 }
Wan-Teh Chang3b8b81c2020-09-28 10:35:08 -07002157 } else if (header.size > (UINT64_MAX - parseOffset)) {
2158 return AVIF_RESULT_BMFF_PARSE_FAILED;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002159 }
Wan-Teh Chang3b8b81c2020-09-28 10:35:08 -07002160 parseOffset += header.size;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002161
2162 if (!memcmp(header.type, "ftyp", 4)) {
Joe Dragobb39aab2020-11-03 19:23:40 -08002163 CHECKERR(!ftypSeen, AVIF_RESULT_BMFF_PARSE_FAILED);
Wan-Teh Chang6fc17582020-09-24 15:16:37 -07002164 avifFileType ftyp;
2165 CHECKERR(avifParseFileTypeBox(&ftyp, boxContents.data, boxContents.size), AVIF_RESULT_BMFF_PARSE_FAILED);
Joe Dragobb39aab2020-11-03 19:23:40 -08002166 if (!avifFileTypeIsCompatible(&ftyp)) {
Wan-Teh Chang6fc17582020-09-24 15:16:37 -07002167 return AVIF_RESULT_INVALID_FTYP;
2168 }
Joe Dragobb39aab2020-11-03 19:23:40 -08002169 ftypSeen = AVIF_TRUE;
2170 needsMeta = avifFileTypeHasBrand(&ftyp, "avif");
2171 needsMoov = avifFileTypeHasBrand(&ftyp, "avis");
Joe Dragobe4cbb92020-09-21 12:14:05 -07002172 } else if (!memcmp(header.type, "meta", 4)) {
Joe Dragobb39aab2020-11-03 19:23:40 -08002173 CHECKERR(!metaSeen, AVIF_RESULT_BMFF_PARSE_FAILED);
Joe Drago468ded82020-09-24 12:52:51 -07002174 CHECKERR(avifParseMetaBox(data->meta, boxContents.data, boxContents.size), AVIF_RESULT_BMFF_PARSE_FAILED);
Joe Dragobb39aab2020-11-03 19:23:40 -08002175 metaSeen = AVIF_TRUE;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002176 } else if (!memcmp(header.type, "moov", 4)) {
Joe Dragobb39aab2020-11-03 19:23:40 -08002177 CHECKERR(!moovSeen, AVIF_RESULT_BMFF_PARSE_FAILED);
Joe Drago468ded82020-09-24 12:52:51 -07002178 CHECKERR(avifParseMoovBox(data, boxContents.data, boxContents.size), AVIF_RESULT_BMFF_PARSE_FAILED);
Joe Dragobb39aab2020-11-03 19:23:40 -08002179 moovSeen = AVIF_TRUE;
2180 }
2181
2182 // See if there is enough information to consider Parse() a success and early-out:
2183 // * If the brand 'avif' is present, require a meta box
2184 // * If the brand 'avis' is present, require a moov box
2185 if (ftypSeen && (!needsMeta || metaSeen) && (!needsMoov || moovSeen)) {
2186 return AVIF_RESULT_OK;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002187 }
Joe Drago8f7a3002019-02-07 19:35:37 -08002188 }
Joe Dragobe4cbb92020-09-21 12:14:05 -07002189 return AVIF_RESULT_OK;
Joe Drago8f7a3002019-02-07 19:35:37 -08002190}
2191
2192// ---------------------------------------------------------------------------
Joe Drago8f7a3002019-02-07 19:35:37 -08002193
Joe Dragobb39aab2020-11-03 19:23:40 -08002194static avifBool avifFileTypeHasBrand(avifFileType * ftyp, const char * brand)
Joe Drago7e37b972019-07-24 12:44:47 -07002195{
Joe Dragobb39aab2020-11-03 19:23:40 -08002196 if (!memcmp(ftyp->majorBrand, brand, 4)) {
2197 return AVIF_TRUE;
2198 }
2199
2200 for (int compatibleBrandIndex = 0; compatibleBrandIndex < ftyp->compatibleBrandsCount; ++compatibleBrandIndex) {
2201 const uint8_t * compatibleBrand = &ftyp->compatibleBrands[4 * compatibleBrandIndex];
2202 if (!memcmp(compatibleBrand, brand, 4)) {
2203 return AVIF_TRUE;
Joe Drago7e37b972019-07-24 12:44:47 -07002204 }
2205 }
Joe Dragobb39aab2020-11-03 19:23:40 -08002206 return AVIF_FALSE;
2207}
2208
2209static avifBool avifFileTypeIsCompatible(avifFileType * ftyp)
2210{
2211 return avifFileTypeHasBrand(ftyp, "avif") || avifFileTypeHasBrand(ftyp, "avis");
Joe Drago7e37b972019-07-24 12:44:47 -07002212}
2213
Wan-Teh Change184dc12020-05-11 12:47:21 -07002214avifBool avifPeekCompatibleFileType(const avifROData * input)
Joe Drago7e37b972019-07-24 12:44:47 -07002215{
2216 BEGIN_STREAM(s, input->data, input->size);
2217
2218 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07002219 CHECK(avifROStreamReadBoxHeader(&s, &header));
wantehchangbc35a5f2020-08-12 15:27:39 -07002220 if (memcmp(header.type, "ftyp", 4)) {
Joe Drago7e37b972019-07-24 12:44:47 -07002221 return AVIF_FALSE;
2222 }
2223
2224 avifFileType ftyp;
2225 memset(&ftyp, 0, sizeof(avifFileType));
Joe Drago345aaa12019-09-25 13:42:12 -07002226 avifBool parsed = avifParseFileTypeBox(&ftyp, avifROStreamCurrent(&s), header.size);
Joe Drago7e37b972019-07-24 12:44:47 -07002227 if (!parsed) {
2228 return AVIF_FALSE;
2229 }
2230 return avifFileTypeIsCompatible(&ftyp);
2231}
2232
2233// ---------------------------------------------------------------------------
2234
Joe Drago0b05eee2019-06-12 13:24:39 -07002235avifDecoder * avifDecoderCreate(void)
2236{
2237 avifDecoder * decoder = (avifDecoder *)avifAlloc(sizeof(avifDecoder));
2238 memset(decoder, 0, sizeof(avifDecoder));
Joe Dragoede5c202020-11-11 09:42:57 -08002239 decoder->maxThreads = 1;
Joe Drago0b05eee2019-06-12 13:24:39 -07002240 return decoder;
2241}
2242
Joe Drago46ea0582019-07-22 15:55:47 -07002243static void avifDecoderCleanup(avifDecoder * decoder)
2244{
2245 if (decoder->data) {
Joe Drago800b47f2020-03-18 16:22:37 -07002246 avifDecoderDataDestroy(decoder->data);
Joe Drago46ea0582019-07-22 15:55:47 -07002247 decoder->data = NULL;
2248 }
2249
2250 if (decoder->image) {
2251 avifImageDestroy(decoder->image);
2252 decoder->image = NULL;
2253 }
2254}
2255
Joe Drago0b05eee2019-06-12 13:24:39 -07002256void avifDecoderDestroy(avifDecoder * decoder)
2257{
Joe Drago46ea0582019-07-22 15:55:47 -07002258 avifDecoderCleanup(decoder);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002259 avifIODestroy(decoder->io);
Joe Drago0b05eee2019-06-12 13:24:39 -07002260 avifFree(decoder);
2261}
2262
Joe Drago46ea0582019-07-22 15:55:47 -07002263avifResult avifDecoderSetSource(avifDecoder * decoder, avifDecoderSource source)
Joe Drago444f0512019-01-23 17:03:24 -08002264{
Joe Drago46ea0582019-07-22 15:55:47 -07002265 decoder->requestedSource = source;
2266 return avifDecoderReset(decoder);
2267}
Joe Drago33f1d362019-02-13 16:46:22 -08002268
Wan-Teh Change67f9362020-10-12 16:07:57 -07002269void avifDecoderSetIO(avifDecoder * decoder, avifIO * io)
Joe Drago46ea0582019-07-22 15:55:47 -07002270{
Joe Dragobe4cbb92020-09-21 12:14:05 -07002271 avifIODestroy(decoder->io);
2272 decoder->io = io;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002273}
2274
Wan-Teh Chang7de44452020-10-08 11:43:18 -07002275avifResult avifDecoderSetIOMemory(avifDecoder * decoder, const uint8_t * data, size_t size)
Joe Dragobe4cbb92020-09-21 12:14:05 -07002276{
Wan-Teh Chang7de44452020-10-08 11:43:18 -07002277 avifIO * io = avifIOCreateMemoryReader(data, size);
Wan-Teh Chang31c7c1a2020-10-13 16:45:41 -07002278 assert(io);
Wan-Teh Change67f9362020-10-12 16:07:57 -07002279 avifDecoderSetIO(decoder, io);
2280 return AVIF_RESULT_OK;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002281}
2282
Joe Dragobe4cbb92020-09-21 12:14:05 -07002283avifResult avifDecoderSetIOFile(avifDecoder * decoder, const char * filename)
2284{
Wan-Teh Chang7de44452020-10-08 11:43:18 -07002285 avifIO * io = avifIOCreateFileReader(filename);
2286 if (!io) {
Wan-Teh Chang31c7c1a2020-10-13 16:45:41 -07002287 return AVIF_RESULT_IO_ERROR;
Wan-Teh Chang7de44452020-10-08 11:43:18 -07002288 }
Wan-Teh Change67f9362020-10-12 16:07:57 -07002289 avifDecoderSetIO(decoder, io);
2290 return AVIF_RESULT_OK;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002291}
2292
Joe Drago4bcdfde2020-11-13 17:50:55 -08002293// 0-byte extents are ignored/overwritten during the merge, as they are the signal from helper
2294// functions that no extent was necessary for this given sample. If both provided extents are
2295// >0 bytes, this will set dst to be an extent that bounds both supplied extents.
Wan-Teh Changd69958e2020-11-17 12:14:27 -08002296static avifResult avifExtentMerge(avifExtent * dst, const avifExtent * src)
Joe Drago4bcdfde2020-11-13 17:50:55 -08002297{
2298 if (!dst->size) {
2299 memcpy(dst, src, sizeof(avifExtent));
Wan-Teh Changd69958e2020-11-17 12:14:27 -08002300 return AVIF_RESULT_OK;
Joe Drago4bcdfde2020-11-13 17:50:55 -08002301 }
2302 if (!src->size) {
Wan-Teh Changd69958e2020-11-17 12:14:27 -08002303 return AVIF_RESULT_OK;
Joe Drago4bcdfde2020-11-13 17:50:55 -08002304 }
2305
2306 const uint64_t minExtent1 = dst->offset;
2307 const uint64_t maxExtent1 = dst->offset + dst->size;
2308 const uint64_t minExtent2 = src->offset;
2309 const uint64_t maxExtent2 = src->offset + src->size;
2310 dst->offset = AVIF_MIN(minExtent1, minExtent2);
Wan-Teh Changd69958e2020-11-17 12:14:27 -08002311 const uint64_t extentLength = AVIF_MAX(maxExtent1, maxExtent2) - dst->offset;
2312 if (extentLength > SIZE_MAX) {
2313 return AVIF_RESULT_BMFF_PARSE_FAILED;
2314 }
2315 dst->size = (size_t)extentLength;
2316 return AVIF_RESULT_OK;
Joe Drago4bcdfde2020-11-13 17:50:55 -08002317}
2318
Joe Drago93d5bf92020-11-17 14:31:57 -08002319avifResult avifDecoderNthImageMaxExtent(const avifDecoder * decoder, uint32_t frameIndex, avifExtent * outExtent)
Joe Drago4bcdfde2020-11-13 17:50:55 -08002320{
2321 if (!decoder->data) {
2322 // Nothing has been parsed yet
2323 return AVIF_RESULT_NO_CONTENT;
2324 }
2325
2326 memset(outExtent, 0, sizeof(avifExtent));
2327
Joe Drago93d5bf92020-11-17 14:31:57 -08002328 uint32_t startFrameIndex = avifDecoderNearestKeyframe(decoder, frameIndex);
Joe Drago4bcdfde2020-11-13 17:50:55 -08002329 uint32_t endFrameIndex = frameIndex;
2330 for (uint32_t currentFrameIndex = startFrameIndex; currentFrameIndex <= endFrameIndex; ++currentFrameIndex) {
2331 for (unsigned int tileIndex = 0; tileIndex < decoder->data->tiles.count; ++tileIndex) {
2332 avifTile * tile = &decoder->data->tiles.tile[tileIndex];
2333 if (currentFrameIndex >= tile->input->samples.count) {
2334 return AVIF_RESULT_NO_IMAGES_REMAINING;
2335 }
2336
2337 avifDecodeSample * sample = &tile->input->samples.sample[currentFrameIndex];
2338 avifExtent sampleExtent;
2339 if (sample->itemID) {
2340 // The data comes from an item. Let avifDecoderItemMaxExtent() do the heavy lifting.
2341
2342 avifDecoderItem * item = avifMetaFindItem(decoder->data->meta, sample->itemID);
2343 avifResult maxExtentResult = avifDecoderItemMaxExtent(item, &sampleExtent);
2344 if (maxExtentResult != AVIF_RESULT_OK) {
2345 return maxExtentResult;
2346 }
2347 } else {
2348 // The data likely comes from a sample table. Use the sample position directly.
2349
2350 sampleExtent.offset = sample->offset;
2351 sampleExtent.size = sample->size;
2352 }
2353
2354 if (sampleExtent.size > UINT64_MAX - sampleExtent.offset) {
2355 return AVIF_RESULT_BMFF_PARSE_FAILED;
2356 }
2357
Wan-Teh Changd69958e2020-11-17 12:14:27 -08002358 avifResult extentMergeResult = avifExtentMerge(outExtent, &sampleExtent);
2359 if (extentMergeResult != AVIF_RESULT_OK) {
2360 return extentMergeResult;
2361 }
Joe Drago4bcdfde2020-11-13 17:50:55 -08002362 }
2363 }
2364 return AVIF_RESULT_OK;
2365}
2366
Joe Dragobe4cbb92020-09-21 12:14:05 -07002367static avifResult avifDecoderPrepareSample(avifDecoder * decoder, avifDecodeSample * sample, size_t partialByteCount)
2368{
2369 if (!sample->data.size || sample->partialData) {
2370 // This sample hasn't been read from IO or had its extents fully merged yet.
2371
2372 if (sample->itemID) {
Wan-Teh Chang4548b162020-11-06 11:48:25 -08002373 // The data comes from an item. Let avifDecoderItemRead() do the heavy lifting.
Joe Dragobe4cbb92020-09-21 12:14:05 -07002374
2375 avifDecoderItem * item = avifMetaFindItem(decoder->data->meta, sample->itemID);
2376 avifROData itemContents;
Wan-Teh Chang4548b162020-11-06 11:48:25 -08002377 avifResult readResult = avifDecoderItemRead(item, decoder->io, &itemContents, partialByteCount);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002378 if (readResult != AVIF_RESULT_OK) {
2379 return readResult;
2380 }
2381
Wan-Teh Chang4548b162020-11-06 11:48:25 -08002382 // avifDecoderItemRead is guaranteed to already be persisted by either the underlying IO
Wan-Teh Chang277d0d72020-10-08 10:24:41 -07002383 // or by mergedExtents; just reuse the buffer here.
Joe Dragobe4cbb92020-09-21 12:14:05 -07002384 memcpy(&sample->data, &itemContents, sizeof(avifROData));
2385 sample->ownsData = AVIF_FALSE;
2386 sample->partialData = item->partialMergedExtents;
2387 } else {
2388 // The data likely comes from a sample table. Pull the sample and make a copy if necessary.
2389
2390 size_t bytesToRead = sample->size;
2391 if (partialByteCount && (bytesToRead > partialByteCount)) {
2392 bytesToRead = partialByteCount;
2393 }
2394
2395 avifROData sampleContents;
Wan-Teh Changb856dc22020-09-28 13:00:56 -07002396 if ((decoder->io->sizeHint > 0) && (sample->offset > decoder->io->sizeHint)) {
2397 return AVIF_RESULT_BMFF_PARSE_FAILED;
2398 }
Joe Dragobe4cbb92020-09-21 12:14:05 -07002399 avifResult readResult = decoder->io->read(decoder->io, 0, sample->offset, bytesToRead, &sampleContents);
2400 if (readResult != AVIF_RESULT_OK) {
2401 return readResult;
2402 }
2403 if (sampleContents.size != bytesToRead) {
2404 return AVIF_RESULT_TRUNCATED_DATA;
2405 }
2406
2407 sample->ownsData = !decoder->io->persistent;
2408 sample->partialData = (bytesToRead != sample->size);
2409 if (decoder->io->persistent) {
2410 memcpy(&sample->data, &sampleContents, sizeof(avifROData));
2411 } else {
2412 avifRWDataSet((avifRWData *)&sample->data, sampleContents.data, sampleContents.size);
2413 }
2414 }
2415 }
2416 return AVIF_RESULT_OK;
2417}
2418
2419avifResult avifDecoderParse(avifDecoder * decoder)
2420{
2421 if (!decoder->io || !decoder->io->read) {
Wan-Teh Chang31c7c1a2020-10-13 16:45:41 -07002422 return AVIF_RESULT_IO_NOT_SET;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002423 }
2424
Joe Drago46ea0582019-07-22 15:55:47 -07002425 // Cleanup anything lingering in the decoder
2426 avifDecoderCleanup(decoder);
2427
Joe Drago444f0512019-01-23 17:03:24 -08002428 // -----------------------------------------------------------------------
2429 // Parse BMFF boxes
2430
Joe Drago800b47f2020-03-18 16:22:37 -07002431 decoder->data = avifDecoderDataCreate();
Joe Drago46ea0582019-07-22 15:55:47 -07002432
Joe Dragobe4cbb92020-09-21 12:14:05 -07002433 avifResult parseResult = avifParse(decoder);
2434 if (parseResult != AVIF_RESULT_OK) {
2435 return parseResult;
Joe Drago444f0512019-01-23 17:03:24 -08002436 }
2437
Joe Drago46ea0582019-07-22 15:55:47 -07002438 // Sanity check items
Joe Drago9f2b87b2020-06-03 19:36:38 -07002439 for (uint32_t itemIndex = 0; itemIndex < decoder->data->meta->items.count; ++itemIndex) {
2440 avifDecoderItem * item = &decoder->data->meta->items.item[itemIndex];
Joe Drago3320e5f2020-04-21 17:36:27 -07002441 if (item->hasUnsupportedEssentialProperty) {
2442 // An essential property isn't supported by libavif; ignore the item.
2443 continue;
2444 }
Joe Drago46ea0582019-07-22 15:55:47 -07002445 }
2446 return avifDecoderReset(decoder);
2447}
2448
Joe Dragobe4cbb92020-09-21 12:14:05 -07002449static avifCodec * avifCodecCreateInternal(avifCodecChoice choice)
Joe Drago46ea0582019-07-22 15:55:47 -07002450{
Joe Dragobe4cbb92020-09-21 12:14:05 -07002451 return avifCodecCreate(choice, AVIF_CODEC_FLAG_CAN_DECODE);
Joe Drago46ea0582019-07-22 15:55:47 -07002452}
2453
Joe Drago22c1ad92019-09-26 12:46:50 -07002454static avifResult avifDecoderFlush(avifDecoder * decoder)
2455{
Joe Drago800b47f2020-03-18 16:22:37 -07002456 avifDecoderDataResetCodec(decoder->data);
Joe Drago22c1ad92019-09-26 12:46:50 -07002457
Joe Drago060d5342020-03-03 10:53:49 -08002458 for (unsigned int i = 0; i < decoder->data->tiles.count; ++i) {
2459 avifTile * tile = &decoder->data->tiles.tile[i];
Joe Dragobe4cbb92020-09-21 12:14:05 -07002460 tile->codec = avifCodecCreateInternal(decoder->codecChoice);
Joe Drago060d5342020-03-03 10:53:49 -08002461 if (!tile->codec) {
Joe Drago53355352019-10-28 19:04:51 -07002462 return AVIF_RESULT_NO_CODEC_AVAILABLE;
2463 }
Joe Dragoede5c202020-11-11 09:42:57 -08002464 if (!tile->codec->open(tile->codec, decoder)) {
Joe Drago060d5342020-03-03 10:53:49 -08002465 return AVIF_RESULT_DECODE_COLOR_FAILED;
Joe Drago22c1ad92019-09-26 12:46:50 -07002466 }
2467 }
2468 return AVIF_RESULT_OK;
2469}
2470
Joe Drago46ea0582019-07-22 15:55:47 -07002471avifResult avifDecoderReset(avifDecoder * decoder)
2472{
Joe Drago800b47f2020-03-18 16:22:37 -07002473 avifDecoderData * data = decoder->data;
Joe Drago46ea0582019-07-22 15:55:47 -07002474 if (!data) {
2475 // Nothing to reset.
2476 return AVIF_RESULT_OK;
2477 }
2478
Joe Drago060d5342020-03-03 10:53:49 -08002479 memset(&data->colorGrid, 0, sizeof(data->colorGrid));
2480 memset(&data->alphaGrid, 0, sizeof(data->alphaGrid));
Joe Drago800b47f2020-03-18 16:22:37 -07002481 avifDecoderDataClearTiles(data);
Joe Drago89f0cc82020-03-09 16:13:27 -07002482
2483 // Prepare / cleanup decoded image state
Joe Dragoa0da4a42020-05-08 14:27:40 -07002484 if (decoder->image) {
2485 avifImageDestroy(decoder->image);
Joe Drago8f7a3002019-02-07 19:35:37 -08002486 }
Joe Dragoa0da4a42020-05-08 14:27:40 -07002487 decoder->image = avifImageCreateEmpty();
2488 data->cicpSet = AVIF_FALSE;
Joe Drago8f7a3002019-02-07 19:35:37 -08002489
Joe Drago70cbf602019-07-24 15:30:55 -07002490 memset(&decoder->ioStats, 0, sizeof(decoder->ioStats));
2491
Joe Drago444f0512019-01-23 17:03:24 -08002492 // -----------------------------------------------------------------------
Joe Drago46ea0582019-07-22 15:55:47 -07002493 // Build decode input
Joe Drago444f0512019-01-23 17:03:24 -08002494
Joe Drago46ea0582019-07-22 15:55:47 -07002495 data->sourceSampleTable = NULL; // Reset
2496 if (decoder->requestedSource == AVIF_DECODER_SOURCE_AUTO) {
2497 if (data->tracks.count > 0) {
2498 data->source = AVIF_DECODER_SOURCE_TRACKS;
2499 } else {
2500 data->source = AVIF_DECODER_SOURCE_PRIMARY_ITEM;
Joe Drago76370232019-07-16 11:00:52 -07002501 }
Joe Drago46ea0582019-07-22 15:55:47 -07002502 } else {
2503 data->source = decoder->requestedSource;
Joe Drago76370232019-07-16 11:00:52 -07002504 }
2505
Joe Dragoa72da5b2020-06-15 19:40:17 -07002506 const avifPropertyArray * colorProperties = NULL;
Joe Drago46ea0582019-07-22 15:55:47 -07002507 if (data->source == AVIF_DECODER_SOURCE_TRACKS) {
2508 avifTrack * colorTrack = NULL;
2509 avifTrack * alphaTrack = NULL;
2510
2511 // Find primary track - this probably needs some better detection
2512 uint32_t colorTrackIndex = 0;
wantehchangb207b4d2020-08-11 17:50:22 -07002513 for (; colorTrackIndex < data->tracks.count; ++colorTrackIndex) {
2514 avifTrack * track = &data->tracks.track[colorTrackIndex];
Joe Drago46ea0582019-07-22 15:55:47 -07002515 if (!track->sampleTable) {
2516 continue;
2517 }
Joe Dragoba1eb492020-06-22 17:05:04 -07002518 if (!track->id) { // trak box might be missing a tkhd box inside, skip it
Joe Drago4a25c192020-06-03 16:29:58 -07002519 continue;
2520 }
Joe Drago46ea0582019-07-22 15:55:47 -07002521 if (!track->sampleTable->chunks.count) {
2522 continue;
2523 }
Joe Drago2c0924c2019-09-26 17:41:01 -07002524 if (!avifSampleTableHasFormat(track->sampleTable, "av01")) {
2525 continue;
2526 }
Joe Drago46ea0582019-07-22 15:55:47 -07002527 if (track->auxForID != 0) {
2528 continue;
2529 }
2530
2531 // Found one!
Joe Drago444f0512019-01-23 17:03:24 -08002532 break;
2533 }
wantehchangb207b4d2020-08-11 17:50:22 -07002534 if (colorTrackIndex == data->tracks.count) {
Joe Drago46ea0582019-07-22 15:55:47 -07002535 return AVIF_RESULT_NO_CONTENT;
Joe Drago444f0512019-01-23 17:03:24 -08002536 }
wantehchangb207b4d2020-08-11 17:50:22 -07002537 colorTrack = &data->tracks.track[colorTrackIndex];
Joe Drago46ea0582019-07-22 15:55:47 -07002538
Joe Dragoa72da5b2020-06-15 19:40:17 -07002539 colorProperties = avifSampleTableGetProperties(colorTrack->sampleTable);
2540 if (!colorProperties) {
2541 return AVIF_RESULT_BMFF_PARSE_FAILED;
2542 }
2543
2544 // Find Exif and/or XMP metadata, if any
2545 if (colorTrack->meta) {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002546 // See the comment above avifDecoderFindMetadata() for the explanation of using 0 here
2547 avifResult findResult = avifDecoderFindMetadata(decoder, colorTrack->meta, decoder->image, 0);
2548 if (findResult != AVIF_RESULT_OK) {
2549 return findResult;
Joe Dragoa72da5b2020-06-15 19:40:17 -07002550 }
2551 }
2552
Joe Drago46ea0582019-07-22 15:55:47 -07002553 uint32_t alphaTrackIndex = 0;
wantehchangb207b4d2020-08-11 17:50:22 -07002554 for (; alphaTrackIndex < data->tracks.count; ++alphaTrackIndex) {
2555 avifTrack * track = &data->tracks.track[alphaTrackIndex];
Joe Drago46ea0582019-07-22 15:55:47 -07002556 if (!track->sampleTable) {
2557 continue;
2558 }
Joe Drago4a25c192020-06-03 16:29:58 -07002559 if (!track->id) {
2560 continue;
2561 }
Joe Drago46ea0582019-07-22 15:55:47 -07002562 if (!track->sampleTable->chunks.count) {
2563 continue;
2564 }
Joe Drago2c0924c2019-09-26 17:41:01 -07002565 if (!avifSampleTableHasFormat(track->sampleTable, "av01")) {
2566 continue;
2567 }
Joe Drago46ea0582019-07-22 15:55:47 -07002568 if (track->auxForID == colorTrack->id) {
2569 // Found it!
2570 break;
2571 }
2572 }
wantehchangb207b4d2020-08-11 17:50:22 -07002573 if (alphaTrackIndex != data->tracks.count) {
2574 alphaTrack = &data->tracks.track[alphaTrackIndex];
Joe Drago8f7a3002019-02-07 19:35:37 -08002575 }
Joe Drago444f0512019-01-23 17:03:24 -08002576
wantehchangb207b4d2020-08-11 17:50:22 -07002577 avifTile * colorTile = avifDecoderDataCreateTile(data);
Joe Drago0675bef2020-09-21 13:03:41 -07002578 if (!avifCodecDecodeInputGetSamples(colorTile->input, colorTrack->sampleTable, decoder->io->sizeHint)) {
Joe Drago46ea0582019-07-22 15:55:47 -07002579 return AVIF_RESULT_BMFF_PARSE_FAILED;
2580 }
wantehchangb207b4d2020-08-11 17:50:22 -07002581 data->colorTileCount = 1;
Joe Drago46ea0582019-07-22 15:55:47 -07002582
2583 if (alphaTrack) {
wantehchang76e16bf2020-08-12 13:01:31 -07002584 avifTile * alphaTile = avifDecoderDataCreateTile(data);
Joe Drago0675bef2020-09-21 13:03:41 -07002585 if (!avifCodecDecodeInputGetSamples(alphaTile->input, alphaTrack->sampleTable, decoder->io->sizeHint)) {
Joe Drago46ea0582019-07-22 15:55:47 -07002586 return AVIF_RESULT_BMFF_PARSE_FAILED;
2587 }
Joe Drago060d5342020-03-03 10:53:49 -08002588 alphaTile->input->alpha = AVIF_TRUE;
wantehchangb207b4d2020-08-11 17:50:22 -07002589 data->alphaTileCount = 1;
Joe Drago46ea0582019-07-22 15:55:47 -07002590 }
2591
2592 // Stash off sample table for future timing information
2593 data->sourceSampleTable = colorTrack->sampleTable;
2594
2595 // Image sequence timing
2596 decoder->imageIndex = -1;
Joe Drago060d5342020-03-03 10:53:49 -08002597 decoder->imageCount = colorTile->input->samples.count;
Joe Drago46ea0582019-07-22 15:55:47 -07002598 decoder->timescale = colorTrack->mediaTimescale;
2599 decoder->durationInTimescales = colorTrack->mediaDuration;
2600 if (colorTrack->mediaTimescale) {
2601 decoder->duration = (double)decoder->durationInTimescales / (double)colorTrack->mediaTimescale;
2602 } else {
2603 decoder->duration = 0;
2604 }
2605 memset(&decoder->imageTiming, 0, sizeof(decoder->imageTiming)); // to be set in avifDecoderNextImage()
Joe Drago41700852019-09-26 17:01:43 -07002606
Joe Dragoc554f5f2020-06-09 18:59:51 -07002607 decoder->image->width = colorTrack->width;
2608 decoder->image->height = colorTrack->height;
Joe Dragoc554f5f2020-06-09 18:59:51 -07002609 decoder->alphaPresent = (alphaTrack != NULL);
Joe Dragoceb2fa02021-01-29 18:14:55 -08002610 decoder->image->alphaPremultiplied = decoder->alphaPresent && (colorTrack->premByID == alphaTrack->id);
Joe Drago46ea0582019-07-22 15:55:47 -07002611 } else {
2612 // Create from items
2613
Joe Dragobe4cbb92020-09-21 12:14:05 -07002614 avifDecoderItem * colorItem = NULL;
2615 avifDecoderItem * alphaItem = NULL;
Joe Drago46ea0582019-07-22 15:55:47 -07002616
Joe Dragof6a42272019-11-21 15:21:41 -08002617 // Find the colorOBU (primary) item
Joe Drago9f2b87b2020-06-03 19:36:38 -07002618 for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) {
2619 avifDecoderItem * item = &data->meta->items.item[itemIndex];
Joe Dragoba1eb492020-06-22 17:05:04 -07002620 if (!item->size) {
2621 continue;
Joe Drago8f7a3002019-02-07 19:35:37 -08002622 }
Joe Drago3320e5f2020-04-21 17:36:27 -07002623 if (item->hasUnsupportedEssentialProperty) {
2624 // An essential property isn't supported by libavif; ignore the item.
2625 continue;
2626 }
Joe Drago951a0022020-03-09 16:19:44 -07002627 avifBool isGrid = (memcmp(item->type, "grid", 4) == 0);
Joe Drago060d5342020-03-03 10:53:49 -08002628 if (memcmp(item->type, "av01", 4) && !isGrid) {
Joe Drago8f7a3002019-02-07 19:35:37 -08002629 // probably exif or some other data
2630 continue;
2631 }
2632 if (item->thumbnailForID != 0) {
2633 // It's a thumbnail, skip it
2634 continue;
2635 }
Joe Drago9f2b87b2020-06-03 19:36:38 -07002636 if ((data->meta->primaryItemID > 0) && (item->id != data->meta->primaryItemID)) {
Joe Dragof6a42272019-11-21 15:21:41 -08002637 // a primary item ID was specified, require it
2638 continue;
2639 }
Joe Drago8f7a3002019-02-07 19:35:37 -08002640
Joe Drago060d5342020-03-03 10:53:49 -08002641 if (isGrid) {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002642 avifROData readData;
Wan-Teh Chang4548b162020-11-06 11:48:25 -08002643 avifResult readResult = avifDecoderItemRead(item, decoder->io, &readData, 0);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002644 if (readResult != AVIF_RESULT_OK) {
2645 return readResult;
2646 }
Wan-Teh Changc443f142020-10-08 14:58:42 -07002647 if (!avifParseImageGridBox(&data->colorGrid, readData.data, readData.size)) {
Joe Drago060d5342020-03-03 10:53:49 -08002648 return AVIF_RESULT_INVALID_IMAGE_GRID;
2649 }
Joe Drago060d5342020-03-03 10:53:49 -08002650 }
2651
Joe Dragobe4cbb92020-09-21 12:14:05 -07002652 colorItem = item;
Joe Drago46ea0582019-07-22 15:55:47 -07002653 break;
2654 }
2655
Joe Dragobe4cbb92020-09-21 12:14:05 -07002656 if (!colorItem) {
Joe Drago060d5342020-03-03 10:53:49 -08002657 return AVIF_RESULT_NO_AV1_ITEMS_FOUND;
2658 }
Joe Dragobe4cbb92020-09-21 12:14:05 -07002659 colorProperties = &colorItem->properties;
Joe Drago46ea0582019-07-22 15:55:47 -07002660
Joe Drago060d5342020-03-03 10:53:49 -08002661 // Find the alphaOBU item, if any
Joe Drago9f2b87b2020-06-03 19:36:38 -07002662 for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) {
2663 avifDecoderItem * item = &data->meta->items.item[itemIndex];
Joe Dragoba1eb492020-06-22 17:05:04 -07002664 if (!item->size) {
2665 continue;
Joe Drago060d5342020-03-03 10:53:49 -08002666 }
Joe Drago3320e5f2020-04-21 17:36:27 -07002667 if (item->hasUnsupportedEssentialProperty) {
2668 // An essential property isn't supported by libavif; ignore the item.
2669 continue;
2670 }
Joe Drago951a0022020-03-09 16:19:44 -07002671 avifBool isGrid = (memcmp(item->type, "grid", 4) == 0);
Joe Drago060d5342020-03-03 10:53:49 -08002672 if (memcmp(item->type, "av01", 4) && !isGrid) {
2673 // probably exif or some other data
2674 continue;
2675 }
Joe Dragof6a42272019-11-21 15:21:41 -08002676
Joe Dragobe4cbb92020-09-21 12:14:05 -07002677 // Is this an alpha auxiliary item of whatever we chose for colorItem?
Joe Dragoa72da5b2020-06-15 19:40:17 -07002678 const avifProperty * auxCProp = avifPropertyArrayFind(&item->properties, "auxC");
Joe Dragobe4cbb92020-09-21 12:14:05 -07002679 if (auxCProp && isAlphaURN(auxCProp->u.auxC.auxType) && (item->auxForID == colorItem->id)) {
Joe Drago060d5342020-03-03 10:53:49 -08002680 if (isGrid) {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002681 avifROData readData;
Wan-Teh Chang4548b162020-11-06 11:48:25 -08002682 avifResult readResult = avifDecoderItemRead(item, decoder->io, &readData, 0);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002683 if (readResult != AVIF_RESULT_OK) {
2684 return readResult;
2685 }
Wan-Teh Changc443f142020-10-08 14:58:42 -07002686 if (!avifParseImageGridBox(&data->alphaGrid, readData.data, readData.size)) {
Joe Drago060d5342020-03-03 10:53:49 -08002687 return AVIF_RESULT_INVALID_IMAGE_GRID;
2688 }
Joe Dragof6a42272019-11-21 15:21:41 -08002689 }
2690
Joe Dragobe4cbb92020-09-21 12:14:05 -07002691 alphaItem = item;
Joe Drago060d5342020-03-03 10:53:49 -08002692 break;
Joe Dragof6a42272019-11-21 15:21:41 -08002693 }
Joe Drago444f0512019-01-23 17:03:24 -08002694 }
Joe Drago444f0512019-01-23 17:03:24 -08002695
Joe Drago060d5342020-03-03 10:53:49 -08002696 // Find Exif and/or XMP metadata, if any
Joe Dragobe4cbb92020-09-21 12:14:05 -07002697 avifResult findResult = avifDecoderFindMetadata(decoder, data->meta, decoder->image, colorItem->id);
2698 if (findResult != AVIF_RESULT_OK) {
2699 return findResult;
Joe Drago060d5342020-03-03 10:53:49 -08002700 }
2701
Wan-Teh Chang4295bcb2020-04-05 15:41:05 -07002702 if ((data->colorGrid.rows > 0) && (data->colorGrid.columns > 0)) {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002703 if (!avifDecoderDataGenerateImageGridTiles(data, &data->colorGrid, colorItem, AVIF_FALSE)) {
Joe Drago060d5342020-03-03 10:53:49 -08002704 return AVIF_RESULT_INVALID_IMAGE_GRID;
2705 }
2706 data->colorTileCount = data->tiles.count;
2707 } else {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002708 if (colorItem->size == 0) {
Joe Drago060d5342020-03-03 10:53:49 -08002709 return AVIF_RESULT_NO_AV1_ITEMS_FOUND;
2710 }
2711
wantehchangb207b4d2020-08-11 17:50:22 -07002712 avifTile * colorTile = avifDecoderDataCreateTile(data);
Joe Dragoe3e3bfa2020-06-02 16:33:53 -07002713 avifDecodeSample * colorSample = (avifDecodeSample *)avifArrayPushPtr(&colorTile->input->samples);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002714 colorSample->itemID = colorItem->id;
2715 colorSample->offset = 0;
2716 colorSample->size = colorItem->size;
Joe Drago060d5342020-03-03 10:53:49 -08002717 colorSample->sync = AVIF_TRUE;
wantehchangb207b4d2020-08-11 17:50:22 -07002718 data->colorTileCount = 1;
Joe Drago060d5342020-03-03 10:53:49 -08002719 }
2720
Joe Dragobe4cbb92020-09-21 12:14:05 -07002721 if (alphaItem) {
Wan-Teh Chang272aafe2020-08-12 17:31:38 -07002722 if ((data->alphaGrid.rows > 0) && (data->alphaGrid.columns > 0)) {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002723 if (!avifDecoderDataGenerateImageGridTiles(data, &data->alphaGrid, alphaItem, AVIF_TRUE)) {
Wan-Teh Chang272aafe2020-08-12 17:31:38 -07002724 return AVIF_RESULT_INVALID_IMAGE_GRID;
2725 }
2726 data->alphaTileCount = data->tiles.count - data->colorTileCount;
2727 } else {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002728 if (alphaItem->size == 0) {
Wan-Teh Chang272aafe2020-08-12 17:31:38 -07002729 return AVIF_RESULT_NO_AV1_ITEMS_FOUND;
2730 }
2731
wantehchang76e16bf2020-08-12 13:01:31 -07002732 avifTile * alphaTile = avifDecoderDataCreateTile(data);
Joe Dragoe3e3bfa2020-06-02 16:33:53 -07002733 avifDecodeSample * alphaSample = (avifDecodeSample *)avifArrayPushPtr(&alphaTile->input->samples);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002734 alphaSample->itemID = alphaItem->id;
2735 alphaSample->offset = 0;
2736 alphaSample->size = alphaItem->size;
Joe Drago060d5342020-03-03 10:53:49 -08002737 alphaSample->sync = AVIF_TRUE;
2738 alphaTile->input->alpha = AVIF_TRUE;
wantehchangb207b4d2020-08-11 17:50:22 -07002739 data->alphaTileCount = 1;
Joe Drago060d5342020-03-03 10:53:49 -08002740 }
Joe Drago444f0512019-01-23 17:03:24 -08002741 }
Joe Drago33f1d362019-02-13 16:46:22 -08002742
Joe Drago46ea0582019-07-22 15:55:47 -07002743 // Set all counts and timing to safe-but-uninteresting values
2744 decoder->imageIndex = -1;
2745 decoder->imageCount = 1;
2746 decoder->imageTiming.timescale = 1;
2747 decoder->imageTiming.pts = 0;
2748 decoder->imageTiming.ptsInTimescales = 0;
2749 decoder->imageTiming.duration = 1;
2750 decoder->imageTiming.durationInTimescales = 1;
2751 decoder->timescale = 1;
2752 decoder->duration = 1;
2753 decoder->durationInTimescales = 1;
Joe Drago70cbf602019-07-24 15:30:55 -07002754
Joe Dragobe4cbb92020-09-21 12:14:05 -07002755 decoder->ioStats.colorOBUSize = colorItem->size;
2756 decoder->ioStats.alphaOBUSize = alphaItem ? alphaItem->size : 0;
Joe Drago41700852019-09-26 17:01:43 -07002757
Joe Dragoa72da5b2020-06-15 19:40:17 -07002758 const avifProperty * ispeProp = avifPropertyArrayFind(colorProperties, "ispe");
2759 if (ispeProp) {
2760 decoder->image->width = ispeProp->u.ispe.width;
2761 decoder->image->height = ispeProp->u.ispe.height;
Joe Drago41700852019-09-26 17:01:43 -07002762 } else {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002763 decoder->image->width = 0;
2764 decoder->image->height = 0;
Joe Drago41700852019-09-26 17:01:43 -07002765 }
Joe Dragobe4cbb92020-09-21 12:14:05 -07002766 decoder->alphaPresent = (alphaItem != NULL);
Joe Dragoceb2fa02021-01-29 18:14:55 -08002767 decoder->image->alphaPremultiplied = decoder->alphaPresent && (colorItem->premByID == alphaItem->id);
Joe Drago00bcaaf2020-06-05 15:29:38 -07002768 }
2769
Joe Drago11f2a5e2020-07-06 10:49:00 -07002770 // Sanity check tiles
2771 for (uint32_t tileIndex = 0; tileIndex < data->tiles.count; ++tileIndex) {
2772 avifTile * tile = &data->tiles.tile[tileIndex];
2773 for (uint32_t sampleIndex = 0; sampleIndex < tile->input->samples.count; ++sampleIndex) {
Joe Drago043311b2020-07-06 16:48:41 -07002774 avifDecodeSample * sample = &tile->input->samples.sample[sampleIndex];
Joe Dragobe4cbb92020-09-21 12:14:05 -07002775 if (!sample->size) {
Joe Drago11f2a5e2020-07-06 10:49:00 -07002776 // Every sample must have some data
2777 return AVIF_RESULT_BMFF_PARSE_FAILED;
2778 }
2779 }
2780 }
2781
Joe Dragobf58fe72020-11-05 13:25:14 -08002782 // Find and adopt all colr boxes "at most one for a given value of colour type" (HEIF 6.5.5.1, from Amendment 3)
2783 // Accept one of each type, and bail out if more than one of a given type is provided.
2784 avifBool colrICCSeen = AVIF_FALSE;
2785 avifBool colrNCLXSeen = AVIF_FALSE;
2786 for (uint32_t propertyIndex = 0; propertyIndex < colorProperties->count; ++propertyIndex) {
2787 avifProperty * prop = &colorProperties->prop[propertyIndex];
2788
2789 if (!memcmp(prop->type, "colr", 4)) {
2790 if (prop->u.colr.hasICC) {
2791 if (colrICCSeen) {
2792 return AVIF_RESULT_BMFF_PARSE_FAILED;
2793 }
2794 colrICCSeen = AVIF_TRUE;
2795 avifImageSetProfileICC(decoder->image, prop->u.colr.icc, prop->u.colr.iccSize);
2796 }
2797 if (prop->u.colr.hasNCLX) {
2798 if (colrNCLXSeen) {
2799 return AVIF_RESULT_BMFF_PARSE_FAILED;
2800 }
2801 colrNCLXSeen = AVIF_TRUE;
2802 data->cicpSet = AVIF_TRUE;
2803 decoder->image->colorPrimaries = prop->u.colr.colorPrimaries;
2804 decoder->image->transferCharacteristics = prop->u.colr.transferCharacteristics;
2805 decoder->image->matrixCoefficients = prop->u.colr.matrixCoefficients;
2806 decoder->image->yuvRange = prop->u.colr.range;
2807 }
Joe Dragoa72da5b2020-06-15 19:40:17 -07002808 }
2809 }
2810
2811 // Transformations
2812 const avifProperty * paspProp = avifPropertyArrayFind(colorProperties, "pasp");
2813 if (paspProp) {
2814 decoder->image->transformFlags |= AVIF_TRANSFORM_PASP;
2815 memcpy(&decoder->image->pasp, &paspProp->u.pasp, sizeof(avifPixelAspectRatioBox));
2816 }
2817 const avifProperty * clapProp = avifPropertyArrayFind(colorProperties, "clap");
2818 if (clapProp) {
2819 decoder->image->transformFlags |= AVIF_TRANSFORM_CLAP;
2820 memcpy(&decoder->image->clap, &clapProp->u.clap, sizeof(avifCleanApertureBox));
2821 }
2822 const avifProperty * irotProp = avifPropertyArrayFind(colorProperties, "irot");
2823 if (irotProp) {
2824 decoder->image->transformFlags |= AVIF_TRANSFORM_IROT;
2825 memcpy(&decoder->image->irot, &irotProp->u.irot, sizeof(avifImageRotation));
2826 }
2827 const avifProperty * imirProp = avifPropertyArrayFind(colorProperties, "imir");
2828 if (imirProp) {
2829 decoder->image->transformFlags |= AVIF_TRANSFORM_IMIR;
2830 memcpy(&decoder->image->imir, &imirProp->u.imir, sizeof(avifImageMirror));
2831 }
2832
wantehchangb207b4d2020-08-11 17:50:22 -07002833 if (!data->cicpSet && (data->tiles.count > 0)) {
Joe Dragoda92a382020-06-09 17:08:45 -07002834 avifTile * firstTile = &data->tiles.tile[0];
2835 if (firstTile->input->samples.count > 0) {
2836 avifDecodeSample * sample = &firstTile->input->samples.sample[0];
Joe Dragobe4cbb92020-09-21 12:14:05 -07002837
2838 // Harvest CICP from the AV1's sequence header, which should be very close to the front
2839 // of the first sample. Read in successively larger chunks until we successfully parse the sequence.
2840 static const size_t searchSampleChunkIncrement = 64;
2841 size_t searchSampleSize = 0;
Wan-Teh Changc6c2fe82020-10-08 10:44:04 -07002842 do {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002843 searchSampleSize += searchSampleChunkIncrement;
2844 if (searchSampleSize > sample->size) {
2845 searchSampleSize = sample->size;
2846 }
2847
2848 avifResult prepareResult = avifDecoderPrepareSample(decoder, sample, searchSampleSize);
2849 if (prepareResult != AVIF_RESULT_OK) {
2850 return prepareResult;
2851 }
2852
2853 avifSequenceHeader sequenceHeader;
2854 if (avifSequenceHeaderParse(&sequenceHeader, &sample->data)) {
2855 data->cicpSet = AVIF_TRUE;
2856 decoder->image->colorPrimaries = sequenceHeader.colorPrimaries;
2857 decoder->image->transferCharacteristics = sequenceHeader.transferCharacteristics;
2858 decoder->image->matrixCoefficients = sequenceHeader.matrixCoefficients;
2859 decoder->image->yuvRange = sequenceHeader.range;
2860 break;
2861 }
Wan-Teh Changc6c2fe82020-10-08 10:44:04 -07002862 } while (searchSampleSize != sample->size);
Joe Dragoda92a382020-06-09 17:08:45 -07002863 }
2864 }
2865
Joe Dragoa72da5b2020-06-15 19:40:17 -07002866 const avifProperty * av1CProp = avifPropertyArrayFind(colorProperties, "av1C");
2867 if (av1CProp) {
Joe Dragob8401122020-06-19 11:45:49 -07002868 decoder->image->depth = avifCodecConfigurationBoxGetDepth(&av1CProp->u.av1C);
2869 if (av1CProp->u.av1C.monochrome) {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002870 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV400;
Joe Drago7b2cf802020-06-09 17:57:23 -07002871 } else {
Joe Dragob8401122020-06-19 11:45:49 -07002872 if (av1CProp->u.av1C.chromaSubsamplingX && av1CProp->u.av1C.chromaSubsamplingY) {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002873 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV420;
Joe Dragob8401122020-06-19 11:45:49 -07002874 } else if (av1CProp->u.av1C.chromaSubsamplingX) {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002875 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV422;
Joe Drago7b2cf802020-06-09 17:57:23 -07002876
2877 } else {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002878 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV444;
Joe Drago7b2cf802020-06-09 17:57:23 -07002879 }
2880 }
Joe Dragob8401122020-06-19 11:45:49 -07002881 decoder->image->yuvChromaSamplePosition = (avifChromaSamplePosition)av1CProp->u.av1C.chromaSamplePosition;
Joe Drago00bcaaf2020-06-05 15:29:38 -07002882 } else {
Joe Dragof48a3382020-06-19 14:13:44 -07002883 // An av1C box is mandatory in all valid AVIF configurations. Bail out.
2884 return AVIF_RESULT_BMFF_PARSE_FAILED;
Joe Drago46ea0582019-07-22 15:55:47 -07002885 }
2886
Joe Drago22c1ad92019-09-26 12:46:50 -07002887 return avifDecoderFlush(decoder);
Joe Drago46ea0582019-07-22 15:55:47 -07002888}
Joe Drago444f0512019-01-23 17:03:24 -08002889
Joe Drago5998f592020-11-13 15:38:20 -08002890avifResult avifDecoderNextImage(avifDecoder * decoder)
Joe Drago46ea0582019-07-22 15:55:47 -07002891{
Wan-Teh Changaf0d4842020-11-17 12:27:34 -08002892 if (!decoder->data) {
2893 // Nothing has been parsed yet
2894 return AVIF_RESULT_NO_CONTENT;
2895 }
2896
Joe Dragobe4cbb92020-09-21 12:14:05 -07002897 if (!decoder->io || !decoder->io->read) {
Wan-Teh Chang31c7c1a2020-10-13 16:45:41 -07002898 return AVIF_RESULT_IO_NOT_SET;
Joe Dragobe4cbb92020-09-21 12:14:05 -07002899 }
2900
Wan-Teh Changaf0d4842020-11-17 12:27:34 -08002901 const uint32_t nextImageIndex = (uint32_t)(decoder->imageIndex + 1);
Joe Dragobe4cbb92020-09-21 12:14:05 -07002902
2903 // Acquire all sample data for the current image first, allowing for any read call to bail out
2904 // with AVIF_RESULT_WAITING_ON_IO harmlessly / idempotently.
2905 for (unsigned int tileIndex = 0; tileIndex < decoder->data->tiles.count; ++tileIndex) {
2906 avifTile * tile = &decoder->data->tiles.tile[tileIndex];
Joe Drago5998f592020-11-13 15:38:20 -08002907 if (nextImageIndex >= tile->input->samples.count) {
Joe Dragobe4cbb92020-09-21 12:14:05 -07002908 return AVIF_RESULT_NO_IMAGES_REMAINING;
2909 }
2910
Joe Drago5998f592020-11-13 15:38:20 -08002911 avifDecodeSample * sample = &tile->input->samples.sample[nextImageIndex];
Joe Dragobe4cbb92020-09-21 12:14:05 -07002912 avifResult prepareResult = avifDecoderPrepareSample(decoder, sample, 0);
2913 if (prepareResult != AVIF_RESULT_OK) {
2914 return prepareResult;
2915 }
2916 }
2917
Wan-Teh Changaf0d4842020-11-17 12:27:34 -08002918 // Decode all tiles now that the sample data is ready.
Joe Drago060d5342020-03-03 10:53:49 -08002919 for (unsigned int tileIndex = 0; tileIndex < decoder->data->tiles.count; ++tileIndex) {
2920 avifTile * tile = &decoder->data->tiles.tile[tileIndex];
Joe Drago41eb62b2019-02-08 15:38:18 -08002921
Wan-Teh Changb4977b32020-10-05 18:04:54 -07002922 const avifDecodeSample * sample = &tile->input->samples.sample[nextImageIndex];
Joe Dragobe4cbb92020-09-21 12:14:05 -07002923
2924 if (!tile->codec->getNextImage(tile->codec, sample, tile->input->alpha, tile->image)) {
Joe Drago060d5342020-03-03 10:53:49 -08002925 if (tile->input->alpha) {
2926 return AVIF_RESULT_DECODE_ALPHA_FAILED;
2927 } else {
2928 if (tile->image->width) {
2929 // We've sent at least one image, but we've run out now.
2930 return AVIF_RESULT_NO_IMAGES_REMAINING;
2931 }
2932 return AVIF_RESULT_DECODE_COLOR_FAILED;
2933 }
Joe Drago46ea0582019-07-22 15:55:47 -07002934 }
Joe Drago060d5342020-03-03 10:53:49 -08002935 }
2936
2937 if (decoder->data->tiles.count != (decoder->data->colorTileCount + decoder->data->alphaTileCount)) {
2938 // TODO: assert here? This should be impossible.
2939 return AVIF_RESULT_UNKNOWN_ERROR;
2940 }
2941
Wan-Teh Changab8d9a52020-08-12 17:17:37 -07002942 if ((decoder->data->colorGrid.rows > 0) && (decoder->data->colorGrid.columns > 0)) {
Joe Drago11d23592021-01-05 14:18:57 -08002943 if (!avifDecoderDataFillImageGrid(decoder->data, &decoder->data->colorGrid, decoder->image, 0, decoder->data->colorTileCount, AVIF_FALSE)) {
Joe Drago060d5342020-03-03 10:53:49 -08002944 return AVIF_RESULT_INVALID_IMAGE_GRID;
2945 }
2946 } else {
2947 // Normal (most common) non-grid path. Just steal the planes from the only "tile".
2948
2949 if (decoder->data->colorTileCount != 1) {
2950 return AVIF_RESULT_DECODE_COLOR_FAILED;
2951 }
2952
2953 avifImage * srcColor = decoder->data->tiles.tile[0].image;
2954
2955 if ((decoder->image->width != srcColor->width) || (decoder->image->height != srcColor->height) ||
2956 (decoder->image->depth != srcColor->depth)) {
2957 avifImageFreePlanes(decoder->image, AVIF_PLANES_ALL);
2958
2959 decoder->image->width = srcColor->width;
2960 decoder->image->height = srcColor->height;
2961 decoder->image->depth = srcColor->depth;
Joe Dragoc554f5f2020-06-09 18:59:51 -07002962 }
psi / Ryo Hirafuji922d8a12020-03-10 03:24:57 +09002963
Joe Dragoc00d5832020-08-13 16:03:28 -07002964#if 0
2965 // This code is currently unnecessary as the CICP is always set by the end of avifDecoderParse().
2966 if (!decoder->data->cicpSet) {
2967 decoder->data->cicpSet = AVIF_TRUE;
2968 decoder->image->colorPrimaries = srcColor->colorPrimaries;
2969 decoder->image->transferCharacteristics = srcColor->transferCharacteristics;
2970 decoder->image->matrixCoefficients = srcColor->matrixCoefficients;
2971 }
2972#endif
2973
Joe Drago060d5342020-03-03 10:53:49 -08002974 avifImageStealPlanes(decoder->image, srcColor, AVIF_PLANES_YUV);
2975 }
2976
Wan-Teh Changab8d9a52020-08-12 17:17:37 -07002977 if ((decoder->data->alphaGrid.rows > 0) && (decoder->data->alphaGrid.columns > 0)) {
Joe Drago11d23592021-01-05 14:18:57 -08002978 if (!avifDecoderDataFillImageGrid(decoder->data,
2979 &decoder->data->alphaGrid,
2980 decoder->image,
2981 decoder->data->colorTileCount,
2982 decoder->data->alphaTileCount,
2983 AVIF_TRUE)) {
Joe Drago060d5342020-03-03 10:53:49 -08002984 return AVIF_RESULT_INVALID_IMAGE_GRID;
2985 }
2986 } else {
2987 // Normal (most common) non-grid path. Just steal the planes from the only "tile".
2988
2989 if (decoder->data->alphaTileCount == 0) {
2990 avifImageFreePlanes(decoder->image, AVIF_PLANES_A); // no alpha
2991 } else {
2992 if (decoder->data->alphaTileCount != 1) {
2993 return AVIF_RESULT_DECODE_ALPHA_FAILED;
2994 }
2995
2996 avifImage * srcAlpha = decoder->data->tiles.tile[decoder->data->colorTileCount].image;
2997 if ((decoder->image->width != srcAlpha->width) || (decoder->image->height != srcAlpha->height) ||
2998 (decoder->image->depth != srcAlpha->depth)) {
2999 return AVIF_RESULT_DECODE_ALPHA_FAILED;
3000 }
3001
3002 avifImageStealPlanes(decoder->image, srcAlpha, AVIF_PLANES_A);
Joe Drago3fd2db22020-08-13 16:07:24 -07003003 decoder->image->alphaRange = srcAlpha->alphaRange;
Joe Drago060d5342020-03-03 10:53:49 -08003004 }
3005 }
Joe Drago7ad3ad62019-02-07 11:17:34 -08003006
Joe Dragobe4cbb92020-09-21 12:14:05 -07003007 decoder->imageIndex = nextImageIndex;
Joe Drago46ea0582019-07-22 15:55:47 -07003008 if (decoder->data->sourceSampleTable) {
3009 // Decoding from a track! Provide timing information.
Joe Drago05559c92019-07-17 16:33:38 -07003010
Joe Dragoe9c58602020-04-13 17:23:13 -07003011 avifResult timingResult = avifDecoderNthImageTiming(decoder, decoder->imageIndex, &decoder->imageTiming);
3012 if (timingResult != AVIF_RESULT_OK) {
3013 return timingResult;
Joe Drago22c1ad92019-09-26 12:46:50 -07003014 }
Joe Dragoe9c58602020-04-13 17:23:13 -07003015 }
3016 return AVIF_RESULT_OK;
3017}
Joe Drago46ea0582019-07-22 15:55:47 -07003018
Wan-Teh Change184dc12020-05-11 12:47:21 -07003019avifResult avifDecoderNthImageTiming(const avifDecoder * decoder, uint32_t frameIndex, avifImageTiming * outTiming)
Joe Dragoe9c58602020-04-13 17:23:13 -07003020{
3021 if (!decoder->data) {
3022 // Nothing has been parsed yet
3023 return AVIF_RESULT_NO_CONTENT;
3024 }
3025
Wan-Teh Chang25b9c702020-10-08 12:17:17 -07003026 if ((frameIndex > INT_MAX) || ((int)frameIndex >= decoder->imageCount)) {
Joe Dragoe9c58602020-04-13 17:23:13 -07003027 // Impossible index
3028 return AVIF_RESULT_NO_IMAGES_REMAINING;
3029 }
3030
3031 if (!decoder->data->sourceSampleTable) {
3032 // There isn't any real timing associated with this decode, so
3033 // just hand back the defaults chosen in avifDecoderReset().
3034 memcpy(outTiming, &decoder->imageTiming, sizeof(avifImageTiming));
3035 return AVIF_RESULT_OK;
3036 }
3037
Wan-Teh Chang167e4062020-04-14 16:06:12 -07003038 outTiming->timescale = decoder->timescale;
3039 outTiming->ptsInTimescales = 0;
Joe Dragoe9c58602020-04-13 17:23:13 -07003040 for (int imageIndex = 0; imageIndex < (int)frameIndex; ++imageIndex) {
Wan-Teh Chang167e4062020-04-14 16:06:12 -07003041 outTiming->ptsInTimescales += avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, imageIndex);
Joe Dragoe9c58602020-04-13 17:23:13 -07003042 }
Wan-Teh Chang167e4062020-04-14 16:06:12 -07003043 outTiming->durationInTimescales = avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, frameIndex);
Joe Dragoe9c58602020-04-13 17:23:13 -07003044
Wan-Teh Chang167e4062020-04-14 16:06:12 -07003045 if (outTiming->timescale > 0) {
3046 outTiming->pts = (double)outTiming->ptsInTimescales / (double)outTiming->timescale;
3047 outTiming->duration = (double)outTiming->durationInTimescales / (double)outTiming->timescale;
Joe Dragoe9c58602020-04-13 17:23:13 -07003048 } else {
Wan-Teh Chang167e4062020-04-14 16:06:12 -07003049 outTiming->pts = 0.0;
3050 outTiming->duration = 0.0;
Joe Drago444f0512019-01-23 17:03:24 -08003051 }
Joe Drago46ea0582019-07-22 15:55:47 -07003052 return AVIF_RESULT_OK;
3053}
3054
Joe Drago22c1ad92019-09-26 12:46:50 -07003055avifResult avifDecoderNthImage(avifDecoder * decoder, uint32_t frameIndex)
3056{
Wan-Teh Chang25b9c702020-10-08 12:17:17 -07003057 if (frameIndex > INT_MAX) {
3058 // Impossible index
3059 return AVIF_RESULT_NO_IMAGES_REMAINING;
3060 }
3061
Joe Drago22c1ad92019-09-26 12:46:50 -07003062 int requestedIndex = (int)frameIndex;
3063 if (requestedIndex == decoder->imageIndex) {
3064 // We're here already, nothing to do
3065 return AVIF_RESULT_OK;
3066 }
3067
3068 if (requestedIndex == (decoder->imageIndex + 1)) {
3069 // it's just the next image, nothing special here
3070 return avifDecoderNextImage(decoder);
3071 }
3072
3073 if (requestedIndex >= decoder->imageCount) {
3074 // Impossible index
3075 return AVIF_RESULT_NO_IMAGES_REMAINING;
3076 }
3077
Wan-Teh Chang722fb7e2020-11-06 12:39:58 -08003078 int nearestKeyFrame = (int)avifDecoderNearestKeyframe(decoder, frameIndex);
3079 if ((nearestKeyFrame > (decoder->imageIndex + 1)) || (requestedIndex < decoder->imageIndex)) {
3080 // If we get here, a decoder flush is necessary
3081 decoder->imageIndex = nearestKeyFrame - 1; // prepare to read nearest keyframe
3082 avifDecoderFlush(decoder);
3083 }
Joe Drago22c1ad92019-09-26 12:46:50 -07003084 for (;;) {
3085 avifResult result = avifDecoderNextImage(decoder);
3086 if (result != AVIF_RESULT_OK) {
3087 return result;
3088 }
3089
3090 if (requestedIndex == decoder->imageIndex) {
3091 break;
3092 }
Joe Dragofc4144e2019-09-27 20:35:06 -07003093 }
Joe Drago22c1ad92019-09-26 12:46:50 -07003094 return AVIF_RESULT_OK;
3095}
3096
Wan-Teh Change184dc12020-05-11 12:47:21 -07003097avifBool avifDecoderIsKeyframe(const avifDecoder * decoder, uint32_t frameIndex)
Joe Drago22c1ad92019-09-26 12:46:50 -07003098{
Wan-Teh Changaf0d4842020-11-17 12:27:34 -08003099 if (!decoder->data) {
3100 // Nothing has been parsed yet
3101 return AVIF_FALSE;
3102 }
3103
Joe Drago060d5342020-03-03 10:53:49 -08003104 if ((decoder->data->tiles.count > 0) && decoder->data->tiles.tile[0].input) {
3105 if (frameIndex < decoder->data->tiles.tile[0].input->samples.count) {
3106 return decoder->data->tiles.tile[0].input->samples.sample[frameIndex].sync;
Joe Drago22c1ad92019-09-26 12:46:50 -07003107 }
3108 }
3109 return AVIF_FALSE;
3110}
3111
Wan-Teh Change184dc12020-05-11 12:47:21 -07003112uint32_t avifDecoderNearestKeyframe(const avifDecoder * decoder, uint32_t frameIndex)
Joe Drago22c1ad92019-09-26 12:46:50 -07003113{
Wan-Teh Changaf0d4842020-11-17 12:27:34 -08003114 if (!decoder->data) {
3115 // Nothing has been parsed yet
3116 return 0;
3117 }
3118
Joe Drago22c1ad92019-09-26 12:46:50 -07003119 for (; frameIndex != 0; --frameIndex) {
3120 if (avifDecoderIsKeyframe(decoder, frameIndex)) {
3121 break;
3122 }
3123 }
3124 return frameIndex;
3125}
3126
Joe Dragobe4cbb92020-09-21 12:14:05 -07003127avifResult avifDecoderRead(avifDecoder * decoder, avifImage * image)
Joe Drago46ea0582019-07-22 15:55:47 -07003128{
Joe Dragobe4cbb92020-09-21 12:14:05 -07003129 avifResult result = avifDecoderParse(decoder);
Joe Drago46ea0582019-07-22 15:55:47 -07003130 if (result != AVIF_RESULT_OK) {
3131 return result;
Joe Drago05559c92019-07-17 16:33:38 -07003132 }
Joe Drago46ea0582019-07-22 15:55:47 -07003133 result = avifDecoderNextImage(decoder);
3134 if (result != AVIF_RESULT_OK) {
3135 return result;
3136 }
Joe Drago250221a2020-06-01 11:11:06 -07003137 avifImageCopy(image, decoder->image, AVIF_PLANES_ALL);
Joe Drago46ea0582019-07-22 15:55:47 -07003138 return AVIF_RESULT_OK;
Joe Drago444f0512019-01-23 17:03:24 -08003139}
Joe Dragobe4cbb92020-09-21 12:14:05 -07003140
Wan-Teh Chang7de44452020-10-08 11:43:18 -07003141avifResult avifDecoderReadMemory(avifDecoder * decoder, avifImage * image, const uint8_t * data, size_t size)
Joe Dragobe4cbb92020-09-21 12:14:05 -07003142{
Wan-Teh Chang7de44452020-10-08 11:43:18 -07003143 avifResult result = avifDecoderSetIOMemory(decoder, data, size);
Joe Dragobe4cbb92020-09-21 12:14:05 -07003144 if (result != AVIF_RESULT_OK) {
3145 return result;
3146 }
3147 return avifDecoderRead(decoder, image);
3148}
3149
3150avifResult avifDecoderReadFile(avifDecoder * decoder, avifImage * image, const char * filename)
3151{
3152 avifResult result = avifDecoderSetIOFile(decoder, filename);
3153 if (result != AVIF_RESULT_OK) {
3154 return result;
3155 }
3156 return avifDecoderRead(decoder, image);
3157}