blob: a4517c073b2b6b84747331455b9bec4c3450d10f [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) ||
wantehchangbc35a5f2020-08-12 15:27:39 -0700695 (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
wantehchangdf586a82020-08-12 13:06:08 -0700702 // Validate grid image size and tile size.
703 //
704 // HEIF (ISO/IEC 23008-12:2017), Section 6.6.2.3.1:
705 // The tiled input images shall completely “cover” the reconstructed image grid canvas, ...
wantehchangbc35a5f2020-08-12 15:27:39 -0700706 if (((firstTile->image->width * grid->columns) < grid->outputWidth) ||
707 ((firstTile->image->height * grid->rows) < grid->outputHeight)) {
wantehchangdf586a82020-08-12 13:06:08 -0700708 return AVIF_FALSE;
709 }
710 // Tiles in the rightmost column and bottommost row must overlap the reconstructed image grid canvas. See MIAF (ISO/IEC 23000-22:2019), Section 7.3.11.4.2, Figure 2.
wantehchangbc35a5f2020-08-12 15:27:39 -0700711 if (((firstTile->image->width * (grid->columns - 1)) >= grid->outputWidth) ||
712 ((firstTile->image->height * (grid->rows - 1)) >= grid->outputHeight)) {
wantehchangdf586a82020-08-12 13:06:08 -0700713 return AVIF_FALSE;
714 }
715 // Check the restrictions in MIAF (ISO/IEC 23000-22:2019), Section 7.3.11.4.2.
716 //
717 // The tile_width shall be greater than or equal to 64, and the tile_height shall be greater than or equal to 64.
wantehchangbc35a5f2020-08-12 15:27:39 -0700718 if ((firstTile->image->width < 64) || (firstTile->image->height < 64)) {
wantehchangdf586a82020-08-12 13:06:08 -0700719 return AVIF_FALSE;
720 }
721 if (!alpha) {
wantehchangbc35a5f2020-08-12 15:27:39 -0700722 if ((firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV422) || (firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV420)) {
wantehchangdf586a82020-08-12 13:06:08 -0700723 // The horizontal tile offsets and widths, and the output width, shall be even numbers.
wantehchangbc35a5f2020-08-12 15:27:39 -0700724 if (((firstTile->image->width & 1) != 0) || ((grid->outputWidth & 1) != 0)) {
wantehchangdf586a82020-08-12 13:06:08 -0700725 return AVIF_FALSE;
726 }
727 }
728 if (firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV420) {
729 // The vertical tile offsets and heights, and the output height, shall be even numbers.
wantehchangbc35a5f2020-08-12 15:27:39 -0700730 if (((firstTile->image->height & 1) != 0) || ((grid->outputHeight & 1) != 0)) {
wantehchangdf586a82020-08-12 13:06:08 -0700731 return AVIF_FALSE;
732 }
733 }
734 }
735
Joe Dragoa0da4a42020-05-08 14:27:40 -0700736 // Lazily populate dstImage with the new frame's properties. If we're decoding alpha,
737 // these values must already match.
738 if ((dstImage->width != grid->outputWidth) || (dstImage->height != grid->outputHeight) ||
739 (dstImage->depth != firstTile->image->depth) || (dstImage->yuvFormat != firstTile->image->yuvFormat)) {
Joe Drago060d5342020-03-03 10:53:49 -0800740 if (alpha) {
741 // Alpha doesn't match size, just bail out
742 return AVIF_FALSE;
743 }
744
745 avifImageFreePlanes(dstImage, AVIF_PLANES_ALL);
746 dstImage->width = grid->outputWidth;
747 dstImage->height = grid->outputHeight;
Joe Dragoa0da4a42020-05-08 14:27:40 -0700748 dstImage->depth = firstTile->image->depth;
749 dstImage->yuvFormat = firstTile->image->yuvFormat;
750 dstImage->yuvRange = firstTile->image->yuvRange;
751 if (!data->cicpSet) {
752 data->cicpSet = AVIF_TRUE;
753 dstImage->colorPrimaries = firstTile->image->colorPrimaries;
754 dstImage->transferCharacteristics = firstTile->image->transferCharacteristics;
755 dstImage->matrixCoefficients = firstTile->image->matrixCoefficients;
psi / Ryo Hirafuji922d8a12020-03-10 03:24:57 +0900756 }
Joe Drago060d5342020-03-03 10:53:49 -0800757 }
Joe Dragod0eeb182020-05-18 17:23:48 -0700758 if (alpha) {
759 dstImage->alphaRange = firstTile->image->alphaRange;
760 }
Joe Drago060d5342020-03-03 10:53:49 -0800761
762 avifImageAllocatePlanes(dstImage, alpha ? AVIF_PLANES_A : AVIF_PLANES_YUV);
763
764 avifPixelFormatInfo formatInfo;
Joe Dragoa0da4a42020-05-08 14:27:40 -0700765 avifGetPixelFormatInfo(firstTile->image->yuvFormat, &formatInfo);
Joe Drago060d5342020-03-03 10:53:49 -0800766
767 unsigned int tileIndex = firstTileIndex;
768 size_t pixelBytes = avifImageUsesU16(dstImage) ? 2 : 1;
769 for (unsigned int rowIndex = 0; rowIndex < grid->rows; ++rowIndex) {
770 for (unsigned int colIndex = 0; colIndex < grid->columns; ++colIndex, ++tileIndex) {
771 avifTile * tile = &data->tiles.tile[tileIndex];
772
Joe Dragoa0da4a42020-05-08 14:27:40 -0700773 unsigned int widthToCopy = firstTile->image->width;
774 unsigned int maxX = firstTile->image->width * (colIndex + 1);
Joe Drago060d5342020-03-03 10:53:49 -0800775 if (maxX > grid->outputWidth) {
776 widthToCopy -= maxX - grid->outputWidth;
777 }
778
Joe Dragoa0da4a42020-05-08 14:27:40 -0700779 unsigned int heightToCopy = firstTile->image->height;
780 unsigned int maxY = firstTile->image->height * (rowIndex + 1);
Joe Drago060d5342020-03-03 10:53:49 -0800781 if (maxY > grid->outputHeight) {
782 heightToCopy -= maxY - grid->outputHeight;
783 }
784
785 // Y and A channels
Joe Dragoa0da4a42020-05-08 14:27:40 -0700786 size_t yaColOffset = colIndex * firstTile->image->width;
787 size_t yaRowOffset = rowIndex * firstTile->image->height;
Joe Drago060d5342020-03-03 10:53:49 -0800788 size_t yaRowBytes = widthToCopy * pixelBytes;
789
790 if (alpha) {
791 // A
792 for (unsigned int j = 0; j < heightToCopy; ++j) {
793 uint8_t * src = &tile->image->alphaPlane[j * tile->image->alphaRowBytes];
794 uint8_t * dst = &dstImage->alphaPlane[(yaColOffset * pixelBytes) + ((yaRowOffset + j) * dstImage->alphaRowBytes)];
795 memcpy(dst, src, yaRowBytes);
796 }
797 } else {
798 // Y
799 for (unsigned int j = 0; j < heightToCopy; ++j) {
800 uint8_t * src = &tile->image->yuvPlanes[AVIF_CHAN_Y][j * tile->image->yuvRowBytes[AVIF_CHAN_Y]];
801 uint8_t * dst =
802 &dstImage->yuvPlanes[AVIF_CHAN_Y][(yaColOffset * pixelBytes) + ((yaRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_Y])];
803 memcpy(dst, src, yaRowBytes);
804 }
805
Joe Dragoa0da4a42020-05-08 14:27:40 -0700806 if (!firstTileUVPresent) {
Joe Drago060d5342020-03-03 10:53:49 -0800807 continue;
808 }
809
810 // UV
Joe Drago060d5342020-03-03 10:53:49 -0800811 heightToCopy >>= formatInfo.chromaShiftY;
812 size_t uvColOffset = yaColOffset >> formatInfo.chromaShiftX;
813 size_t uvRowOffset = yaRowOffset >> formatInfo.chromaShiftY;
814 size_t uvRowBytes = yaRowBytes >> formatInfo.chromaShiftX;
815 for (unsigned int j = 0; j < heightToCopy; ++j) {
816 uint8_t * srcU = &tile->image->yuvPlanes[AVIF_CHAN_U][j * tile->image->yuvRowBytes[AVIF_CHAN_U]];
817 uint8_t * dstU =
818 &dstImage->yuvPlanes[AVIF_CHAN_U][(uvColOffset * pixelBytes) + ((uvRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_U])];
819 memcpy(dstU, srcU, uvRowBytes);
820
821 uint8_t * srcV = &tile->image->yuvPlanes[AVIF_CHAN_V][j * tile->image->yuvRowBytes[AVIF_CHAN_V]];
822 uint8_t * dstV =
823 &dstImage->yuvPlanes[AVIF_CHAN_V][(uvColOffset * pixelBytes) + ((uvRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_V])];
824 memcpy(dstV, srcV, uvRowBytes);
825 }
826 }
827 }
828 }
829
830 return AVIF_TRUE;
831}
832
Joe Drago39267fd2020-06-19 15:21:14 -0700833// If colorId == 0 (a sentinel value as item IDs must be nonzero), accept any found EXIF/XMP metadata. Passing in 0
834// 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 -0700835// inside of a trak box are implicitly associated to the track.
Joe Drago85050a32020-06-15 20:05:37 -0700836static avifBool avifDecoderDataFindMetadata(avifDecoderData * data, avifMeta * meta, avifImage * image, uint32_t colorId)
Joe Dragoa72da5b2020-06-15 19:40:17 -0700837{
838 avifROData exifData = AVIF_DATA_EMPTY;
839 avifROData xmpData = AVIF_DATA_EMPTY;
840
841 for (uint32_t itemIndex = 0; itemIndex < meta->items.count; ++itemIndex) {
842 avifDecoderItem * item = &meta->items.item[itemIndex];
Joe Dragoba1eb492020-06-22 17:05:04 -0700843 if (!item->size) {
844 continue;
Joe Dragoa72da5b2020-06-15 19:40:17 -0700845 }
846 if (item->hasUnsupportedEssentialProperty) {
847 // An essential property isn't supported by libavif; ignore the item.
848 continue;
849 }
850
851 if ((colorId > 0) && (item->descForID != colorId)) {
852 // Not a content description (metadata) for the colorOBU, skip it
853 continue;
854 }
855
856 if (!memcmp(item->type, "Exif", 4)) {
857 // Advance past Annex A.2.1's header
858 const uint8_t * boxPtr = avifDecoderDataCalcItemPtr(data, item);
859 BEGIN_STREAM(exifBoxStream, boxPtr, item->size);
860 uint32_t exifTiffHeaderOffset;
861 CHECK(avifROStreamReadU32(&exifBoxStream, &exifTiffHeaderOffset)); // unsigned int(32) exif_tiff_header_offset;
862
863 exifData.data = avifROStreamCurrent(&exifBoxStream);
864 exifData.size = avifROStreamRemainingBytes(&exifBoxStream);
Joe Dragoba1eb492020-06-22 17:05:04 -0700865 } else if (!memcmp(item->type, "mime", 4) && !memcmp(item->contentType.contentType, xmpContentType, xmpContentTypeSize)) {
Joe Dragoa72da5b2020-06-15 19:40:17 -0700866 xmpData.data = avifDecoderDataCalcItemPtr(data, item);
867 xmpData.size = item->size;
868 }
869 }
870
871 if (exifData.data && exifData.size) {
872 avifImageSetMetadataExif(image, exifData.data, exifData.size);
873 }
874 if (xmpData.data && xmpData.size) {
875 avifImageSetMetadataXMP(image, xmpData.data, xmpData.size);
876 }
877 return AVIF_TRUE;
878}
879
Joe Drago8f7a3002019-02-07 19:35:37 -0800880// ---------------------------------------------------------------------------
Joe Dragocd1e4c32019-02-08 11:26:31 -0800881// URN
882
Joe Dragoa72da5b2020-06-15 19:40:17 -0700883static avifBool isAlphaURN(const char * urn)
Joe Dragocd1e4c32019-02-08 11:26:31 -0800884{
wantehchang3fde0d02020-03-10 23:58:32 -0700885 return !strcmp(urn, URN_ALPHA0) || !strcmp(urn, URN_ALPHA1);
Joe Dragocd1e4c32019-02-08 11:26:31 -0800886}
887
888// ---------------------------------------------------------------------------
Joe Drago8f7a3002019-02-07 19:35:37 -0800889// BMFF Parsing
890
Joe Drago9f2b87b2020-06-03 19:36:38 -0700891static avifBool avifParseItemLocationBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago444f0512019-01-23 17:03:24 -0800892{
Joe Drago8f7a3002019-02-07 19:35:37 -0800893 BEGIN_STREAM(s, raw, rawLen);
Joe Drago444f0512019-01-23 17:03:24 -0800894
Joe Dragof6a42272019-11-21 15:21:41 -0800895 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -0700896 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Dragof6a42272019-11-21 15:21:41 -0800897 if (version > 2) {
898 return AVIF_FALSE;
899 }
Joe Drago444f0512019-01-23 17:03:24 -0800900
Joe Drago8f7a3002019-02-07 19:35:37 -0800901 uint8_t offsetSizeAndLengthSize;
Joe Drago345aaa12019-09-25 13:42:12 -0700902 CHECK(avifROStreamRead(&s, &offsetSizeAndLengthSize, 1));
Joe Drago8f7a3002019-02-07 19:35:37 -0800903 uint8_t offsetSize = (offsetSizeAndLengthSize >> 4) & 0xf; // unsigned int(4) offset_size;
904 uint8_t lengthSize = (offsetSizeAndLengthSize >> 0) & 0xf; // unsigned int(4) length_size;
Joe Drago444f0512019-01-23 17:03:24 -0800905
Joe Dragof6a42272019-11-21 15:21:41 -0800906 uint8_t baseOffsetSizeAndIndexSize;
907 CHECK(avifROStreamRead(&s, &baseOffsetSizeAndIndexSize, 1));
908 uint8_t baseOffsetSize = (baseOffsetSizeAndIndexSize >> 4) & 0xf; // unsigned int(4) base_offset_size;
909 uint8_t indexSize = 0;
910 if ((version == 1) || (version == 2)) {
911 indexSize = baseOffsetSizeAndIndexSize & 0xf; // unsigned int(4) index_size;
912 if (indexSize != 0) {
913 // extent_index unsupported
914 return AVIF_FALSE;
915 }
916 }
Joe Drago444f0512019-01-23 17:03:24 -0800917
Joe Dragof6a42272019-11-21 15:21:41 -0800918 uint16_t tmp16;
919 uint32_t itemCount;
920 if (version < 2) {
921 CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_count;
922 itemCount = tmp16;
923 } else {
924 CHECK(avifROStreamReadU32(&s, &itemCount)); // unsigned int(32) item_count;
925 }
926 for (uint32_t i = 0; i < itemCount; ++i) {
927 uint32_t itemID;
928 uint32_t idatID = 0;
929 if (version < 2) {
930 CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID;
931 itemID = tmp16;
932 } else {
933 CHECK(avifROStreamReadU32(&s, &itemID)); // unsigned int(32) item_ID;
934 }
935
936 if ((version == 1) || (version == 2)) {
937 uint8_t ignored;
938 uint8_t constructionMethod;
939 CHECK(avifROStreamRead(&s, &ignored, 1)); // unsigned int(12) reserved = 0;
940 CHECK(avifROStreamRead(&s, &constructionMethod, 1)); // unsigned int(4) construction_method;
941 constructionMethod = constructionMethod & 0xf;
942 if ((constructionMethod != 0 /* file */) && (constructionMethod != 1 /* idat */)) {
943 // construction method item(2) unsupported
944 return AVIF_FALSE;
945 }
946 if (constructionMethod == 1) {
Joe Dragoba1eb492020-06-22 17:05:04 -0700947 idatID = meta->idatID;
Joe Dragof6a42272019-11-21 15:21:41 -0800948 }
949 }
950
Joe Drago345aaa12019-09-25 13:42:12 -0700951 uint16_t dataReferenceIndex; // unsigned int(16) data_ref rence_index;
952 CHECK(avifROStreamReadU16(&s, &dataReferenceIndex)); //
953 uint64_t baseOffset; // unsigned int(base_offset_size*8) base_offset;
954 CHECK(avifROStreamReadUX8(&s, &baseOffset, baseOffsetSize)); //
955 uint16_t extentCount; // unsigned int(16) extent_count;
956 CHECK(avifROStreamReadU16(&s, &extentCount)); //
Joe Drago8f7a3002019-02-07 19:35:37 -0800957 if (extentCount == 1) {
Joe Dragof6a42272019-11-21 15:21:41 -0800958 // If extent_index is ever supported, this spec must be implemented here:
959 // :: if (((version == 1) || (version == 2)) && (index_size > 0)) {
960 // :: unsigned int(index_size*8) extent_index;
961 // :: }
962
Joe Drago8f7a3002019-02-07 19:35:37 -0800963 uint64_t extentOffset; // unsigned int(offset_size*8) extent_offset;
Joe Drago345aaa12019-09-25 13:42:12 -0700964 CHECK(avifROStreamReadUX8(&s, &extentOffset, offsetSize));
Joe Drago8f7a3002019-02-07 19:35:37 -0800965 uint64_t extentLength; // unsigned int(offset_size*8) extent_length;
Joe Drago345aaa12019-09-25 13:42:12 -0700966 CHECK(avifROStreamReadUX8(&s, &extentLength, lengthSize));
Joe Drago444f0512019-01-23 17:03:24 -0800967
Joe Drago9f2b87b2020-06-03 19:36:38 -0700968 avifDecoderItem * item = avifMetaFindItem(meta, itemID);
Joe Drago46ea0582019-07-22 15:55:47 -0700969 if (!item) {
970 return AVIF_FALSE;
971 }
Joe Drago05559c92019-07-17 16:33:38 -0700972 item->id = itemID;
973 item->offset = (uint32_t)(baseOffset + extentOffset);
974 item->size = (uint32_t)extentLength;
Joe Dragof6a42272019-11-21 15:21:41 -0800975 item->idatID = idatID;
Joe Drago444f0512019-01-23 17:03:24 -0800976 } else {
Joe Drago8f7a3002019-02-07 19:35:37 -0800977 // TODO: support more than one extent
978 return AVIF_FALSE;
Joe Drago444f0512019-01-23 17:03:24 -0800979 }
980 }
981 return AVIF_TRUE;
982}
983
Joe Drago060d5342020-03-03 10:53:49 -0800984static avifBool avifParseImageGridBox(avifImageGrid * grid, const uint8_t * raw, size_t rawLen)
985{
986 BEGIN_STREAM(s, raw, rawLen);
987
988 uint8_t version, flags;
989 CHECK(avifROStreamRead(&s, &version, 1)); // unsigned int(8) version = 0;
990 if (version != 0) {
991 return AVIF_FALSE;
992 }
993 CHECK(avifROStreamRead(&s, &flags, 1)); // unsigned int(8) flags;
994 CHECK(avifROStreamRead(&s, &grid->rows, 1)); // unsigned int(8) rows_minus_one;
995 CHECK(avifROStreamRead(&s, &grid->columns, 1)); // unsigned int(8) columns_minus_one;
996 ++grid->rows;
997 ++grid->columns;
998
999 uint32_t fieldLength = ((flags & 1) + 1) * 16;
1000 if (fieldLength == 16) {
1001 uint16_t outputWidth16, outputHeight16;
1002 CHECK(avifROStreamReadU16(&s, &outputWidth16)); // unsigned int(FieldLength) output_width;
1003 CHECK(avifROStreamReadU16(&s, &outputHeight16)); // unsigned int(FieldLength) output_height;
1004 grid->outputWidth = outputWidth16;
1005 grid->outputHeight = outputHeight16;
1006 } else {
1007 if (fieldLength != 32) {
1008 // This should be impossible
1009 return AVIF_FALSE;
1010 }
1011 CHECK(avifROStreamReadU32(&s, &grid->outputWidth)); // unsigned int(FieldLength) output_width;
1012 CHECK(avifROStreamReadU32(&s, &grid->outputHeight)); // unsigned int(FieldLength) output_height;
1013 }
wantehchangbc35a5f2020-08-12 15:27:39 -07001014 if ((grid->outputWidth == 0) || (grid->outputHeight == 0) || (grid->outputWidth > (AVIF_MAX_IMAGE_SIZE / grid->outputHeight))) {
Wan-Teh Chang0a8e7242020-08-10 13:24:59 -07001015 return AVIF_FALSE;
1016 }
wantehchangae2074b2020-08-12 13:02:27 -07001017 return avifROStreamRemainingBytes(&s) == 0;
Joe Drago060d5342020-03-03 10:53:49 -08001018}
1019
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001020static avifBool avifParseImageSpatialExtentsProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001021{
1022 BEGIN_STREAM(s, raw, rawLen);
Joe Drago345aaa12019-09-25 13:42:12 -07001023 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Drago8f7a3002019-02-07 19:35:37 -08001024
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001025 avifImageSpatialExtents * ispe = &prop->u.ispe;
1026 CHECK(avifROStreamReadU32(&s, &ispe->width));
1027 CHECK(avifROStreamReadU32(&s, &ispe->height));
Joe Drago8f7a3002019-02-07 19:35:37 -08001028 return AVIF_TRUE;
1029}
1030
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001031static avifBool avifParseAuxiliaryTypeProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Dragocd1e4c32019-02-08 11:26:31 -08001032{
1033 BEGIN_STREAM(s, raw, rawLen);
Joe Drago345aaa12019-09-25 13:42:12 -07001034 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragocd1e4c32019-02-08 11:26:31 -08001035
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001036 CHECK(avifROStreamReadString(&s, prop->u.auxC.auxType, AUXTYPE_SIZE));
Joe Dragocd1e4c32019-02-08 11:26:31 -08001037 return AVIF_TRUE;
1038}
1039
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001040static avifBool avifParseColourInformationBox(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago41eb62b2019-02-08 15:38:18 -08001041{
1042 BEGIN_STREAM(s, raw, rawLen);
Joe Dragoe7ce20d2019-02-11 16:37:38 -08001043
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001044 avifColourInformationBox * colr = &prop->u.colr;
1045 colr->hasICC = AVIF_FALSE;
1046 colr->hasNCLX = AVIF_FALSE;
Joe Dragoe7ce20d2019-02-11 16:37:38 -08001047
Joe Dragoa0da4a42020-05-08 14:27:40 -07001048 uint8_t colorType[4]; // unsigned int(32) colour_type;
1049 CHECK(avifROStreamRead(&s, colorType, 4));
1050 if (!memcmp(colorType, "rICC", 4) || !memcmp(colorType, "prof", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001051 colr->hasICC = AVIF_TRUE;
1052 colr->icc = avifROStreamCurrent(&s);
1053 colr->iccSize = avifROStreamRemainingBytes(&s);
Joe Dragoa0da4a42020-05-08 14:27:40 -07001054 } else if (!memcmp(colorType, "nclx", 4)) {
Joe Drago74cd1c92020-04-16 12:17:11 -07001055 uint16_t tmp16;
Joe Drago97b071c2019-07-17 14:24:56 -07001056 // unsigned int(16) colour_primaries;
Joe Drago74cd1c92020-04-16 12:17:11 -07001057 CHECK(avifROStreamReadU16(&s, &tmp16));
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001058 colr->colorPrimaries = (avifColorPrimaries)tmp16;
Joe Drago97b071c2019-07-17 14:24:56 -07001059 // unsigned int(16) transfer_characteristics;
Joe Drago74cd1c92020-04-16 12:17:11 -07001060 CHECK(avifROStreamReadU16(&s, &tmp16));
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001061 colr->transferCharacteristics = (avifTransferCharacteristics)tmp16;
Joe Drago97b071c2019-07-17 14:24:56 -07001062 // unsigned int(16) matrix_coefficients;
Joe Drago74cd1c92020-04-16 12:17:11 -07001063 CHECK(avifROStreamReadU16(&s, &tmp16));
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001064 colr->matrixCoefficients = (avifMatrixCoefficients)tmp16;
Joe Drago97b071c2019-07-17 14:24:56 -07001065 // unsigned int(1) full_range_flag;
1066 // unsigned int(7) reserved = 0;
Joe Drago74cd1c92020-04-16 12:17:11 -07001067 uint8_t tmp8;
1068 CHECK(avifROStreamRead(&s, &tmp8, 1));
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001069 colr->range = (tmp8 & 0x80) ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED;
1070 colr->hasNCLX = AVIF_TRUE;
Joe Drago41eb62b2019-02-08 15:38:18 -08001071 }
1072 return AVIF_TRUE;
1073}
1074
Joe Drago6500fd62019-10-08 17:17:34 -07001075static avifBool avifParseAV1CodecConfigurationBox(const uint8_t * raw, size_t rawLen, avifCodecConfigurationBox * av1C)
1076{
1077 BEGIN_STREAM(s, raw, rawLen);
1078
1079 uint8_t markerAndVersion = 0;
1080 CHECK(avifROStreamRead(&s, &markerAndVersion, 1));
1081 uint8_t seqProfileAndIndex = 0;
1082 CHECK(avifROStreamRead(&s, &seqProfileAndIndex, 1));
1083 uint8_t rawFlags = 0;
1084 CHECK(avifROStreamRead(&s, &rawFlags, 1));
1085
1086 if (markerAndVersion != 0x81) {
1087 // Marker and version must both == 1
1088 return AVIF_FALSE;
1089 }
1090
1091 av1C->seqProfile = (seqProfileAndIndex >> 5) & 0x7; // unsigned int (3) seq_profile;
1092 av1C->seqLevelIdx0 = (seqProfileAndIndex >> 0) & 0x1f; // unsigned int (5) seq_level_idx_0;
1093 av1C->seqTier0 = (rawFlags >> 7) & 0x1; // unsigned int (1) seq_tier_0;
1094 av1C->highBitdepth = (rawFlags >> 6) & 0x1; // unsigned int (1) high_bitdepth;
1095 av1C->twelveBit = (rawFlags >> 5) & 0x1; // unsigned int (1) twelve_bit;
1096 av1C->monochrome = (rawFlags >> 4) & 0x1; // unsigned int (1) monochrome;
1097 av1C->chromaSubsamplingX = (rawFlags >> 3) & 0x1; // unsigned int (1) chroma_subsampling_x;
1098 av1C->chromaSubsamplingY = (rawFlags >> 2) & 0x1; // unsigned int (1) chroma_subsampling_y;
1099 av1C->chromaSamplePosition = (rawFlags >> 0) & 0x3; // unsigned int (2) chroma_sample_position;
1100 return AVIF_TRUE;
1101}
1102
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001103static avifBool avifParseAV1CodecConfigurationBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago6500fd62019-10-08 17:17:34 -07001104{
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001105 return avifParseAV1CodecConfigurationBox(raw, rawLen, &prop->u.av1C);
Joe Drago6500fd62019-10-08 17:17:34 -07001106}
1107
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001108static avifBool avifParsePixelAspectRatioBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001109{
1110 BEGIN_STREAM(s, raw, rawLen);
1111
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001112 avifPixelAspectRatioBox * pasp = &prop->u.pasp;
Joe Drago89f0cc82020-03-09 16:13:27 -07001113 CHECK(avifROStreamReadU32(&s, &pasp->hSpacing)); // unsigned int(32) hSpacing;
1114 CHECK(avifROStreamReadU32(&s, &pasp->vSpacing)); // unsigned int(32) vSpacing;
1115 return AVIF_TRUE;
1116}
1117
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001118static avifBool avifParseCleanApertureBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001119{
1120 BEGIN_STREAM(s, raw, rawLen);
1121
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001122 avifCleanApertureBox * clap = &prop->u.clap;
Joe Drago89f0cc82020-03-09 16:13:27 -07001123 CHECK(avifROStreamReadU32(&s, &clap->widthN)); // unsigned int(32) cleanApertureWidthN;
1124 CHECK(avifROStreamReadU32(&s, &clap->widthD)); // unsigned int(32) cleanApertureWidthD;
1125 CHECK(avifROStreamReadU32(&s, &clap->heightN)); // unsigned int(32) cleanApertureHeightN;
1126 CHECK(avifROStreamReadU32(&s, &clap->heightD)); // unsigned int(32) cleanApertureHeightD;
1127 CHECK(avifROStreamReadU32(&s, &clap->horizOffN)); // unsigned int(32) horizOffN;
1128 CHECK(avifROStreamReadU32(&s, &clap->horizOffD)); // unsigned int(32) horizOffD;
1129 CHECK(avifROStreamReadU32(&s, &clap->vertOffN)); // unsigned int(32) vertOffN;
1130 CHECK(avifROStreamReadU32(&s, &clap->vertOffD)); // unsigned int(32) vertOffD;
1131 return AVIF_TRUE;
1132}
1133
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001134static avifBool avifParseImageRotationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001135{
1136 BEGIN_STREAM(s, raw, rawLen);
1137
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001138 avifImageRotation * irot = &prop->u.irot;
Joe Drago89f0cc82020-03-09 16:13:27 -07001139 CHECK(avifROStreamRead(&s, &irot->angle, 1)); // unsigned int (6) reserved = 0; unsigned int (2) angle;
1140 if ((irot->angle & 0xfc) != 0) {
1141 // reserved bits must be 0
1142 return AVIF_FALSE;
1143 }
1144 return AVIF_TRUE;
1145}
1146
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001147static avifBool avifParseImageMirrorProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago89f0cc82020-03-09 16:13:27 -07001148{
1149 BEGIN_STREAM(s, raw, rawLen);
1150
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001151 avifImageMirror * imir = &prop->u.imir;
Joe Drago89f0cc82020-03-09 16:13:27 -07001152 CHECK(avifROStreamRead(&s, &imir->axis, 1)); // unsigned int (7) reserved = 0; unsigned int (1) axis;
1153 if ((imir->axis & 0xfe) != 0) {
1154 // reserved bits must be 0
1155 return AVIF_FALSE;
1156 }
1157 return AVIF_TRUE;
1158}
1159
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001160static avifBool avifParsePixelInformationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen)
Joe Drago60421562020-04-23 11:32:26 -07001161{
1162 BEGIN_STREAM(s, raw, rawLen);
1163 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
1164
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001165 avifPixelInformationProperty * pixi = &prop->u.pixi;
Joe Drago60421562020-04-23 11:32:26 -07001166 CHECK(avifROStreamRead(&s, &pixi->planeCount, 1)); // unsigned int (8) num_channels;
1167 if (pixi->planeCount > MAX_PIXI_PLANE_DEPTHS) {
1168 return AVIF_FALSE;
1169 }
1170 for (uint8_t i = 0; i < pixi->planeCount; ++i) {
1171 CHECK(avifROStreamRead(&s, &pixi->planeDepths[i], 1)); // unsigned int (8) bits_per_channel;
1172 }
1173 return AVIF_TRUE;
1174}
1175
Joe Dragoa72da5b2020-06-15 19:40:17 -07001176static avifBool avifParseItemPropertyContainerBox(avifPropertyArray * properties, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001177{
1178 BEGIN_STREAM(s, raw, rawLen);
1179
Joe Drago345aaa12019-09-25 13:42:12 -07001180 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001181 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001182 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago8f7a3002019-02-07 19:35:37 -08001183
Joe Dragoa72da5b2020-06-15 19:40:17 -07001184 int propertyIndex = avifArrayPushIndex(properties);
1185 avifProperty * prop = &properties->prop[propertyIndex];
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001186 memcpy(prop->type, header.type, 4);
Joe Drago8f7a3002019-02-07 19:35:37 -08001187 if (!memcmp(header.type, "ispe", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001188 CHECK(avifParseImageSpatialExtentsProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001189 } else if (!memcmp(header.type, "auxC", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001190 CHECK(avifParseAuxiliaryTypeProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001191 } else if (!memcmp(header.type, "colr", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001192 CHECK(avifParseColourInformationBox(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001193 } else if (!memcmp(header.type, "av1C", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001194 CHECK(avifParseAV1CodecConfigurationBoxProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001195 } else if (!memcmp(header.type, "pasp", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001196 CHECK(avifParsePixelAspectRatioBoxProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001197 } else if (!memcmp(header.type, "clap", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001198 CHECK(avifParseCleanApertureBoxProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001199 } else if (!memcmp(header.type, "irot", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001200 CHECK(avifParseImageRotationProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001201 } else if (!memcmp(header.type, "imir", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001202 CHECK(avifParseImageMirrorProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001203 } else if (!memcmp(header.type, "pixi", 4)) {
Wan-Teh Chang2031dc12020-05-22 09:24:46 -07001204 CHECK(avifParsePixelInformationProperty(prop, avifROStreamCurrent(&s), header.size));
Joe Drago60421562020-04-23 11:32:26 -07001205 }
Joe Drago8f7a3002019-02-07 19:35:37 -08001206
Joe Drago345aaa12019-09-25 13:42:12 -07001207 CHECK(avifROStreamSkip(&s, header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001208 }
1209 return AVIF_TRUE;
1210}
1211
Joe Drago9f2b87b2020-06-03 19:36:38 -07001212static avifBool avifParseItemPropertyAssociation(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001213{
1214 BEGIN_STREAM(s, raw, rawLen);
1215
1216 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -07001217 uint32_t flags;
1218 CHECK(avifROStreamReadVersionAndFlags(&s, &version, &flags));
1219 avifBool propertyIndexIsU16 = ((flags & 0x1) != 0);
Joe Drago8f7a3002019-02-07 19:35:37 -08001220
1221 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001222 CHECK(avifROStreamReadU32(&s, &entryCount));
Joe Drago8f7a3002019-02-07 19:35:37 -08001223 for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) {
1224 unsigned int itemID;
1225 if (version < 1) {
1226 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001227 CHECK(avifROStreamReadU16(&s, &tmp));
Joe Drago8f7a3002019-02-07 19:35:37 -08001228 itemID = tmp;
1229 } else {
Joe Drago345aaa12019-09-25 13:42:12 -07001230 CHECK(avifROStreamReadU32(&s, &itemID));
Joe Drago8f7a3002019-02-07 19:35:37 -08001231 }
1232 uint8_t associationCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001233 CHECK(avifROStreamRead(&s, &associationCount, 1));
Joe Drago8f7a3002019-02-07 19:35:37 -08001234 for (uint8_t associationIndex = 0; associationIndex < associationCount; ++associationIndex) {
Joe Drago3320e5f2020-04-21 17:36:27 -07001235 avifBool essential = AVIF_FALSE;
Joe Drago8f7a3002019-02-07 19:35:37 -08001236 uint16_t propertyIndex = 0;
1237 if (propertyIndexIsU16) {
Joe Drago345aaa12019-09-25 13:42:12 -07001238 CHECK(avifROStreamReadU16(&s, &propertyIndex));
Joe Drago3320e5f2020-04-21 17:36:27 -07001239 essential = ((propertyIndex & 0x8000) != 0);
Joe Drago8f7a3002019-02-07 19:35:37 -08001240 propertyIndex &= 0x7fff;
1241 } else {
1242 uint8_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001243 CHECK(avifROStreamRead(&s, &tmp, 1));
Joe Drago3320e5f2020-04-21 17:36:27 -07001244 essential = ((tmp & 0x80) != 0);
Joe Drago8f7a3002019-02-07 19:35:37 -08001245 propertyIndex = tmp & 0x7f;
1246 }
1247
1248 if (propertyIndex == 0) {
1249 // Not associated with any item
1250 continue;
1251 }
1252 --propertyIndex; // 1-indexed
1253
Joe Drago9f2b87b2020-06-03 19:36:38 -07001254 if (propertyIndex >= meta->properties.count) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001255 return AVIF_FALSE;
1256 }
1257
Joe Drago9f2b87b2020-06-03 19:36:38 -07001258 avifDecoderItem * item = avifMetaFindItem(meta, itemID);
Joe Drago46ea0582019-07-22 15:55:47 -07001259 if (!item) {
1260 return AVIF_FALSE;
1261 }
Joe Drago8f7a3002019-02-07 19:35:37 -08001262
Joe Dragoa72da5b2020-06-15 19:40:17 -07001263 // Copy property to item
1264 avifProperty * srcProp = &meta->properties.prop[propertyIndex];
1265
1266 static const char * supportedTypes[] = { "ispe", "auxC", "colr", "av1C", "pasp", "clap", "irot", "imir", "pixi" };
1267 size_t supportedTypesCount = sizeof(supportedTypes) / sizeof(supportedTypes[0]);
1268 avifBool supportedType = AVIF_FALSE;
1269 for (size_t i = 0; i < supportedTypesCount; ++i) {
1270 if (!memcmp(srcProp->type, supportedTypes[i], 4)) {
1271 supportedType = AVIF_TRUE;
1272 break;
1273 }
1274 }
1275 if (supportedType) {
1276 avifProperty * dstProp = (avifProperty *)avifArrayPushPtr(&item->properties);
1277 memcpy(dstProp, srcProp, sizeof(avifProperty));
Joe Drago3320e5f2020-04-21 17:36:27 -07001278 } else {
1279 if (essential) {
1280 // Discovered an essential item property that libavif doesn't support!
1281 // Make a note to ignore this item later.
1282 item->hasUnsupportedEssentialProperty = AVIF_TRUE;
1283 }
Joe Drago8f7a3002019-02-07 19:35:37 -08001284 }
1285 }
1286 }
1287
1288 return AVIF_TRUE;
1289}
1290
Joe Drago9f2b87b2020-06-03 19:36:38 -07001291static avifBool avifParsePrimaryItemBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Dragof6a42272019-11-21 15:21:41 -08001292{
Joe Drago9f2b87b2020-06-03 19:36:38 -07001293 if (meta->primaryItemID > 0) {
Joe Dragof6a42272019-11-21 15:21:41 -08001294 // Illegal to have multiple pitm boxes, bail out
1295 return AVIF_FALSE;
1296 }
1297
1298 BEGIN_STREAM(s, raw, rawLen);
1299
1300 uint8_t version;
1301 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
1302
1303 if (version == 0) {
1304 uint16_t tmp16;
1305 CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID;
Joe Drago9f2b87b2020-06-03 19:36:38 -07001306 meta->primaryItemID = tmp16;
Joe Dragof6a42272019-11-21 15:21:41 -08001307 } else {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001308 CHECK(avifROStreamReadU32(&s, &meta->primaryItemID)); // unsigned int(32) item_ID;
Joe Dragof6a42272019-11-21 15:21:41 -08001309 }
1310 return AVIF_TRUE;
1311}
1312
Joe Drago9f2b87b2020-06-03 19:36:38 -07001313static avifBool avifParseItemDataBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Dragof6a42272019-11-21 15:21:41 -08001314{
Joe Dragof6a42272019-11-21 15:21:41 -08001315 // 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 -07001316 for (uint32_t i = 0; i < meta->idats.count; ++i) {
Joe Dragoba1eb492020-06-22 17:05:04 -07001317 if (meta->idats.idat[i].id == meta->idatID) {
Joe Dragof6a42272019-11-21 15:21:41 -08001318 return AVIF_FALSE;
1319 }
1320 }
1321
Joe Drago9f2b87b2020-06-03 19:36:38 -07001322 int index = avifArrayPushIndex(&meta->idats);
1323 avifDecoderItemData * idat = &meta->idats.idat[index];
Joe Dragoba1eb492020-06-22 17:05:04 -07001324 idat->id = meta->idatID;
Joe Dragof6a42272019-11-21 15:21:41 -08001325 idat->data.data = raw;
1326 idat->data.size = rawLen;
1327 return AVIF_TRUE;
1328}
1329
Joe Drago9f2b87b2020-06-03 19:36:38 -07001330static avifBool avifParseItemPropertiesBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001331{
1332 BEGIN_STREAM(s, raw, rawLen);
1333
1334 avifBoxHeader ipcoHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001335 CHECK(avifROStreamReadBoxHeader(&s, &ipcoHeader));
wantehchangbc35a5f2020-08-12 15:27:39 -07001336 if (memcmp(ipcoHeader.type, "ipco", 4)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001337 return AVIF_FALSE;
1338 }
1339
1340 // Read all item properties inside of ItemPropertyContainerBox
Joe Dragoa72da5b2020-06-15 19:40:17 -07001341 CHECK(avifParseItemPropertyContainerBox(&meta->properties, avifROStreamCurrent(&s), ipcoHeader.size));
Joe Drago345aaa12019-09-25 13:42:12 -07001342 CHECK(avifROStreamSkip(&s, ipcoHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001343
1344 // Now read all ItemPropertyAssociation until the end of the box, and make associations
Joe Drago345aaa12019-09-25 13:42:12 -07001345 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001346 avifBoxHeader ipmaHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001347 CHECK(avifROStreamReadBoxHeader(&s, &ipmaHeader));
Joe Drago8f7a3002019-02-07 19:35:37 -08001348
1349 if (!memcmp(ipmaHeader.type, "ipma", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001350 CHECK(avifParseItemPropertyAssociation(meta, avifROStreamCurrent(&s), ipmaHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001351 } else {
1352 // These must all be type ipma
1353 return AVIF_FALSE;
1354 }
1355
Joe Drago345aaa12019-09-25 13:42:12 -07001356 CHECK(avifROStreamSkip(&s, ipmaHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001357 }
1358 return AVIF_TRUE;
1359}
1360
Joe Drago9f2b87b2020-06-03 19:36:38 -07001361static avifBool avifParseItemInfoEntry(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001362{
1363 BEGIN_STREAM(s, raw, rawLen);
1364
Joe Drago345aaa12019-09-25 13:42:12 -07001365 CHECK(avifROStreamReadAndEnforceVersion(&s, 2)); // TODO: support version > 2? 2+ is required for item_type
Joe Drago8f7a3002019-02-07 19:35:37 -08001366
Joe Drago345aaa12019-09-25 13:42:12 -07001367 uint16_t itemID; // unsigned int(16) item_ID;
1368 CHECK(avifROStreamReadU16(&s, &itemID)); //
1369 uint16_t itemProtectionIndex; // unsigned int(16) item_protection_index;
1370 CHECK(avifROStreamReadU16(&s, &itemProtectionIndex)); //
1371 uint8_t itemType[4]; // unsigned int(32) item_type;
1372 CHECK(avifROStreamRead(&s, itemType, 4)); //
Joe Drago8f7a3002019-02-07 19:35:37 -08001373
Joe Dragof6a42272019-11-21 15:21:41 -08001374 avifContentType contentType;
1375 if (!memcmp(itemType, "mime", 4)) {
1376 CHECK(avifROStreamReadString(&s, NULL, 0)); // string item_name; (skipped)
1377 CHECK(avifROStreamReadString(&s, contentType.contentType, CONTENTTYPE_SIZE)); // string content_type;
1378 } else {
1379 memset(&contentType, 0, sizeof(contentType));
1380 }
1381
Joe Drago9f2b87b2020-06-03 19:36:38 -07001382 avifDecoderItem * item = avifMetaFindItem(meta, itemID);
Joe Drago46ea0582019-07-22 15:55:47 -07001383 if (!item) {
1384 return AVIF_FALSE;
1385 }
1386
Joe Drago05559c92019-07-17 16:33:38 -07001387 memcpy(item->type, itemType, sizeof(itemType));
Joe Dragof6a42272019-11-21 15:21:41 -08001388 memcpy(&item->contentType, &contentType, sizeof(contentType));
Joe Drago8f7a3002019-02-07 19:35:37 -08001389 return AVIF_TRUE;
1390}
1391
Joe Drago9f2b87b2020-06-03 19:36:38 -07001392static avifBool avifParseItemInfoBox(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 uint32_t entryCount;
1399 if (version == 0) {
1400 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001401 CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) entry_count;
Joe Drago8f7a3002019-02-07 19:35:37 -08001402 entryCount = tmp;
1403 } else if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001404 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Drago8f7a3002019-02-07 19:35:37 -08001405 } else {
1406 return AVIF_FALSE;
1407 }
1408
Joe Drago678b9382019-02-09 03:17:47 -08001409 for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001410 avifBoxHeader infeHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001411 CHECK(avifROStreamReadBoxHeader(&s, &infeHeader));
Joe Drago8f7a3002019-02-07 19:35:37 -08001412
1413 if (!memcmp(infeHeader.type, "infe", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001414 CHECK(avifParseItemInfoEntry(meta, avifROStreamCurrent(&s), infeHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001415 } else {
1416 // These must all be type ipma
1417 return AVIF_FALSE;
1418 }
1419
Joe Drago345aaa12019-09-25 13:42:12 -07001420 CHECK(avifROStreamSkip(&s, infeHeader.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001421 }
1422
1423 return AVIF_TRUE;
1424}
1425
Joe Drago9f2b87b2020-06-03 19:36:38 -07001426static avifBool avifParseItemReferenceBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001427{
1428 BEGIN_STREAM(s, raw, rawLen);
1429
1430 uint8_t version;
Joe Drago345aaa12019-09-25 13:42:12 -07001431 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Drago8f7a3002019-02-07 19:35:37 -08001432
Joe Drago345aaa12019-09-25 13:42:12 -07001433 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001434 avifBoxHeader irefHeader;
Joe Drago345aaa12019-09-25 13:42:12 -07001435 CHECK(avifROStreamReadBoxHeader(&s, &irefHeader));
Joe Drago8f7a3002019-02-07 19:35:37 -08001436
1437 uint32_t fromID = 0;
1438 if (version == 0) {
1439 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001440 CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) from_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001441 fromID = tmp;
1442 } else if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001443 CHECK(avifROStreamReadU32(&s, &fromID)); // unsigned int(32) from_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001444 } else {
1445 // unsupported iref version, skip it
1446 break;
1447 }
1448
1449 uint16_t referenceCount = 0;
Joe Drago345aaa12019-09-25 13:42:12 -07001450 CHECK(avifROStreamReadU16(&s, &referenceCount)); // unsigned int(16) reference_count;
Joe Drago8f7a3002019-02-07 19:35:37 -08001451
1452 for (uint16_t refIndex = 0; refIndex < referenceCount; ++refIndex) {
1453 uint32_t toID = 0;
1454 if (version == 0) {
1455 uint16_t tmp;
Joe Drago345aaa12019-09-25 13:42:12 -07001456 CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) to_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001457 toID = tmp;
1458 } else if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001459 CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) to_item_ID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001460 } else {
1461 // unsupported iref version, skip it
1462 break;
1463 }
1464
1465 // Read this reference as "{fromID} is a {irefType} for {toID}"
1466 if (fromID && toID) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001467 avifDecoderItem * item = avifMetaFindItem(meta, fromID);
Joe Drago46ea0582019-07-22 15:55:47 -07001468 if (!item) {
1469 return AVIF_FALSE;
1470 }
1471
Joe Drago8f7a3002019-02-07 19:35:37 -08001472 if (!memcmp(irefHeader.type, "thmb", 4)) {
Joe Drago05559c92019-07-17 16:33:38 -07001473 item->thumbnailForID = toID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001474 }
1475 if (!memcmp(irefHeader.type, "auxl", 4)) {
Joe Drago05559c92019-07-17 16:33:38 -07001476 item->auxForID = toID;
Joe Drago8f7a3002019-02-07 19:35:37 -08001477 }
Joe Dragof6a42272019-11-21 15:21:41 -08001478 if (!memcmp(irefHeader.type, "cdsc", 4)) {
1479 item->descForID = toID;
1480 }
Joe Drago060d5342020-03-03 10:53:49 -08001481 if (!memcmp(irefHeader.type, "dimg", 4)) {
1482 // derived images refer in the opposite direction
Joe Drago9f2b87b2020-06-03 19:36:38 -07001483 avifDecoderItem * dimg = avifMetaFindItem(meta, toID);
Joe Drago060d5342020-03-03 10:53:49 -08001484 if (!dimg) {
1485 return AVIF_FALSE;
1486 }
1487
1488 dimg->dimgForID = fromID;
1489 }
Joe Drago8f7a3002019-02-07 19:35:37 -08001490 }
1491 }
1492 }
1493
1494 return AVIF_TRUE;
1495}
1496
Joe Drago9f2b87b2020-06-03 19:36:38 -07001497static avifBool avifParseMetaBox(avifMeta * meta, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001498{
1499 BEGIN_STREAM(s, raw, rawLen);
1500
Joe Drago345aaa12019-09-25 13:42:12 -07001501 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Drago8f7a3002019-02-07 19:35:37 -08001502
Joe Dragoba1eb492020-06-22 17:05:04 -07001503 ++meta->idatID; // for tracking idat
Joe Dragof6a42272019-11-21 15:21:41 -08001504
Joe Drago345aaa12019-09-25 13:42:12 -07001505 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001506 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001507 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago8f7a3002019-02-07 19:35:37 -08001508
1509 if (!memcmp(header.type, "iloc", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001510 CHECK(avifParseItemLocationBox(meta, avifROStreamCurrent(&s), header.size));
Joe Dragof6a42272019-11-21 15:21:41 -08001511 } else if (!memcmp(header.type, "pitm", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001512 CHECK(avifParsePrimaryItemBox(meta, avifROStreamCurrent(&s), header.size));
Joe Dragof6a42272019-11-21 15:21:41 -08001513 } else if (!memcmp(header.type, "idat", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001514 CHECK(avifParseItemDataBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001515 } else if (!memcmp(header.type, "iprp", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001516 CHECK(avifParseItemPropertiesBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001517 } else if (!memcmp(header.type, "iinf", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001518 CHECK(avifParseItemInfoBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001519 } else if (!memcmp(header.type, "iref", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001520 CHECK(avifParseItemReferenceBox(meta, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001521 }
1522
Joe Drago345aaa12019-09-25 13:42:12 -07001523 CHECK(avifROStreamSkip(&s, header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001524 }
1525 return AVIF_TRUE;
1526}
1527
Joe Drago9f2b87b2020-06-03 19:36:38 -07001528static avifBool avifParseTrackHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001529{
1530 BEGIN_STREAM(s, raw, rawLen);
1531
1532 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -07001533 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001534
1535 uint32_t ignored32, trackID;
1536 uint64_t ignored64;
1537 if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001538 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time;
1539 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time;
1540 CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID;
Joe Dragofc4144e2019-09-27 20:35:06 -07001541 CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0;
1542 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001543 } else if (version == 0) {
Joe Drago345aaa12019-09-25 13:42:12 -07001544 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time;
1545 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time;
1546 CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID;
Joe Dragofc4144e2019-09-27 20:35:06 -07001547 CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0;
1548 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001549 } else {
1550 // Unsupported version
1551 return AVIF_FALSE;
1552 }
1553
Joe Dragofc4144e2019-09-27 20:35:06 -07001554 // Skipping the following 52 bytes here:
1555 // ------------------------------------
1556 // const unsigned int(32)[2] reserved = 0;
1557 // template int(16) layer = 0;
1558 // template int(16) alternate_group = 0;
1559 // template int(16) volume = {if track_is_audio 0x0100 else 0};
1560 // const unsigned int(16) reserved = 0;
1561 // template int(32)[9] matrix= { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 }; // unity matrix
1562 CHECK(avifROStreamSkip(&s, 52));
1563
1564 uint32_t width, height;
1565 CHECK(avifROStreamReadU32(&s, &width)); // unsigned int(32) width;
1566 CHECK(avifROStreamReadU32(&s, &height)); // unsigned int(32) height;
1567 track->width = width >> 16;
1568 track->height = height >> 16;
1569
Joe Dragoae7e2c32019-07-18 15:22:25 -07001570 // TODO: support scaling based on width/height track header info?
1571
1572 track->id = trackID;
1573 return AVIF_TRUE;
1574}
1575
Joe Drago9f2b87b2020-06-03 19:36:38 -07001576static avifBool avifParseMediaHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001577{
1578 BEGIN_STREAM(s, raw, rawLen);
1579
1580 uint8_t version;
Joe Drago4a25c192020-06-03 16:29:58 -07001581 CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001582
1583 uint32_t ignored32, mediaTimescale, mediaDuration32;
1584 uint64_t ignored64, mediaDuration64;
1585 if (version == 1) {
Joe Drago345aaa12019-09-25 13:42:12 -07001586 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time;
1587 CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time;
1588 CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale;
1589 CHECK(avifROStreamReadU64(&s, &mediaDuration64)); // unsigned int(64) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001590 track->mediaDuration = mediaDuration64;
1591 } else if (version == 0) {
Joe Drago345aaa12019-09-25 13:42:12 -07001592 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time;
1593 CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time;
1594 CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale;
1595 CHECK(avifROStreamReadU32(&s, &mediaDuration32)); // unsigned int(32) duration;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001596 track->mediaDuration = (uint64_t)mediaDuration32;
1597 } else {
1598 // Unsupported version
1599 return AVIF_FALSE;
1600 }
1601
1602 track->mediaTimescale = mediaTimescale;
1603 return AVIF_TRUE;
1604}
1605
Joe Drago9f2b87b2020-06-03 19:36:38 -07001606static avifBool avifParseChunkOffsetBox(avifSampleTable * sampleTable, avifBool largeOffsets, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001607{
1608 BEGIN_STREAM(s, raw, rawLen);
1609
Joe Drago345aaa12019-09-25 13:42:12 -07001610 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001611
1612 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001613 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001614 for (uint32_t i = 0; i < entryCount; ++i) {
1615 uint64_t offset;
1616 if (largeOffsets) {
Joe Drago345aaa12019-09-25 13:42:12 -07001617 CHECK(avifROStreamReadU64(&s, &offset)); // unsigned int(32) chunk_offset;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001618 } else {
1619 uint32_t offset32;
Joe Drago345aaa12019-09-25 13:42:12 -07001620 CHECK(avifROStreamReadU32(&s, &offset32)); // unsigned int(32) chunk_offset;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001621 offset = (uint64_t)offset32;
1622 }
1623
1624 avifSampleTableChunk * chunk = (avifSampleTableChunk *)avifArrayPushPtr(&sampleTable->chunks);
1625 chunk->offset = offset;
1626 }
1627 return AVIF_TRUE;
1628}
1629
Joe Drago9f2b87b2020-06-03 19:36:38 -07001630static avifBool avifParseSampleToChunkBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001631{
1632 BEGIN_STREAM(s, raw, rawLen);
1633
Joe Drago345aaa12019-09-25 13:42:12 -07001634 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001635
1636 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001637 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001638 for (uint32_t i = 0; i < entryCount; ++i) {
1639 avifSampleTableSampleToChunk * sampleToChunk = (avifSampleTableSampleToChunk *)avifArrayPushPtr(&sampleTable->sampleToChunks);
Joe Drago345aaa12019-09-25 13:42:12 -07001640 CHECK(avifROStreamReadU32(&s, &sampleToChunk->firstChunk)); // unsigned int(32) first_chunk;
1641 CHECK(avifROStreamReadU32(&s, &sampleToChunk->samplesPerChunk)); // unsigned int(32) samples_per_chunk;
1642 CHECK(avifROStreamReadU32(&s, &sampleToChunk->sampleDescriptionIndex)); // unsigned int(32) sample_description_index;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001643 }
1644 return AVIF_TRUE;
1645}
1646
Joe Drago9f2b87b2020-06-03 19:36:38 -07001647static avifBool avifParseSampleSizeBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001648{
1649 BEGIN_STREAM(s, raw, rawLen);
1650
Joe Drago345aaa12019-09-25 13:42:12 -07001651 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001652
Joe Drago370be3f2020-02-07 15:59:42 -08001653 uint32_t allSamplesSize, sampleCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001654 CHECK(avifROStreamReadU32(&s, &allSamplesSize)); // unsigned int(32) sample_size;
Joe Drago370be3f2020-02-07 15:59:42 -08001655 CHECK(avifROStreamReadU32(&s, &sampleCount)); // unsigned int(32) sample_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001656
Joe Drago370be3f2020-02-07 15:59:42 -08001657 if (allSamplesSize > 0) {
1658 sampleTable->allSamplesSize = allSamplesSize;
1659 } else {
1660 for (uint32_t i = 0; i < sampleCount; ++i) {
1661 avifSampleTableSampleSize * sampleSize = (avifSampleTableSampleSize *)avifArrayPushPtr(&sampleTable->sampleSizes);
Joe Drago345aaa12019-09-25 13:42:12 -07001662 CHECK(avifROStreamReadU32(&s, &sampleSize->size)); // unsigned int(32) entry_size;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001663 }
1664 }
1665 return AVIF_TRUE;
1666}
1667
Joe Drago9f2b87b2020-06-03 19:36:38 -07001668static avifBool avifParseSyncSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Drago22c1ad92019-09-26 12:46:50 -07001669{
1670 BEGIN_STREAM(s, raw, rawLen);
Joe Drago22c1ad92019-09-26 12:46:50 -07001671
1672 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
1673
1674 uint32_t entryCount;
1675 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
1676
1677 for (uint32_t i = 0; i < entryCount; ++i) {
1678 uint32_t sampleNumber = 0;
1679 CHECK(avifROStreamReadU32(&s, &sampleNumber)); // unsigned int(32) sample_number;
1680 avifSyncSample * syncSample = (avifSyncSample *)avifArrayPushPtr(&sampleTable->syncSamples);
1681 syncSample->sampleNumber = sampleNumber;
1682 }
1683 return AVIF_TRUE;
1684}
1685
Joe Drago9f2b87b2020-06-03 19:36:38 -07001686static avifBool avifParseTimeToSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001687{
1688 BEGIN_STREAM(s, raw, rawLen);
1689
Joe Drago345aaa12019-09-25 13:42:12 -07001690 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001691
1692 uint32_t entryCount;
Joe Drago345aaa12019-09-25 13:42:12 -07001693 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001694
1695 for (uint32_t i = 0; i < entryCount; ++i) {
1696 avifSampleTableTimeToSample * timeToSample = (avifSampleTableTimeToSample *)avifArrayPushPtr(&sampleTable->timeToSamples);
Joe Drago345aaa12019-09-25 13:42:12 -07001697 CHECK(avifROStreamReadU32(&s, &timeToSample->sampleCount)); // unsigned int(32) sample_count;
1698 CHECK(avifROStreamReadU32(&s, &timeToSample->sampleDelta)); // unsigned int(32) sample_delta;
Joe Dragoae7e2c32019-07-18 15:22:25 -07001699 }
1700 return AVIF_TRUE;
1701}
1702
Joe Drago9f2b87b2020-06-03 19:36:38 -07001703static avifBool avifParseSampleDescriptionBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen)
Joe Drago2c0924c2019-09-26 17:41:01 -07001704{
1705 BEGIN_STREAM(s, raw, rawLen);
Joe Drago2c0924c2019-09-26 17:41:01 -07001706
1707 CHECK(avifROStreamReadAndEnforceVersion(&s, 0));
1708
1709 uint32_t entryCount;
1710 CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count;
1711
1712 for (uint32_t i = 0; i < entryCount; ++i) {
1713 avifBoxHeader sampleEntryHeader;
1714 CHECK(avifROStreamReadBoxHeader(&s, &sampleEntryHeader));
1715
1716 avifSampleDescription * description = (avifSampleDescription *)avifArrayPushPtr(&sampleTable->sampleDescriptions);
Joe Dragoa72da5b2020-06-15 19:40:17 -07001717 avifArrayCreate(&description->properties, sizeof(avifProperty), 16);
Joe Drago2c0924c2019-09-26 17:41:01 -07001718 memcpy(description->format, sampleEntryHeader.type, sizeof(description->format));
Joe Drago6500fd62019-10-08 17:17:34 -07001719 size_t remainingBytes = avifROStreamRemainingBytes(&s);
1720 if (!memcmp(description->format, "av01", 4) && (remainingBytes > VISUALSAMPLEENTRY_SIZE)) {
Joe Dragoa72da5b2020-06-15 19:40:17 -07001721 CHECK(avifParseItemPropertyContainerBox(
1722 &description->properties, avifROStreamCurrent(&s) + VISUALSAMPLEENTRY_SIZE, remainingBytes - VISUALSAMPLEENTRY_SIZE));
Joe Drago6500fd62019-10-08 17:17:34 -07001723 }
Joe Drago2c0924c2019-09-26 17:41:01 -07001724
1725 CHECK(avifROStreamSkip(&s, sampleEntryHeader.size));
1726 }
1727 return AVIF_TRUE;
1728}
1729
Joe Drago9f2b87b2020-06-03 19:36:38 -07001730static avifBool avifParseSampleTableBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001731{
1732 if (track->sampleTable) {
1733 // A TrackBox may only have one SampleTable
1734 return AVIF_FALSE;
1735 }
1736 track->sampleTable = avifSampleTableCreate();
1737
1738 BEGIN_STREAM(s, raw, rawLen);
1739
Joe Drago345aaa12019-09-25 13:42:12 -07001740 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07001741 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001742 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001743
1744 if (!memcmp(header.type, "stco", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001745 CHECK(avifParseChunkOffsetBox(track->sampleTable, AVIF_FALSE, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001746 } else if (!memcmp(header.type, "co64", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001747 CHECK(avifParseChunkOffsetBox(track->sampleTable, AVIF_TRUE, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001748 } else if (!memcmp(header.type, "stsc", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001749 CHECK(avifParseSampleToChunkBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001750 } else if (!memcmp(header.type, "stsz", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001751 CHECK(avifParseSampleSizeBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Drago22c1ad92019-09-26 12:46:50 -07001752 } else if (!memcmp(header.type, "stss", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001753 CHECK(avifParseSyncSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001754 } else if (!memcmp(header.type, "stts", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001755 CHECK(avifParseTimeToSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Drago2c0924c2019-09-26 17:41:01 -07001756 } else if (!memcmp(header.type, "stsd", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001757 CHECK(avifParseSampleDescriptionBox(track->sampleTable, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001758 }
1759
Joe Drago345aaa12019-09-25 13:42:12 -07001760 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001761 }
Joe Dragoae7e2c32019-07-18 15:22:25 -07001762 return AVIF_TRUE;
1763}
1764
Joe Drago9f2b87b2020-06-03 19:36:38 -07001765static avifBool avifParseMediaInformationBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001766{
1767 BEGIN_STREAM(s, raw, rawLen);
1768
Joe Drago345aaa12019-09-25 13:42:12 -07001769 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07001770 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001771 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001772
1773 if (!memcmp(header.type, "stbl", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001774 CHECK(avifParseSampleTableBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001775 }
1776
Joe Drago345aaa12019-09-25 13:42:12 -07001777 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001778 }
1779 return AVIF_TRUE;
1780}
1781
Joe Drago9f2b87b2020-06-03 19:36:38 -07001782static avifBool avifParseMediaBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001783{
1784 BEGIN_STREAM(s, raw, rawLen);
1785
Joe Drago345aaa12019-09-25 13:42:12 -07001786 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07001787 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001788 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001789
1790 if (!memcmp(header.type, "mdhd", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001791 CHECK(avifParseMediaHeaderBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001792 } else if (!memcmp(header.type, "minf", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001793 CHECK(avifParseMediaInformationBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001794 }
1795
Joe Drago345aaa12019-09-25 13:42:12 -07001796 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001797 }
1798 return AVIF_TRUE;
1799}
1800
Joe Drago9f2b87b2020-06-03 19:36:38 -07001801static avifBool avifTrackReferenceBox(avifTrack * track, const uint8_t * raw, size_t rawLen)
Joe Drago46ea0582019-07-22 15:55:47 -07001802{
1803 BEGIN_STREAM(s, raw, rawLen);
1804
Joe Drago345aaa12019-09-25 13:42:12 -07001805 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago46ea0582019-07-22 15:55:47 -07001806 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001807 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago46ea0582019-07-22 15:55:47 -07001808
1809 if (!memcmp(header.type, "auxl", 4)) {
1810 uint32_t toID;
Joe Drago345aaa12019-09-25 13:42:12 -07001811 CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) track_IDs[]
1812 CHECK(avifROStreamSkip(&s, header.size - sizeof(uint32_t))); // just take the first one
Joe Drago46ea0582019-07-22 15:55:47 -07001813 track->auxForID = toID;
1814 } else {
Joe Drago345aaa12019-09-25 13:42:12 -07001815 CHECK(avifROStreamSkip(&s, header.size));
Joe Drago46ea0582019-07-22 15:55:47 -07001816 }
1817 }
1818 return AVIF_TRUE;
1819}
1820
Joe Drago800b47f2020-03-18 16:22:37 -07001821static avifBool avifParseTrackBox(avifDecoderData * data, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001822{
1823 BEGIN_STREAM(s, raw, rawLen);
1824
Joe Dragoa72da5b2020-06-15 19:40:17 -07001825 avifTrack * track = avifDecoderDataCreateTrack(data);
Joe Dragoae7e2c32019-07-18 15:22:25 -07001826
Joe Drago345aaa12019-09-25 13:42:12 -07001827 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07001828 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001829 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001830
1831 if (!memcmp(header.type, "tkhd", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001832 CHECK(avifParseTrackHeaderBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoa72da5b2020-06-15 19:40:17 -07001833 } else if (!memcmp(header.type, "meta", 4)) {
1834 CHECK(avifParseMetaBox(track->meta, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001835 } else if (!memcmp(header.type, "mdia", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001836 CHECK(avifParseMediaBox(track, avifROStreamCurrent(&s), header.size));
Joe Drago46ea0582019-07-22 15:55:47 -07001837 } else if (!memcmp(header.type, "tref", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001838 CHECK(avifTrackReferenceBox(track, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001839 }
1840
Joe Drago345aaa12019-09-25 13:42:12 -07001841 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001842 }
1843 return AVIF_TRUE;
1844}
1845
Joe Drago800b47f2020-03-18 16:22:37 -07001846static avifBool avifParseMoovBox(avifDecoderData * data, const uint8_t * raw, size_t rawLen)
Joe Dragoae7e2c32019-07-18 15:22:25 -07001847{
1848 BEGIN_STREAM(s, raw, rawLen);
1849
Joe Drago345aaa12019-09-25 13:42:12 -07001850 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Dragoae7e2c32019-07-18 15:22:25 -07001851 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001852 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001853
1854 if (!memcmp(header.type, "trak", 4)) {
Joe Drago345aaa12019-09-25 13:42:12 -07001855 CHECK(avifParseTrackBox(data, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001856 }
1857
Joe Drago345aaa12019-09-25 13:42:12 -07001858 CHECK(avifROStreamSkip(&s, header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001859 }
1860 return AVIF_TRUE;
1861}
1862
Joe Drago345aaa12019-09-25 13:42:12 -07001863static avifBool avifParseFileTypeBox(avifFileType * ftyp, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001864{
1865 BEGIN_STREAM(s, raw, rawLen);
1866
Joe Drago345aaa12019-09-25 13:42:12 -07001867 CHECK(avifROStreamRead(&s, ftyp->majorBrand, 4));
1868 CHECK(avifROStreamReadU32(&s, &ftyp->minorVersion));
Joe Drago8f7a3002019-02-07 19:35:37 -08001869
Joe Drago345aaa12019-09-25 13:42:12 -07001870 size_t compatibleBrandsBytes = avifROStreamRemainingBytes(&s);
Joe Drago8f7a3002019-02-07 19:35:37 -08001871 if ((compatibleBrandsBytes % 4) != 0) {
1872 return AVIF_FALSE;
1873 }
Wan-Teh Chang6da0a882020-07-01 12:19:31 -07001874 ftyp->compatibleBrands = avifROStreamCurrent(&s);
1875 CHECK(avifROStreamSkip(&s, compatibleBrandsBytes));
Joe Drago7e37b972019-07-24 12:44:47 -07001876 ftyp->compatibleBrandsCount = (int)compatibleBrandsBytes / 4;
Joe Drago8f7a3002019-02-07 19:35:37 -08001877
1878 return AVIF_TRUE;
1879}
1880
Joe Drago800b47f2020-03-18 16:22:37 -07001881static avifBool avifParse(avifDecoderData * data, const uint8_t * raw, size_t rawLen)
Joe Drago8f7a3002019-02-07 19:35:37 -08001882{
1883 BEGIN_STREAM(s, raw, rawLen);
1884
Joe Drago345aaa12019-09-25 13:42:12 -07001885 while (avifROStreamHasBytesLeft(&s, 1)) {
Joe Drago8f7a3002019-02-07 19:35:37 -08001886 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001887 CHECK(avifROStreamReadBoxHeader(&s, &header));
Joe Drago8f7a3002019-02-07 19:35:37 -08001888
1889 if (!memcmp(header.type, "ftyp", 4)) {
Joe Drago345aaa12019-09-25 13:42:12 -07001890 CHECK(avifParseFileTypeBox(&data->ftyp, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001891 } else if (!memcmp(header.type, "meta", 4)) {
Joe Drago9f2b87b2020-06-03 19:36:38 -07001892 CHECK(avifParseMetaBox(data->meta, avifROStreamCurrent(&s), header.size));
Joe Dragoae7e2c32019-07-18 15:22:25 -07001893 } else if (!memcmp(header.type, "moov", 4)) {
Joe Drago345aaa12019-09-25 13:42:12 -07001894 CHECK(avifParseMoovBox(data, avifROStreamCurrent(&s), header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001895 }
1896
Joe Drago345aaa12019-09-25 13:42:12 -07001897 CHECK(avifROStreamSkip(&s, header.size));
Joe Drago8f7a3002019-02-07 19:35:37 -08001898 }
1899 return AVIF_TRUE;
1900}
1901
1902// ---------------------------------------------------------------------------
Joe Drago8f7a3002019-02-07 19:35:37 -08001903
Joe Drago7e37b972019-07-24 12:44:47 -07001904static avifBool avifFileTypeIsCompatible(avifFileType * ftyp)
1905{
wantehchangbc35a5f2020-08-12 15:27:39 -07001906 avifBool avifCompatible = (!memcmp(ftyp->majorBrand, "avif", 4) || !memcmp(ftyp->majorBrand, "avis", 4));
Joe Drago2c0924c2019-09-26 17:41:01 -07001907 if (!avifCompatible) {
1908 for (int compatibleBrandIndex = 0; compatibleBrandIndex < ftyp->compatibleBrandsCount; ++compatibleBrandIndex) {
Wan-Teh Chang6da0a882020-07-01 12:19:31 -07001909 const uint8_t * compatibleBrand = &ftyp->compatibleBrands[4 * compatibleBrandIndex];
Wan-Teh Changbb8e2a82020-06-29 18:13:28 -07001910 if (!memcmp(compatibleBrand, "avif", 4) || !memcmp(compatibleBrand, "avis", 4)) {
Joe Drago2c0924c2019-09-26 17:41:01 -07001911 avifCompatible = AVIF_TRUE;
1912 break;
Joe Drago7e37b972019-07-24 12:44:47 -07001913 }
1914 }
1915 }
1916 return avifCompatible;
1917}
1918
Wan-Teh Change184dc12020-05-11 12:47:21 -07001919avifBool avifPeekCompatibleFileType(const avifROData * input)
Joe Drago7e37b972019-07-24 12:44:47 -07001920{
1921 BEGIN_STREAM(s, input->data, input->size);
1922
1923 avifBoxHeader header;
Joe Drago345aaa12019-09-25 13:42:12 -07001924 CHECK(avifROStreamReadBoxHeader(&s, &header));
wantehchangbc35a5f2020-08-12 15:27:39 -07001925 if (memcmp(header.type, "ftyp", 4)) {
Joe Drago7e37b972019-07-24 12:44:47 -07001926 return AVIF_FALSE;
1927 }
1928
1929 avifFileType ftyp;
1930 memset(&ftyp, 0, sizeof(avifFileType));
Joe Drago345aaa12019-09-25 13:42:12 -07001931 avifBool parsed = avifParseFileTypeBox(&ftyp, avifROStreamCurrent(&s), header.size);
Joe Drago7e37b972019-07-24 12:44:47 -07001932 if (!parsed) {
1933 return AVIF_FALSE;
1934 }
1935 return avifFileTypeIsCompatible(&ftyp);
1936}
1937
1938// ---------------------------------------------------------------------------
1939
Joe Drago0b05eee2019-06-12 13:24:39 -07001940avifDecoder * avifDecoderCreate(void)
1941{
1942 avifDecoder * decoder = (avifDecoder *)avifAlloc(sizeof(avifDecoder));
1943 memset(decoder, 0, sizeof(avifDecoder));
1944 return decoder;
1945}
1946
Joe Drago46ea0582019-07-22 15:55:47 -07001947static void avifDecoderCleanup(avifDecoder * decoder)
1948{
1949 if (decoder->data) {
Joe Drago800b47f2020-03-18 16:22:37 -07001950 avifDecoderDataDestroy(decoder->data);
Joe Drago46ea0582019-07-22 15:55:47 -07001951 decoder->data = NULL;
1952 }
1953
1954 if (decoder->image) {
1955 avifImageDestroy(decoder->image);
1956 decoder->image = NULL;
1957 }
1958}
1959
Joe Drago0b05eee2019-06-12 13:24:39 -07001960void avifDecoderDestroy(avifDecoder * decoder)
1961{
Joe Drago46ea0582019-07-22 15:55:47 -07001962 avifDecoderCleanup(decoder);
Joe Drago0b05eee2019-06-12 13:24:39 -07001963 avifFree(decoder);
1964}
1965
Joe Drago46ea0582019-07-22 15:55:47 -07001966avifResult avifDecoderSetSource(avifDecoder * decoder, avifDecoderSource source)
Joe Drago444f0512019-01-23 17:03:24 -08001967{
Joe Drago46ea0582019-07-22 15:55:47 -07001968 decoder->requestedSource = source;
1969 return avifDecoderReset(decoder);
1970}
Joe Drago33f1d362019-02-13 16:46:22 -08001971
Wan-Teh Change184dc12020-05-11 12:47:21 -07001972avifResult avifDecoderParse(avifDecoder * decoder, const avifROData * rawInput)
Joe Drago46ea0582019-07-22 15:55:47 -07001973{
Joe Drago46ea0582019-07-22 15:55:47 -07001974 // Cleanup anything lingering in the decoder
1975 avifDecoderCleanup(decoder);
1976
Joe Drago444f0512019-01-23 17:03:24 -08001977 // -----------------------------------------------------------------------
1978 // Parse BMFF boxes
1979
Joe Drago800b47f2020-03-18 16:22:37 -07001980 decoder->data = avifDecoderDataCreate();
Joe Drago46ea0582019-07-22 15:55:47 -07001981
1982 // Shallow copy, on purpose
Joe Drago345aaa12019-09-25 13:42:12 -07001983 memcpy(&decoder->data->rawInput, rawInput, sizeof(avifROData));
Joe Drago46ea0582019-07-22 15:55:47 -07001984
1985 if (!avifParse(decoder->data, decoder->data->rawInput.data, decoder->data->rawInput.size)) {
1986 return AVIF_RESULT_BMFF_PARSE_FAILED;
Joe Drago444f0512019-01-23 17:03:24 -08001987 }
1988
Joe Drago7e37b972019-07-24 12:44:47 -07001989 avifBool avifCompatible = avifFileTypeIsCompatible(&decoder->data->ftyp);
Joe Drago8f7a3002019-02-07 19:35:37 -08001990 if (!avifCompatible) {
Joe Drago46ea0582019-07-22 15:55:47 -07001991 return AVIF_RESULT_INVALID_FTYP;
1992 }
1993
1994 // Sanity check items
Joe Drago9f2b87b2020-06-03 19:36:38 -07001995 for (uint32_t itemIndex = 0; itemIndex < decoder->data->meta->items.count; ++itemIndex) {
1996 avifDecoderItem * item = &decoder->data->meta->items.item[itemIndex];
Joe Drago3320e5f2020-04-21 17:36:27 -07001997 if (item->hasUnsupportedEssentialProperty) {
1998 // An essential property isn't supported by libavif; ignore the item.
1999 continue;
2000 }
Joe Drago800b47f2020-03-18 16:22:37 -07002001 const uint8_t * p = avifDecoderDataCalcItemPtr(decoder->data, item);
Joe Dragof6a42272019-11-21 15:21:41 -08002002 if (p == NULL) {
Joe Drago46ea0582019-07-22 15:55:47 -07002003 return AVIF_RESULT_BMFF_PARSE_FAILED;
2004 }
2005 }
2006
2007 // Sanity check tracks
2008 for (uint32_t trackIndex = 0; trackIndex < decoder->data->tracks.count; ++trackIndex) {
2009 avifTrack * track = &decoder->data->tracks.track[trackIndex];
2010 if (!track->sampleTable) {
2011 continue;
2012 }
2013
2014 for (uint32_t chunkIndex = 0; chunkIndex < track->sampleTable->chunks.count; ++chunkIndex) {
2015 avifSampleTableChunk * chunk = &track->sampleTable->chunks.chunk[chunkIndex];
2016 if (chunk->offset > decoder->data->rawInput.size) {
2017 return AVIF_RESULT_BMFF_PARSE_FAILED;
2018 }
2019 }
2020 }
2021 return avifDecoderReset(decoder);
2022}
2023
Joe Drago53355352019-10-28 19:04:51 -07002024static avifCodec * avifCodecCreateInternal(avifCodecChoice choice, avifCodecDecodeInput * decodeInput)
Joe Drago46ea0582019-07-22 15:55:47 -07002025{
Joe Drago53355352019-10-28 19:04:51 -07002026 avifCodec * codec = avifCodecCreate(choice, AVIF_CODEC_FLAG_CAN_DECODE);
Joe Drago46ea0582019-07-22 15:55:47 -07002027 if (codec) {
2028 codec->decodeInput = decodeInput;
2029 }
2030 return codec;
2031}
2032
Joe Drago22c1ad92019-09-26 12:46:50 -07002033static avifResult avifDecoderFlush(avifDecoder * decoder)
2034{
Joe Drago800b47f2020-03-18 16:22:37 -07002035 avifDecoderDataResetCodec(decoder->data);
Joe Drago22c1ad92019-09-26 12:46:50 -07002036
Joe Drago060d5342020-03-03 10:53:49 -08002037 for (unsigned int i = 0; i < decoder->data->tiles.count; ++i) {
2038 avifTile * tile = &decoder->data->tiles.tile[i];
2039 tile->codec = avifCodecCreateInternal(decoder->codecChoice, tile->input);
2040 if (!tile->codec) {
Joe Drago53355352019-10-28 19:04:51 -07002041 return AVIF_RESULT_NO_CODEC_AVAILABLE;
2042 }
Joe Drago060d5342020-03-03 10:53:49 -08002043 if (!tile->codec->open(tile->codec, decoder->imageIndex + 1)) {
2044 return AVIF_RESULT_DECODE_COLOR_FAILED;
Joe Drago22c1ad92019-09-26 12:46:50 -07002045 }
2046 }
2047 return AVIF_RESULT_OK;
2048}
2049
Joe Drago46ea0582019-07-22 15:55:47 -07002050avifResult avifDecoderReset(avifDecoder * decoder)
2051{
Joe Drago800b47f2020-03-18 16:22:37 -07002052 avifDecoderData * data = decoder->data;
Joe Drago46ea0582019-07-22 15:55:47 -07002053 if (!data) {
2054 // Nothing to reset.
2055 return AVIF_RESULT_OK;
2056 }
2057
Joe Drago060d5342020-03-03 10:53:49 -08002058 memset(&data->colorGrid, 0, sizeof(data->colorGrid));
2059 memset(&data->alphaGrid, 0, sizeof(data->alphaGrid));
Joe Drago800b47f2020-03-18 16:22:37 -07002060 avifDecoderDataClearTiles(data);
Joe Drago89f0cc82020-03-09 16:13:27 -07002061
2062 // Prepare / cleanup decoded image state
Joe Dragoa0da4a42020-05-08 14:27:40 -07002063 if (decoder->image) {
2064 avifImageDestroy(decoder->image);
Joe Drago8f7a3002019-02-07 19:35:37 -08002065 }
Joe Dragoa0da4a42020-05-08 14:27:40 -07002066 decoder->image = avifImageCreateEmpty();
2067 data->cicpSet = AVIF_FALSE;
Joe Drago8f7a3002019-02-07 19:35:37 -08002068
Joe Drago70cbf602019-07-24 15:30:55 -07002069 memset(&decoder->ioStats, 0, sizeof(decoder->ioStats));
2070
Joe Drago444f0512019-01-23 17:03:24 -08002071 // -----------------------------------------------------------------------
Joe Drago46ea0582019-07-22 15:55:47 -07002072 // Build decode input
Joe Drago444f0512019-01-23 17:03:24 -08002073
Joe Drago46ea0582019-07-22 15:55:47 -07002074 data->sourceSampleTable = NULL; // Reset
2075 if (decoder->requestedSource == AVIF_DECODER_SOURCE_AUTO) {
2076 if (data->tracks.count > 0) {
2077 data->source = AVIF_DECODER_SOURCE_TRACKS;
2078 } else {
2079 data->source = AVIF_DECODER_SOURCE_PRIMARY_ITEM;
Joe Drago76370232019-07-16 11:00:52 -07002080 }
Joe Drago46ea0582019-07-22 15:55:47 -07002081 } else {
2082 data->source = decoder->requestedSource;
Joe Drago76370232019-07-16 11:00:52 -07002083 }
2084
Joe Dragoa72da5b2020-06-15 19:40:17 -07002085 const avifPropertyArray * colorProperties = NULL;
Joe Drago46ea0582019-07-22 15:55:47 -07002086 if (data->source == AVIF_DECODER_SOURCE_TRACKS) {
2087 avifTrack * colorTrack = NULL;
2088 avifTrack * alphaTrack = NULL;
2089
2090 // Find primary track - this probably needs some better detection
2091 uint32_t colorTrackIndex = 0;
wantehchangb207b4d2020-08-11 17:50:22 -07002092 for (; colorTrackIndex < data->tracks.count; ++colorTrackIndex) {
2093 avifTrack * track = &data->tracks.track[colorTrackIndex];
Joe Drago46ea0582019-07-22 15:55:47 -07002094 if (!track->sampleTable) {
2095 continue;
2096 }
Joe Dragoba1eb492020-06-22 17:05:04 -07002097 if (!track->id) { // trak box might be missing a tkhd box inside, skip it
Joe Drago4a25c192020-06-03 16:29:58 -07002098 continue;
2099 }
Joe Drago46ea0582019-07-22 15:55:47 -07002100 if (!track->sampleTable->chunks.count) {
2101 continue;
2102 }
Joe Drago2c0924c2019-09-26 17:41:01 -07002103 if (!avifSampleTableHasFormat(track->sampleTable, "av01")) {
2104 continue;
2105 }
Joe Drago46ea0582019-07-22 15:55:47 -07002106 if (track->auxForID != 0) {
2107 continue;
2108 }
2109
2110 // Found one!
Joe Drago444f0512019-01-23 17:03:24 -08002111 break;
2112 }
wantehchangb207b4d2020-08-11 17:50:22 -07002113 if (colorTrackIndex == data->tracks.count) {
Joe Drago46ea0582019-07-22 15:55:47 -07002114 return AVIF_RESULT_NO_CONTENT;
Joe Drago444f0512019-01-23 17:03:24 -08002115 }
wantehchangb207b4d2020-08-11 17:50:22 -07002116 colorTrack = &data->tracks.track[colorTrackIndex];
Joe Drago46ea0582019-07-22 15:55:47 -07002117
Joe Dragoa72da5b2020-06-15 19:40:17 -07002118 colorProperties = avifSampleTableGetProperties(colorTrack->sampleTable);
2119 if (!colorProperties) {
2120 return AVIF_RESULT_BMFF_PARSE_FAILED;
2121 }
2122
2123 // Find Exif and/or XMP metadata, if any
2124 if (colorTrack->meta) {
Joe Drago39267fd2020-06-19 15:21:14 -07002125 // See the comment above avifDecoderDataFindMetadata() for the explanation of using 0 here
Joe Dragoa72da5b2020-06-15 19:40:17 -07002126 if (!avifDecoderDataFindMetadata(data, colorTrack->meta, decoder->image, 0)) {
2127 return AVIF_RESULT_BMFF_PARSE_FAILED;
2128 }
2129 }
2130
Joe Drago46ea0582019-07-22 15:55:47 -07002131 uint32_t alphaTrackIndex = 0;
wantehchangb207b4d2020-08-11 17:50:22 -07002132 for (; alphaTrackIndex < data->tracks.count; ++alphaTrackIndex) {
2133 avifTrack * track = &data->tracks.track[alphaTrackIndex];
Joe Drago46ea0582019-07-22 15:55:47 -07002134 if (!track->sampleTable) {
2135 continue;
2136 }
Joe Drago4a25c192020-06-03 16:29:58 -07002137 if (!track->id) {
2138 continue;
2139 }
Joe Drago46ea0582019-07-22 15:55:47 -07002140 if (!track->sampleTable->chunks.count) {
2141 continue;
2142 }
Joe Drago2c0924c2019-09-26 17:41:01 -07002143 if (!avifSampleTableHasFormat(track->sampleTable, "av01")) {
2144 continue;
2145 }
Joe Drago46ea0582019-07-22 15:55:47 -07002146 if (track->auxForID == colorTrack->id) {
2147 // Found it!
2148 break;
2149 }
2150 }
wantehchangb207b4d2020-08-11 17:50:22 -07002151 if (alphaTrackIndex != data->tracks.count) {
2152 alphaTrack = &data->tracks.track[alphaTrackIndex];
Joe Drago8f7a3002019-02-07 19:35:37 -08002153 }
Joe Drago444f0512019-01-23 17:03:24 -08002154
wantehchangb207b4d2020-08-11 17:50:22 -07002155 avifTile * colorTile = avifDecoderDataCreateTile(data);
2156 if (!avifCodecDecodeInputGetSamples(colorTile->input, colorTrack->sampleTable, &data->rawInput)) {
Joe Drago46ea0582019-07-22 15:55:47 -07002157 return AVIF_RESULT_BMFF_PARSE_FAILED;
2158 }
wantehchangb207b4d2020-08-11 17:50:22 -07002159 data->colorTileCount = 1;
Joe Drago46ea0582019-07-22 15:55:47 -07002160
2161 if (alphaTrack) {
wantehchang76e16bf2020-08-12 13:01:31 -07002162 avifTile * alphaTile = avifDecoderDataCreateTile(data);
wantehchangb207b4d2020-08-11 17:50:22 -07002163 if (!avifCodecDecodeInputGetSamples(alphaTile->input, alphaTrack->sampleTable, &data->rawInput)) {
Joe Drago46ea0582019-07-22 15:55:47 -07002164 return AVIF_RESULT_BMFF_PARSE_FAILED;
2165 }
Joe Drago060d5342020-03-03 10:53:49 -08002166 alphaTile->input->alpha = AVIF_TRUE;
wantehchangb207b4d2020-08-11 17:50:22 -07002167 data->alphaTileCount = 1;
Joe Drago46ea0582019-07-22 15:55:47 -07002168 }
2169
2170 // Stash off sample table for future timing information
2171 data->sourceSampleTable = colorTrack->sampleTable;
2172
2173 // Image sequence timing
2174 decoder->imageIndex = -1;
Joe Drago060d5342020-03-03 10:53:49 -08002175 decoder->imageCount = colorTile->input->samples.count;
Joe Drago46ea0582019-07-22 15:55:47 -07002176 decoder->timescale = colorTrack->mediaTimescale;
2177 decoder->durationInTimescales = colorTrack->mediaDuration;
2178 if (colorTrack->mediaTimescale) {
2179 decoder->duration = (double)decoder->durationInTimescales / (double)colorTrack->mediaTimescale;
2180 } else {
2181 decoder->duration = 0;
2182 }
2183 memset(&decoder->imageTiming, 0, sizeof(decoder->imageTiming)); // to be set in avifDecoderNextImage()
Joe Drago41700852019-09-26 17:01:43 -07002184
Joe Dragoc554f5f2020-06-09 18:59:51 -07002185 decoder->image->width = colorTrack->width;
2186 decoder->image->height = colorTrack->height;
Joe Dragoc554f5f2020-06-09 18:59:51 -07002187 decoder->alphaPresent = (alphaTrack != NULL);
Joe Drago46ea0582019-07-22 15:55:47 -07002188 } else {
2189 // Create from items
2190
Joe Drago345aaa12019-09-25 13:42:12 -07002191 avifROData colorOBU = AVIF_DATA_EMPTY;
2192 avifROData alphaOBU = AVIF_DATA_EMPTY;
Joe Drago800b47f2020-03-18 16:22:37 -07002193 avifDecoderItem * colorOBUItem = NULL;
2194 avifDecoderItem * alphaOBUItem = NULL;
Joe Drago46ea0582019-07-22 15:55:47 -07002195
Joe Dragof6a42272019-11-21 15:21:41 -08002196 // Find the colorOBU (primary) item
Joe Drago9f2b87b2020-06-03 19:36:38 -07002197 for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) {
2198 avifDecoderItem * item = &data->meta->items.item[itemIndex];
Joe Dragoba1eb492020-06-22 17:05:04 -07002199 if (!item->size) {
2200 continue;
Joe Drago8f7a3002019-02-07 19:35:37 -08002201 }
Joe Drago3320e5f2020-04-21 17:36:27 -07002202 if (item->hasUnsupportedEssentialProperty) {
2203 // An essential property isn't supported by libavif; ignore the item.
2204 continue;
2205 }
Joe Drago951a0022020-03-09 16:19:44 -07002206 avifBool isGrid = (memcmp(item->type, "grid", 4) == 0);
Joe Drago060d5342020-03-03 10:53:49 -08002207 if (memcmp(item->type, "av01", 4) && !isGrid) {
Joe Drago8f7a3002019-02-07 19:35:37 -08002208 // probably exif or some other data
2209 continue;
2210 }
2211 if (item->thumbnailForID != 0) {
2212 // It's a thumbnail, skip it
2213 continue;
2214 }
Joe Drago9f2b87b2020-06-03 19:36:38 -07002215 if ((data->meta->primaryItemID > 0) && (item->id != data->meta->primaryItemID)) {
Joe Dragof6a42272019-11-21 15:21:41 -08002216 // a primary item ID was specified, require it
2217 continue;
2218 }
Joe Drago8f7a3002019-02-07 19:35:37 -08002219
Joe Drago060d5342020-03-03 10:53:49 -08002220 if (isGrid) {
Wan-Teh Changcb164942020-08-10 15:47:29 -07002221 if (decoder->disableGridImages) {
2222 return AVIF_RESULT_BMFF_PARSE_FAILED;
2223 }
Joe Drago800b47f2020-03-18 16:22:37 -07002224 const uint8_t * itemPtr = avifDecoderDataCalcItemPtr(data, item);
Joe Drago060d5342020-03-03 10:53:49 -08002225 if (itemPtr == NULL) {
2226 return AVIF_RESULT_BMFF_PARSE_FAILED;
2227 }
2228 if (!avifParseImageGridBox(&data->colorGrid, itemPtr, item->size)) {
2229 return AVIF_RESULT_INVALID_IMAGE_GRID;
2230 }
2231 } else {
Joe Drago800b47f2020-03-18 16:22:37 -07002232 colorOBU.data = avifDecoderDataCalcItemPtr(data, item);
Joe Drago060d5342020-03-03 10:53:49 -08002233 colorOBU.size = item->size;
2234 }
2235
Joe Drago46ea0582019-07-22 15:55:47 -07002236 colorOBUItem = item;
Joe Drago46ea0582019-07-22 15:55:47 -07002237 break;
2238 }
2239
Joe Drago060d5342020-03-03 10:53:49 -08002240 if (!colorOBUItem) {
2241 return AVIF_RESULT_NO_AV1_ITEMS_FOUND;
2242 }
Joe Dragoa72da5b2020-06-15 19:40:17 -07002243 colorProperties = &colorOBUItem->properties;
Joe Drago46ea0582019-07-22 15:55:47 -07002244
Joe Drago060d5342020-03-03 10:53:49 -08002245 // Find the alphaOBU item, if any
Joe Drago9f2b87b2020-06-03 19:36:38 -07002246 for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) {
2247 avifDecoderItem * item = &data->meta->items.item[itemIndex];
Joe Dragoba1eb492020-06-22 17:05:04 -07002248 if (!item->size) {
2249 continue;
Joe Drago060d5342020-03-03 10:53:49 -08002250 }
Joe Drago3320e5f2020-04-21 17:36:27 -07002251 if (item->hasUnsupportedEssentialProperty) {
2252 // An essential property isn't supported by libavif; ignore the item.
2253 continue;
2254 }
Joe Drago951a0022020-03-09 16:19:44 -07002255 avifBool isGrid = (memcmp(item->type, "grid", 4) == 0);
Joe Drago060d5342020-03-03 10:53:49 -08002256 if (memcmp(item->type, "av01", 4) && !isGrid) {
2257 // probably exif or some other data
2258 continue;
2259 }
2260 if (item->thumbnailForID != 0) {
2261 // It's a thumbnail, skip it
2262 continue;
Joe Drago8f7a3002019-02-07 19:35:37 -08002263 }
Joe Dragof6a42272019-11-21 15:21:41 -08002264
Joe Dragoa72da5b2020-06-15 19:40:17 -07002265 const avifProperty * auxCProp = avifPropertyArrayFind(&item->properties, "auxC");
2266 if (auxCProp && isAlphaURN(auxCProp->u.auxC.auxType) && (item->auxForID == colorOBUItem->id)) {
Joe Drago060d5342020-03-03 10:53:49 -08002267 if (isGrid) {
Wan-Teh Changcb164942020-08-10 15:47:29 -07002268 if (decoder->disableGridImages) {
2269 return AVIF_RESULT_BMFF_PARSE_FAILED;
2270 }
Joe Drago800b47f2020-03-18 16:22:37 -07002271 const uint8_t * itemPtr = avifDecoderDataCalcItemPtr(data, item);
Joe Drago060d5342020-03-03 10:53:49 -08002272 if (itemPtr == NULL) {
2273 return AVIF_RESULT_BMFF_PARSE_FAILED;
2274 }
2275 if (!avifParseImageGridBox(&data->alphaGrid, itemPtr, item->size)) {
2276 return AVIF_RESULT_INVALID_IMAGE_GRID;
2277 }
2278 } else {
Joe Drago800b47f2020-03-18 16:22:37 -07002279 alphaOBU.data = avifDecoderDataCalcItemPtr(data, item);
Joe Drago060d5342020-03-03 10:53:49 -08002280 alphaOBU.size = item->size;
Joe Dragof6a42272019-11-21 15:21:41 -08002281 }
2282
Joe Drago060d5342020-03-03 10:53:49 -08002283 alphaOBUItem = item;
2284 break;
Joe Dragof6a42272019-11-21 15:21:41 -08002285 }
Joe Drago444f0512019-01-23 17:03:24 -08002286 }
Joe Drago444f0512019-01-23 17:03:24 -08002287
Joe Drago060d5342020-03-03 10:53:49 -08002288 // Find Exif and/or XMP metadata, if any
Joe Dragoa72da5b2020-06-15 19:40:17 -07002289 if (!avifDecoderDataFindMetadata(data, data->meta, decoder->image, colorOBUItem->id)) {
2290 return AVIF_RESULT_BMFF_PARSE_FAILED;
Joe Drago060d5342020-03-03 10:53:49 -08002291 }
2292
Wan-Teh Chang4295bcb2020-04-05 15:41:05 -07002293 if ((data->colorGrid.rows > 0) && (data->colorGrid.columns > 0)) {
Joe Drago800b47f2020-03-18 16:22:37 -07002294 if (!avifDecoderDataGenerateImageGridTiles(data, &data->colorGrid, colorOBUItem, AVIF_FALSE)) {
Joe Drago060d5342020-03-03 10:53:49 -08002295 return AVIF_RESULT_INVALID_IMAGE_GRID;
2296 }
2297 data->colorTileCount = data->tiles.count;
2298 } else {
2299 if (colorOBU.size == 0) {
2300 return AVIF_RESULT_NO_AV1_ITEMS_FOUND;
2301 }
2302
wantehchangb207b4d2020-08-11 17:50:22 -07002303 avifTile * colorTile = avifDecoderDataCreateTile(data);
Joe Dragoe3e3bfa2020-06-02 16:33:53 -07002304 avifDecodeSample * colorSample = (avifDecodeSample *)avifArrayPushPtr(&colorTile->input->samples);
Joe Drago060d5342020-03-03 10:53:49 -08002305 memcpy(&colorSample->data, &colorOBU, sizeof(avifROData));
2306 colorSample->sync = AVIF_TRUE;
wantehchangb207b4d2020-08-11 17:50:22 -07002307 data->colorTileCount = 1;
Joe Drago060d5342020-03-03 10:53:49 -08002308 }
2309
Benbuck Nason78e3c9d2020-03-27 14:35:27 -07002310 if ((data->alphaGrid.rows > 0) && (data->alphaGrid.columns > 0) && alphaOBUItem) {
Wan-Teh Chang5b992162020-08-10 14:57:05 -07002311 if (!avifDecoderDataGenerateImageGridTiles(data, &data->alphaGrid, alphaOBUItem, AVIF_TRUE)) {
Joe Drago060d5342020-03-03 10:53:49 -08002312 return AVIF_RESULT_INVALID_IMAGE_GRID;
2313 }
2314 data->alphaTileCount = data->tiles.count - data->colorTileCount;
2315 } else {
Joe Drago060d5342020-03-03 10:53:49 -08002316 if (alphaOBU.size > 0) {
wantehchang76e16bf2020-08-12 13:01:31 -07002317 avifTile * alphaTile = avifDecoderDataCreateTile(data);
Joe Dragoe3e3bfa2020-06-02 16:33:53 -07002318 avifDecodeSample * alphaSample = (avifDecodeSample *)avifArrayPushPtr(&alphaTile->input->samples);
Joe Drago060d5342020-03-03 10:53:49 -08002319 memcpy(&alphaSample->data, &alphaOBU, sizeof(avifROData));
2320 alphaSample->sync = AVIF_TRUE;
2321 alphaTile->input->alpha = AVIF_TRUE;
wantehchangb207b4d2020-08-11 17:50:22 -07002322 data->alphaTileCount = 1;
Joe Drago060d5342020-03-03 10:53:49 -08002323 }
Joe Drago444f0512019-01-23 17:03:24 -08002324 }
Joe Drago33f1d362019-02-13 16:46:22 -08002325
Joe Drago46ea0582019-07-22 15:55:47 -07002326 // Set all counts and timing to safe-but-uninteresting values
2327 decoder->imageIndex = -1;
2328 decoder->imageCount = 1;
2329 decoder->imageTiming.timescale = 1;
2330 decoder->imageTiming.pts = 0;
2331 decoder->imageTiming.ptsInTimescales = 0;
2332 decoder->imageTiming.duration = 1;
2333 decoder->imageTiming.durationInTimescales = 1;
2334 decoder->timescale = 1;
2335 decoder->duration = 1;
2336 decoder->durationInTimescales = 1;
Joe Drago70cbf602019-07-24 15:30:55 -07002337
2338 decoder->ioStats.colorOBUSize = colorOBU.size;
2339 decoder->ioStats.alphaOBUSize = alphaOBU.size;
Joe Drago41700852019-09-26 17:01:43 -07002340
Joe Dragoa72da5b2020-06-15 19:40:17 -07002341 const avifProperty * ispeProp = avifPropertyArrayFind(colorProperties, "ispe");
2342 if (ispeProp) {
2343 decoder->image->width = ispeProp->u.ispe.width;
2344 decoder->image->height = ispeProp->u.ispe.height;
Joe Drago41700852019-09-26 17:01:43 -07002345 } else {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002346 decoder->image->width = 0;
2347 decoder->image->height = 0;
Joe Drago41700852019-09-26 17:01:43 -07002348 }
Joe Dragoc554f5f2020-06-09 18:59:51 -07002349 decoder->alphaPresent = (alphaOBUItem != NULL);
Joe Drago00bcaaf2020-06-05 15:29:38 -07002350 }
2351
Joe Drago11f2a5e2020-07-06 10:49:00 -07002352 // Sanity check tiles
2353 for (uint32_t tileIndex = 0; tileIndex < data->tiles.count; ++tileIndex) {
2354 avifTile * tile = &data->tiles.tile[tileIndex];
2355 for (uint32_t sampleIndex = 0; sampleIndex < tile->input->samples.count; ++sampleIndex) {
Joe Drago043311b2020-07-06 16:48:41 -07002356 avifDecodeSample * sample = &tile->input->samples.sample[sampleIndex];
Joe Drago11f2a5e2020-07-06 10:49:00 -07002357 if (!sample->data.data || !sample->data.size) {
2358 // Every sample must have some data
2359 return AVIF_RESULT_BMFF_PARSE_FAILED;
2360 }
2361 }
2362 }
2363
Joe Dragoa72da5b2020-06-15 19:40:17 -07002364 const avifProperty * colrProp = avifPropertyArrayFind(colorProperties, "colr");
2365 if (colrProp) {
2366 if (colrProp->u.colr.hasICC) {
2367 avifImageSetProfileICC(decoder->image, colrProp->u.colr.icc, colrProp->u.colr.iccSize);
2368 } else if (colrProp->u.colr.hasNCLX) {
2369 data->cicpSet = AVIF_TRUE;
2370 decoder->image->colorPrimaries = colrProp->u.colr.colorPrimaries;
2371 decoder->image->transferCharacteristics = colrProp->u.colr.transferCharacteristics;
2372 decoder->image->matrixCoefficients = colrProp->u.colr.matrixCoefficients;
2373 decoder->image->yuvRange = colrProp->u.colr.range;
2374 }
2375 }
2376
2377 // Transformations
2378 const avifProperty * paspProp = avifPropertyArrayFind(colorProperties, "pasp");
2379 if (paspProp) {
2380 decoder->image->transformFlags |= AVIF_TRANSFORM_PASP;
2381 memcpy(&decoder->image->pasp, &paspProp->u.pasp, sizeof(avifPixelAspectRatioBox));
2382 }
2383 const avifProperty * clapProp = avifPropertyArrayFind(colorProperties, "clap");
2384 if (clapProp) {
2385 decoder->image->transformFlags |= AVIF_TRANSFORM_CLAP;
2386 memcpy(&decoder->image->clap, &clapProp->u.clap, sizeof(avifCleanApertureBox));
2387 }
2388 const avifProperty * irotProp = avifPropertyArrayFind(colorProperties, "irot");
2389 if (irotProp) {
2390 decoder->image->transformFlags |= AVIF_TRANSFORM_IROT;
2391 memcpy(&decoder->image->irot, &irotProp->u.irot, sizeof(avifImageRotation));
2392 }
2393 const avifProperty * imirProp = avifPropertyArrayFind(colorProperties, "imir");
2394 if (imirProp) {
2395 decoder->image->transformFlags |= AVIF_TRANSFORM_IMIR;
2396 memcpy(&decoder->image->imir, &imirProp->u.imir, sizeof(avifImageMirror));
2397 }
2398
wantehchangb207b4d2020-08-11 17:50:22 -07002399 if (!data->cicpSet && (data->tiles.count > 0)) {
Joe Dragoda92a382020-06-09 17:08:45 -07002400 avifTile * firstTile = &data->tiles.tile[0];
2401 if (firstTile->input->samples.count > 0) {
2402 avifDecodeSample * sample = &firstTile->input->samples.sample[0];
2403 avifSequenceHeader sequenceHeader;
2404 if (avifSequenceHeaderParse(&sequenceHeader, &sample->data)) {
wantehchangb207b4d2020-08-11 17:50:22 -07002405 data->cicpSet = AVIF_TRUE;
Joe Dragoda92a382020-06-09 17:08:45 -07002406 decoder->image->colorPrimaries = sequenceHeader.colorPrimaries;
2407 decoder->image->transferCharacteristics = sequenceHeader.transferCharacteristics;
2408 decoder->image->matrixCoefficients = sequenceHeader.matrixCoefficients;
Joe Dragoc1d18952020-06-09 18:24:36 -07002409 decoder->image->yuvRange = sequenceHeader.range;
Joe Dragoda92a382020-06-09 17:08:45 -07002410 }
2411 }
2412 }
2413
Joe Dragoa72da5b2020-06-15 19:40:17 -07002414 const avifProperty * av1CProp = avifPropertyArrayFind(colorProperties, "av1C");
2415 if (av1CProp) {
Joe Dragob8401122020-06-19 11:45:49 -07002416 decoder->image->depth = avifCodecConfigurationBoxGetDepth(&av1CProp->u.av1C);
2417 if (av1CProp->u.av1C.monochrome) {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002418 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV400;
Joe Drago7b2cf802020-06-09 17:57:23 -07002419 } else {
Joe Dragob8401122020-06-19 11:45:49 -07002420 if (av1CProp->u.av1C.chromaSubsamplingX && av1CProp->u.av1C.chromaSubsamplingY) {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002421 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV420;
Joe Dragob8401122020-06-19 11:45:49 -07002422 } else if (av1CProp->u.av1C.chromaSubsamplingX) {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002423 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV422;
Joe Drago7b2cf802020-06-09 17:57:23 -07002424
2425 } else {
Joe Dragoc554f5f2020-06-09 18:59:51 -07002426 decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV444;
Joe Drago7b2cf802020-06-09 17:57:23 -07002427 }
2428 }
Joe Dragob8401122020-06-19 11:45:49 -07002429 decoder->image->yuvChromaSamplePosition = (avifChromaSamplePosition)av1CProp->u.av1C.chromaSamplePosition;
Joe Drago00bcaaf2020-06-05 15:29:38 -07002430 } else {
Joe Dragof48a3382020-06-19 14:13:44 -07002431 // An av1C box is mandatory in all valid AVIF configurations. Bail out.
2432 return AVIF_RESULT_BMFF_PARSE_FAILED;
Joe Drago46ea0582019-07-22 15:55:47 -07002433 }
2434
Joe Drago22c1ad92019-09-26 12:46:50 -07002435 return avifDecoderFlush(decoder);
Joe Drago46ea0582019-07-22 15:55:47 -07002436}
Joe Drago444f0512019-01-23 17:03:24 -08002437
Joe Drago46ea0582019-07-22 15:55:47 -07002438avifResult avifDecoderNextImage(avifDecoder * decoder)
2439{
Joe Drago060d5342020-03-03 10:53:49 -08002440 for (unsigned int tileIndex = 0; tileIndex < decoder->data->tiles.count; ++tileIndex) {
2441 avifTile * tile = &decoder->data->tiles.tile[tileIndex];
Joe Drago41eb62b2019-02-08 15:38:18 -08002442
Joe Drago060d5342020-03-03 10:53:49 -08002443 if (!tile->codec->getNextImage(tile->codec, tile->image)) {
2444 if (tile->input->alpha) {
2445 return AVIF_RESULT_DECODE_ALPHA_FAILED;
2446 } else {
2447 if (tile->image->width) {
2448 // We've sent at least one image, but we've run out now.
2449 return AVIF_RESULT_NO_IMAGES_REMAINING;
2450 }
2451 return AVIF_RESULT_DECODE_COLOR_FAILED;
2452 }
Joe Drago46ea0582019-07-22 15:55:47 -07002453 }
Joe Drago060d5342020-03-03 10:53:49 -08002454 }
2455
2456 if (decoder->data->tiles.count != (decoder->data->colorTileCount + decoder->data->alphaTileCount)) {
2457 // TODO: assert here? This should be impossible.
2458 return AVIF_RESULT_UNKNOWN_ERROR;
2459 }
2460
2461 if ((decoder->data->colorGrid.rows > 0) || (decoder->data->colorGrid.columns > 0)) {
Joe Drago800b47f2020-03-18 16:22:37 -07002462 if (!avifDecoderDataFillImageGrid(
2463 decoder->data, &decoder->data->colorGrid, decoder->image, 0, decoder->data->colorTileCount, AVIF_FALSE)) {
Joe Drago060d5342020-03-03 10:53:49 -08002464 return AVIF_RESULT_INVALID_IMAGE_GRID;
2465 }
2466 } else {
2467 // Normal (most common) non-grid path. Just steal the planes from the only "tile".
2468
2469 if (decoder->data->colorTileCount != 1) {
2470 return AVIF_RESULT_DECODE_COLOR_FAILED;
2471 }
2472
2473 avifImage * srcColor = decoder->data->tiles.tile[0].image;
2474
2475 if ((decoder->image->width != srcColor->width) || (decoder->image->height != srcColor->height) ||
2476 (decoder->image->depth != srcColor->depth)) {
2477 avifImageFreePlanes(decoder->image, AVIF_PLANES_ALL);
2478
2479 decoder->image->width = srcColor->width;
2480 decoder->image->height = srcColor->height;
2481 decoder->image->depth = srcColor->depth;
Joe Dragoc554f5f2020-06-09 18:59:51 -07002482 }
psi / Ryo Hirafuji922d8a12020-03-10 03:24:57 +09002483
Joe Dragoba1eb492020-06-22 17:05:04 -07002484#if 0
2485 // This code is currently unnecessary as the CICP is always set by the end of avifDecoderParse().
Joe Dragoc554f5f2020-06-09 18:59:51 -07002486 if (!decoder->data->cicpSet) {
2487 decoder->data->cicpSet = AVIF_TRUE;
2488 decoder->image->colorPrimaries = srcColor->colorPrimaries;
2489 decoder->image->transferCharacteristics = srcColor->transferCharacteristics;
2490 decoder->image->matrixCoefficients = srcColor->matrixCoefficients;
Joe Drago060d5342020-03-03 10:53:49 -08002491 }
Joe Dragoba1eb492020-06-22 17:05:04 -07002492#endif
Joe Drago060d5342020-03-03 10:53:49 -08002493
2494 avifImageStealPlanes(decoder->image, srcColor, AVIF_PLANES_YUV);
2495 }
2496
2497 if ((decoder->data->alphaGrid.rows > 0) || (decoder->data->alphaGrid.columns > 0)) {
Joe Drago800b47f2020-03-18 16:22:37 -07002498 if (!avifDecoderDataFillImageGrid(
Joe Drago060d5342020-03-03 10:53:49 -08002499 decoder->data, &decoder->data->alphaGrid, decoder->image, decoder->data->colorTileCount, decoder->data->alphaTileCount, AVIF_TRUE)) {
2500 return AVIF_RESULT_INVALID_IMAGE_GRID;
2501 }
2502 } else {
2503 // Normal (most common) non-grid path. Just steal the planes from the only "tile".
2504
2505 if (decoder->data->alphaTileCount == 0) {
2506 avifImageFreePlanes(decoder->image, AVIF_PLANES_A); // no alpha
2507 } else {
2508 if (decoder->data->alphaTileCount != 1) {
2509 return AVIF_RESULT_DECODE_ALPHA_FAILED;
2510 }
2511
2512 avifImage * srcAlpha = decoder->data->tiles.tile[decoder->data->colorTileCount].image;
2513 if ((decoder->image->width != srcAlpha->width) || (decoder->image->height != srcAlpha->height) ||
2514 (decoder->image->depth != srcAlpha->depth)) {
2515 return AVIF_RESULT_DECODE_ALPHA_FAILED;
2516 }
2517
2518 avifImageStealPlanes(decoder->image, srcAlpha, AVIF_PLANES_A);
2519 }
2520 }
Joe Drago7ad3ad62019-02-07 11:17:34 -08002521
Joe Drago46ea0582019-07-22 15:55:47 -07002522 ++decoder->imageIndex;
2523 if (decoder->data->sourceSampleTable) {
2524 // Decoding from a track! Provide timing information.
Joe Drago05559c92019-07-17 16:33:38 -07002525
Joe Dragoe9c58602020-04-13 17:23:13 -07002526 avifResult timingResult = avifDecoderNthImageTiming(decoder, decoder->imageIndex, &decoder->imageTiming);
2527 if (timingResult != AVIF_RESULT_OK) {
2528 return timingResult;
Joe Drago22c1ad92019-09-26 12:46:50 -07002529 }
Joe Dragoe9c58602020-04-13 17:23:13 -07002530 }
2531 return AVIF_RESULT_OK;
2532}
Joe Drago46ea0582019-07-22 15:55:47 -07002533
Wan-Teh Change184dc12020-05-11 12:47:21 -07002534avifResult avifDecoderNthImageTiming(const avifDecoder * decoder, uint32_t frameIndex, avifImageTiming * outTiming)
Joe Dragoe9c58602020-04-13 17:23:13 -07002535{
2536 if (!decoder->data) {
2537 // Nothing has been parsed yet
2538 return AVIF_RESULT_NO_CONTENT;
2539 }
2540
2541 if ((int)frameIndex >= decoder->imageCount) {
2542 // Impossible index
2543 return AVIF_RESULT_NO_IMAGES_REMAINING;
2544 }
2545
2546 if (!decoder->data->sourceSampleTable) {
2547 // There isn't any real timing associated with this decode, so
2548 // just hand back the defaults chosen in avifDecoderReset().
2549 memcpy(outTiming, &decoder->imageTiming, sizeof(avifImageTiming));
2550 return AVIF_RESULT_OK;
2551 }
2552
Wan-Teh Chang167e4062020-04-14 16:06:12 -07002553 outTiming->timescale = decoder->timescale;
2554 outTiming->ptsInTimescales = 0;
Joe Dragoe9c58602020-04-13 17:23:13 -07002555 for (int imageIndex = 0; imageIndex < (int)frameIndex; ++imageIndex) {
Wan-Teh Chang167e4062020-04-14 16:06:12 -07002556 outTiming->ptsInTimescales += avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, imageIndex);
Joe Dragoe9c58602020-04-13 17:23:13 -07002557 }
Wan-Teh Chang167e4062020-04-14 16:06:12 -07002558 outTiming->durationInTimescales = avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, frameIndex);
Joe Dragoe9c58602020-04-13 17:23:13 -07002559
Wan-Teh Chang167e4062020-04-14 16:06:12 -07002560 if (outTiming->timescale > 0) {
2561 outTiming->pts = (double)outTiming->ptsInTimescales / (double)outTiming->timescale;
2562 outTiming->duration = (double)outTiming->durationInTimescales / (double)outTiming->timescale;
Joe Dragoe9c58602020-04-13 17:23:13 -07002563 } else {
Wan-Teh Chang167e4062020-04-14 16:06:12 -07002564 outTiming->pts = 0.0;
2565 outTiming->duration = 0.0;
Joe Drago444f0512019-01-23 17:03:24 -08002566 }
Joe Drago46ea0582019-07-22 15:55:47 -07002567 return AVIF_RESULT_OK;
2568}
2569
Joe Drago22c1ad92019-09-26 12:46:50 -07002570avifResult avifDecoderNthImage(avifDecoder * decoder, uint32_t frameIndex)
2571{
2572 int requestedIndex = (int)frameIndex;
2573 if (requestedIndex == decoder->imageIndex) {
2574 // We're here already, nothing to do
2575 return AVIF_RESULT_OK;
2576 }
2577
2578 if (requestedIndex == (decoder->imageIndex + 1)) {
2579 // it's just the next image, nothing special here
2580 return avifDecoderNextImage(decoder);
2581 }
2582
2583 if (requestedIndex >= decoder->imageCount) {
2584 // Impossible index
2585 return AVIF_RESULT_NO_IMAGES_REMAINING;
2586 }
2587
2588 // If we get here, a decoder flush is necessary
Joe Drago22c1ad92019-09-26 12:46:50 -07002589 decoder->imageIndex = ((int)avifDecoderNearestKeyframe(decoder, frameIndex)) - 1; // prepare to read nearest keyframe
Joe Drago81978022019-09-26 18:23:57 -07002590 avifDecoderFlush(decoder);
Joe Drago22c1ad92019-09-26 12:46:50 -07002591 for (;;) {
2592 avifResult result = avifDecoderNextImage(decoder);
2593 if (result != AVIF_RESULT_OK) {
2594 return result;
2595 }
2596
2597 if (requestedIndex == decoder->imageIndex) {
2598 break;
2599 }
Joe Dragofc4144e2019-09-27 20:35:06 -07002600 }
Joe Drago22c1ad92019-09-26 12:46:50 -07002601 return AVIF_RESULT_OK;
2602}
2603
Wan-Teh Change184dc12020-05-11 12:47:21 -07002604avifBool avifDecoderIsKeyframe(const avifDecoder * decoder, uint32_t frameIndex)
Joe Drago22c1ad92019-09-26 12:46:50 -07002605{
Joe Drago060d5342020-03-03 10:53:49 -08002606 if ((decoder->data->tiles.count > 0) && decoder->data->tiles.tile[0].input) {
2607 if (frameIndex < decoder->data->tiles.tile[0].input->samples.count) {
2608 return decoder->data->tiles.tile[0].input->samples.sample[frameIndex].sync;
Joe Drago22c1ad92019-09-26 12:46:50 -07002609 }
2610 }
2611 return AVIF_FALSE;
2612}
2613
Wan-Teh Change184dc12020-05-11 12:47:21 -07002614uint32_t avifDecoderNearestKeyframe(const avifDecoder * decoder, uint32_t frameIndex)
Joe Drago22c1ad92019-09-26 12:46:50 -07002615{
2616 for (; frameIndex != 0; --frameIndex) {
2617 if (avifDecoderIsKeyframe(decoder, frameIndex)) {
2618 break;
2619 }
2620 }
2621 return frameIndex;
2622}
2623
Wan-Teh Change184dc12020-05-11 12:47:21 -07002624avifResult avifDecoderRead(avifDecoder * decoder, avifImage * image, const avifROData * input)
Joe Drago46ea0582019-07-22 15:55:47 -07002625{
2626 avifResult result = avifDecoderParse(decoder, input);
2627 if (result != AVIF_RESULT_OK) {
2628 return result;
Joe Drago05559c92019-07-17 16:33:38 -07002629 }
Joe Drago46ea0582019-07-22 15:55:47 -07002630 result = avifDecoderNextImage(decoder);
2631 if (result != AVIF_RESULT_OK) {
2632 return result;
2633 }
Joe Drago250221a2020-06-01 11:11:06 -07002634 avifImageCopy(image, decoder->image, AVIF_PLANES_ALL);
Joe Drago46ea0582019-07-22 15:55:47 -07002635 return AVIF_RESULT_OK;
Joe Drago444f0512019-01-23 17:03:24 -08002636}