blob: 3def77eb8653860e849dfc7987f826b225511f96 [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
Joe Drago444f0512019-01-23 17:03:24 -08006#include <string.h>
7
Joe Dragocd1e4c32019-02-08 11:26:31 -08008#define AUXTYPE_SIZE 64
Joe Dragof6a42272019-11-21 15:21:41 -08009#define CONTENTTYPE_SIZE 64
Joe Drago8f7a3002019-02-07 19:35:37 -080010
Joe Drago6500fd62019-10-08 17:17:34 -070011// class VisualSampleEntry(codingname) extends SampleEntry(codingname) {
12// unsigned int(16) pre_defined = 0;
13// const unsigned int(16) reserved = 0;
14// unsigned int(32)[3] pre_defined = 0;
15// unsigned int(16) width;
16// unsigned int(16) height;
17// template unsigned int(32) horizresolution = 0x00480000; // 72 dpi
18// template unsigned int(32) vertresolution = 0x00480000; // 72 dpi
19// const unsigned int(32) reserved = 0;
20// template unsigned int(16) frame_count = 1;
21// string[32] compressorname;
22// template unsigned int(16) depth = 0x0018;
23// int(16) pre_defined = -1;
24// // other boxes from derived specifications
25// CleanApertureBox clap; // optional
26// PixelAspectRatioBox pasp; // optional
27// }
28static const size_t VISUALSAMPLEENTRY_SIZE = 78;
29
Joe Dragof6a42272019-11-21 15:21:41 -080030static const char xmpContentType[] = CONTENT_TYPE_XMP;
31static const size_t xmpContentTypeSize = sizeof(xmpContentType);
32
Joe Dragob13e5722019-02-08 19:07:25 -080033// ---------------------------------------------------------------------------
34// Box data structures
35
36// ftyp
37typedef struct avifFileType
38{
39 uint8_t majorBrand[4];
40 uint32_t minorVersion;
Wan-Teh Chang6da0a882020-07-01 12:19:31 -070041 // If not null, points to a memory block of 4 * compatibleBrandsCount bytes.
42 const uint8_t * compatibleBrands;
Joe Dragob13e5722019-02-08 19:07:25 -080043 int compatibleBrandsCount;
44} avifFileType;
45
46// ispe
Joe Drago8f7a3002019-02-07 19:35:37 -080047typedef struct avifImageSpatialExtents
48{
49 uint32_t width;
50 uint32_t height;
51} avifImageSpatialExtents;
Joe Drago444f0512019-01-23 17:03:24 -080052
Joe Dragob13e5722019-02-08 19:07:25 -080053// auxC
Joe Dragocd1e4c32019-02-08 11:26:31 -080054typedef struct avifAuxiliaryType
55{
56 char auxType[AUXTYPE_SIZE];
57} avifAuxiliaryType;
58
Joe Dragof6a42272019-11-21 15:21:41 -080059// infe mime content_type
60typedef struct avifContentType
61{
62 char contentType[CONTENTTYPE_SIZE];
63} avifContentType;
64
Joe Dragob13e5722019-02-08 19:07:25 -080065// colr
Joe Drago41eb62b2019-02-08 15:38:18 -080066typedef struct avifColourInformationBox
67{
Joe Dragoa0da4a42020-05-08 14:27:40 -070068 avifBool hasICC;
Joe Drago345aaa12019-09-25 13:42:12 -070069 const uint8_t * icc;
Joe Drago41eb62b2019-02-08 15:38:18 -080070 size_t iccSize;
Joe Dragoa0da4a42020-05-08 14:27:40 -070071
72 avifBool hasNCLX;
73 avifColorPrimaries colorPrimaries;
74 avifTransferCharacteristics transferCharacteristics;
75 avifMatrixCoefficients matrixCoefficients;
76 avifRange range;
Joe Drago41eb62b2019-02-08 15:38:18 -080077} avifColourInformationBox;
78
Joe Drago60421562020-04-23 11:32:26 -070079#define MAX_PIXI_PLANE_DEPTHS 4
80typedef struct avifPixelInformationProperty
81{
82 uint8_t planeDepths[MAX_PIXI_PLANE_DEPTHS];
83 uint8_t planeCount;
84} avifPixelInformationProperty;
85
Joe Dragob13e5722019-02-08 19:07:25 -080086// ---------------------------------------------------------------------------
87// Top-level structures
88
Joe Drago9f2b87b2020-06-03 19:36:38 -070089struct avifMeta;
90
Joe Dragoa72da5b2020-06-15 19:40:17 -070091// Temporary storage for ipco/stsd contents until they can be associated and memcpy'd to an avifDecoderItem
Joe Drago8f7a3002019-02-07 19:35:37 -080092typedef struct avifProperty
93{
94 uint8_t type[4];
Wan-Teh Chang2031dc12020-05-22 09:24:46 -070095 union
96 {
97 avifImageSpatialExtents ispe;
98 avifAuxiliaryType auxC;
99 avifColourInformationBox colr;
100 avifCodecConfigurationBox av1C;
101 avifPixelAspectRatioBox pasp;
102 avifCleanApertureBox clap;
103 avifImageRotation irot;
104 avifImageMirror imir;
105 avifPixelInformationProperty pixi;
106 } u;
Joe Drago8f7a3002019-02-07 19:35:37 -0800107} avifProperty;
Joe Drago05559c92019-07-17 16:33:38 -0700108AVIF_ARRAY_DECLARE(avifPropertyArray, avifProperty, prop);
Joe Drago8f7a3002019-02-07 19:35:37 -0800109
Joe Dragoa72da5b2020-06-15 19:40:17 -0700110static const avifProperty * avifPropertyArrayFind(const avifPropertyArray * properties, const char * type)
111{
112 for (uint32_t propertyIndex = 0; propertyIndex < properties->count; ++propertyIndex) {
113 avifProperty * prop = &properties->prop[propertyIndex];
114 if (!memcmp(prop->type, type, 4)) {
115 return prop;
116 }
117 }
118 return NULL;
119}
120
121// one "item" worth for decoding (all iref, iloc, iprp, etc refer to one of these)
122typedef struct avifDecoderItem
123{
124 uint32_t id;
Joe Dragoba1eb492020-06-22 17:05:04 -0700125 struct avifMeta * meta; // Unowned; A back-pointer for convenience
Joe Dragoa72da5b2020-06-15 19:40:17 -0700126 uint8_t type[4];
127 uint32_t offset;
128 uint32_t size;
129 uint32_t idatID; // If non-zero, offset is relative to this idat box (iloc construction_method==1)
130 avifContentType contentType;
131 avifPropertyArray properties;
132 uint32_t thumbnailForID; // if non-zero, this item is a thumbnail for Item #{thumbnailForID}
133 uint32_t auxForID; // if non-zero, this item is an auxC plane for Item #{auxForID}
134 uint32_t descForID; // if non-zero, this item is a content description for Item #{descForID}
135 uint32_t dimgForID; // if non-zero, this item is a derived image for Item #{dimgForID}
136 avifBool hasUnsupportedEssentialProperty; // If true, this item cites a property flagged as 'essential' that libavif doesn't support (yet). Ignore the item, if so.
137} avifDecoderItem;
138AVIF_ARRAY_DECLARE(avifDecoderItemArray, avifDecoderItem, item);
139
Joe Dragof6a42272019-11-21 15:21:41 -0800140// idat storage
Joe Drago800b47f2020-03-18 16:22:37 -0700141typedef struct avifDecoderItemData
Joe Dragof6a42272019-11-21 15:21:41 -0800142{
143 uint32_t id;
144 avifROData data;
Joe Drago800b47f2020-03-18 16:22:37 -0700145} avifDecoderItemData;
146AVIF_ARRAY_DECLARE(avifDecoderItemDataArray, avifDecoderItemData, idat);
Joe Dragof6a42272019-11-21 15:21:41 -0800147
Joe Drago060d5342020-03-03 10:53:49 -0800148// grid storage
149typedef struct avifImageGrid
150{
151 uint8_t rows;
152 uint8_t columns;
153 uint32_t outputWidth;
154 uint32_t outputHeight;
155} avifImageGrid;
156
Joe Dragoae7e2c32019-07-18 15:22:25 -0700157// ---------------------------------------------------------------------------
158// avifTrack
159
160typedef struct avifSampleTableChunk
161{
162 uint64_t offset;
Joe Dragoae7e2c32019-07-18 15:22:25 -0700163} avifSampleTableChunk;
164AVIF_ARRAY_DECLARE(avifSampleTableChunkArray, avifSampleTableChunk, chunk);
165
166typedef struct avifSampleTableSampleToChunk
167{
168 uint32_t firstChunk;
169 uint32_t samplesPerChunk;
170 uint32_t sampleDescriptionIndex;
171} avifSampleTableSampleToChunk;
172AVIF_ARRAY_DECLARE(avifSampleTableSampleToChunkArray, avifSampleTableSampleToChunk, sampleToChunk);
173
174typedef struct avifSampleTableSampleSize
175{
176 uint32_t size;
177} avifSampleTableSampleSize;
178AVIF_ARRAY_DECLARE(avifSampleTableSampleSizeArray, avifSampleTableSampleSize, sampleSize);
179
180typedef struct avifSampleTableTimeToSample
181{
182 uint32_t sampleCount;
183 uint32_t sampleDelta;
184} avifSampleTableTimeToSample;
185AVIF_ARRAY_DECLARE(avifSampleTableTimeToSampleArray, avifSampleTableTimeToSample, timeToSample);
186
Joe Drago22c1ad92019-09-26 12:46:50 -0700187typedef struct avifSyncSample
188{
189 uint32_t sampleNumber;
190} avifSyncSample;
191AVIF_ARRAY_DECLARE(avifSyncSampleArray, avifSyncSample, syncSample);
192
Joe Drago2c0924c2019-09-26 17:41:01 -0700193typedef struct avifSampleDescription
194{
195 uint8_t format[4];
Joe Dragoa72da5b2020-06-15 19:40:17 -0700196 avifPropertyArray properties;
Joe Drago2c0924c2019-09-26 17:41:01 -0700197} avifSampleDescription;
198AVIF_ARRAY_DECLARE(avifSampleDescriptionArray, avifSampleDescription, description);
199
Joe Dragoae7e2c32019-07-18 15:22:25 -0700200typedef struct avifSampleTable
201{
202 avifSampleTableChunkArray chunks;
Joe Drago2c0924c2019-09-26 17:41:01 -0700203 avifSampleDescriptionArray sampleDescriptions;
Joe Dragoae7e2c32019-07-18 15:22:25 -0700204 avifSampleTableSampleToChunkArray sampleToChunks;
205 avifSampleTableSampleSizeArray sampleSizes;
206 avifSampleTableTimeToSampleArray timeToSamples;
Joe Drago22c1ad92019-09-26 12:46:50 -0700207 avifSyncSampleArray syncSamples;
Joe Drago370be3f2020-02-07 15:59:42 -0800208 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 -0700209} avifSampleTable;
210
Joe Drago46ea0582019-07-22 15:55:47 -0700211static avifSampleTable * avifSampleTableCreate()
Joe Dragoae7e2c32019-07-18 15:22:25 -0700212{
213 avifSampleTable * sampleTable = (avifSampleTable *)avifAlloc(sizeof(avifSampleTable));
214 memset(sampleTable, 0, sizeof(avifSampleTable));
215 avifArrayCreate(&sampleTable->chunks, sizeof(avifSampleTableChunk), 16);
Joe Drago2c0924c2019-09-26 17:41:01 -0700216 avifArrayCreate(&sampleTable->sampleDescriptions, sizeof(avifSampleDescription), 2);
Joe Dragoae7e2c32019-07-18 15:22:25 -0700217 avifArrayCreate(&sampleTable->sampleToChunks, sizeof(avifSampleTableSampleToChunk), 16);
218 avifArrayCreate(&sampleTable->sampleSizes, sizeof(avifSampleTableSampleSize), 16);
219 avifArrayCreate(&sampleTable->timeToSamples, sizeof(avifSampleTableTimeToSample), 16);
Joe Drago759e6742019-09-26 18:07:21 -0700220 avifArrayCreate(&sampleTable->syncSamples, sizeof(avifSyncSample), 16);
Joe Dragoae7e2c32019-07-18 15:22:25 -0700221 return sampleTable;
222}
223
Joe Drago46ea0582019-07-22 15:55:47 -0700224static void avifSampleTableDestroy(avifSampleTable * sampleTable)
Joe Dragoae7e2c32019-07-18 15:22:25 -0700225{
226 avifArrayDestroy(&sampleTable->chunks);
Joe Dragoa72da5b2020-06-15 19:40:17 -0700227 for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) {
228 avifSampleDescription * description = &sampleTable->sampleDescriptions.description[i];
229 avifArrayDestroy(&description->properties);
230 }
Joe Drago2c0924c2019-09-26 17:41:01 -0700231 avifArrayDestroy(&sampleTable->sampleDescriptions);
Joe Dragoae7e2c32019-07-18 15:22:25 -0700232 avifArrayDestroy(&sampleTable->sampleToChunks);
233 avifArrayDestroy(&sampleTable->sampleSizes);
234 avifArrayDestroy(&sampleTable->timeToSamples);
Joe Drago22c1ad92019-09-26 12:46:50 -0700235 avifArrayDestroy(&sampleTable->syncSamples);
Joe Dragoae7e2c32019-07-18 15:22:25 -0700236 avifFree(sampleTable);
237}
238
Wan-Teh Chang306c3062020-04-05 12:17:33 -0700239static uint32_t avifSampleTableGetImageDelta(const avifSampleTable * sampleTable, int imageIndex)
Joe Drago46ea0582019-07-22 15:55:47 -0700240{
241 int maxSampleIndex = 0;
242 for (uint32_t i = 0; i < sampleTable->timeToSamples.count; ++i) {
Wan-Teh Chang306c3062020-04-05 12:17:33 -0700243 const avifSampleTableTimeToSample * timeToSample = &sampleTable->timeToSamples.timeToSample[i];
Joe Drago46ea0582019-07-22 15:55:47 -0700244 maxSampleIndex += timeToSample->sampleCount;
245 if ((imageIndex < maxSampleIndex) || (i == (sampleTable->timeToSamples.count - 1))) {
246 return timeToSample->sampleDelta;
247 }
248 }
249
250 // TODO: fail here?
251 return 1;
252}
253
Wan-Teh Chang306c3062020-04-05 12:17:33 -0700254static avifBool avifSampleTableHasFormat(const avifSampleTable * sampleTable, const char * format)
Joe Drago2c0924c2019-09-26 17:41:01 -0700255{
256 for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) {
257 if (!memcmp(sampleTable->sampleDescriptions.description[i].format, format, 4)) {
258 return AVIF_TRUE;
259 }
260 }
261 return AVIF_FALSE;
262}
263
Wan-Teh Chang306c3062020-04-05 12:17:33 -0700264static uint32_t avifCodecConfigurationBoxGetDepth(const avifCodecConfigurationBox * av1C)
Joe Drago6500fd62019-10-08 17:17:34 -0700265{
266 if (av1C->twelveBit) {
267 return 12;
268 } else if (av1C->highBitdepth) {
269 return 10;
270 }
271 return 8;
272}
273
Joe Dragoa72da5b2020-06-15 19:40:17 -0700274static const avifPropertyArray * avifSampleTableGetProperties(const avifSampleTable * sampleTable)
Joe Drago6500fd62019-10-08 17:17:34 -0700275{
276 for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) {
Wan-Teh Chang306c3062020-04-05 12:17:33 -0700277 const avifSampleDescription * description = &sampleTable->sampleDescriptions.description[i];
Joe Dragoa72da5b2020-06-15 19:40:17 -0700278 if (!memcmp(description->format, "av01", 4)) {
279 return &description->properties;
Joe Drago6500fd62019-10-08 17:17:34 -0700280 }
281 }
Joe Drago00bcaaf2020-06-05 15:29:38 -0700282 return NULL;
Joe Drago6500fd62019-10-08 17:17:34 -0700283}
284
Joe Dragoae7e2c32019-07-18 15:22:25 -0700285// one video track ("trak" contents)
286typedef struct avifTrack
287{
288 uint32_t id;
289 uint32_t auxForID; // if non-zero, this item is an auxC plane for Track #{auxForID}
290 uint32_t mediaTimescale;
291 uint64_t mediaDuration;
Joe Dragofc4144e2019-09-27 20:35:06 -0700292 uint32_t width;
293 uint32_t height;
Joe Dragoae7e2c32019-07-18 15:22:25 -0700294 avifSampleTable * sampleTable;
Joe Dragoa72da5b2020-06-15 19:40:17 -0700295 struct avifMeta * meta;
Joe Dragoae7e2c32019-07-18 15:22:25 -0700296} avifTrack;
297AVIF_ARRAY_DECLARE(avifTrackArray, avifTrack, track);
298
299// ---------------------------------------------------------------------------
Joe Drago46ea0582019-07-22 15:55:47 -0700300// avifCodecDecodeInput
301
Joe Drago399df4f2019-07-23 16:45:14 -0700302avifCodecDecodeInput * avifCodecDecodeInputCreate(void)
Joe Drago46ea0582019-07-22 15:55:47 -0700303{
304 avifCodecDecodeInput * decodeInput = (avifCodecDecodeInput *)avifAlloc(sizeof(avifCodecDecodeInput));
305 memset(decodeInput, 0, sizeof(avifCodecDecodeInput));
Joe Dragoe3e3bfa2020-06-02 16:33:53 -0700306 avifArrayCreate(&decodeInput->samples, sizeof(avifDecodeSample), 1);
Joe Drago46ea0582019-07-22 15:55:47 -0700307 return decodeInput;
308}
309
Joe Drago8b34ad72019-07-22 16:56:32 -0700310void avifCodecDecodeInputDestroy(avifCodecDecodeInput * decodeInput)
Joe Drago46ea0582019-07-22 15:55:47 -0700311{
312 avifArrayDestroy(&decodeInput->samples);
313 avifFree(decodeInput);
314}
315
Joe Drago345aaa12019-09-25 13:42:12 -0700316static avifBool avifCodecDecodeInputGetSamples(avifCodecDecodeInput * decodeInput, avifSampleTable * sampleTable, avifROData * rawInput)
Joe Drago46ea0582019-07-22 15:55:47 -0700317{
318 uint32_t sampleSizeIndex = 0;
319 for (uint32_t chunkIndex = 0; chunkIndex < sampleTable->chunks.count; ++chunkIndex) {
320 avifSampleTableChunk * chunk = &sampleTable->chunks.chunk[chunkIndex];
321
322 // First, figure out how many samples are in this chunk
323 uint32_t sampleCount = 0;
324 for (int sampleToChunkIndex = sampleTable->sampleToChunks.count - 1; sampleToChunkIndex >= 0; --sampleToChunkIndex) {
325 avifSampleTableSampleToChunk * sampleToChunk = &sampleTable->sampleToChunks.sampleToChunk[sampleToChunkIndex];
326 if (sampleToChunk->firstChunk <= (chunkIndex + 1)) {
327 sampleCount = sampleToChunk->samplesPerChunk;
328 break;
329 }
330 }
331 if (sampleCount == 0) {
332 // chunks with 0 samples are invalid
333 return AVIF_FALSE;
334 }
335
336 uint64_t sampleOffset = chunk->offset;
337 for (uint32_t sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex) {
Joe Drago370be3f2020-02-07 15:59:42 -0800338 uint32_t sampleSize = sampleTable->allSamplesSize;
339 if (sampleSize == 0) {
340 if (sampleSizeIndex >= sampleTable->sampleSizes.count) {
341 // We've run out of samples to sum
342 return AVIF_FALSE;
343 }
344 avifSampleTableSampleSize * sampleSizePtr = &sampleTable->sampleSizes.sampleSize[sampleSizeIndex];
345 sampleSize = sampleSizePtr->size;
Joe Drago46ea0582019-07-22 15:55:47 -0700346 }
347
Joe Dragoe3e3bfa2020-06-02 16:33:53 -0700348 avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&decodeInput->samples);
Joe Drago22c1ad92019-09-26 12:46:50 -0700349 sample->data.data = rawInput->data + sampleOffset;
Joe Drago370be3f2020-02-07 15:59:42 -0800350 sample->data.size = sampleSize;
Joe Drago22c1ad92019-09-26 12:46:50 -0700351 sample->sync = AVIF_FALSE; // to potentially be set to true following the outer loop
Joe Drago46ea0582019-07-22 15:55:47 -0700352
Joe Drago34c0d312020-04-30 15:23:03 -0700353 if ((sampleOffset + sampleSize) > (uint64_t)rawInput->size) {
354 return AVIF_FALSE;
355 }
Joe Drago46ea0582019-07-22 15:55:47 -0700356
Joe Drago370be3f2020-02-07 15:59:42 -0800357 sampleOffset += sampleSize;
Joe Drago46ea0582019-07-22 15:55:47 -0700358 ++sampleSizeIndex;
359 }
360 }
Joe Drago22c1ad92019-09-26 12:46:50 -0700361
362 // Mark appropriate samples as sync
363 for (uint32_t syncSampleIndex = 0; syncSampleIndex < sampleTable->syncSamples.count; ++syncSampleIndex) {
364 uint32_t frameIndex = sampleTable->syncSamples.syncSample[syncSampleIndex].sampleNumber - 1; // sampleNumber is 1-based
365 if (frameIndex < decodeInput->samples.count) {
366 decodeInput->samples.sample[frameIndex].sync = AVIF_TRUE;
367 }
368 }
369
370 // Assume frame 0 is sync, just in case the stss box is absent in the BMFF. (Unnecessary?)
371 if (decodeInput->samples.count > 0) {
372 decodeInput->samples.sample[0].sync = AVIF_TRUE;
373 }
Joe Drago46ea0582019-07-22 15:55:47 -0700374 return AVIF_TRUE;
375}
376
377// ---------------------------------------------------------------------------
Joe Dragoa72da5b2020-06-15 19:40:17 -0700378// Helper macros
379
380#define BEGIN_STREAM(VARNAME, PTR, SIZE) \
381 avifROStream VARNAME; \
382 avifROData VARNAME##_roData; \
383 VARNAME##_roData.data = PTR; \
384 VARNAME##_roData.size = SIZE; \
385 avifROStreamStart(&VARNAME, &VARNAME##_roData)
386
387// ---------------------------------------------------------------------------
Joe Drago800b47f2020-03-18 16:22:37 -0700388// avifDecoderData
Joe Dragoae7e2c32019-07-18 15:22:25 -0700389
Joe Drago060d5342020-03-03 10:53:49 -0800390typedef struct avifTile
391{
392 avifCodecDecodeInput * input;
393 struct avifCodec * codec;
394 avifImage * image;
395} avifTile;
396AVIF_ARRAY_DECLARE(avifTileArray, avifTile, tile);
397
Joe Dragoba1eb492020-06-22 17:05:04 -0700398// This holds one "meta" box (from the BMFF and HEIF standards) worth of relevant-to-AVIF information.
399// * If a meta box is parsed from the root level of the BMFF, it can contain the information about
400// "items" which might be color planes, alpha planes, or EXIF or XMP metadata.
401// * If a meta box is parsed from inside of a track ("trak") box, any metadata (EXIF/XMP) items inside
402// of that box are implicitly associated with that track.
Joe Drago9f2b87b2020-06-03 19:36:38 -0700403typedef struct avifMeta
Joe Drago444f0512019-01-23 17:03:24 -0800404{
Joe Dragoba1eb492020-06-22 17:05:04 -0700405 // Items (from HEIF) are the generic storage for any data that does not require timed processing
Wan-Teh Chang4b331fb2020-07-07 14:10:02 -0700406 // (single image color planes, alpha planes, EXIF, XMP, etc). Each item has a unique integer ID >1,
407 // and is defined by a series of child boxes in a meta box:
Joe Dragoba1eb492020-06-22 17:05:04 -0700408 // * iloc - location: byte offset to item data, item size in bytes
409 // * iinf - information: type of item (color planes, alpha plane, EXIF, XMP)
410 // * ipco - properties: dimensions, aspect ratio, image transformations, references to other items
411 // * ipma - associations: Attaches an item in the properties list to a given item
412 //
413 // Items are lazily created in this array when any of the above boxes refer to one by a new (unseen) ID,
414 // and are then further modified/updated as new information for an item's ID is parsed.
Joe Drago800b47f2020-03-18 16:22:37 -0700415 avifDecoderItemArray items;
Joe Dragoba1eb492020-06-22 17:05:04 -0700416
417 // Any ipco boxes explained above are populated into this array as a staging area, which are
418 // then duplicated into the appropriate items upon encountering an item property association
419 // (ipma) box.
Joe Drago05559c92019-07-17 16:33:38 -0700420 avifPropertyArray properties;
Joe Dragoba1eb492020-06-22 17:05:04 -0700421
422 // Filled with the contents of "idat" boxes, which are raw data that an item can directly refer to in its
423 // item location box (iloc) instead of just giving an offset into the overall file. If all items' iloc boxes
424 // simply point at an offset/length in the file itself, this array will likely be empty.
Joe Drago800b47f2020-03-18 16:22:37 -0700425 avifDecoderItemDataArray idats;
Joe Dragoba1eb492020-06-22 17:05:04 -0700426
427 // Ever-incrementing ID for uniquely identifying which 'meta' box contains an idat (when
Wan-Teh Chang4b331fb2020-07-07 14:10:02 -0700428 // multiple meta boxes exist as BMFF siblings). Each time avifParseMetaBox() is called on an
Joe Dragoba1eb492020-06-22 17:05:04 -0700429 // avifMeta struct, this value is incremented. Any time an additional meta box is detected at
430 // the same "level" (root level, trak level, etc), this ID helps distinguish which meta box's
Wan-Teh Chang4b331fb2020-07-07 14:10:02 -0700431 // "idat" is which, as items implicitly reference idat boxes that exist in the same meta
Joe Dragoba1eb492020-06-22 17:05:04 -0700432 // box.
433 uint32_t idatID;
434
435 // Contents of a pitm box, which signal which of the items in this file is the main image. For
436 // AVIF, this should point at an av01 type item containing color planes, and all other items
437 // are ignored unless they refer to this item in some way (alpha plane, EXIF/XMP metadata).
Joe Drago9f2b87b2020-06-03 19:36:38 -0700438 uint32_t primaryItemID;
439} avifMeta;
440
441static avifMeta * avifMetaCreate()
442{
443 avifMeta * meta = (avifMeta *)avifAlloc(sizeof(avifMeta));
444 memset(meta, 0, sizeof(avifMeta));
445 avifArrayCreate(&meta->items, sizeof(avifDecoderItem), 8);
446 avifArrayCreate(&meta->properties, sizeof(avifProperty), 16);
447 avifArrayCreate(&meta->idats, sizeof(avifDecoderItemData), 1);
448 return meta;
449}
450
451static void avifMetaDestroy(avifMeta * meta)
452{
Joe Dragoa72da5b2020-06-15 19:40:17 -0700453 for (uint32_t i = 0; i < meta->items.count; ++i) {
454 avifDecoderItem * item = &meta->items.item[i];
455 avifArrayDestroy(&item->properties);
456 }
Joe Drago9f2b87b2020-06-03 19:36:38 -0700457 avifArrayDestroy(&meta->items);
458 avifArrayDestroy(&meta->properties);
459 avifArrayDestroy(&meta->idats);
460 avifFree(meta);
461}
462
463static avifDecoderItem * avifMetaFindItem(avifMeta * meta, uint32_t itemID)
464{
465 if (itemID == 0) {
466 return NULL;
467 }
468
469 for (uint32_t i = 0; i < meta->items.count; ++i) {
470 if (meta->items.item[i].id == itemID) {
471 return &meta->items.item[i];
472 }
473 }
474
475 avifDecoderItem * item = (avifDecoderItem *)avifArrayPushPtr(&meta->items);
Joe Dragoa72da5b2020-06-15 19:40:17 -0700476 avifArrayCreate(&item->properties, sizeof(avifProperty), 16);
Joe Drago9f2b87b2020-06-03 19:36:38 -0700477 item->id = itemID;
478 item->meta = meta;
479 return item;
480}
481
482typedef struct avifDecoderData
483{
484 avifFileType ftyp;
Joe Dragoba1eb492020-06-22 17:05:04 -0700485 avifMeta * meta; // The root-level meta box
Joe Dragoae7e2c32019-07-18 15:22:25 -0700486 avifTrackArray tracks;
Joe Drago345aaa12019-09-25 13:42:12 -0700487 avifROData rawInput;
Joe Drago060d5342020-03-03 10:53:49 -0800488 avifTileArray tiles;
489 unsigned int colorTileCount;
490 unsigned int alphaTileCount;
491 avifImageGrid colorGrid;
492 avifImageGrid alphaGrid;
Joe Drago46ea0582019-07-22 15:55:47 -0700493 avifDecoderSource source;
Wan-Teh Chang306c3062020-04-05 12:17:33 -0700494 const avifSampleTable * sourceSampleTable; // NULL unless (source == AVIF_DECODER_SOURCE_TRACKS), owned by an avifTrack
Joe Drago9f2b87b2020-06-03 19:36:38 -0700495 avifBool cicpSet; // True if avifDecoder's image has had its CICP set correctly yet.
496 // This allows nclx colr boxes to override AV1 CICP, as specified in the MIAF
497 // standard (ISO/IEC 23000-22:2019), section 7.3.6.4:
498 //
499 // "The colour information property takes precedence over any colour information in the image
500 // bitstream, i.e. if the property is present, colour information in the bitstream shall be ignored."
Joe Drago800b47f2020-03-18 16:22:37 -0700501} avifDecoderData;
Joe Drago444f0512019-01-23 17:03:24 -0800502
Joe Drago800b47f2020-03-18 16:22:37 -0700503static avifDecoderData * avifDecoderDataCreate()
Joe Drago05559c92019-07-17 16:33:38 -0700504{
Joe Drago800b47f2020-03-18 16:22:37 -0700505 avifDecoderData * data = (avifDecoderData *)avifAlloc(sizeof(avifDecoderData));
506 memset(data, 0, sizeof(avifDecoderData));
Joe Drago9f2b87b2020-06-03 19:36:38 -0700507 data->meta = avifMetaCreate();
Joe Dragoae7e2c32019-07-18 15:22:25 -0700508 avifArrayCreate(&data->tracks, sizeof(avifTrack), 2);
Joe Drago060d5342020-03-03 10:53:49 -0800509 avifArrayCreate(&data->tiles, sizeof(avifTile), 8);
Joe Drago05559c92019-07-17 16:33:38 -0700510 return data;
511}
512
Joe Drago800b47f2020-03-18 16:22:37 -0700513static void avifDecoderDataResetCodec(avifDecoderData * data)
Joe Drago05559c92019-07-17 16:33:38 -0700514{
Joe Drago060d5342020-03-03 10:53:49 -0800515 for (unsigned int i = 0; i < data->tiles.count; ++i) {
516 avifTile * tile = &data->tiles.tile[i];
Joe Drago9d368782020-03-04 17:53:17 -0800517 if (tile->image) {
518 avifImageFreePlanes(tile->image, AVIF_PLANES_ALL); // forget any pointers into codec image buffers
519 }
Joe Drago060d5342020-03-03 10:53:49 -0800520 if (tile->codec) {
521 avifCodecDestroy(tile->codec);
522 tile->codec = NULL;
Joe Drago46ea0582019-07-22 15:55:47 -0700523 }
524 }
525}
526
Joe Drago800b47f2020-03-18 16:22:37 -0700527static avifTile * avifDecoderDataCreateTile(avifDecoderData * data)
Joe Drago060d5342020-03-03 10:53:49 -0800528{
529 avifTile * tile = (avifTile *)avifArrayPushPtr(&data->tiles);
530 tile->image = avifImageCreateEmpty();
531 tile->input = avifCodecDecodeInputCreate();
532 return tile;
533}
534
Joe Dragoa72da5b2020-06-15 19:40:17 -0700535static avifTrack * avifDecoderDataCreateTrack(avifDecoderData * data)
536{
537 avifTrack * track = (avifTrack *)avifArrayPushPtr(&data->tracks);
538 track->meta = avifMetaCreate();
539 return track;
540}
541
Joe Drago800b47f2020-03-18 16:22:37 -0700542static void avifDecoderDataClearTiles(avifDecoderData * data)
Joe Drago060d5342020-03-03 10:53:49 -0800543{
544 for (unsigned int i = 0; i < data->tiles.count; ++i) {
545 avifTile * tile = &data->tiles.tile[i];
546 if (tile->input) {
547 avifCodecDecodeInputDestroy(tile->input);
548 tile->input = NULL;
549 }
550 if (tile->codec) {
551 avifCodecDestroy(tile->codec);
552 tile->codec = NULL;
553 }
554 if (tile->image) {
555 avifImageDestroy(tile->image);
556 tile->image = NULL;
557 }
558 }
559 data->tiles.count = 0;
560 data->colorTileCount = 0;
561 data->alphaTileCount = 0;
562}
563
Joe Drago800b47f2020-03-18 16:22:37 -0700564static void avifDecoderDataDestroy(avifDecoderData * data)
Joe Drago46ea0582019-07-22 15:55:47 -0700565{
Joe Drago9f2b87b2020-06-03 19:36:38 -0700566 avifMetaDestroy(data->meta);
Joe Dragoae7e2c32019-07-18 15:22:25 -0700567 for (uint32_t i = 0; i < data->tracks.count; ++i) {
Joe Dragoa72da5b2020-06-15 19:40:17 -0700568 avifTrack * track = &data->tracks.track[i];
569 if (track->sampleTable) {
570 avifSampleTableDestroy(track->sampleTable);
571 }
572 if (track->meta) {
573 avifMetaDestroy(track->meta);
Joe Dragoae7e2c32019-07-18 15:22:25 -0700574 }
575 }
576 avifArrayDestroy(&data->tracks);
Joe Drago800b47f2020-03-18 16:22:37 -0700577 avifDecoderDataClearTiles(data);
Joe Drago060d5342020-03-03 10:53:49 -0800578 avifArrayDestroy(&data->tiles);
Joe Drago05559c92019-07-17 16:33:38 -0700579 avifFree(data);
580}
581
Joe Drago800b47f2020-03-18 16:22:37 -0700582static const uint8_t * avifDecoderDataCalcItemPtr(avifDecoderData * data, avifDecoderItem * item)
Joe Dragof6a42272019-11-21 15:21:41 -0800583{
584 avifROData * offsetBuffer = NULL;
585 if (item->idatID == 0) {
586 // construction_method: file(0)
587
588 offsetBuffer = &data->rawInput;
589 } else {
590 // construction_method: idat(1)
591
592 // Find associated idat block
Joe Drago9f2b87b2020-06-03 19:36:38 -0700593 for (uint32_t i = 0; i < item->meta->idats.count; ++i) {
594 if (item->meta->idats.idat[i].id == item->idatID) {
595 offsetBuffer = &item->meta->idats.idat[i].data;
Joe Dragof6a42272019-11-21 15:21:41 -0800596 break;
597 }
598 }
599
600 if (offsetBuffer == NULL) {
601 // no idat box was found in this meta box, bail out
602 return NULL;
603 }
604 }
605
606 if (item->offset > offsetBuffer->size) {
607 return NULL;
608 }
609 uint64_t offsetSize = (uint64_t)item->offset + (uint64_t)item->size;
610 if (offsetSize > (uint64_t)offsetBuffer->size) {
611 return NULL;
612 }
613 return offsetBuffer->data + item->offset;
614}
615
Joe Drago800b47f2020-03-18 16:22:37 -0700616static avifBool avifDecoderDataGenerateImageGridTiles(avifDecoderData * data, avifImageGrid * grid, avifDecoderItem * gridItem, avifBool alpha)
Joe Drago060d5342020-03-03 10:53:49 -0800617{
618 unsigned int tilesRequested = (unsigned int)grid->rows * (unsigned int)grid->columns;
619
620 // Count number of dimg for this item, bail out if it doesn't match perfectly
621 unsigned int tilesAvailable = 0;
Joe Drago9f2b87b2020-06-03 19:36:38 -0700622 for (uint32_t i = 0; i < gridItem->meta->items.count; ++i) {
623 avifDecoderItem * item = &gridItem->meta->items.item[i];
Joe Drago060d5342020-03-03 10:53:49 -0800624 if (item->dimgForID == gridItem->id) {
625 if (memcmp(item->type, "av01", 4)) {
626 continue;
627 }
Joe Drago3320e5f2020-04-21 17:36:27 -0700628 if (item->hasUnsupportedEssentialProperty) {
Wan-Teh Chang29aaade2020-08-10 16:14:16 -0700629 // An essential property isn't supported by libavif; can't
630 // decode a grid image if any tile in the grid isn't supported.
631 return AVIF_FALSE;
Joe Drago3320e5f2020-04-21 17:36:27 -0700632 }
Joe Drago060d5342020-03-03 10:53:49 -0800633
634 ++tilesAvailable;
635 }
636 }
637
638 if (tilesRequested != tilesAvailable) {
639 return AVIF_FALSE;
640 }
641
Joe Drago9c5f5652020-06-29 15:13:44 -0700642 avifBool firstTile = AVIF_TRUE;
Joe Drago9f2b87b2020-06-03 19:36:38 -0700643 for (uint32_t i = 0; i < gridItem->meta->items.count; ++i) {
644 avifDecoderItem * item = &gridItem->meta->items.item[i];
Joe Drago060d5342020-03-03 10:53:49 -0800645 if (item->dimgForID == gridItem->id) {
646 if (memcmp(item->type, "av01", 4)) {
647 continue;
648 }
649
Joe Drago800b47f2020-03-18 16:22:37 -0700650 avifTile * tile = avifDecoderDataCreateTile(data);
Joe Dragoe3e3bfa2020-06-02 16:33:53 -0700651 avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&tile->input->samples);
Joe Drago800b47f2020-03-18 16:22:37 -0700652 sample->data.data = avifDecoderDataCalcItemPtr(data, item);
Joe Drago060d5342020-03-03 10:53:49 -0800653 sample->data.size = item->size;
654 sample->sync = AVIF_TRUE;
655 tile->input->alpha = alpha;
Joe Drago9c5f5652020-06-29 15:13:44 -0700656
657 if (firstTile) {
658 firstTile = AVIF_FALSE;
659
660 // Adopt the av1C property of the first av01 tile, so that it can be queried from
661 // the top-level color/alpha item during avifDecoderReset().
662 const avifProperty * srcProp = avifPropertyArrayFind(&item->properties, "av1C");
663 if (!srcProp) {
664 return AVIF_FALSE;
665 }
666 avifProperty * dstProp = (avifProperty *)avifArrayPushPtr(&gridItem->properties);
667 memcpy(dstProp, srcProp, sizeof(avifProperty));
668 }
Joe Drago060d5342020-03-03 10:53:49 -0800669 }
670 }
671 return AVIF_TRUE;
672}
673
Joe Drago800b47f2020-03-18 16:22:37 -0700674static avifBool avifDecoderDataFillImageGrid(avifDecoderData * data,
675 avifImageGrid * grid,
676 avifImage * dstImage,
677 unsigned int firstTileIndex,
678 unsigned int tileCount,
679 avifBool alpha)
Joe Drago060d5342020-03-03 10:53:49 -0800680{
681 if (tileCount == 0) {
682 return AVIF_FALSE;
683 }
684
685 avifTile * firstTile = &data->tiles.tile[firstTileIndex];
Joe Dragoa0da4a42020-05-08 14:27:40 -0700686 avifBool firstTileUVPresent = (firstTile->image->yuvPlanes[AVIF_CHAN_U] && firstTile->image->yuvPlanes[AVIF_CHAN_V]);
psi / Ryo Hirafuji922d8a12020-03-10 03:24:57 +0900687
Joe Dragoa0da4a42020-05-08 14:27:40 -0700688 // Check for tile consistency: All tiles in a grid image should match in the properties checked below.
Joe Drago060d5342020-03-03 10:53:49 -0800689 for (unsigned int i = 1; i < tileCount; ++i) {
690 avifTile * tile = &data->tiles.tile[firstTileIndex + i];
Joe Drago951a0022020-03-09 16:19:44 -0700691 avifBool uvPresent = (tile->image->yuvPlanes[AVIF_CHAN_U] && tile->image->yuvPlanes[AVIF_CHAN_V]);
Joe Dragoa0da4a42020-05-08 14:27:40 -0700692 if ((tile->image->width != firstTile->image->width) || (tile->image->height != firstTile->image->height) ||
693 (tile->image->depth != firstTile->image->depth) || (tile->image->yuvFormat != firstTile->image->yuvFormat) ||
694 (tile->image->yuvRange != firstTile->image->yuvRange) || (uvPresent != firstTileUVPresent) ||
695 ((tile->image->colorPrimaries != firstTile->image->colorPrimaries) ||
696 (tile->image->transferCharacteristics != firstTile->image->transferCharacteristics) ||
697 (tile->image->matrixCoefficients != firstTile->image->matrixCoefficients))) {
Joe Drago060d5342020-03-03 10:53:49 -0800698 return AVIF_FALSE;
699 }
700 }
701
Joe Dragoa0da4a42020-05-08 14:27:40 -0700702 // Lazily populate dstImage with the new frame's properties. If we're decoding alpha,
703 // these values must already match.
704 if ((dstImage->width != grid->outputWidth) || (dstImage->height != grid->outputHeight) ||
705 (dstImage->depth != firstTile->image->depth) || (dstImage->yuvFormat != firstTile->image->yuvFormat)) {
Joe Drago060d5342020-03-03 10:53:49 -0800706 if (alpha) {
707 // Alpha doesn't match size, just bail out
708 return AVIF_FALSE;
709 }
710
711 avifImageFreePlanes(dstImage, AVIF_PLANES_ALL);
712 dstImage->width = grid->outputWidth;
713 dstImage->height = grid->outputHeight;
Joe Dragoa0da4a42020-05-08 14:27:40 -0700714 dstImage->depth = firstTile->image->depth;
715 dstImage->yuvFormat = firstTile->image->yuvFormat;
716 dstImage->yuvRange = firstTile->image->yuvRange;
717 if (!data->cicpSet) {
718 data->cicpSet = AVIF_TRUE;
719 dstImage->colorPrimaries = firstTile->image->colorPrimaries;
720 dstImage->transferCharacteristics = firstTile->image->transferCharacteristics;
721 dstImage->matrixCoefficients = firstTile->image->matrixCoefficients;
psi / Ryo Hirafuji922d8a12020-03-10 03:24:57 +0900722 }
Joe Drago060d5342020-03-03 10:53:49 -0800723 }
Joe Dragod0eeb182020-05-18 17:23:48 -0700724 if (alpha) {
725 dstImage->alphaRange = firstTile->image->alphaRange;
726 }
Joe Drago060d5342020-03-03 10:53:49 -0800727
728 avifImageAllocatePlanes(dstImage, alpha ? AVIF_PLANES_A : AVIF_PLANES_YUV);
729
730 avifPixelFormatInfo formatInfo;
Joe Dragoa0da4a42020-05-08 14:27:40 -0700731 avifGetPixelFormatInfo(firstTile->image->yuvFormat, &formatInfo);
Joe Drago060d5342020-03-03 10:53:49 -0800732
733 unsigned int tileIndex = firstTileIndex;
734 size_t pixelBytes = avifImageUsesU16(dstImage) ? 2 : 1;
735 for (unsigned int rowIndex = 0; rowIndex < grid->rows; ++rowIndex) {
736 for (unsigned int colIndex = 0; colIndex < grid->columns; ++colIndex, ++tileIndex) {
737 avifTile * tile = &data->tiles.tile[tileIndex];
738
Joe Dragoa0da4a42020-05-08 14:27:40 -0700739 unsigned int widthToCopy = firstTile->image->width;
740 unsigned int maxX = firstTile->image->width * (colIndex + 1);
Joe Drago060d5342020-03-03 10:53:49 -0800741 if (maxX > grid->outputWidth) {
742 widthToCopy -= maxX - grid->outputWidth;
743 }
744
Joe Dragoa0da4a42020-05-08 14:27:40 -0700745 unsigned int heightToCopy = firstTile->image->height;
746 unsigned int maxY = firstTile->image->height * (rowIndex + 1);
Joe Drago060d5342020-03-03 10:53:49 -0800747 if (maxY > grid->outputHeight) {
748 heightToCopy -= maxY - grid->outputHeight;
749 }
750
751 // Y and A channels
Joe Dragoa0da4a42020-05-08 14:27:40 -0700752 size_t yaColOffset = colIndex * firstTile->image->width;
753 size_t yaRowOffset = rowIndex * firstTile->image->height;
Joe Drago060d5342020-03-03 10:53:49 -0800754 size_t yaRowBytes = widthToCopy * pixelBytes;
755
756 if (alpha) {
757 // A
758 for (unsigned int j = 0; j < heightToCopy; ++j) {
759 uint8_t * src = &tile->image->alphaPlane[j * tile->image->alphaRowBytes];
760 uint8_t * dst = &dstImage->alphaPlane[(yaColOffset * pixelBytes) + ((yaRowOffset + j) * dstImage->alphaRowBytes)];
761 memcpy(dst, src, yaRowBytes);
762 }
763 } else {
764 // Y
765 for (unsigned int j = 0; j < heightToCopy; ++j) {
766 uint8_t * src = &tile->image->yuvPlanes[AVIF_CHAN_Y][j * tile->image->yuvRowBytes[AVIF_CHAN_Y]];
767 uint8_t * dst =
768 &dstImage->yuvPlanes[AVIF_CHAN_Y][(yaColOffset * pixelBytes) + ((yaRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_Y])];
769 memcpy(dst, src, yaRowBytes);
770 }
771
Joe Dragoa0da4a42020-05-08 14:27:40 -0700772 if (!firstTileUVPresent) {
Joe Drago060d5342020-03-03 10:53:49 -0800773 continue;
774 }
775
776 // UV
Joe Drago060d5342020-03-03 10:53:49 -0800777 heightToCopy >>= formatInfo.chromaShiftY;
778 size_t uvColOffset = yaColOffset >> formatInfo.chromaShiftX;
779 size_t uvRowOffset = yaRowOffset >> formatInfo.chromaShiftY;
780 size_t uvRowBytes = yaRowBytes >> formatInfo.chromaShiftX;
781 for (unsigned int j = 0; j < heightToCopy; ++j) {
782 uint8_t * srcU = &tile->image->yuvPlanes[AVIF_CHAN_U][j * tile->image->yuvRowBytes[AVIF_CHAN_U]];
783 uint8_t * dstU =
784 &dstImage->yuvPlanes[AVIF_CHAN_U][(uvColOffset * pixelBytes) + ((uvRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_U])];
785 memcpy(dstU, srcU, uvRowBytes);
786
787 uint8_t * srcV = &tile->image->yuvPlanes[AVIF_CHAN_V][j * tile->image->yuvRowBytes[AVIF_CHAN_V]];
788 uint8_t * dstV =
789 &dstImage->yuvPlanes[AVIF_CHAN_V][(uvColOffset * pixelBytes) + ((uvRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_V])];
790 memcpy(dstV, srcV, uvRowBytes);
791 }
792 }
793 }
794 }
795
796 return AVIF_TRUE;
797}
798
Joe Drago39267fd2020-06-19 15:21:14 -0700799// If colorId == 0 (a sentinel value as item IDs must be nonzero), accept any found EXIF/XMP metadata. Passing in 0
800// 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 -0700801// inside of a trak box are implicitly associated to the track.
Joe Drago85050a32020-06-15 20:05:37 -0700802static avifBool avifDecoderDataFindMetadata(avifDecoderData * data, avifMeta * meta, avifImage * image, uint32_t colorId)
Joe Dragoa72da5b2020-06-15 19:40:17 -0700803{
804 avifROData exifData = AVIF_DATA_EMPTY;
805 avifROData xmpData = AVIF_DATA_EMPTY;
806
807 for (uint32_t itemIndex = 0; itemIndex < meta->items.count; ++itemIndex) {
808 avifDecoderItem * item = &meta->items.item[itemIndex];
Joe Dragoba1eb492020-06-22 17:05:04 -0700809 if (!item->size) {
810 continue;
Joe Dragoa72da5b2020-06-15 19:40:17 -0700811 }
812 if (item->hasUnsupportedEssentialProperty) {
813 // An essential property isn't supported by libavif; ignore the item.
814 continue;
815 }
816
817 if ((colorId > 0) && (item->descForID != colorId)) {
818 // Not a content description (metadata) for the colorOBU, skip it
819 continue;
820 }
821
822 if (!memcmp(item->type, "Exif", 4)) {
823 // Advance past Annex A.2.1's header
824 const uint8_t * boxPtr = avifDecoderDataCalcItemPtr(data, item);
825 BEGIN_STREAM(exifBoxStream, boxPtr, item->size);
826 uint32_t exifTiffHeaderOffset;
827 CHECK(avifROStreamReadU32(&exifBoxStream, &exifTiffHeaderOffset)); // unsigned int(32) exif_tiff_header_offset;
828
829 exifData.data = avifROStreamCurrent(&exifBoxStream);
830 exifData.size = avifROStreamRemainingBytes(&exifBoxStream);
Joe Dragoba1eb492020-06-22 17:05:04 -0700831 } else if (!memcmp(item->type, "mime", 4) && !memcmp(item->contentType.contentType, xmpContentType, xmpContentTypeSize)) {
Joe Dragoa72da5b2020-06-15 19:40:17 -0700832 xmpData.data = avifDecoderDataCalcItemPtr(data, item);
833 xmpData.size = item->size;
834 }
835 }
836
837 if (exifData.data && exifData.size) {
838 avifImageSetMetadataExif(image, exifData.data, exifData.size);
839 }
840 if (xmpData.data && xmpData.size) {
841 avifImageSetMetadataXMP(image, xmpData.data, xmpData.size);
842 }
843 return AVIF_TRUE;
844}
845
Joe Drago8f7a3002019-02-07 19:35:37 -0800846// ---------------------------------------------------------------------------
Joe Dragocd1e4c32019-02-08 11:26:31 -0800847// URN
848
Joe Dragoa72da5b2020-06-15 19:40:17 -0700849static avifBool isAlphaURN(const char * urn)
Joe Dragocd1e4c32019-02-08 11:26:31 -0800850{
wantehchang3fde0d02020-03-10 23:58:32 -0700851 return !strcmp(urn, URN_ALPHA0) || !strcmp(urn, URN_ALPHA1);
Joe Dragocd1e4c32019-02-08 11:26:31 -0800852}
853
854// ---------------------------------------------------------------------------
Joe Drago8f7a3002019-02-07 19:35:37 -0800855// BMFF Parsing
856
Joe Drago9f2b87b2020-06-03 19:36:38 -0700857static avifBool avifParseItemLocationBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago444f0512019-01-23 17:03:24 -0800858{
Joe Drago8f7a3002019-02-07 19:35:37 -0800859 BEGIN_STREAM(s, raw, rawLen);
Joe Drago444f0512019-01-23 17:03:24 -0800860
Joe Dragof6a42272019-11-21 15:21:41 -0800861 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -0700862 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Dragof6a42272019-11-21 15:21:41 -0800863 if (version > 2) {
864 return AVIF_FALSE;
865 }
Joe Drago444f0512019-01-23 17:03:24 -0800866
Joe Drago8f7a3002019-02-07 19:35:37 -0800867 uint8_t offsetSizeAndLengthSize;
Joe Drago345aaa12019-09-25 13:42:12 -0700868 CHECK(avifROStreamRead(&s, &offsetSizeAndLengthSize, 1));
Joe Drago8f7a3002019-02-07 19:35:37 -0800869 uint8_t offsetSize = (offsetSizeAndLengthSize >> 4) & 0xf; // unsigned int(4) offset_size;
870 uint8_t lengthSize = (offsetSizeAndLengthSize >> 0) & 0xf; // unsigned int(4) length_size;
Joe Drago444f0512019-01-23 17:03:24 -0800871
Joe Dragof6a42272019-11-21 15:21:41 -0800872 uint8_t baseOffsetSizeAndIndexSize;
873 CHECK(avifROStreamRead(&s, &baseOffsetSizeAndIndexSize, 1));
874 uint8_t baseOffsetSize = (baseOffsetSizeAndIndexSize >> 4) & 0xf; // unsigned int(4) base_offset_size;
875 uint8_t indexSize = 0;
876 if ((version == 1) || (version == 2)) {
877 indexSize = baseOffsetSizeAndIndexSize & 0xf; // unsigned int(4) index_size;
878 if (indexSize != 0) {
879 // extent_index unsupported
880 return AVIF_FALSE;
881 }
882 }
Joe Drago444f0512019-01-23 17:03:24 -0800883
Joe Dragof6a42272019-11-21 15:21:41 -0800884 uint16_t tmp16;
885 uint32_t itemCount;
886 if (version < 2) {
887 CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_count;
888 itemCount = tmp16;
889 } else {
890 CHECK(avifROStreamReadU32(&s, &itemCount)); // unsigned int(32) item_count;
891 }
892 for (uint32_t i = 0; i < itemCount; ++i) {
893 uint32_t itemID;
894 uint32_t idatID = 0;
895 if (version < 2) {
896 CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID;
897 itemID = tmp16;
898 } else {
899 CHECK(avifROStreamReadU32(&s, &itemID)); // unsigned int(32) item_ID;
900 }
901
902 if ((version == 1) || (version == 2)) {
903 uint8_t ignored;
904 uint8_t constructionMethod;
905 CHECK(avifROStreamRead(&s, &ignored, 1)); // unsigned int(12) reserved = 0;
906 CHECK(avifROStreamRead(&s, &constructionMethod, 1)); // unsigned int(4) construction_method;
907 constructionMethod = constructionMethod & 0xf;
908 if ((constructionMethod != 0 /* file */) && (constructionMethod != 1 /* idat */)) {
909 // construction method item(2) unsupported
910 return AVIF_FALSE;
911 }
912 if (constructionMethod == 1) {
Joe Dragoba1eb492020-06-22 17:05:04 -0700913 idatID = meta->idatID;
Joe Dragof6a42272019-11-21 15:21:41 -0800914 }
915 }
916
Joe Drago345aaa12019-09-25 13:42:12 -0700917 uint16_t dataReferenceIndex; // unsigned int(16) data_ref rence_index;
918 CHECK(avifROStreamReadU16(&s, &dataReferenceIndex)); //
919 uint64_t baseOffset; // unsigned int(base_offset_size*8) base_offset;
920 CHECK(avifROStreamReadUX8(&s, &baseOffset, baseOffsetSize)); //
921 uint16_t extentCount; // unsigned int(16) extent_count;
922 CHECK(avifROStreamReadU16(&s, &extentCount)); //
Joe Drago8f7a3002019-02-07 19:35:37 -0800923 if (extentCount == 1) {
Joe Dragof6a42272019-11-21 15:21:41 -0800924 // If extent_index is ever supported, this spec must be implemented here:
925 // :: if (((version == 1) || (version == 2)) && (index_size > 0)) {
926 // :: unsigned int(index_size*8) extent_index;
927 // :: }
928
Joe Drago8f7a3002019-02-07 19:35:37 -0800929 uint64_t extentOffset; // unsigned int(offset_size*8) extent_offset;
Joe Drago345aaa12019-09-25 13:42:12 -0700930 CHECK(avifROStreamReadUX8(&s, &extentOffset, offsetSize));
Joe Drago8f7a3002019-02-07 19:35:37 -0800931 uint64_t extentLength; // unsigned int(offset_size*8) extent_length;
Joe Drago345aaa12019-09-25 13:42:12 -0700932 CHECK(avifROStreamReadUX8(&s, &extentLength, lengthSize));
Joe Drago444f0512019-01-23 17:03:24 -0800933
Joe Drago9f2b87b2020-06-03 19:36:38 -0700934 avifDecoderItem * item = avifMetaFindItem(meta, itemID);
Joe Drago46ea0582019-07-22 15:55:47 -0700935 if (!item) {
936 return AVIF_FALSE;
937 }
Joe Drago05559c92019-07-17 16:33:38 -0700938 item->id = itemID;
939 item->offset = (uint32_t)(baseOffset + extentOffset);
940 item->size = (uint32_t)extentLength;
Joe Dragof6a42272019-11-21 15:21:41 -0800941 item->idatID = idatID;
Joe Drago444f0512019-01-23 17:03:24 -0800942 } else {
Joe Drago8f7a3002019-02-07 19:35:37 -0800943 // TODO: support more than one extent
944 return AVIF_FALSE;
Joe Drago444f0512019-01-23 17:03:24 -0800945 }
946 }
947 return AVIF_TRUE;
948}
949
Joe Drago060d5342020-03-03 10:53:49 -0800950static avifBool avifParseImageGridBox(avifImageGrid * grid, const uint8_t * raw, size_t rawLen)
951{
952 BEGIN_STREAM(s, raw, rawLen);
953
954 uint8_t version, flags;
955 CHECK(avifROStreamRead(&s, &version, 1)); // unsigned int(8) version = 0;
956 if (version != 0) {
957 return AVIF_FALSE;
958 }
959 CHECK(avifROStreamRead(&s, &flags, 1)); // unsigned int(8) flags;
960 CHECK(avifROStreamRead(&s, &grid->rows, 1)); // unsigned int(8) rows_minus_one;
961 CHECK(avifROStreamRead(&s, &grid->columns, 1)); // unsigned int(8) columns_minus_one;
962 ++grid->rows;
963 ++grid->columns;
964
965 uint32_t fieldLength = ((flags & 1) + 1) * 16;
966 if (fieldLength == 16) {
967 uint16_t outputWidth16, outputHeight16;
968 CHECK(avifROStreamReadU16(&s, &outputWidth16)); // unsigned int(FieldLength) output_width;
969 CHECK(avifROStreamReadU16(&s, &outputHeight16)); // unsigned int(FieldLength) output_height;
970 grid->outputWidth = outputWidth16;
971 grid->outputHeight = outputHeight16;
972 } else {
973 if (fieldLength != 32) {
974 // This should be impossible
975 return AVIF_FALSE;
976 }
977 CHECK(avifROStreamReadU32(&s, &grid->outputWidth)); // unsigned int(FieldLength) output_width;
978 CHECK(avifROStreamReadU32(&s, &grid->outputHeight)); // unsigned int(FieldLength) output_height;
979 }
Wan-Teh Chang0a8e7242020-08-10 13:24:59 -0700980 if (grid->outputWidth > AVIF_MAX_IMAGE_SIZE / grid->outputHeight) {
981 return AVIF_FALSE;
982 }
Joe Drago060d5342020-03-03 10:53:49 -0800983 return AVIF_TRUE;
984}
985
Wan-Teh Chang2031dc12020-05-22 09:24:46 -0700986static avifBool avifParseImageSpatialExtentsProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -0800987{
988 BEGIN_STREAM(s, raw, rawLen);
Joe Drago345aaa12019-09-25 13:42:12 -0700989 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Drago8f7a3002019-02-07 19:35:37 -0800990
Wan-Teh Chang2031dc12020-05-22 09:24:46 -0700991 avifImageSpatialExtents * ispe = &prop->u.ispe;
992 CHECK(avifROStreamReadU32(&s, &ispe->width));
993 CHECK(avifROStreamReadU32(&s, &ispe->height));
Joe Drago8f7a3002019-02-07 19:35:37 -0800994 return AVIF_TRUE;
995}
996
Wan-Teh Chang2031dc12020-05-22 09:24:46 -0700997static avifBool avifParseAuxiliaryTypeProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Dragocd1e4c32019-02-08 11:26:31 -0800998{
999 BEGIN_STREAM(s, raw, rawLen);
Joe Drago345aaa12019-09-25 13:42:12 -07001000 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragocd1e4c32019-02-08 11:26:31 -08001001
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001002 CHECK(avifROStreamReadString(&s, prop->u.auxC.auxType, AUXTYPE_SIZE));
Joe Dragocd1e4c32019-02-08 11:26:31 -08001003 return AVIF_TRUE;
1004}
1005
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001006static avifBool avifParseColourInformationBox(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago41eb62b2019-02-08 15:38:18 -08001007{
1008 BEGIN_STREAM(s, raw, rawLen);
Joe Dragoe7ce20d2019-02-11 16:37:38 -08001009
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001010 avifColourInformationBox * colr = &prop->u.colr;
1011 colr->hasICC = AVIF_FALSE;
1012 colr->hasNCLX = AVIF_FALSE;
Joe Dragoe7ce20d2019-02-11 16:37:38 -08001013
Joe Dragoa0da4a42020-05-08 14:27:40 -07001014 uint8_t colorType[4]; // unsigned int(32) colour_type;
1015 CHECK(avifROStreamRead(&s, colorType, 4));
1016 if (!memcmp(colorType, "rICC", 4) || !memcmp(colorType, "prof", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001017 colr->hasICC = AVIF_TRUE;
1018 colr->icc = avifROStreamCurrent(&s);
1019 colr->iccSize = avifROStreamRemainingBytes(&s);
Joe Dragoa0da4a42020-05-08 14:27:40 -07001020 } else if (!memcmp(colorType, "nclx", 4)) {
Joe Drago74cd1c92020-04-16 12:17:11 -07001021 uint16_t tmp16;
Joe Drago97b071c2019-07-17 14:24:56 -07001022 // unsigned int(16) colour_primaries;
Joe Drago74cd1c92020-04-16 12:17:11 -07001023 CHECK(avifROStreamReadU16(&s, &tmp16));
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001024 colr->colorPrimaries = (avifColorPrimaries)tmp16;
Joe Drago97b071c2019-07-17 14:24:56 -07001025 // unsigned int(16) transfer_characteristics;
Joe Drago74cd1c92020-04-16 12:17:11 -07001026 CHECK(avifROStreamReadU16(&s, &tmp16));
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001027 colr->transferCharacteristics = (avifTransferCharacteristics)tmp16;
Joe Drago97b071c2019-07-17 14:24:56 -07001028 // unsigned int(16) matrix_coefficients;
Joe Drago74cd1c92020-04-16 12:17:11 -07001029 CHECK(avifROStreamReadU16(&s, &tmp16));
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001030 colr->matrixCoefficients = (avifMatrixCoefficients)tmp16;
Joe Drago97b071c2019-07-17 14:24:56 -07001031 // unsigned int(1) full_range_flag;
1032 // unsigned int(7) reserved = 0;
Joe Drago74cd1c92020-04-16 12:17:11 -07001033 uint8_t tmp8;
1034 CHECK(avifROStreamRead(&s, &tmp8, 1));
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001035 colr->range = (tmp8 & 0x80) ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED;
1036 colr->hasNCLX = AVIF_TRUE;
Joe Drago41eb62b2019-02-08 15:38:18 -08001037 }
1038 return AVIF_TRUE;
1039}
1040
Joe Drago6500fd62019-10-08 17:17:34 -07001041static avifBool avifParseAV1CodecConfigurationBox(const uint8_t * raw, size_t rawLen, avifCodecConfigurationBox * av1C)
1042{
1043 BEGIN_STREAM(s, raw, rawLen);
1044
1045 uint8_t markerAndVersion = 0;
1046 CHECK(avifROStreamRead(&s, &markerAndVersion, 1));
1047 uint8_t seqProfileAndIndex = 0;
1048 CHECK(avifROStreamRead(&s, &seqProfileAndIndex, 1));
1049 uint8_t rawFlags = 0;
1050 CHECK(avifROStreamRead(&s, &rawFlags, 1));
1051
1052 if (markerAndVersion != 0x81) {
1053 // Marker and version must both == 1
1054 return AVIF_FALSE;
1055 }
1056
1057 av1C->seqProfile = (seqProfileAndIndex >> 5) & 0x7; // unsigned int (3) seq_profile;
1058 av1C->seqLevelIdx0 = (seqProfileAndIndex >> 0) & 0x1f; // unsigned int (5) seq_level_idx_0;
1059 av1C->seqTier0 = (rawFlags >> 7) & 0x1; // unsigned int (1) seq_tier_0;
1060 av1C->highBitdepth = (rawFlags >> 6) & 0x1; // unsigned int (1) high_bitdepth;
1061 av1C->twelveBit = (rawFlags >> 5) & 0x1; // unsigned int (1) twelve_bit;
1062 av1C->monochrome = (rawFlags >> 4) & 0x1; // unsigned int (1) monochrome;
1063 av1C->chromaSubsamplingX = (rawFlags >> 3) & 0x1; // unsigned int (1) chroma_subsampling_x;
1064 av1C->chromaSubsamplingY = (rawFlags >> 2) & 0x1; // unsigned int (1) chroma_subsampling_y;
1065 av1C->chromaSamplePosition = (rawFlags >> 0) & 0x3; // unsigned int (2) chroma_sample_position;
1066 return AVIF_TRUE;
1067}
1068
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001069static avifBool avifParseAV1CodecConfigurationBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago6500fd62019-10-08 17:17:34 -07001070{
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001071 return avifParseAV1CodecConfigurationBox(raw, rawLen, &prop->u.av1C);
Joe Drago6500fd62019-10-08 17:17:34 -07001072}
1073
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001074static avifBool avifParsePixelAspectRatioBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001075{
1076 BEGIN_STREAM(s, raw, rawLen);
1077
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001078 avifPixelAspectRatioBox * pasp = &prop->u.pasp;
Joe Drago89f0cc82020-03-09 16:13:27 -07001079 CHECK(avifROStreamReadU32(&s, &pasp->hSpacing)); // unsigned int(32) hSpacing;
1080 CHECK(avifROStreamReadU32(&s, &pasp->vSpacing)); // unsigned int(32) vSpacing;
1081 return AVIF_TRUE;
1082}
1083
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001084static avifBool avifParseCleanApertureBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001085{
1086 BEGIN_STREAM(s, raw, rawLen);
1087
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001088 avifCleanApertureBox * clap = &prop->u.clap;
Joe Drago89f0cc82020-03-09 16:13:27 -07001089 CHECK(avifROStreamReadU32(&s, &clap->widthN)); // unsigned int(32) cleanApertureWidthN;
1090 CHECK(avifROStreamReadU32(&s, &clap->widthD)); // unsigned int(32) cleanApertureWidthD;
1091 CHECK(avifROStreamReadU32(&s, &clap->heightN)); // unsigned int(32) cleanApertureHeightN;
1092 CHECK(avifROStreamReadU32(&s, &clap->heightD)); // unsigned int(32) cleanApertureHeightD;
1093 CHECK(avifROStreamReadU32(&s, &clap->horizOffN)); // unsigned int(32) horizOffN;
1094 CHECK(avifROStreamReadU32(&s, &clap->horizOffD)); // unsigned int(32) horizOffD;
1095 CHECK(avifROStreamReadU32(&s, &clap->vertOffN)); // unsigned int(32) vertOffN;
1096 CHECK(avifROStreamReadU32(&s, &clap->vertOffD)); // unsigned int(32) vertOffD;
1097 return AVIF_TRUE;
1098}
1099
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001100static avifBool avifParseImageRotationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001101{
1102 BEGIN_STREAM(s, raw, rawLen);
1103
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001104 avifImageRotation * irot = &prop->u.irot;
Joe Drago89f0cc82020-03-09 16:13:27 -07001105 CHECK(avifROStreamRead(&s, &irot->angle, 1)); // unsigned int (6) reserved = 0; unsigned int (2) angle;
1106 if ((irot->angle & 0xfc) != 0) {
1107 // reserved bits must be 0
1108 return AVIF_FALSE;
1109 }
1110 return AVIF_TRUE;
1111}
1112
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001113static avifBool avifParseImageMirrorProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001114{
1115 BEGIN_STREAM(s, raw, rawLen);
1116
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001117 avifImageMirror * imir = &prop->u.imir;
Joe Drago89f0cc82020-03-09 16:13:27 -07001118 CHECK(avifROStreamRead(&s, &imir->axis, 1)); // unsigned int (7) reserved = 0; unsigned int (1) axis;
1119 if ((imir->axis & 0xfe) != 0) {
1120 // reserved bits must be 0
1121 return AVIF_FALSE;
1122 }
1123 return AVIF_TRUE;
1124}
1125
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001126static avifBool avifParsePixelInformationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago60421562020-04-23 11:32:26 -07001127{
1128 BEGIN_STREAM(s, raw, rawLen);
1129 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
1130
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001131 avifPixelInformationProperty * pixi = &prop->u.pixi;
Joe Drago60421562020-04-23 11:32:26 -07001132 CHECK(avifROStreamRead(&s, &pixi->planeCount, 1)); // unsigned int (8) num_channels;
1133 if (pixi->planeCount > MAX_PIXI_PLANE_DEPTHS) {
1134 return AVIF_FALSE;
1135 }
1136 for (uint8_t i = 0; i < pixi->planeCount; ++i) {
1137 CHECK(avifROStreamRead(&s, &pixi->planeDepths[i], 1)); // unsigned int (8) bits_per_channel;
1138 }
1139 return AVIF_TRUE;
1140}
1141
Joe Dragoa72da5b2020-06-15 19:40:17 -07001142static avifBool avifParseItemPropertyContainerBox(avifPropertyArray * properties, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001143{
1144 BEGIN_STREAM(s, raw, rawLen);
1145
Joe Drago345aaa12019-09-25 13:42:12 -07001146 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001147 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001148 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago8f7a3002019-02-07 19:35:37 -08001149
Joe Dragoa72da5b2020-06-15 19:40:17 -07001150 int propertyIndex = avifArrayPushIndex(properties);
1151 avifProperty * prop = &properties->prop[propertyIndex];
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001152 memcpy(prop->type, header.type, 4);
Joe Drago8f7a3002019-02-07 19:35:37 -08001153 if (!memcmp(header.type, "ispe", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001154 CHECK(avifParseImageSpatialExtentsProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001155 } else if (!memcmp(header.type, "auxC", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001156 CHECK(avifParseAuxiliaryTypeProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001157 } else if (!memcmp(header.type, "colr", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001158 CHECK(avifParseColourInformationBox(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001159 } else if (!memcmp(header.type, "av1C", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001160 CHECK(avifParseAV1CodecConfigurationBoxProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001161 } else if (!memcmp(header.type, "pasp", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001162 CHECK(avifParsePixelAspectRatioBoxProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001163 } else if (!memcmp(header.type, "clap", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001164 CHECK(avifParseCleanApertureBoxProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001165 } else if (!memcmp(header.type, "irot", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001166 CHECK(avifParseImageRotationProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001167 } else if (!memcmp(header.type, "imir", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001168 CHECK(avifParseImageMirrorProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001169 } else if (!memcmp(header.type, "pixi", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001170 CHECK(avifParsePixelInformationProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Drago60421562020-04-23 11:32:26 -07001171 }
Joe Drago8f7a3002019-02-07 19:35:37 -08001172
Joe Drago345aaa12019-09-25 13:42:12 -07001173 CHECK(avifROStreamSkip(&s, header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001174 }
1175 return AVIF_TRUE;
1176}
1177
Joe Drago9f2b87b2020-06-03 19:36:38 -07001178static avifBool avifParseItemPropertyAssociation(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001179{
1180 BEGIN_STREAM(s, raw, rawLen);
1181
1182 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -07001183 uint32_t flags;
1184 CHECK(avifROStreamReadVersionAndFlags(&s, &version, &flags));
1185 avifBool propertyIndexIsU16 = ((flags & 0x1) != 0);
Joe Drago8f7a3002019-02-07 19:35:37 -08001186
1187 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001188 CHECK(avifROStreamReadU32(&s, &entryCount));
Joe Drago8f7a3002019-02-07 19:35:37 -08001189 for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) {
1190 unsigned int itemID;
1191 if (version < 1) {
1192 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001193 CHECK(avifROStreamReadU16(&s, &tmp));
Joe Drago8f7a3002019-02-07 19:35:37 -08001194 itemID = tmp;
1195 } else {
Joe Drago345aaa12019-09-25 13:42:12 -07001196 CHECK(avifROStreamReadU32(&s, &itemID));
Joe Drago8f7a3002019-02-07 19:35:37 -08001197 }
1198 uint8_t associationCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001199 CHECK(avifROStreamRead(&s, &associationCount, 1));
Joe Drago8f7a3002019-02-07 19:35:37 -08001200 for (uint8_t associationIndex = 0; associationIndex < associationCount; ++associationIndex) {
Joe Drago3320e5f2020-04-21 17:36:27 -07001201 avifBool essential = AVIF_FALSE;
Joe Drago8f7a3002019-02-07 19:35:37 -08001202 uint16_t propertyIndex = 0;
1203 if (propertyIndexIsU16) {
Joe Drago345aaa12019-09-25 13:42:12 -07001204 CHECK(avifROStreamReadU16(&s, &propertyIndex));
Joe Drago3320e5f2020-04-21 17:36:27 -07001205 essential = ((propertyIndex & 0x8000) != 0);
Joe Drago8f7a3002019-02-07 19:35:37 -08001206 propertyIndex &= 0x7fff;
1207 } else {
1208 uint8_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001209 CHECK(avifROStreamRead(&s, &tmp, 1));
Joe Drago3320e5f2020-04-21 17:36:27 -07001210 essential = ((tmp & 0x80) != 0);
Joe Drago8f7a3002019-02-07 19:35:37 -08001211 propertyIndex = tmp & 0x7f;
1212 }
1213
1214 if (propertyIndex == 0) {
1215 // Not associated with any item
1216 continue;
1217 }
1218 --propertyIndex; // 1-indexed
1219
Joe Drago9f2b87b2020-06-03 19:36:38 -07001220 if (propertyIndex >= meta->properties.count) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001221 return AVIF_FALSE;
1222 }
1223
Joe Drago9f2b87b2020-06-03 19:36:38 -07001224 avifDecoderItem * item = avifMetaFindItem(meta, itemID);
Joe Drago46ea0582019-07-22 15:55:47 -07001225 if (!item) {
1226 return AVIF_FALSE;
1227 }
Joe Drago8f7a3002019-02-07 19:35:37 -08001228
Joe Dragoa72da5b2020-06-15 19:40:17 -07001229 // Copy property to item
1230 avifProperty * srcProp = &meta->properties.prop[propertyIndex];
1231
1232 static const char * supportedTypes[] = { "ispe", "auxC", "colr", "av1C", "pasp", "clap", "irot", "imir", "pixi" };
1233 size_t supportedTypesCount = sizeof(supportedTypes) / sizeof(supportedTypes[0]);
1234 avifBool supportedType = AVIF_FALSE;
1235 for (size_t i = 0; i < supportedTypesCount; ++i) {
1236 if (!memcmp(srcProp->type, supportedTypes[i], 4)) {
1237 supportedType = AVIF_TRUE;
1238 break;
1239 }
1240 }
1241 if (supportedType) {
1242 avifProperty * dstProp = (avifProperty *)avifArrayPushPtr(&item->properties);
1243 memcpy(dstProp, srcProp, sizeof(avifProperty));
Joe Drago3320e5f2020-04-21 17:36:27 -07001244 } else {
1245 if (essential) {
1246 // Discovered an essential item property that libavif doesn't support!
1247 // Make a note to ignore this item later.
1248 item->hasUnsupportedEssentialProperty = AVIF_TRUE;
1249 }
Joe Drago8f7a3002019-02-07 19:35:37 -08001250 }
1251 }
1252 }
1253
1254 return AVIF_TRUE;
1255}
1256
Joe Drago9f2b87b2020-06-03 19:36:38 -07001257static avifBool avifParsePrimaryItemBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Dragof6a42272019-11-21 15:21:41 -08001258{
Joe Drago9f2b87b2020-06-03 19:36:38 -07001259 if (meta->primaryItemID > 0) {
Joe Dragof6a42272019-11-21 15:21:41 -08001260 // Illegal to have multiple pitm boxes, bail out
1261 return AVIF_FALSE;
1262 }
1263
1264 BEGIN_STREAM(s, raw, rawLen);
1265
1266 uint8_t version;
1267 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
1268
1269 if (version == 0) {
1270 uint16_t tmp16;
1271 CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID;
Joe Drago9f2b87b2020-06-03 19:36:38 -07001272 meta->primaryItemID = tmp16;
Joe Dragof6a42272019-11-21 15:21:41 -08001273 } else {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001274 CHECK(avifROStreamReadU32(&s, &meta->primaryItemID)); // unsigned int(32) item_ID;
Joe Dragof6a42272019-11-21 15:21:41 -08001275 }
1276 return AVIF_TRUE;
1277}
1278
Joe Drago9f2b87b2020-06-03 19:36:38 -07001279static avifBool avifParseItemDataBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Dragof6a42272019-11-21 15:21:41 -08001280{
Joe Dragof6a42272019-11-21 15:21:41 -08001281 // 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 -07001282 for (uint32_t i = 0; i < meta->idats.count; ++i) {
Joe Dragoba1eb492020-06-22 17:05:04 -07001283 if (meta->idats.idat[i].id == meta->idatID) {
Joe Dragof6a42272019-11-21 15:21:41 -08001284 return AVIF_FALSE;
1285 }
1286 }
1287
Joe Drago9f2b87b2020-06-03 19:36:38 -07001288 int index = avifArrayPushIndex(&meta->idats);
1289 avifDecoderItemData * idat = &meta->idats.idat[index];
Joe Dragoba1eb492020-06-22 17:05:04 -07001290 idat->id = meta->idatID;
Joe Dragof6a42272019-11-21 15:21:41 -08001291 idat->data.data = raw;
1292 idat->data.size = rawLen;
1293 return AVIF_TRUE;
1294}
1295
Joe Drago9f2b87b2020-06-03 19:36:38 -07001296static avifBool avifParseItemPropertiesBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001297{
1298 BEGIN_STREAM(s, raw, rawLen);
1299
1300 avifBoxHeader ipcoHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001301 CHECK(avifROStreamReadBoxHeader(&s, &ipcoHeader));
Joe Drago8f7a3002019-02-07 19:35:37 -08001302 if (memcmp(ipcoHeader.type, "ipco", 4) != 0) {
1303 return AVIF_FALSE;
1304 }
1305
1306 // Read all item properties inside of ItemPropertyContainerBox
Joe Dragoa72da5b2020-06-15 19:40:17 -07001307 CHECK(avifParseItemPropertyContainerBox(&meta->properties, avifROStreamCurrent(&s), ipcoHeader.size));
Joe Drago345aaa12019-09-25 13:42:12 -07001308 CHECK(avifROStreamSkip(&s, ipcoHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001309
1310 // Now read all ItemPropertyAssociation until the end of the box, and make associations
Joe Drago345aaa12019-09-25 13:42:12 -07001311 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001312 avifBoxHeader ipmaHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001313 CHECK(avifROStreamReadBoxHeader(&s, &ipmaHeader));
Joe Drago8f7a3002019-02-07 19:35:37 -08001314
1315 if (!memcmp(ipmaHeader.type, "ipma", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001316 CHECK(avifParseItemPropertyAssociation(meta, avifROStreamCurrent(&s), ipmaHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001317 } else {
1318 // These must all be type ipma
1319 return AVIF_FALSE;
1320 }
1321
Joe Drago345aaa12019-09-25 13:42:12 -07001322 CHECK(avifROStreamSkip(&s, ipmaHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001323 }
1324 return AVIF_TRUE;
1325}
1326
Joe Drago9f2b87b2020-06-03 19:36:38 -07001327static avifBool avifParseItemInfoEntry(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001328{
1329 BEGIN_STREAM(s, raw, rawLen);
1330
Joe Drago345aaa12019-09-25 13:42:12 -07001331 CHECK(avifROStreamReadAndEnforceVersion(&s, 2)); // TODO: support version > 2? 2+ is required for item_type
Joe Drago8f7a3002019-02-07 19:35:37 -08001332
Joe Drago345aaa12019-09-25 13:42:12 -07001333 uint16_t itemID; // unsigned int(16) item_ID;
1334 CHECK(avifROStreamReadU16(&s, &itemID)); //
1335 uint16_t itemProtectionIndex; // unsigned int(16) item_protection_index;
1336 CHECK(avifROStreamReadU16(&s, &itemProtectionIndex)); //
1337 uint8_t itemType[4]; // unsigned int(32) item_type;
1338 CHECK(avifROStreamRead(&s, itemType, 4)); //
Joe Drago8f7a3002019-02-07 19:35:37 -08001339
Joe Dragof6a42272019-11-21 15:21:41 -08001340 avifContentType contentType;
1341 if (!memcmp(itemType, "mime", 4)) {
1342 CHECK(avifROStreamReadString(&s, NULL, 0)); // string item_name; (skipped)
1343 CHECK(avifROStreamReadString(&s, contentType.contentType, CONTENTTYPE_SIZE)); // string content_type;
1344 } else {
1345 memset(&contentType, 0, sizeof(contentType));
1346 }
1347
Joe Drago9f2b87b2020-06-03 19:36:38 -07001348 avifDecoderItem * item = avifMetaFindItem(meta, itemID);
Joe Drago46ea0582019-07-22 15:55:47 -07001349 if (!item) {
1350 return AVIF_FALSE;
1351 }
1352
Joe Drago05559c92019-07-17 16:33:38 -07001353 memcpy(item->type, itemType, sizeof(itemType));
Joe Dragof6a42272019-11-21 15:21:41 -08001354 memcpy(&item->contentType, &contentType, sizeof(contentType));
Joe Drago8f7a3002019-02-07 19:35:37 -08001355 return AVIF_TRUE;
1356}
1357
Joe Drago9f2b87b2020-06-03 19:36:38 -07001358static avifBool avifParseItemInfoBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001359{
1360 BEGIN_STREAM(s, raw, rawLen);
1361
1362 uint8_t version;
Joe Drago345aaa12019-09-25 13:42:12 -07001363 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Drago8f7a3002019-02-07 19:35:37 -08001364 uint32_t entryCount;
1365 if (version == 0) {
1366 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001367 CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) entry_count;
Joe Drago8f7a3002019-02-07 19:35:37 -08001368 entryCount = tmp;
1369 } else if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001370 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Drago8f7a3002019-02-07 19:35:37 -08001371 } else {
1372 return AVIF_FALSE;
1373 }
1374
Joe Drago678b9382019-02-09 03:17:47 -08001375 for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001376 avifBoxHeader infeHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001377 CHECK(avifROStreamReadBoxHeader(&s, &infeHeader));
Joe Drago8f7a3002019-02-07 19:35:37 -08001378
1379 if (!memcmp(infeHeader.type, "infe", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001380 CHECK(avifParseItemInfoEntry(meta, avifROStreamCurrent(&s), infeHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001381 } else {
1382 // These must all be type ipma
1383 return AVIF_FALSE;
1384 }
1385
Joe Drago345aaa12019-09-25 13:42:12 -07001386 CHECK(avifROStreamSkip(&s, infeHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001387 }
1388
1389 return AVIF_TRUE;
1390}
1391
Joe Drago9f2b87b2020-06-03 19:36:38 -07001392static avifBool avifParseItemReferenceBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001393{
1394 BEGIN_STREAM(s, raw, rawLen);
1395
1396 uint8_t version;
Joe Drago345aaa12019-09-25 13:42:12 -07001397 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Drago8f7a3002019-02-07 19:35:37 -08001398
Joe Drago345aaa12019-09-25 13:42:12 -07001399 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001400 avifBoxHeader irefHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001401 CHECK(avifROStreamReadBoxHeader(&s, &irefHeader));
Joe Drago8f7a3002019-02-07 19:35:37 -08001402
1403 uint32_t fromID = 0;
1404 if (version == 0) {
1405 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001406 CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) from_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001407 fromID = tmp;
1408 } else if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001409 CHECK(avifROStreamReadU32(&s, &fromID)); // unsigned int(32) from_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001410 } else {
1411 // unsupported iref version, skip it
1412 break;
1413 }
1414
1415 uint16_t referenceCount = 0;
Joe Drago345aaa12019-09-25 13:42:12 -07001416 CHECK(avifROStreamReadU16(&s, &referenceCount)); // unsigned int(16) reference_count;
Joe Drago8f7a3002019-02-07 19:35:37 -08001417
1418 for (uint16_t refIndex = 0; refIndex < referenceCount; ++refIndex) {
1419 uint32_t toID = 0;
1420 if (version == 0) {
1421 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001422 CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) to_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001423 toID = tmp;
1424 } else if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001425 CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) to_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001426 } else {
1427 // unsupported iref version, skip it
1428 break;
1429 }
1430
1431 // Read this reference as "{fromID} is a {irefType} for {toID}"
1432 if (fromID && toID) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001433 avifDecoderItem * item = avifMetaFindItem(meta, fromID);
Joe Drago46ea0582019-07-22 15:55:47 -07001434 if (!item) {
1435 return AVIF_FALSE;
1436 }
1437
Joe Drago8f7a3002019-02-07 19:35:37 -08001438 if (!memcmp(irefHeader.type, "thmb", 4)) {
Joe Drago05559c92019-07-17 16:33:38 -07001439 item->thumbnailForID = toID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001440 }
1441 if (!memcmp(irefHeader.type, "auxl", 4)) {
Joe Drago05559c92019-07-17 16:33:38 -07001442 item->auxForID = toID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001443 }
Joe Dragof6a42272019-11-21 15:21:41 -08001444 if (!memcmp(irefHeader.type, "cdsc", 4)) {
1445 item->descForID = toID;
1446 }
Joe Drago060d5342020-03-03 10:53:49 -08001447 if (!memcmp(irefHeader.type, "dimg", 4)) {
1448 // derived images refer in the opposite direction
Joe Drago9f2b87b2020-06-03 19:36:38 -07001449 avifDecoderItem * dimg = avifMetaFindItem(meta, toID);
Joe Drago060d5342020-03-03 10:53:49 -08001450 if (!dimg) {
1451 return AVIF_FALSE;
1452 }
1453
1454 dimg->dimgForID = fromID;
1455 }
Joe Drago8f7a3002019-02-07 19:35:37 -08001456 }
1457 }
1458 }
1459
1460 return AVIF_TRUE;
1461}
1462
Joe Drago9f2b87b2020-06-03 19:36:38 -07001463static avifBool avifParseMetaBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001464{
1465 BEGIN_STREAM(s, raw, rawLen);
1466
Joe Drago345aaa12019-09-25 13:42:12 -07001467 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Drago8f7a3002019-02-07 19:35:37 -08001468
Joe Dragoba1eb492020-06-22 17:05:04 -07001469 ++meta->idatID; // for tracking idat
Joe Dragof6a42272019-11-21 15:21:41 -08001470
Joe Drago345aaa12019-09-25 13:42:12 -07001471 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001472 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001473 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago8f7a3002019-02-07 19:35:37 -08001474
1475 if (!memcmp(header.type, "iloc", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001476 CHECK(avifParseItemLocationBox(meta, avifROStreamCurrent(&s), header.size));
Joe Dragof6a42272019-11-21 15:21:41 -08001477 } else if (!memcmp(header.type, "pitm", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001478 CHECK(avifParsePrimaryItemBox(meta, avifROStreamCurrent(&s), header.size));
Joe Dragof6a42272019-11-21 15:21:41 -08001479 } else if (!memcmp(header.type, "idat", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001480 CHECK(avifParseItemDataBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001481 } else if (!memcmp(header.type, "iprp", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001482 CHECK(avifParseItemPropertiesBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001483 } else if (!memcmp(header.type, "iinf", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001484 CHECK(avifParseItemInfoBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001485 } else if (!memcmp(header.type, "iref", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001486 CHECK(avifParseItemReferenceBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001487 }
1488
Joe Drago345aaa12019-09-25 13:42:12 -07001489 CHECK(avifROStreamSkip(&s, header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001490 }
1491 return AVIF_TRUE;
1492}
1493
Joe Drago9f2b87b2020-06-03 19:36:38 -07001494static avifBool avifParseTrackHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001495{
1496 BEGIN_STREAM(s, raw, rawLen);
1497
1498 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -07001499 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001500
1501 uint32_t ignored32, trackID;
1502 uint64_t ignored64;
1503 if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001504 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time;
1505 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time;
1506 CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID;
Joe Dragofc4144e2019-09-27 20:35:06 -07001507 CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0;
1508 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001509 } else if (version == 0) {
Joe Drago345aaa12019-09-25 13:42:12 -07001510 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time;
1511 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time;
1512 CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID;
Joe Dragofc4144e2019-09-27 20:35:06 -07001513 CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0;
1514 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001515 } else {
1516 // Unsupported version
1517 return AVIF_FALSE;
1518 }
1519
Joe Dragofc4144e2019-09-27 20:35:06 -07001520 // Skipping the following 52 bytes here:
1521 // ------------------------------------
1522 // const unsigned int(32)[2] reserved = 0;
1523 // template int(16) layer = 0;
1524 // template int(16) alternate_group = 0;
1525 // template int(16) volume = {if track_is_audio 0x0100 else 0};
1526 // const unsigned int(16) reserved = 0;
1527 // template int(32)[9] matrix= { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 }; // unity matrix
1528 CHECK(avifROStreamSkip(&s, 52));
1529
1530 uint32_t width, height;
1531 CHECK(avifROStreamReadU32(&s, &width)); // unsigned int(32) width;
1532 CHECK(avifROStreamReadU32(&s, &height)); // unsigned int(32) height;
1533 track->width = width >> 16;
1534 track->height = height >> 16;
1535
Joe Dragoae7e2c32019-07-18 15:22:25 -07001536 // TODO: support scaling based on width/height track header info?
1537
1538 track->id = trackID;
1539 return AVIF_TRUE;
1540}
1541
Joe Drago9f2b87b2020-06-03 19:36:38 -07001542static avifBool avifParseMediaHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001543{
1544 BEGIN_STREAM(s, raw, rawLen);
1545
1546 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -07001547 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001548
1549 uint32_t ignored32, mediaTimescale, mediaDuration32;
1550 uint64_t ignored64, mediaDuration64;
1551 if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001552 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time;
1553 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time;
1554 CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale;
1555 CHECK(avifROStreamReadU64(&s, &mediaDuration64)); // unsigned int(64) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001556 track->mediaDuration = mediaDuration64;
1557 } else if (version == 0) {
Joe Drago345aaa12019-09-25 13:42:12 -07001558 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time;
1559 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time;
1560 CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale;
1561 CHECK(avifROStreamReadU32(&s, &mediaDuration32)); // unsigned int(32) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001562 track->mediaDuration = (uint64_t)mediaDuration32;
1563 } else {
1564 // Unsupported version
1565 return AVIF_FALSE;
1566 }
1567
1568 track->mediaTimescale = mediaTimescale;
1569 return AVIF_TRUE;
1570}
1571
Joe Drago9f2b87b2020-06-03 19:36:38 -07001572static avifBool avifParseChunkOffsetBox(avifSampleTable * sampleTable, avifBool largeOffsets, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001573{
1574 BEGIN_STREAM(s, raw, rawLen);
1575
Joe Drago345aaa12019-09-25 13:42:12 -07001576 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001577
1578 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001579 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001580 for (uint32_t i = 0; i < entryCount; ++i) {
1581 uint64_t offset;
1582 if (largeOffsets) {
Joe Drago345aaa12019-09-25 13:42:12 -07001583 CHECK(avifROStreamReadU64(&s, &offset)); // unsigned int(32) chunk_offset;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001584 } else {
1585 uint32_t offset32;
Joe Drago345aaa12019-09-25 13:42:12 -07001586 CHECK(avifROStreamReadU32(&s, &offset32)); // unsigned int(32) chunk_offset;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001587 offset = (uint64_t)offset32;
1588 }
1589
1590 avifSampleTableChunk * chunk = (avifSampleTableChunk *)avifArrayPushPtr(&sampleTable->chunks);
1591 chunk->offset = offset;
1592 }
1593 return AVIF_TRUE;
1594}
1595
Joe Drago9f2b87b2020-06-03 19:36:38 -07001596static avifBool avifParseSampleToChunkBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001597{
1598 BEGIN_STREAM(s, raw, rawLen);
1599
Joe Drago345aaa12019-09-25 13:42:12 -07001600 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001601
1602 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001603 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001604 for (uint32_t i = 0; i < entryCount; ++i) {
1605 avifSampleTableSampleToChunk * sampleToChunk = (avifSampleTableSampleToChunk *)avifArrayPushPtr(&sampleTable->sampleToChunks);
Joe Drago345aaa12019-09-25 13:42:12 -07001606 CHECK(avifROStreamReadU32(&s, &sampleToChunk->firstChunk)); // unsigned int(32) first_chunk;
1607 CHECK(avifROStreamReadU32(&s, &sampleToChunk->samplesPerChunk)); // unsigned int(32) samples_per_chunk;
1608 CHECK(avifROStreamReadU32(&s, &sampleToChunk->sampleDescriptionIndex)); // unsigned int(32) sample_description_index;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001609 }
1610 return AVIF_TRUE;
1611}
1612
Joe Drago9f2b87b2020-06-03 19:36:38 -07001613static avifBool avifParseSampleSizeBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001614{
1615 BEGIN_STREAM(s, raw, rawLen);
1616
Joe Drago345aaa12019-09-25 13:42:12 -07001617 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001618
Joe Drago370be3f2020-02-07 15:59:42 -08001619 uint32_t allSamplesSize, sampleCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001620 CHECK(avifROStreamReadU32(&s, &allSamplesSize)); // unsigned int(32) sample_size;
Joe Drago370be3f2020-02-07 15:59:42 -08001621 CHECK(avifROStreamReadU32(&s, &sampleCount)); // unsigned int(32) sample_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001622
Joe Drago370be3f2020-02-07 15:59:42 -08001623 if (allSamplesSize > 0) {
1624 sampleTable->allSamplesSize = allSamplesSize;
1625 } else {
1626 for (uint32_t i = 0; i < sampleCount; ++i) {
1627 avifSampleTableSampleSize * sampleSize = (avifSampleTableSampleSize *)avifArrayPushPtr(&sampleTable->sampleSizes);
Joe Drago345aaa12019-09-25 13:42:12 -07001628 CHECK(avifROStreamReadU32(&s, &sampleSize->size)); // unsigned int(32) entry_size;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001629 }
1630 }
1631 return AVIF_TRUE;
1632}
1633
Joe Drago9f2b87b2020-06-03 19:36:38 -07001634static avifBool avifParseSyncSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Drago22c1ad92019-09-26 12:46:50 -07001635{
1636 BEGIN_STREAM(s, raw, rawLen);
Joe Drago22c1ad92019-09-26 12:46:50 -07001637
1638 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
1639
1640 uint32_t entryCount;
1641 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
1642
1643 for (uint32_t i = 0; i < entryCount; ++i) {
1644 uint32_t sampleNumber = 0;
1645 CHECK(avifROStreamReadU32(&s, &sampleNumber)); // unsigned int(32) sample_number;
1646 avifSyncSample * syncSample = (avifSyncSample *)avifArrayPushPtr(&sampleTable->syncSamples);
1647 syncSample->sampleNumber = sampleNumber;
1648 }
1649 return AVIF_TRUE;
1650}
1651
Joe Drago9f2b87b2020-06-03 19:36:38 -07001652static avifBool avifParseTimeToSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001653{
1654 BEGIN_STREAM(s, raw, rawLen);
1655
Joe Drago345aaa12019-09-25 13:42:12 -07001656 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001657
1658 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001659 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001660
1661 for (uint32_t i = 0; i < entryCount; ++i) {
1662 avifSampleTableTimeToSample * timeToSample = (avifSampleTableTimeToSample *)avifArrayPushPtr(&sampleTable->timeToSamples);
Joe Drago345aaa12019-09-25 13:42:12 -07001663 CHECK(avifROStreamReadU32(&s, &timeToSample->sampleCount)); // unsigned int(32) sample_count;
1664 CHECK(avifROStreamReadU32(&s, &timeToSample->sampleDelta)); // unsigned int(32) sample_delta;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001665 }
1666 return AVIF_TRUE;
1667}
1668
Joe Drago9f2b87b2020-06-03 19:36:38 -07001669static avifBool avifParseSampleDescriptionBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Drago2c0924c2019-09-26 17:41:01 -07001670{
1671 BEGIN_STREAM(s, raw, rawLen);
Joe Drago2c0924c2019-09-26 17:41:01 -07001672
1673 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
1674
1675 uint32_t entryCount;
1676 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
1677
1678 for (uint32_t i = 0; i < entryCount; ++i) {
1679 avifBoxHeader sampleEntryHeader;
1680 CHECK(avifROStreamReadBoxHeader(&s, &sampleEntryHeader));
1681
1682 avifSampleDescription * description = (avifSampleDescription *)avifArrayPushPtr(&sampleTable->sampleDescriptions);
Joe Dragoa72da5b2020-06-15 19:40:17 -07001683 avifArrayCreate(&description->properties, sizeof(avifProperty), 16);
Joe Drago2c0924c2019-09-26 17:41:01 -07001684 memcpy(description->format, sampleEntryHeader.type, sizeof(description->format));
Joe Drago6500fd62019-10-08 17:17:34 -07001685 size_t remainingBytes = avifROStreamRemainingBytes(&s);
1686 if (!memcmp(description->format, "av01", 4) && (remainingBytes > VISUALSAMPLEENTRY_SIZE)) {
Joe Dragoa72da5b2020-06-15 19:40:17 -07001687 CHECK(avifParseItemPropertyContainerBox(
1688 &description->properties, avifROStreamCurrent(&s) + VISUALSAMPLEENTRY_SIZE, remainingBytes - VISUALSAMPLEENTRY_SIZE));
Joe Drago6500fd62019-10-08 17:17:34 -07001689 }
Joe Drago2c0924c2019-09-26 17:41:01 -07001690
1691 CHECK(avifROStreamSkip(&s, sampleEntryHeader.size));
1692 }
1693 return AVIF_TRUE;
1694}
1695
Joe Drago9f2b87b2020-06-03 19:36:38 -07001696static avifBool avifParseSampleTableBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001697{
1698 if (track->sampleTable) {
1699 // A TrackBox may only have one SampleTable
1700 return AVIF_FALSE;
1701 }
1702 track->sampleTable = avifSampleTableCreate();
1703
1704 BEGIN_STREAM(s, raw, rawLen);
1705
Joe Drago345aaa12019-09-25 13:42:12 -07001706 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07001707 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001708 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001709
1710 if (!memcmp(header.type, "stco", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001711 CHECK(avifParseChunkOffsetBox(track->sampleTable, AVIF_FALSE, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001712 } else if (!memcmp(header.type, "co64", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001713 CHECK(avifParseChunkOffsetBox(track->sampleTable, AVIF_TRUE, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001714 } else if (!memcmp(header.type, "stsc", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001715 CHECK(avifParseSampleToChunkBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001716 } else if (!memcmp(header.type, "stsz", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001717 CHECK(avifParseSampleSizeBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Drago22c1ad92019-09-26 12:46:50 -07001718 } else if (!memcmp(header.type, "stss", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001719 CHECK(avifParseSyncSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001720 } else if (!memcmp(header.type, "stts", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001721 CHECK(avifParseTimeToSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Drago2c0924c2019-09-26 17:41:01 -07001722 } else if (!memcmp(header.type, "stsd", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001723 CHECK(avifParseSampleDescriptionBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001724 }
1725
Joe Drago345aaa12019-09-25 13:42:12 -07001726 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001727 }
Joe Dragoae7e2c32019-07-18 15:22:25 -07001728 return AVIF_TRUE;
1729}
1730
Joe Drago9f2b87b2020-06-03 19:36:38 -07001731static avifBool avifParseMediaInformationBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001732{
1733 BEGIN_STREAM(s, raw, rawLen);
1734
Joe Drago345aaa12019-09-25 13:42:12 -07001735 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07001736 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001737 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001738
1739 if (!memcmp(header.type, "stbl", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001740 CHECK(avifParseSampleTableBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001741 }
1742
Joe Drago345aaa12019-09-25 13:42:12 -07001743 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001744 }
1745 return AVIF_TRUE;
1746}
1747
Joe Drago9f2b87b2020-06-03 19:36:38 -07001748static avifBool avifParseMediaBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001749{
1750 BEGIN_STREAM(s, raw, rawLen);
1751
Joe Drago345aaa12019-09-25 13:42:12 -07001752 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07001753 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001754 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001755
1756 if (!memcmp(header.type, "mdhd", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001757 CHECK(avifParseMediaHeaderBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001758 } else if (!memcmp(header.type, "minf", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001759 CHECK(avifParseMediaInformationBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001760 }
1761
Joe Drago345aaa12019-09-25 13:42:12 -07001762 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001763 }
1764 return AVIF_TRUE;
1765}
1766
Joe Drago9f2b87b2020-06-03 19:36:38 -07001767static avifBool avifTrackReferenceBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Drago46ea0582019-07-22 15:55:47 -07001768{
1769 BEGIN_STREAM(s, raw, rawLen);
1770
Joe Drago345aaa12019-09-25 13:42:12 -07001771 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago46ea0582019-07-22 15:55:47 -07001772 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001773 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago46ea0582019-07-22 15:55:47 -07001774
1775 if (!memcmp(header.type, "auxl", 4)) {
1776 uint32_t toID;
Joe Drago345aaa12019-09-25 13:42:12 -07001777 CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) track_IDs[]
1778 CHECK(avifROStreamSkip(&s, header.size - sizeof(uint32_t))); // just take the first one
Joe Drago46ea0582019-07-22 15:55:47 -07001779 track->auxForID = toID;
1780 } else {
Joe Drago345aaa12019-09-25 13:42:12 -07001781 CHECK(avifROStreamSkip(&s, header.size));
Joe Drago46ea0582019-07-22 15:55:47 -07001782 }
1783 }
1784 return AVIF_TRUE;
1785}
1786
Joe Drago800b47f2020-03-18 16:22:37 -07001787static avifBool avifParseTrackBox(avifDecoderData * data, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001788{
1789 BEGIN_STREAM(s, raw, rawLen);
1790
Joe Dragoa72da5b2020-06-15 19:40:17 -07001791 avifTrack * track = avifDecoderDataCreateTrack(data);
Joe Dragoae7e2c32019-07-18 15:22:25 -07001792
Joe Drago345aaa12019-09-25 13:42:12 -07001793 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07001794 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001795 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001796
1797 if (!memcmp(header.type, "tkhd", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001798 CHECK(avifParseTrackHeaderBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001799 } else if (!memcmp(header.type, "meta", 4)) {
1800 CHECK(avifParseMetaBox(track->meta, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001801 } else if (!memcmp(header.type, "mdia", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001802 CHECK(avifParseMediaBox(track, avifROStreamCurrent(&s), header.size));
Joe Drago46ea0582019-07-22 15:55:47 -07001803 } else if (!memcmp(header.type, "tref", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001804 CHECK(avifTrackReferenceBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001805 }
1806
Joe Drago345aaa12019-09-25 13:42:12 -07001807 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001808 }
1809 return AVIF_TRUE;
1810}
1811
Joe Drago800b47f2020-03-18 16:22:37 -07001812static avifBool avifParseMoovBox(avifDecoderData * data, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001813{
1814 BEGIN_STREAM(s, raw, rawLen);
1815
Joe Drago345aaa12019-09-25 13:42:12 -07001816 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07001817 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001818 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001819
1820 if (!memcmp(header.type, "trak", 4)) {
Joe Drago345aaa12019-09-25 13:42:12 -07001821 CHECK(avifParseTrackBox(data, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001822 }
1823
Joe Drago345aaa12019-09-25 13:42:12 -07001824 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001825 }
1826 return AVIF_TRUE;
1827}
1828
Joe Drago345aaa12019-09-25 13:42:12 -07001829static avifBool avifParseFileTypeBox(avifFileType * ftyp, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001830{
1831 BEGIN_STREAM(s, raw, rawLen);
1832
Joe Drago345aaa12019-09-25 13:42:12 -07001833 CHECK(avifROStreamRead(&s, ftyp->majorBrand, 4));
1834 CHECK(avifROStreamReadU32(&s, &ftyp->minorVersion));
Joe Drago8f7a3002019-02-07 19:35:37 -08001835
Joe Drago345aaa12019-09-25 13:42:12 -07001836 size_t compatibleBrandsBytes = avifROStreamRemainingBytes(&s);
Joe Drago8f7a3002019-02-07 19:35:37 -08001837 if ((compatibleBrandsBytes % 4) != 0) {
1838 return AVIF_FALSE;
1839 }
Wan-Teh Chang6da0a882020-07-01 12:19:31 -07001840 ftyp->compatibleBrands = avifROStreamCurrent(&s);
1841 CHECK(avifROStreamSkip(&s, compatibleBrandsBytes));
Joe Drago7e37b972019-07-24 12:44:47 -07001842 ftyp->compatibleBrandsCount = (int)compatibleBrandsBytes / 4;
Joe Drago8f7a3002019-02-07 19:35:37 -08001843
1844 return AVIF_TRUE;
1845}
1846
Joe Drago800b47f2020-03-18 16:22:37 -07001847static avifBool avifParse(avifDecoderData * data, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001848{
1849 BEGIN_STREAM(s, raw, rawLen);
1850
Joe Drago345aaa12019-09-25 13:42:12 -07001851 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001852 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001853 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago8f7a3002019-02-07 19:35:37 -08001854
1855 if (!memcmp(header.type, "ftyp", 4)) {
Joe Drago345aaa12019-09-25 13:42:12 -07001856 CHECK(avifParseFileTypeBox(&data->ftyp, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001857 } else if (!memcmp(header.type, "meta", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001858 CHECK(avifParseMetaBox(data->meta, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001859 } else if (!memcmp(header.type, "moov", 4)) {
Joe Drago345aaa12019-09-25 13:42:12 -07001860 CHECK(avifParseMoovBox(data, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001861 }
1862
Joe Drago345aaa12019-09-25 13:42:12 -07001863 CHECK(avifROStreamSkip(&s, header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001864 }
1865 return AVIF_TRUE;
1866}
1867
1868// ---------------------------------------------------------------------------
Joe Drago8f7a3002019-02-07 19:35:37 -08001869
Joe Drago7e37b972019-07-24 12:44:47 -07001870static avifBool avifFileTypeIsCompatible(avifFileType * ftyp)
1871{
Wan-Teh Changbb8e2a82020-06-29 18:13:28 -07001872 avifBool avifCompatible = (memcmp(ftyp->majorBrand, "avif", 4) == 0 || memcmp(ftyp->majorBrand, "avis", 4) == 0);
Joe Drago2c0924c2019-09-26 17:41:01 -07001873 if (!avifCompatible) {
1874 for (int compatibleBrandIndex = 0; compatibleBrandIndex < ftyp->compatibleBrandsCount; ++compatibleBrandIndex) {
Wan-Teh Chang6da0a882020-07-01 12:19:31 -07001875 const uint8_t * compatibleBrand = &ftyp->compatibleBrands[4 * compatibleBrandIndex];
Wan-Teh Changbb8e2a82020-06-29 18:13:28 -07001876 if (!memcmp(compatibleBrand, "avif", 4) || !memcmp(compatibleBrand, "avis", 4)) {
Joe Drago2c0924c2019-09-26 17:41:01 -07001877 avifCompatible = AVIF_TRUE;
1878 break;
Joe Drago7e37b972019-07-24 12:44:47 -07001879 }
1880 }
1881 }
1882 return avifCompatible;
1883}
1884
Wan-Teh Change184dc12020-05-11 12:47:21 -07001885avifBool avifPeekCompatibleFileType(const avifROData * input)
Joe Drago7e37b972019-07-24 12:44:47 -07001886{
1887 BEGIN_STREAM(s, input->data, input->size);
1888
1889 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001890 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago7e37b972019-07-24 12:44:47 -07001891 if (memcmp(header.type, "ftyp", 4) != 0) {
1892 return AVIF_FALSE;
1893 }
1894
1895 avifFileType ftyp;
1896 memset(&ftyp, 0, sizeof(avifFileType));
Joe Drago345aaa12019-09-25 13:42:12 -07001897 avifBool parsed = avifParseFileTypeBox(&ftyp, avifROStreamCurrent(&s), header.size);
Joe Drago7e37b972019-07-24 12:44:47 -07001898 if (!parsed) {
1899 return AVIF_FALSE;
1900 }
1901 return avifFileTypeIsCompatible(&ftyp);
1902}
1903
1904// ---------------------------------------------------------------------------
1905
Joe Drago0b05eee2019-06-12 13:24:39 -07001906avifDecoder * avifDecoderCreate(void)
1907{
1908 avifDecoder * decoder = (avifDecoder *)avifAlloc(sizeof(avifDecoder));
1909 memset(decoder, 0, sizeof(avifDecoder));
1910 return decoder;
1911}
1912
Joe Drago46ea0582019-07-22 15:55:47 -07001913static void avifDecoderCleanup(avifDecoder * decoder)
1914{
1915 if (decoder->data) {
Joe Drago800b47f2020-03-18 16:22:37 -07001916 avifDecoderDataDestroy(decoder->data);
Joe Drago46ea0582019-07-22 15:55:47 -07001917 decoder->data = NULL;
1918 }
1919
1920 if (decoder->image) {
1921 avifImageDestroy(decoder->image);
1922 decoder->image = NULL;
1923 }
1924}
1925
Joe Drago0b05eee2019-06-12 13:24:39 -07001926void avifDecoderDestroy(avifDecoder * decoder)
1927{
Joe Drago46ea0582019-07-22 15:55:47 -07001928 avifDecoderCleanup(decoder);
Joe Drago0b05eee2019-06-12 13:24:39 -07001929 avifFree(decoder);
1930}
1931
Joe Drago46ea0582019-07-22 15:55:47 -07001932avifResult avifDecoderSetSource(avifDecoder * decoder, avifDecoderSource source)
Joe Drago444f0512019-01-23 17:03:24 -08001933{
Joe Drago46ea0582019-07-22 15:55:47 -07001934 decoder->requestedSource = source;
1935 return avifDecoderReset(decoder);
1936}
Joe Drago33f1d362019-02-13 16:46:22 -08001937
Wan-Teh Change184dc12020-05-11 12:47:21 -07001938avifResult avifDecoderParse(avifDecoder * decoder, const avifROData * rawInput)
Joe Drago46ea0582019-07-22 15:55:47 -07001939{
Joe Drago46ea0582019-07-22 15:55:47 -07001940 // Cleanup anything lingering in the decoder
1941 avifDecoderCleanup(decoder);
1942
Joe Drago444f0512019-01-23 17:03:24 -08001943 // -----------------------------------------------------------------------
1944 // Parse BMFF boxes
1945
Joe Drago800b47f2020-03-18 16:22:37 -07001946 decoder->data = avifDecoderDataCreate();
Joe Drago46ea0582019-07-22 15:55:47 -07001947
1948 // Shallow copy, on purpose
Joe Drago345aaa12019-09-25 13:42:12 -07001949 memcpy(&decoder->data->rawInput, rawInput, sizeof(avifROData));
Joe Drago46ea0582019-07-22 15:55:47 -07001950
1951 if (!avifParse(decoder->data, decoder->data->rawInput.data, decoder->data->rawInput.size)) {
1952 return AVIF_RESULT_BMFF_PARSE_FAILED;
Joe Drago444f0512019-01-23 17:03:24 -08001953 }
1954
Joe Drago7e37b972019-07-24 12:44:47 -07001955 avifBool avifCompatible = avifFileTypeIsCompatible(&decoder->data->ftyp);
Joe Drago8f7a3002019-02-07 19:35:37 -08001956 if (!avifCompatible) {
Joe Drago46ea0582019-07-22 15:55:47 -07001957 return AVIF_RESULT_INVALID_FTYP;
1958 }
1959
1960 // Sanity check items
Joe Drago9f2b87b2020-06-03 19:36:38 -07001961 for (uint32_t itemIndex = 0; itemIndex < decoder->data->meta->items.count; ++itemIndex) {
1962 avifDecoderItem * item = &decoder->data->meta->items.item[itemIndex];
Joe Drago3320e5f2020-04-21 17:36:27 -07001963 if (item->hasUnsupportedEssentialProperty) {
1964 // An essential property isn't supported by libavif; ignore the item.
1965 continue;
1966 }
Joe Drago800b47f2020-03-18 16:22:37 -07001967 const uint8_t * p = avifDecoderDataCalcItemPtr(decoder->data, item);
Joe Dragof6a42272019-11-21 15:21:41 -08001968 if (p == NULL) {
Joe Drago46ea0582019-07-22 15:55:47 -07001969 return AVIF_RESULT_BMFF_PARSE_FAILED;
1970 }
1971 }
1972
1973 // Sanity check tracks
1974 for (uint32_t trackIndex = 0; trackIndex < decoder->data->tracks.count; ++trackIndex) {
1975 avifTrack * track = &decoder->data->tracks.track[trackIndex];
1976 if (!track->sampleTable) {
1977 continue;
1978 }
1979
1980 for (uint32_t chunkIndex = 0; chunkIndex < track->sampleTable->chunks.count; ++chunkIndex) {
1981 avifSampleTableChunk * chunk = &track->sampleTable->chunks.chunk[chunkIndex];
1982 if (chunk->offset > decoder->data->rawInput.size) {
1983 return AVIF_RESULT_BMFF_PARSE_FAILED;
1984 }
1985 }
1986 }
1987 return avifDecoderReset(decoder);
1988}
1989
Joe Drago53355352019-10-28 19:04:51 -07001990static avifCodec * avifCodecCreateInternal(avifCodecChoice choice, avifCodecDecodeInput * decodeInput)
Joe Drago46ea0582019-07-22 15:55:47 -07001991{
Joe Drago53355352019-10-28 19:04:51 -07001992 avifCodec * codec = avifCodecCreate(choice, AVIF_CODEC_FLAG_CAN_DECODE);
Joe Drago46ea0582019-07-22 15:55:47 -07001993 if (codec) {
1994 codec->decodeInput = decodeInput;
1995 }
1996 return codec;
1997}
1998
Joe Drago22c1ad92019-09-26 12:46:50 -07001999static avifResult avifDecoderFlush(avifDecoder * decoder)
2000{
Joe Drago800b47f2020-03-18 16:22:37 -07002001 avifDecoderDataResetCodec(decoder->data);
Joe Drago22c1ad92019-09-26 12:46:50 -07002002
Joe Drago060d5342020-03-03 10:53:49 -08002003 for (unsigned int i = 0; i < decoder->data->tiles.count; ++i) {
2004 avifTile * tile = &decoder->data->tiles.tile[i];
2005 tile->codec = avifCodecCreateInternal(decoder->codecChoice, tile->input);
2006 if (!tile->codec) {
Joe Drago53355352019-10-28 19:04:51 -07002007 return AVIF_RESULT_NO_CODEC_AVAILABLE;
2008 }
Joe Drago060d5342020-03-03 10:53:49 -08002009 if (!tile->codec->open(tile->codec, decoder->imageIndex + 1)) {
2010 return AVIF_RESULT_DECODE_COLOR_FAILED;
Joe Drago22c1ad92019-09-26 12:46:50 -07002011 }
2012 }
2013 return AVIF_RESULT_OK;
2014}
2015
Joe Drago46ea0582019-07-22 15:55:47 -07002016avifResult avifDecoderReset(avifDecoder * decoder)
2017{
Joe Drago800b47f2020-03-18 16:22:37 -07002018 avifDecoderData * data = decoder->data;
Joe Drago46ea0582019-07-22 15:55:47 -07002019 if (!data) {
2020 // Nothing to reset.
2021 return AVIF_RESULT_OK;
2022 }
2023
Joe Drago060d5342020-03-03 10:53:49 -08002024 memset(&data->colorGrid, 0, sizeof(data->colorGrid));
2025 memset(&data->alphaGrid, 0, sizeof(data->alphaGrid));
Joe Drago800b47f2020-03-18 16:22:37 -07002026 avifDecoderDataClearTiles(data);
Joe Drago89f0cc82020-03-09 16:13:27 -07002027
2028 // Prepare / cleanup decoded image state
Joe Dragoa0da4a42020-05-08 14:27:40 -07002029 if (decoder->image) {
2030 avifImageDestroy(decoder->image);
Joe Drago8f7a3002019-02-07 19:35:37 -08002031 }
Joe Dragoa0da4a42020-05-08 14:27:40 -07002032 decoder->image = avifImageCreateEmpty();
2033 data->cicpSet = AVIF_FALSE;
Joe Drago8f7a3002019-02-07 19:35:37 -08002034
Joe Drago70cbf602019-07-24 15:30:55 -07002035 memset(&decoder->ioStats, 0, sizeof(decoder->ioStats));
2036
Joe Drago444f0512019-01-23 17:03:24 -08002037 // -----------------------------------------------------------------------
Joe Drago46ea0582019-07-22 15:55:47 -07002038 // Build decode input
Joe Drago444f0512019-01-23 17:03:24 -08002039
Joe Drago46ea0582019-07-22 15:55:47 -07002040 data->sourceSampleTable = NULL; // Reset
2041 if (decoder->requestedSource == AVIF_DECODER_SOURCE_AUTO) {
2042 if (data->tracks.count > 0) {
2043 data->source = AVIF_DECODER_SOURCE_TRACKS;
2044 } else {
2045 data->source = AVIF_DECODER_SOURCE_PRIMARY_ITEM;
Joe Drago76370232019-07-16 11:00:52 -07002046 }
Joe Drago46ea0582019-07-22 15:55:47 -07002047 } else {
2048 data->source = decoder->requestedSource;
Joe Drago76370232019-07-16 11:00:52 -07002049 }
2050
Joe Dragoa72da5b2020-06-15 19:40:17 -07002051 const avifPropertyArray * colorProperties = NULL;
Joe Drago46ea0582019-07-22 15:55:47 -07002052 if (data->source == AVIF_DECODER_SOURCE_TRACKS) {
2053 avifTrack * colorTrack = NULL;
2054 avifTrack * alphaTrack = NULL;
2055
2056 // Find primary track - this probably needs some better detection
2057 uint32_t colorTrackIndex = 0;
2058 for (; colorTrackIndex < decoder->data->tracks.count; ++colorTrackIndex) {
2059 avifTrack * track = &decoder->data->tracks.track[colorTrackIndex];
2060 if (!track->sampleTable) {
2061 continue;
2062 }
Joe Dragoba1eb492020-06-22 17:05:04 -07002063 if (!track->id) { // trak box might be missing a tkhd box inside, skip it
Joe Drago4a25c192020-06-03 16:29:58 -07002064 continue;
2065 }
Joe Drago46ea0582019-07-22 15:55:47 -07002066 if (!track->sampleTable->chunks.count) {
2067 continue;
2068 }
Joe Drago2c0924c2019-09-26 17:41:01 -07002069 if (!avifSampleTableHasFormat(track->sampleTable, "av01")) {
2070 continue;
2071 }
Joe Drago46ea0582019-07-22 15:55:47 -07002072 if (track->auxForID != 0) {
2073 continue;
2074 }
2075
2076 // Found one!
Joe Drago444f0512019-01-23 17:03:24 -08002077 break;
2078 }
Joe Drago46ea0582019-07-22 15:55:47 -07002079 if (colorTrackIndex == decoder->data->tracks.count) {
2080 return AVIF_RESULT_NO_CONTENT;
Joe Drago444f0512019-01-23 17:03:24 -08002081 }
Joe Drago46ea0582019-07-22 15:55:47 -07002082 colorTrack = &decoder->data->tracks.track[colorTrackIndex];
2083
Joe Dragoa72da5b2020-06-15 19:40:17 -07002084 colorProperties = avifSampleTableGetProperties(colorTrack->sampleTable);
2085 if (!colorProperties) {
2086 return AVIF_RESULT_BMFF_PARSE_FAILED;
2087 }
2088
2089 // Find Exif and/or XMP metadata, if any
2090 if (colorTrack->meta) {
Joe Drago39267fd2020-06-19 15:21:14 -07002091 // See the comment above avifDecoderDataFindMetadata() for the explanation of using 0 here
Joe Dragoa72da5b2020-06-15 19:40:17 -07002092 if (!avifDecoderDataFindMetadata(data, colorTrack->meta, decoder->image, 0)) {
2093 return AVIF_RESULT_BMFF_PARSE_FAILED;
2094 }
2095 }
2096
Joe Drago46ea0582019-07-22 15:55:47 -07002097 uint32_t alphaTrackIndex = 0;
2098 for (; alphaTrackIndex < decoder->data->tracks.count; ++alphaTrackIndex) {
2099 avifTrack * track = &decoder->data->tracks.track[alphaTrackIndex];
2100 if (!track->sampleTable) {
2101 continue;
2102 }
Joe Drago4a25c192020-06-03 16:29:58 -07002103 if (!track->id) {
2104 continue;
2105 }
Joe Drago46ea0582019-07-22 15:55:47 -07002106 if (!track->sampleTable->chunks.count) {
2107 continue;
2108 }
Joe Drago2c0924c2019-09-26 17:41:01 -07002109 if (!avifSampleTableHasFormat(track->sampleTable, "av01")) {
2110 continue;
2111 }
Joe Drago46ea0582019-07-22 15:55:47 -07002112 if (track->auxForID == colorTrack->id) {
2113 // Found it!
2114 break;
2115 }
2116 }
2117 if (alphaTrackIndex != decoder->data->tracks.count) {
2118 alphaTrack = &decoder->data->tracks.track[alphaTrackIndex];
Joe Drago8f7a3002019-02-07 19:35:37 -08002119 }
Joe Drago444f0512019-01-23 17:03:24 -08002120
Joe Drago800b47f2020-03-18 16:22:37 -07002121 avifTile * colorTile = avifDecoderDataCreateTile(decoder->data);
Joe Drago060d5342020-03-03 10:53:49 -08002122 if (!avifCodecDecodeInputGetSamples(colorTile->input, colorTrack->sampleTable, &decoder->data->rawInput)) {
Joe Drago46ea0582019-07-22 15:55:47 -07002123 return AVIF_RESULT_BMFF_PARSE_FAILED;
2124 }
Joe Drago060d5342020-03-03 10:53:49 -08002125 decoder->data->colorTileCount = 1;
Joe Drago46ea0582019-07-22 15:55:47 -07002126
Joe Drago060d5342020-03-03 10:53:49 -08002127 avifTile * alphaTile = NULL;
Joe Drago46ea0582019-07-22 15:55:47 -07002128 if (alphaTrack) {
Joe Drago800b47f2020-03-18 16:22:37 -07002129 alphaTile = avifDecoderDataCreateTile(decoder->data);
Joe Drago060d5342020-03-03 10:53:49 -08002130 if (!avifCodecDecodeInputGetSamples(alphaTile->input, alphaTrack->sampleTable, &decoder->data->rawInput)) {
Joe Drago46ea0582019-07-22 15:55:47 -07002131 return AVIF_RESULT_BMFF_PARSE_FAILED;
2132 }
Joe Drago060d5342020-03-03 10:53:49 -08002133 alphaTile->input->alpha = AVIF_TRUE;
2134 decoder->data->alphaTileCount = 1;
Joe Drago46ea0582019-07-22 15:55:47 -07002135 }
2136
2137 // Stash off sample table for future timing information
2138 data->sourceSampleTable = colorTrack->sampleTable;
2139
2140 // Image sequence timing
2141 decoder->imageIndex = -1;
Joe Drago060d5342020-03-03 10:53:49 -08002142 decoder->imageCount = colorTile->input->samples.count;
Joe Drago46ea0582019-07-22 15:55:47 -07002143 decoder->timescale = colorTrack->mediaTimescale;
2144 decoder->durationInTimescales = colorTrack->mediaDuration;
2145 if (colorTrack->mediaTimescale) {
2146 decoder->duration = (double)decoder->durationInTimescales / (double)colorTrack->mediaTimescale;
2147 } else {
2148 decoder->duration = 0;
2149 }
2150 memset(&decoder->imageTiming, 0, sizeof(decoder->imageTiming)); // to be set in avifDecoderNextImage()
Joe Drago41700852019-09-26 17:01:43 -07002151
Joe Dragoc554f5f2020-06-09 18:59:51 -07002152 decoder->image->width = colorTrack->width;
2153 decoder->image->height = colorTrack->height;
Joe Dragoc554f5f2020-06-09 18:59:51 -07002154 decoder->alphaPresent = (alphaTrack != NULL);
Joe Drago46ea0582019-07-22 15:55:47 -07002155 } else {
2156 // Create from items
2157
Joe Drago345aaa12019-09-25 13:42:12 -07002158 avifROData colorOBU = AVIF_DATA_EMPTY;
2159 avifROData alphaOBU = AVIF_DATA_EMPTY;
Joe Drago800b47f2020-03-18 16:22:37 -07002160 avifDecoderItem * colorOBUItem = NULL;
2161 avifDecoderItem * alphaOBUItem = NULL;
Joe Drago46ea0582019-07-22 15:55:47 -07002162
Joe Dragof6a42272019-11-21 15:21:41 -08002163 // Find the colorOBU (primary) item
Joe Drago9f2b87b2020-06-03 19:36:38 -07002164 for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) {
2165 avifDecoderItem * item = &data->meta->items.item[itemIndex];
Joe Dragoba1eb492020-06-22 17:05:04 -07002166 if (!item->size) {
2167 continue;
Joe Drago8f7a3002019-02-07 19:35:37 -08002168 }
Joe Drago3320e5f2020-04-21 17:36:27 -07002169 if (item->hasUnsupportedEssentialProperty) {
2170 // An essential property isn't supported by libavif; ignore the item.
2171 continue;
2172 }
Joe Drago951a0022020-03-09 16:19:44 -07002173 avifBool isGrid = (memcmp(item->type, "grid", 4) == 0);
Joe Drago060d5342020-03-03 10:53:49 -08002174 if (memcmp(item->type, "av01", 4) && !isGrid) {
Joe Drago8f7a3002019-02-07 19:35:37 -08002175 // probably exif or some other data
2176 continue;
2177 }
2178 if (item->thumbnailForID != 0) {
2179 // It's a thumbnail, skip it
2180 continue;
2181 }
Joe Drago9f2b87b2020-06-03 19:36:38 -07002182 if ((data->meta->primaryItemID > 0) && (item->id != data->meta->primaryItemID)) {
Joe Dragof6a42272019-11-21 15:21:41 -08002183 // a primary item ID was specified, require it
2184 continue;
2185 }
Joe Drago8f7a3002019-02-07 19:35:37 -08002186
Joe Drago060d5342020-03-03 10:53:49 -08002187 if (isGrid) {
Wan-Teh Changcb164942020-08-10 15:47:29 -07002188 if (decoder->disableGridImages) {
2189 return AVIF_RESULT_BMFF_PARSE_FAILED;
2190 }
Joe Drago800b47f2020-03-18 16:22:37 -07002191 const uint8_t * itemPtr = avifDecoderDataCalcItemPtr(data, item);
Joe Drago060d5342020-03-03 10:53:49 -08002192 if (itemPtr == NULL) {
2193 return AVIF_RESULT_BMFF_PARSE_FAILED;
2194 }
2195 if (!avifParseImageGridBox(&data->colorGrid, itemPtr, item->size)) {
2196 return AVIF_RESULT_INVALID_IMAGE_GRID;
2197 }
2198 } else {
Joe Drago800b47f2020-03-18 16:22:37 -07002199 colorOBU.data = avifDecoderDataCalcItemPtr(data, item);
Joe Drago060d5342020-03-03 10:53:49 -08002200 colorOBU.size = item->size;
2201 }
2202
Joe Drago46ea0582019-07-22 15:55:47 -07002203 colorOBUItem = item;
Joe Drago46ea0582019-07-22 15:55:47 -07002204 break;
2205 }
2206
Joe Drago060d5342020-03-03 10:53:49 -08002207 if (!colorOBUItem) {
2208 return AVIF_RESULT_NO_AV1_ITEMS_FOUND;
2209 }
Joe Dragoa72da5b2020-06-15 19:40:17 -07002210 colorProperties = &colorOBUItem->properties;
Joe Drago46ea0582019-07-22 15:55:47 -07002211
Joe Drago060d5342020-03-03 10:53:49 -08002212 // Find the alphaOBU item, if any
Joe Drago9f2b87b2020-06-03 19:36:38 -07002213 for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) {
2214 avifDecoderItem * item = &data->meta->items.item[itemIndex];
Joe Dragoba1eb492020-06-22 17:05:04 -07002215 if (!item->size) {
2216 continue;
Joe Drago060d5342020-03-03 10:53:49 -08002217 }
Joe Drago3320e5f2020-04-21 17:36:27 -07002218 if (item->hasUnsupportedEssentialProperty) {
2219 // An essential property isn't supported by libavif; ignore the item.
2220 continue;
2221 }
Joe Drago951a0022020-03-09 16:19:44 -07002222 avifBool isGrid = (memcmp(item->type, "grid", 4) == 0);
Joe Drago060d5342020-03-03 10:53:49 -08002223 if (memcmp(item->type, "av01", 4) && !isGrid) {
2224 // probably exif or some other data
2225 continue;
2226 }
2227 if (item->thumbnailForID != 0) {
2228 // It's a thumbnail, skip it
2229 continue;
Joe Drago8f7a3002019-02-07 19:35:37 -08002230 }
Joe Dragof6a42272019-11-21 15:21:41 -08002231
Joe Dragoa72da5b2020-06-15 19:40:17 -07002232 const avifProperty * auxCProp = avifPropertyArrayFind(&item->properties, "auxC");
2233 if (auxCProp && isAlphaURN(auxCProp->u.auxC.auxType) && (item->auxForID == colorOBUItem->id)) {
Joe Drago060d5342020-03-03 10:53:49 -08002234 if (isGrid) {
Wan-Teh Changcb164942020-08-10 15:47:29 -07002235 if (decoder->disableGridImages) {
2236 return AVIF_RESULT_BMFF_PARSE_FAILED;
2237 }
Joe Drago800b47f2020-03-18 16:22:37 -07002238 const uint8_t * itemPtr = avifDecoderDataCalcItemPtr(data, item);
Joe Drago060d5342020-03-03 10:53:49 -08002239 if (itemPtr == NULL) {
2240 return AVIF_RESULT_BMFF_PARSE_FAILED;
2241 }
2242 if (!avifParseImageGridBox(&data->alphaGrid, itemPtr, item->size)) {
2243 return AVIF_RESULT_INVALID_IMAGE_GRID;
2244 }
2245 } else {
Joe Drago800b47f2020-03-18 16:22:37 -07002246 alphaOBU.data = avifDecoderDataCalcItemPtr(data, item);
Joe Drago060d5342020-03-03 10:53:49 -08002247 alphaOBU.size = item->size;
Joe Dragof6a42272019-11-21 15:21:41 -08002248 }
2249
Joe Drago060d5342020-03-03 10:53:49 -08002250 alphaOBUItem = item;
2251 break;
Joe Dragof6a42272019-11-21 15:21:41 -08002252 }
Joe Drago444f0512019-01-23 17:03:24 -08002253 }
Joe Drago444f0512019-01-23 17:03:24 -08002254
Joe Drago060d5342020-03-03 10:53:49 -08002255 // Find Exif and/or XMP metadata, if any
Joe Dragoa72da5b2020-06-15 19:40:17 -07002256 if (!avifDecoderDataFindMetadata(data, data->meta, decoder->image, colorOBUItem->id)) {
2257 return AVIF_RESULT_BMFF_PARSE_FAILED;
Joe Drago060d5342020-03-03 10:53:49 -08002258 }
2259
Wan-Teh Chang4295bcb2020-04-05 15:41:05 -07002260 if ((data->colorGrid.rows > 0) && (data->colorGrid.columns > 0)) {
Joe Drago800b47f2020-03-18 16:22:37 -07002261 if (!avifDecoderDataGenerateImageGridTiles(data, &data->colorGrid, colorOBUItem, AVIF_FALSE)) {
Joe Drago060d5342020-03-03 10:53:49 -08002262 return AVIF_RESULT_INVALID_IMAGE_GRID;
2263 }
2264 data->colorTileCount = data->tiles.count;
2265 } else {
2266 if (colorOBU.size == 0) {
2267 return AVIF_RESULT_NO_AV1_ITEMS_FOUND;
2268 }
2269
Joe Drago800b47f2020-03-18 16:22:37 -07002270 avifTile * colorTile = avifDecoderDataCreateTile(decoder->data);
Joe Dragoe3e3bfa2020-06-02 16:33:53 -07002271 avifDecodeSample * colorSample = (avifDecodeSample *)avifArrayPushPtr(&colorTile->input->samples);
Joe Drago060d5342020-03-03 10:53:49 -08002272 memcpy(&colorSample->data, &colorOBU, sizeof(avifROData));
2273 colorSample->sync = AVIF_TRUE;
2274 decoder->data->colorTileCount = 1;
2275 }
2276
Benbuck Nason78e3c9d2020-03-27 14:35:27 -07002277 if ((data->alphaGrid.rows > 0) && (data->alphaGrid.columns > 0) && alphaOBUItem) {
Joe Drago800b47f2020-03-18 16:22:37 -07002278 if (!avifDecoderDataGenerateImageGridTiles(data, &data->alphaGrid, alphaOBUItem, AVIF_FALSE)) {
Joe Drago060d5342020-03-03 10:53:49 -08002279 return AVIF_RESULT_INVALID_IMAGE_GRID;
2280 }
2281 data->alphaTileCount = data->tiles.count - data->colorTileCount;
2282 } else {
2283 avifTile * alphaTile = NULL;
2284 if (alphaOBU.size > 0) {
Joe Drago800b47f2020-03-18 16:22:37 -07002285 alphaTile = avifDecoderDataCreateTile(decoder->data);
Joe Drago060d5342020-03-03 10:53:49 -08002286
Joe Dragoe3e3bfa2020-06-02 16:33:53 -07002287 avifDecodeSample * alphaSample = (avifDecodeSample *)avifArrayPushPtr(&alphaTile->input->samples);
Joe Drago060d5342020-03-03 10:53:49 -08002288 memcpy(&alphaSample->data, &alphaOBU, sizeof(avifROData));
2289 alphaSample->sync = AVIF_TRUE;
2290 alphaTile->input->alpha = AVIF_TRUE;
2291 decoder->data->alphaTileCount = 1;
2292 }
Joe Drago444f0512019-01-23 17:03:24 -08002293 }
Joe Drago33f1d362019-02-13 16:46:22 -08002294
Joe Drago46ea0582019-07-22 15:55:47 -07002295 // Set all counts and timing to safe-but-uninteresting values
2296 decoder->imageIndex = -1;
2297 decoder->imageCount = 1;
2298 decoder->imageTiming.timescale = 1;
2299 decoder->imageTiming.pts = 0;
2300 decoder->imageTiming.ptsInTimescales = 0;
2301 decoder->imageTiming.duration = 1;
2302 decoder->imageTiming.durationInTimescales = 1;
2303 decoder->timescale = 1;
2304 decoder->duration = 1;
2305 decoder->durationInTimescales = 1;
Joe Drago70cbf602019-07-24 15:30:55 -07002306
2307 decoder->ioStats.colorOBUSize = colorOBU.size;
2308 decoder->ioStats.alphaOBUSize = alphaOBU.size;
Joe Drago41700852019-09-26 17:01:43 -07002309
Joe Dragoa72da5b2020-06-15 19:40:17 -07002310 const avifProperty * ispeProp = avifPropertyArrayFind(colorProperties, "ispe");
2311 if (ispeProp) {
2312 decoder->image->width = ispeProp->u.ispe.width;
2313 decoder->image->height = ispeProp->u.ispe.height;
Joe Drago41700852019-09-26 17:01:43 -07002314 } else {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002315 decoder->image->width = 0;
2316 decoder->image->height = 0;
Joe Drago41700852019-09-26 17:01:43 -07002317 }
Joe Dragoc554f5f2020-06-09 18:59:51 -07002318 decoder->alphaPresent = (alphaOBUItem != NULL);
Joe Drago00bcaaf2020-06-05 15:29:38 -07002319 }
2320
Joe Drago11f2a5e2020-07-06 10:49:00 -07002321 // Sanity check tiles
2322 for (uint32_t tileIndex = 0; tileIndex < data->tiles.count; ++tileIndex) {
2323 avifTile * tile = &data->tiles.tile[tileIndex];
2324 for (uint32_t sampleIndex = 0; sampleIndex < tile->input->samples.count; ++sampleIndex) {
Joe Drago043311b2020-07-06 16:48:41 -07002325 avifDecodeSample * sample = &tile->input->samples.sample[sampleIndex];
Joe Drago11f2a5e2020-07-06 10:49:00 -07002326 if (!sample->data.data || !sample->data.size) {
2327 // Every sample must have some data
2328 return AVIF_RESULT_BMFF_PARSE_FAILED;
2329 }
2330 }
2331 }
2332
Joe Dragoa72da5b2020-06-15 19:40:17 -07002333 const avifProperty * colrProp = avifPropertyArrayFind(colorProperties, "colr");
2334 if (colrProp) {
2335 if (colrProp->u.colr.hasICC) {
2336 avifImageSetProfileICC(decoder->image, colrProp->u.colr.icc, colrProp->u.colr.iccSize);
2337 } else if (colrProp->u.colr.hasNCLX) {
2338 data->cicpSet = AVIF_TRUE;
2339 decoder->image->colorPrimaries = colrProp->u.colr.colorPrimaries;
2340 decoder->image->transferCharacteristics = colrProp->u.colr.transferCharacteristics;
2341 decoder->image->matrixCoefficients = colrProp->u.colr.matrixCoefficients;
2342 decoder->image->yuvRange = colrProp->u.colr.range;
2343 }
2344 }
2345
2346 // Transformations
2347 const avifProperty * paspProp = avifPropertyArrayFind(colorProperties, "pasp");
2348 if (paspProp) {
2349 decoder->image->transformFlags |= AVIF_TRANSFORM_PASP;
2350 memcpy(&decoder->image->pasp, &paspProp->u.pasp, sizeof(avifPixelAspectRatioBox));
2351 }
2352 const avifProperty * clapProp = avifPropertyArrayFind(colorProperties, "clap");
2353 if (clapProp) {
2354 decoder->image->transformFlags |= AVIF_TRANSFORM_CLAP;
2355 memcpy(&decoder->image->clap, &clapProp->u.clap, sizeof(avifCleanApertureBox));
2356 }
2357 const avifProperty * irotProp = avifPropertyArrayFind(colorProperties, "irot");
2358 if (irotProp) {
2359 decoder->image->transformFlags |= AVIF_TRANSFORM_IROT;
2360 memcpy(&decoder->image->irot, &irotProp->u.irot, sizeof(avifImageRotation));
2361 }
2362 const avifProperty * imirProp = avifPropertyArrayFind(colorProperties, "imir");
2363 if (imirProp) {
2364 decoder->image->transformFlags |= AVIF_TRANSFORM_IMIR;
2365 memcpy(&decoder->image->imir, &imirProp->u.imir, sizeof(avifImageMirror));
2366 }
2367
Joe Dragoda92a382020-06-09 17:08:45 -07002368 if (!decoder->data->cicpSet && (data->tiles.count > 0)) {
2369 avifTile * firstTile = &data->tiles.tile[0];
2370 if (firstTile->input->samples.count > 0) {
2371 avifDecodeSample * sample = &firstTile->input->samples.sample[0];
2372 avifSequenceHeader sequenceHeader;
2373 if (avifSequenceHeaderParse(&sequenceHeader, &sample->data)) {
2374 decoder->data->cicpSet = AVIF_TRUE;
2375 decoder->image->colorPrimaries = sequenceHeader.colorPrimaries;
2376 decoder->image->transferCharacteristics = sequenceHeader.transferCharacteristics;
2377 decoder->image->matrixCoefficients = sequenceHeader.matrixCoefficients;
Joe Dragoc1d18952020-06-09 18:24:36 -07002378 decoder->image->yuvRange = sequenceHeader.range;
Joe Dragoda92a382020-06-09 17:08:45 -07002379 }
2380 }
2381 }
2382
Joe Dragoa72da5b2020-06-15 19:40:17 -07002383 const avifProperty * av1CProp = avifPropertyArrayFind(colorProperties, "av1C");
2384 if (av1CProp) {
Joe Dragob8401122020-06-19 11:45:49 -07002385 decoder->image->depth = avifCodecConfigurationBoxGetDepth(&av1CProp->u.av1C);
2386 if (av1CProp->u.av1C.monochrome) {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002387 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV400;
Joe Drago7b2cf802020-06-09 17:57:23 -07002388 } else {
Joe Dragob8401122020-06-19 11:45:49 -07002389 if (av1CProp->u.av1C.chromaSubsamplingX && av1CProp->u.av1C.chromaSubsamplingY) {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002390 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV420;
Joe Dragob8401122020-06-19 11:45:49 -07002391 } else if (av1CProp->u.av1C.chromaSubsamplingX) {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002392 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV422;
Joe Drago7b2cf802020-06-09 17:57:23 -07002393
2394 } else {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002395 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV444;
Joe Drago7b2cf802020-06-09 17:57:23 -07002396 }
2397 }
Joe Dragob8401122020-06-19 11:45:49 -07002398 decoder->image->yuvChromaSamplePosition = (avifChromaSamplePosition)av1CProp->u.av1C.chromaSamplePosition;
Joe Drago00bcaaf2020-06-05 15:29:38 -07002399 } else {
Joe Dragof48a3382020-06-19 14:13:44 -07002400 // An av1C box is mandatory in all valid AVIF configurations. Bail out.
2401 return AVIF_RESULT_BMFF_PARSE_FAILED;
Joe Drago46ea0582019-07-22 15:55:47 -07002402 }
2403
Joe Drago22c1ad92019-09-26 12:46:50 -07002404 return avifDecoderFlush(decoder);
Joe Drago46ea0582019-07-22 15:55:47 -07002405}
Joe Drago444f0512019-01-23 17:03:24 -08002406
Joe Drago46ea0582019-07-22 15:55:47 -07002407avifResult avifDecoderNextImage(avifDecoder * decoder)
2408{
Joe Drago060d5342020-03-03 10:53:49 -08002409 for (unsigned int tileIndex = 0; tileIndex < decoder->data->tiles.count; ++tileIndex) {
2410 avifTile * tile = &decoder->data->tiles.tile[tileIndex];
Joe Drago41eb62b2019-02-08 15:38:18 -08002411
Joe Drago060d5342020-03-03 10:53:49 -08002412 if (!tile->codec->getNextImage(tile->codec, tile->image)) {
2413 if (tile->input->alpha) {
2414 return AVIF_RESULT_DECODE_ALPHA_FAILED;
2415 } else {
2416 if (tile->image->width) {
2417 // We've sent at least one image, but we've run out now.
2418 return AVIF_RESULT_NO_IMAGES_REMAINING;
2419 }
2420 return AVIF_RESULT_DECODE_COLOR_FAILED;
2421 }
Joe Drago46ea0582019-07-22 15:55:47 -07002422 }
Joe Drago060d5342020-03-03 10:53:49 -08002423 }
2424
2425 if (decoder->data->tiles.count != (decoder->data->colorTileCount + decoder->data->alphaTileCount)) {
2426 // TODO: assert here? This should be impossible.
2427 return AVIF_RESULT_UNKNOWN_ERROR;
2428 }
2429
2430 if ((decoder->data->colorGrid.rows > 0) || (decoder->data->colorGrid.columns > 0)) {
Joe Drago800b47f2020-03-18 16:22:37 -07002431 if (!avifDecoderDataFillImageGrid(
2432 decoder->data, &decoder->data->colorGrid, decoder->image, 0, decoder->data->colorTileCount, AVIF_FALSE)) {
Joe Drago060d5342020-03-03 10:53:49 -08002433 return AVIF_RESULT_INVALID_IMAGE_GRID;
2434 }
2435 } else {
2436 // Normal (most common) non-grid path. Just steal the planes from the only "tile".
2437
2438 if (decoder->data->colorTileCount != 1) {
2439 return AVIF_RESULT_DECODE_COLOR_FAILED;
2440 }
2441
2442 avifImage * srcColor = decoder->data->tiles.tile[0].image;
2443
2444 if ((decoder->image->width != srcColor->width) || (decoder->image->height != srcColor->height) ||
2445 (decoder->image->depth != srcColor->depth)) {
2446 avifImageFreePlanes(decoder->image, AVIF_PLANES_ALL);
2447
2448 decoder->image->width = srcColor->width;
2449 decoder->image->height = srcColor->height;
2450 decoder->image->depth = srcColor->depth;
Joe Dragoc554f5f2020-06-09 18:59:51 -07002451 }
psi / Ryo Hirafuji922d8a12020-03-10 03:24:57 +09002452
Joe Dragoba1eb492020-06-22 17:05:04 -07002453#if 0
2454 // This code is currently unnecessary as the CICP is always set by the end of avifDecoderParse().
Joe Dragoc554f5f2020-06-09 18:59:51 -07002455 if (!decoder->data->cicpSet) {
2456 decoder->data->cicpSet = AVIF_TRUE;
2457 decoder->image->colorPrimaries = srcColor->colorPrimaries;
2458 decoder->image->transferCharacteristics = srcColor->transferCharacteristics;
2459 decoder->image->matrixCoefficients = srcColor->matrixCoefficients;
Joe Drago060d5342020-03-03 10:53:49 -08002460 }
Joe Dragoba1eb492020-06-22 17:05:04 -07002461#endif
Joe Drago060d5342020-03-03 10:53:49 -08002462
2463 avifImageStealPlanes(decoder->image, srcColor, AVIF_PLANES_YUV);
2464 }
2465
2466 if ((decoder->data->alphaGrid.rows > 0) || (decoder->data->alphaGrid.columns > 0)) {
Joe Drago800b47f2020-03-18 16:22:37 -07002467 if (!avifDecoderDataFillImageGrid(
Joe Drago060d5342020-03-03 10:53:49 -08002468 decoder->data, &decoder->data->alphaGrid, decoder->image, decoder->data->colorTileCount, decoder->data->alphaTileCount, AVIF_TRUE)) {
2469 return AVIF_RESULT_INVALID_IMAGE_GRID;
2470 }
2471 } else {
2472 // Normal (most common) non-grid path. Just steal the planes from the only "tile".
2473
2474 if (decoder->data->alphaTileCount == 0) {
2475 avifImageFreePlanes(decoder->image, AVIF_PLANES_A); // no alpha
2476 } else {
2477 if (decoder->data->alphaTileCount != 1) {
2478 return AVIF_RESULT_DECODE_ALPHA_FAILED;
2479 }
2480
2481 avifImage * srcAlpha = decoder->data->tiles.tile[decoder->data->colorTileCount].image;
2482 if ((decoder->image->width != srcAlpha->width) || (decoder->image->height != srcAlpha->height) ||
2483 (decoder->image->depth != srcAlpha->depth)) {
2484 return AVIF_RESULT_DECODE_ALPHA_FAILED;
2485 }
2486
2487 avifImageStealPlanes(decoder->image, srcAlpha, AVIF_PLANES_A);
2488 }
2489 }
Joe Drago7ad3ad62019-02-07 11:17:34 -08002490
Joe Drago46ea0582019-07-22 15:55:47 -07002491 ++decoder->imageIndex;
2492 if (decoder->data->sourceSampleTable) {
2493 // Decoding from a track! Provide timing information.
Joe Drago05559c92019-07-17 16:33:38 -07002494
Joe Dragoe9c58602020-04-13 17:23:13 -07002495 avifResult timingResult = avifDecoderNthImageTiming(decoder, decoder->imageIndex, &decoder->imageTiming);
2496 if (timingResult != AVIF_RESULT_OK) {
2497 return timingResult;
Joe Drago22c1ad92019-09-26 12:46:50 -07002498 }
Joe Dragoe9c58602020-04-13 17:23:13 -07002499 }
2500 return AVIF_RESULT_OK;
2501}
Joe Drago46ea0582019-07-22 15:55:47 -07002502
Wan-Teh Change184dc12020-05-11 12:47:21 -07002503avifResult avifDecoderNthImageTiming(const avifDecoder * decoder, uint32_t frameIndex, avifImageTiming * outTiming)
Joe Dragoe9c58602020-04-13 17:23:13 -07002504{
2505 if (!decoder->data) {
2506 // Nothing has been parsed yet
2507 return AVIF_RESULT_NO_CONTENT;
2508 }
2509
2510 if ((int)frameIndex >= decoder->imageCount) {
2511 // Impossible index
2512 return AVIF_RESULT_NO_IMAGES_REMAINING;
2513 }
2514
2515 if (!decoder->data->sourceSampleTable) {
2516 // There isn't any real timing associated with this decode, so
2517 // just hand back the defaults chosen in avifDecoderReset().
2518 memcpy(outTiming, &decoder->imageTiming, sizeof(avifImageTiming));
2519 return AVIF_RESULT_OK;
2520 }
2521
Wan-Teh Chang167e4062020-04-14 16:06:12 -07002522 outTiming->timescale = decoder->timescale;
2523 outTiming->ptsInTimescales = 0;
Joe Dragoe9c58602020-04-13 17:23:13 -07002524 for (int imageIndex = 0; imageIndex < (int)frameIndex; ++imageIndex) {
Wan-Teh Chang167e4062020-04-14 16:06:12 -07002525 outTiming->ptsInTimescales += avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, imageIndex);
Joe Dragoe9c58602020-04-13 17:23:13 -07002526 }
Wan-Teh Chang167e4062020-04-14 16:06:12 -07002527 outTiming->durationInTimescales = avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, frameIndex);
Joe Dragoe9c58602020-04-13 17:23:13 -07002528
Wan-Teh Chang167e4062020-04-14 16:06:12 -07002529 if (outTiming->timescale > 0) {
2530 outTiming->pts = (double)outTiming->ptsInTimescales / (double)outTiming->timescale;
2531 outTiming->duration = (double)outTiming->durationInTimescales / (double)outTiming->timescale;
Joe Dragoe9c58602020-04-13 17:23:13 -07002532 } else {
Wan-Teh Chang167e4062020-04-14 16:06:12 -07002533 outTiming->pts = 0.0;
2534 outTiming->duration = 0.0;
Joe Drago444f0512019-01-23 17:03:24 -08002535 }
Joe Drago46ea0582019-07-22 15:55:47 -07002536 return AVIF_RESULT_OK;
2537}
2538
Joe Drago22c1ad92019-09-26 12:46:50 -07002539avifResult avifDecoderNthImage(avifDecoder * decoder, uint32_t frameIndex)
2540{
2541 int requestedIndex = (int)frameIndex;
2542 if (requestedIndex == decoder->imageIndex) {
2543 // We're here already, nothing to do
2544 return AVIF_RESULT_OK;
2545 }
2546
2547 if (requestedIndex == (decoder->imageIndex + 1)) {
2548 // it's just the next image, nothing special here
2549 return avifDecoderNextImage(decoder);
2550 }
2551
2552 if (requestedIndex >= decoder->imageCount) {
2553 // Impossible index
2554 return AVIF_RESULT_NO_IMAGES_REMAINING;
2555 }
2556
2557 // If we get here, a decoder flush is necessary
Joe Drago22c1ad92019-09-26 12:46:50 -07002558 decoder->imageIndex = ((int)avifDecoderNearestKeyframe(decoder, frameIndex)) - 1; // prepare to read nearest keyframe
Joe Drago81978022019-09-26 18:23:57 -07002559 avifDecoderFlush(decoder);
Joe Drago22c1ad92019-09-26 12:46:50 -07002560 for (;;) {
2561 avifResult result = avifDecoderNextImage(decoder);
2562 if (result != AVIF_RESULT_OK) {
2563 return result;
2564 }
2565
2566 if (requestedIndex == decoder->imageIndex) {
2567 break;
2568 }
Joe Dragofc4144e2019-09-27 20:35:06 -07002569 }
Joe Drago22c1ad92019-09-26 12:46:50 -07002570 return AVIF_RESULT_OK;
2571}
2572
Wan-Teh Change184dc12020-05-11 12:47:21 -07002573avifBool avifDecoderIsKeyframe(const avifDecoder * decoder, uint32_t frameIndex)
Joe Drago22c1ad92019-09-26 12:46:50 -07002574{
Joe Drago060d5342020-03-03 10:53:49 -08002575 if ((decoder->data->tiles.count > 0) && decoder->data->tiles.tile[0].input) {
2576 if (frameIndex < decoder->data->tiles.tile[0].input->samples.count) {
2577 return decoder->data->tiles.tile[0].input->samples.sample[frameIndex].sync;
Joe Drago22c1ad92019-09-26 12:46:50 -07002578 }
2579 }
2580 return AVIF_FALSE;
2581}
2582
Wan-Teh Change184dc12020-05-11 12:47:21 -07002583uint32_t avifDecoderNearestKeyframe(const avifDecoder * decoder, uint32_t frameIndex)
Joe Drago22c1ad92019-09-26 12:46:50 -07002584{
2585 for (; frameIndex != 0; --frameIndex) {
2586 if (avifDecoderIsKeyframe(decoder, frameIndex)) {
2587 break;
2588 }
2589 }
2590 return frameIndex;
2591}
2592
Wan-Teh Change184dc12020-05-11 12:47:21 -07002593avifResult avifDecoderRead(avifDecoder * decoder, avifImage * image, const avifROData * input)
Joe Drago46ea0582019-07-22 15:55:47 -07002594{
2595 avifResult result = avifDecoderParse(decoder, input);
2596 if (result != AVIF_RESULT_OK) {
2597 return result;
Joe Drago05559c92019-07-17 16:33:38 -07002598 }
Joe Drago46ea0582019-07-22 15:55:47 -07002599 result = avifDecoderNextImage(decoder);
2600 if (result != AVIF_RESULT_OK) {
2601 return result;
2602 }
Joe Drago250221a2020-06-01 11:11:06 -07002603 avifImageCopy(image, decoder->image, AVIF_PLANES_ALL);
Joe Drago46ea0582019-07-22 15:55:47 -07002604 return AVIF_RESULT_OK;
Joe Drago444f0512019-01-23 17:03:24 -08002605}