Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1 | // Copyright 2019 Joe Drago. All rights reserved. |
| 2 | // SPDX-License-Identifier: BSD-2-Clause |
| 3 | |
| 4 | #include "avif/internal.h" |
| 5 | |
Wan-Teh Chang | b856dc2 | 2020-09-28 13:00:56 -0700 | [diff] [blame] | 6 | #include <assert.h> |
Wan-Teh Chang | 25b9c70 | 2020-10-08 12:17:17 -0700 | [diff] [blame] | 7 | #include <limits.h> |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 8 | #include <string.h> |
| 9 | |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 10 | #define AUXTYPE_SIZE 64 |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 11 | #define CONTENTTYPE_SIZE 64 |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 12 | |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 13 | // class VisualSampleEntry(codingname) extends SampleEntry(codingname) { |
| 14 | // unsigned int(16) pre_defined = 0; |
| 15 | // const unsigned int(16) reserved = 0; |
| 16 | // unsigned int(32)[3] pre_defined = 0; |
| 17 | // unsigned int(16) width; |
| 18 | // unsigned int(16) height; |
| 19 | // template unsigned int(32) horizresolution = 0x00480000; // 72 dpi |
| 20 | // template unsigned int(32) vertresolution = 0x00480000; // 72 dpi |
| 21 | // const unsigned int(32) reserved = 0; |
| 22 | // template unsigned int(16) frame_count = 1; |
| 23 | // string[32] compressorname; |
| 24 | // template unsigned int(16) depth = 0x0018; |
| 25 | // int(16) pre_defined = -1; |
| 26 | // // other boxes from derived specifications |
| 27 | // CleanApertureBox clap; // optional |
| 28 | // PixelAspectRatioBox pasp; // optional |
| 29 | // } |
| 30 | static const size_t VISUALSAMPLEENTRY_SIZE = 78; |
| 31 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 32 | static const char xmpContentType[] = CONTENT_TYPE_XMP; |
| 33 | static const size_t xmpContentTypeSize = sizeof(xmpContentType); |
| 34 | |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 35 | // --------------------------------------------------------------------------- |
| 36 | // Box data structures |
| 37 | |
| 38 | // ftyp |
| 39 | typedef struct avifFileType |
| 40 | { |
| 41 | uint8_t majorBrand[4]; |
| 42 | uint32_t minorVersion; |
Wan-Teh Chang | 6da0a88 | 2020-07-01 12:19:31 -0700 | [diff] [blame] | 43 | // If not null, points to a memory block of 4 * compatibleBrandsCount bytes. |
| 44 | const uint8_t * compatibleBrands; |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 45 | int compatibleBrandsCount; |
| 46 | } avifFileType; |
| 47 | |
| 48 | // ispe |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 49 | typedef struct avifImageSpatialExtents |
| 50 | { |
| 51 | uint32_t width; |
| 52 | uint32_t height; |
| 53 | } avifImageSpatialExtents; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 54 | |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 55 | // auxC |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 56 | typedef struct avifAuxiliaryType |
| 57 | { |
| 58 | char auxType[AUXTYPE_SIZE]; |
| 59 | } avifAuxiliaryType; |
| 60 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 61 | // infe mime content_type |
| 62 | typedef struct avifContentType |
| 63 | { |
| 64 | char contentType[CONTENTTYPE_SIZE]; |
| 65 | } avifContentType; |
| 66 | |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 67 | // colr |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 68 | typedef struct avifColourInformationBox |
| 69 | { |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 70 | avifBool hasICC; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 71 | const uint8_t * icc; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 72 | size_t iccSize; |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 73 | |
| 74 | avifBool hasNCLX; |
Wan-Teh Chang | 559def5 | 2021-02-01 14:25:31 -0800 | [diff] [blame] | 75 | avifColorPrimaries colorPrimaries; |
| 76 | avifTransferCharacteristics transferCharacteristics; |
| 77 | avifMatrixCoefficients matrixCoefficients; |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 78 | avifRange range; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 79 | } avifColourInformationBox; |
| 80 | |
Joe Drago | 6042156 | 2020-04-23 11:32:26 -0700 | [diff] [blame] | 81 | #define MAX_PIXI_PLANE_DEPTHS 4 |
| 82 | typedef struct avifPixelInformationProperty |
| 83 | { |
| 84 | uint8_t planeDepths[MAX_PIXI_PLANE_DEPTHS]; |
| 85 | uint8_t planeCount; |
| 86 | } avifPixelInformationProperty; |
| 87 | |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 88 | // --------------------------------------------------------------------------- |
| 89 | // Top-level structures |
| 90 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 91 | struct avifMeta; |
| 92 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 93 | // Temporary storage for ipco/stsd contents until they can be associated and memcpy'd to an avifDecoderItem |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 94 | typedef struct avifProperty |
| 95 | { |
| 96 | uint8_t type[4]; |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 97 | union |
| 98 | { |
| 99 | avifImageSpatialExtents ispe; |
| 100 | avifAuxiliaryType auxC; |
| 101 | avifColourInformationBox colr; |
| 102 | avifCodecConfigurationBox av1C; |
| 103 | avifPixelAspectRatioBox pasp; |
| 104 | avifCleanApertureBox clap; |
| 105 | avifImageRotation irot; |
| 106 | avifImageMirror imir; |
| 107 | avifPixelInformationProperty pixi; |
| 108 | } u; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 109 | } avifProperty; |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 110 | AVIF_ARRAY_DECLARE(avifPropertyArray, avifProperty, prop); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 111 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 112 | static const avifProperty * avifPropertyArrayFind(const avifPropertyArray * properties, const char * type) |
| 113 | { |
| 114 | for (uint32_t propertyIndex = 0; propertyIndex < properties->count; ++propertyIndex) { |
| 115 | avifProperty * prop = &properties->prop[propertyIndex]; |
| 116 | if (!memcmp(prop->type, type, 4)) { |
| 117 | return prop; |
| 118 | } |
| 119 | } |
| 120 | return NULL; |
| 121 | } |
| 122 | |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 123 | AVIF_ARRAY_DECLARE(avifExtentArray, avifExtent, extent); |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 124 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 125 | // one "item" worth for decoding (all iref, iloc, iprp, etc refer to one of these) |
| 126 | typedef struct avifDecoderItem |
| 127 | { |
| 128 | uint32_t id; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 129 | struct avifMeta * meta; // Unowned; A back-pointer for convenience |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 130 | uint8_t type[4]; |
Joe Drago | 217056b | 2020-11-13 16:19:35 -0800 | [diff] [blame] | 131 | size_t size; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 132 | uint32_t idatID; // If non-zero, offset is relative to this idat box (iloc construction_method==1) |
| 133 | avifContentType contentType; |
| 134 | avifPropertyArray properties; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 135 | avifExtentArray extents; // All extent offsets/sizes |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 136 | avifRWData mergedExtents; // if set, is a single contiguous block of this item's extents (unused when extents.count == 1) |
| 137 | avifBool ownsMergedExtents; // if true, mergedExtents must be freed when this item is destroyed |
| 138 | avifBool partialMergedExtents; // If true, mergedExtents doesn't have all of the item data yet |
| 139 | uint32_t thumbnailForID; // if non-zero, this item is a thumbnail for Item #{thumbnailForID} |
| 140 | uint32_t auxForID; // if non-zero, this item is an auxC plane for Item #{auxForID} |
| 141 | uint32_t descForID; // if non-zero, this item is a content description for Item #{descForID} |
| 142 | uint32_t dimgForID; // if non-zero, this item is a derived image for Item #{dimgForID} |
Yuan Tong | e4850be | 2021-01-22 14:21:25 +0800 | [diff] [blame] | 143 | uint32_t premByID; // if non-zero, this item is premultiplied by Item #{premByID} |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 144 | avifBool hasUnsupportedEssentialProperty; // If true, this item cites a property flagged as 'essential' that libavif doesn't support (yet). Ignore the item, if so. |
Wan-Teh Chang | f4c6538 | 2020-11-03 14:27:45 -0800 | [diff] [blame] | 145 | avifBool ipmaSeen; // if true, this item already received a property association |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 146 | } avifDecoderItem; |
| 147 | AVIF_ARRAY_DECLARE(avifDecoderItemArray, avifDecoderItem, item); |
| 148 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 149 | // idat storage |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 150 | typedef struct avifDecoderItemData |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 151 | { |
| 152 | uint32_t id; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 153 | avifRWData data; |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 154 | } avifDecoderItemData; |
| 155 | AVIF_ARRAY_DECLARE(avifDecoderItemDataArray, avifDecoderItemData, idat); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 156 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 157 | // grid storage |
| 158 | typedef struct avifImageGrid |
| 159 | { |
Joe Drago | 79ebcf3 | 2020-11-18 22:37:10 -0800 | [diff] [blame] | 160 | uint32_t rows; // Legal range: [1-256] |
| 161 | uint32_t columns; // Legal range: [1-256] |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 162 | uint32_t outputWidth; |
| 163 | uint32_t outputHeight; |
| 164 | } avifImageGrid; |
| 165 | |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 166 | // --------------------------------------------------------------------------- |
| 167 | // avifTrack |
| 168 | |
| 169 | typedef struct avifSampleTableChunk |
| 170 | { |
| 171 | uint64_t offset; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 172 | } avifSampleTableChunk; |
| 173 | AVIF_ARRAY_DECLARE(avifSampleTableChunkArray, avifSampleTableChunk, chunk); |
| 174 | |
| 175 | typedef struct avifSampleTableSampleToChunk |
| 176 | { |
| 177 | uint32_t firstChunk; |
| 178 | uint32_t samplesPerChunk; |
| 179 | uint32_t sampleDescriptionIndex; |
| 180 | } avifSampleTableSampleToChunk; |
| 181 | AVIF_ARRAY_DECLARE(avifSampleTableSampleToChunkArray, avifSampleTableSampleToChunk, sampleToChunk); |
| 182 | |
| 183 | typedef struct avifSampleTableSampleSize |
| 184 | { |
| 185 | uint32_t size; |
| 186 | } avifSampleTableSampleSize; |
| 187 | AVIF_ARRAY_DECLARE(avifSampleTableSampleSizeArray, avifSampleTableSampleSize, sampleSize); |
| 188 | |
| 189 | typedef struct avifSampleTableTimeToSample |
| 190 | { |
| 191 | uint32_t sampleCount; |
| 192 | uint32_t sampleDelta; |
| 193 | } avifSampleTableTimeToSample; |
| 194 | AVIF_ARRAY_DECLARE(avifSampleTableTimeToSampleArray, avifSampleTableTimeToSample, timeToSample); |
| 195 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 196 | typedef struct avifSyncSample |
| 197 | { |
| 198 | uint32_t sampleNumber; |
| 199 | } avifSyncSample; |
| 200 | AVIF_ARRAY_DECLARE(avifSyncSampleArray, avifSyncSample, syncSample); |
| 201 | |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 202 | typedef struct avifSampleDescription |
| 203 | { |
| 204 | uint8_t format[4]; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 205 | avifPropertyArray properties; |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 206 | } avifSampleDescription; |
| 207 | AVIF_ARRAY_DECLARE(avifSampleDescriptionArray, avifSampleDescription, description); |
| 208 | |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 209 | typedef struct avifSampleTable |
| 210 | { |
| 211 | avifSampleTableChunkArray chunks; |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 212 | avifSampleDescriptionArray sampleDescriptions; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 213 | avifSampleTableSampleToChunkArray sampleToChunks; |
| 214 | avifSampleTableSampleSizeArray sampleSizes; |
| 215 | avifSampleTableTimeToSampleArray timeToSamples; |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 216 | avifSyncSampleArray syncSamples; |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 217 | uint32_t allSamplesSize; // If this is non-zero, sampleSizes will be empty and all samples will be this size |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 218 | } avifSampleTable; |
| 219 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 220 | static avifSampleTable * avifSampleTableCreate() |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 221 | { |
| 222 | avifSampleTable * sampleTable = (avifSampleTable *)avifAlloc(sizeof(avifSampleTable)); |
| 223 | memset(sampleTable, 0, sizeof(avifSampleTable)); |
| 224 | avifArrayCreate(&sampleTable->chunks, sizeof(avifSampleTableChunk), 16); |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 225 | avifArrayCreate(&sampleTable->sampleDescriptions, sizeof(avifSampleDescription), 2); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 226 | avifArrayCreate(&sampleTable->sampleToChunks, sizeof(avifSampleTableSampleToChunk), 16); |
| 227 | avifArrayCreate(&sampleTable->sampleSizes, sizeof(avifSampleTableSampleSize), 16); |
| 228 | avifArrayCreate(&sampleTable->timeToSamples, sizeof(avifSampleTableTimeToSample), 16); |
Joe Drago | 759e674 | 2019-09-26 18:07:21 -0700 | [diff] [blame] | 229 | avifArrayCreate(&sampleTable->syncSamples, sizeof(avifSyncSample), 16); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 230 | return sampleTable; |
| 231 | } |
| 232 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 233 | static void avifSampleTableDestroy(avifSampleTable * sampleTable) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 234 | { |
| 235 | avifArrayDestroy(&sampleTable->chunks); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 236 | for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) { |
| 237 | avifSampleDescription * description = &sampleTable->sampleDescriptions.description[i]; |
| 238 | avifArrayDestroy(&description->properties); |
| 239 | } |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 240 | avifArrayDestroy(&sampleTable->sampleDescriptions); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 241 | avifArrayDestroy(&sampleTable->sampleToChunks); |
| 242 | avifArrayDestroy(&sampleTable->sampleSizes); |
| 243 | avifArrayDestroy(&sampleTable->timeToSamples); |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 244 | avifArrayDestroy(&sampleTable->syncSamples); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 245 | avifFree(sampleTable); |
| 246 | } |
| 247 | |
Wan-Teh Chang | 306c306 | 2020-04-05 12:17:33 -0700 | [diff] [blame] | 248 | static uint32_t avifSampleTableGetImageDelta(const avifSampleTable * sampleTable, int imageIndex) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 249 | { |
| 250 | int maxSampleIndex = 0; |
| 251 | for (uint32_t i = 0; i < sampleTable->timeToSamples.count; ++i) { |
Wan-Teh Chang | 306c306 | 2020-04-05 12:17:33 -0700 | [diff] [blame] | 252 | const avifSampleTableTimeToSample * timeToSample = &sampleTable->timeToSamples.timeToSample[i]; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 253 | maxSampleIndex += timeToSample->sampleCount; |
| 254 | if ((imageIndex < maxSampleIndex) || (i == (sampleTable->timeToSamples.count - 1))) { |
| 255 | return timeToSample->sampleDelta; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // TODO: fail here? |
| 260 | return 1; |
| 261 | } |
| 262 | |
Wan-Teh Chang | 306c306 | 2020-04-05 12:17:33 -0700 | [diff] [blame] | 263 | static avifBool avifSampleTableHasFormat(const avifSampleTable * sampleTable, const char * format) |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 264 | { |
| 265 | for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) { |
| 266 | if (!memcmp(sampleTable->sampleDescriptions.description[i].format, format, 4)) { |
| 267 | return AVIF_TRUE; |
| 268 | } |
| 269 | } |
| 270 | return AVIF_FALSE; |
| 271 | } |
| 272 | |
Wan-Teh Chang | 306c306 | 2020-04-05 12:17:33 -0700 | [diff] [blame] | 273 | static uint32_t avifCodecConfigurationBoxGetDepth(const avifCodecConfigurationBox * av1C) |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 274 | { |
| 275 | if (av1C->twelveBit) { |
| 276 | return 12; |
| 277 | } else if (av1C->highBitdepth) { |
| 278 | return 10; |
| 279 | } |
| 280 | return 8; |
| 281 | } |
| 282 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 283 | static const avifPropertyArray * avifSampleTableGetProperties(const avifSampleTable * sampleTable) |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 284 | { |
| 285 | for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) { |
Wan-Teh Chang | 306c306 | 2020-04-05 12:17:33 -0700 | [diff] [blame] | 286 | const avifSampleDescription * description = &sampleTable->sampleDescriptions.description[i]; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 287 | if (!memcmp(description->format, "av01", 4)) { |
| 288 | return &description->properties; |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
Joe Drago | 00bcaaf | 2020-06-05 15:29:38 -0700 | [diff] [blame] | 291 | return NULL; |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 294 | // one video track ("trak" contents) |
| 295 | typedef struct avifTrack |
| 296 | { |
| 297 | uint32_t id; |
Wan-Teh Chang | 53adb6a | 2021-02-20 16:58:43 -0800 | [diff] [blame] | 298 | uint32_t auxForID; // if non-zero, this track is an auxC plane for Track #{auxForID} |
| 299 | uint32_t premByID; // if non-zero, this track is premultiplied by Track #{premByID} |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 300 | uint32_t mediaTimescale; |
| 301 | uint64_t mediaDuration; |
Joe Drago | fc4144e | 2019-09-27 20:35:06 -0700 | [diff] [blame] | 302 | uint32_t width; |
| 303 | uint32_t height; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 304 | avifSampleTable * sampleTable; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 305 | struct avifMeta * meta; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 306 | } avifTrack; |
| 307 | AVIF_ARRAY_DECLARE(avifTrackArray, avifTrack, track); |
| 308 | |
| 309 | // --------------------------------------------------------------------------- |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 310 | // avifCodecDecodeInput |
| 311 | |
Joe Drago | 399df4f | 2019-07-23 16:45:14 -0700 | [diff] [blame] | 312 | avifCodecDecodeInput * avifCodecDecodeInputCreate(void) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 313 | { |
| 314 | avifCodecDecodeInput * decodeInput = (avifCodecDecodeInput *)avifAlloc(sizeof(avifCodecDecodeInput)); |
| 315 | memset(decodeInput, 0, sizeof(avifCodecDecodeInput)); |
Joe Drago | e3e3bfa | 2020-06-02 16:33:53 -0700 | [diff] [blame] | 316 | avifArrayCreate(&decodeInput->samples, sizeof(avifDecodeSample), 1); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 317 | return decodeInput; |
| 318 | } |
| 319 | |
Joe Drago | 8b34ad7 | 2019-07-22 16:56:32 -0700 | [diff] [blame] | 320 | void avifCodecDecodeInputDestroy(avifCodecDecodeInput * decodeInput) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 321 | { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 322 | for (uint32_t sampleIndex = 0; sampleIndex < decodeInput->samples.count; ++sampleIndex) { |
| 323 | avifDecodeSample * sample = &decodeInput->samples.sample[sampleIndex]; |
| 324 | if (sample->ownsData) { |
| 325 | avifRWDataFree((avifRWData *)&sample->data); |
| 326 | } |
| 327 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 328 | avifArrayDestroy(&decodeInput->samples); |
| 329 | avifFree(decodeInput); |
| 330 | } |
| 331 | |
Wan-Teh Chang | b8c8944 | 2021-03-19 14:17:57 -0700 | [diff] [blame^] | 332 | // Returns how many samples are in the chunk. |
| 333 | static uint32_t avifGetSampleCountOfChunk(const avifSampleTableSampleToChunkArray * sampleToChunks, uint32_t chunkIndex) |
| 334 | { |
| 335 | uint32_t sampleCount = 0; |
| 336 | for (int sampleToChunkIndex = sampleToChunks->count - 1; sampleToChunkIndex >= 0; --sampleToChunkIndex) { |
| 337 | const avifSampleTableSampleToChunk * sampleToChunk = &sampleToChunks->sampleToChunk[sampleToChunkIndex]; |
| 338 | if (sampleToChunk->firstChunk <= (chunkIndex + 1)) { |
| 339 | sampleCount = sampleToChunk->samplesPerChunk; |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | return sampleCount; |
| 344 | } |
| 345 | |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 346 | static avifBool avifCodecDecodeInputGetSamples(avifCodecDecodeInput * decodeInput, |
| 347 | avifSampleTable * sampleTable, |
Wan-Teh Chang | b8c8944 | 2021-03-19 14:17:57 -0700 | [diff] [blame^] | 348 | const uint32_t imageCountLimit, |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 349 | const uint64_t sizeHint) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 350 | { |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 351 | if (imageCountLimit) { |
| 352 | // Verify that the we're not about to exceed the frame count limit. |
| 353 | |
Wan-Teh Chang | b8c8944 | 2021-03-19 14:17:57 -0700 | [diff] [blame^] | 354 | uint32_t imageCountLeft = imageCountLimit; |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 355 | for (uint32_t chunkIndex = 0; chunkIndex < sampleTable->chunks.count; ++chunkIndex) { |
| 356 | // First, figure out how many samples are in this chunk |
Wan-Teh Chang | b8c8944 | 2021-03-19 14:17:57 -0700 | [diff] [blame^] | 357 | uint32_t sampleCount = avifGetSampleCountOfChunk(&sampleTable->sampleToChunks, chunkIndex); |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 358 | if (sampleCount == 0) { |
| 359 | // chunks with 0 samples are invalid |
| 360 | return AVIF_FALSE; |
| 361 | } |
| 362 | |
Wan-Teh Chang | b8c8944 | 2021-03-19 14:17:57 -0700 | [diff] [blame^] | 363 | if (sampleCount > imageCountLeft) { |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 364 | // This file exceeds the imageCountLimit, bail out |
| 365 | return AVIF_FALSE; |
| 366 | } |
Wan-Teh Chang | b8c8944 | 2021-03-19 14:17:57 -0700 | [diff] [blame^] | 367 | imageCountLeft -= sampleCount; |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 371 | uint32_t sampleSizeIndex = 0; |
| 372 | for (uint32_t chunkIndex = 0; chunkIndex < sampleTable->chunks.count; ++chunkIndex) { |
| 373 | avifSampleTableChunk * chunk = &sampleTable->chunks.chunk[chunkIndex]; |
| 374 | |
| 375 | // First, figure out how many samples are in this chunk |
Wan-Teh Chang | b8c8944 | 2021-03-19 14:17:57 -0700 | [diff] [blame^] | 376 | uint32_t sampleCount = avifGetSampleCountOfChunk(&sampleTable->sampleToChunks, chunkIndex); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 377 | if (sampleCount == 0) { |
| 378 | // chunks with 0 samples are invalid |
| 379 | return AVIF_FALSE; |
| 380 | } |
| 381 | |
| 382 | uint64_t sampleOffset = chunk->offset; |
| 383 | for (uint32_t sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex) { |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 384 | uint32_t sampleSize = sampleTable->allSamplesSize; |
| 385 | if (sampleSize == 0) { |
| 386 | if (sampleSizeIndex >= sampleTable->sampleSizes.count) { |
| 387 | // We've run out of samples to sum |
| 388 | return AVIF_FALSE; |
| 389 | } |
| 390 | avifSampleTableSampleSize * sampleSizePtr = &sampleTable->sampleSizes.sampleSize[sampleSizeIndex]; |
| 391 | sampleSize = sampleSizePtr->size; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Joe Drago | e3e3bfa | 2020-06-02 16:33:53 -0700 | [diff] [blame] | 394 | avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&decodeInput->samples); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 395 | sample->offset = sampleOffset; |
| 396 | sample->size = sampleSize; |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 397 | sample->sync = AVIF_FALSE; // to potentially be set to true following the outer loop |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 398 | |
Wan-Teh Chang | 3ca1424 | 2020-09-30 16:39:38 -0700 | [diff] [blame] | 399 | if (sampleSize > UINT64_MAX - sampleOffset) { |
| 400 | return AVIF_FALSE; |
| 401 | } |
Wan-Teh Chang | 136d757 | 2020-10-08 15:13:42 -0700 | [diff] [blame] | 402 | if (sizeHint && ((sampleOffset + sampleSize) > sizeHint)) { |
Joe Drago | 34c0d31 | 2020-04-30 15:23:03 -0700 | [diff] [blame] | 403 | return AVIF_FALSE; |
| 404 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 405 | |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 406 | sampleOffset += sampleSize; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 407 | ++sampleSizeIndex; |
| 408 | } |
| 409 | } |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 410 | |
| 411 | // Mark appropriate samples as sync |
| 412 | for (uint32_t syncSampleIndex = 0; syncSampleIndex < sampleTable->syncSamples.count; ++syncSampleIndex) { |
| 413 | uint32_t frameIndex = sampleTable->syncSamples.syncSample[syncSampleIndex].sampleNumber - 1; // sampleNumber is 1-based |
| 414 | if (frameIndex < decodeInput->samples.count) { |
| 415 | decodeInput->samples.sample[frameIndex].sync = AVIF_TRUE; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // Assume frame 0 is sync, just in case the stss box is absent in the BMFF. (Unnecessary?) |
| 420 | if (decodeInput->samples.count > 0) { |
| 421 | decodeInput->samples.sample[0].sync = AVIF_TRUE; |
| 422 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 423 | return AVIF_TRUE; |
| 424 | } |
| 425 | |
| 426 | // --------------------------------------------------------------------------- |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 427 | // Helper macros / functions |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 428 | |
| 429 | #define BEGIN_STREAM(VARNAME, PTR, SIZE) \ |
| 430 | avifROStream VARNAME; \ |
| 431 | avifROData VARNAME##_roData; \ |
| 432 | VARNAME##_roData.data = PTR; \ |
| 433 | VARNAME##_roData.size = SIZE; \ |
| 434 | avifROStreamStart(&VARNAME, &VARNAME##_roData) |
| 435 | |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 436 | // Use this to keep track of whether or not a child box that must be unique (0 or 1 present) has |
| 437 | // been seen yet, when parsing a parent box. If the "seen" bit is already set for a given box when |
| 438 | // it is encountered during parse, an error is thrown. Which bit corresponds to which box is |
| 439 | // dictated entirely by the calling function. |
| 440 | static avifBool uniqueBoxSeen(uint32_t * uniqueBoxFlags, uint32_t whichFlag) |
| 441 | { |
| 442 | const uint32_t flag = 1 << whichFlag; |
| 443 | if (*uniqueBoxFlags & flag) { |
| 444 | // This box has already been seen. Error! |
| 445 | return AVIF_FALSE; |
| 446 | } |
| 447 | |
| 448 | // Mark this box as seen. |
| 449 | *uniqueBoxFlags |= flag; |
| 450 | return AVIF_TRUE; |
| 451 | } |
| 452 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 453 | // --------------------------------------------------------------------------- |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 454 | // avifDecoderData |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 455 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 456 | typedef struct avifTile |
| 457 | { |
| 458 | avifCodecDecodeInput * input; |
| 459 | struct avifCodec * codec; |
| 460 | avifImage * image; |
| 461 | } avifTile; |
| 462 | AVIF_ARRAY_DECLARE(avifTileArray, avifTile, tile); |
| 463 | |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 464 | // This holds one "meta" box (from the BMFF and HEIF standards) worth of relevant-to-AVIF information. |
| 465 | // * If a meta box is parsed from the root level of the BMFF, it can contain the information about |
| 466 | // "items" which might be color planes, alpha planes, or EXIF or XMP metadata. |
| 467 | // * If a meta box is parsed from inside of a track ("trak") box, any metadata (EXIF/XMP) items inside |
| 468 | // of that box are implicitly associated with that track. |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 469 | typedef struct avifMeta |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 470 | { |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 471 | // Items (from HEIF) are the generic storage for any data that does not require timed processing |
Wan-Teh Chang | 4b331fb | 2020-07-07 14:10:02 -0700 | [diff] [blame] | 472 | // (single image color planes, alpha planes, EXIF, XMP, etc). Each item has a unique integer ID >1, |
| 473 | // and is defined by a series of child boxes in a meta box: |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 474 | // * iloc - location: byte offset to item data, item size in bytes |
| 475 | // * iinf - information: type of item (color planes, alpha plane, EXIF, XMP) |
| 476 | // * ipco - properties: dimensions, aspect ratio, image transformations, references to other items |
| 477 | // * ipma - associations: Attaches an item in the properties list to a given item |
| 478 | // |
| 479 | // Items are lazily created in this array when any of the above boxes refer to one by a new (unseen) ID, |
| 480 | // and are then further modified/updated as new information for an item's ID is parsed. |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 481 | avifDecoderItemArray items; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 482 | |
| 483 | // Any ipco boxes explained above are populated into this array as a staging area, which are |
| 484 | // then duplicated into the appropriate items upon encountering an item property association |
| 485 | // (ipma) box. |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 486 | avifPropertyArray properties; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 487 | |
| 488 | // Filled with the contents of "idat" boxes, which are raw data that an item can directly refer to in its |
| 489 | // item location box (iloc) instead of just giving an offset into the overall file. If all items' iloc boxes |
| 490 | // simply point at an offset/length in the file itself, this array will likely be empty. |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 491 | avifDecoderItemDataArray idats; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 492 | |
| 493 | // Ever-incrementing ID for uniquely identifying which 'meta' box contains an idat (when |
Wan-Teh Chang | 4b331fb | 2020-07-07 14:10:02 -0700 | [diff] [blame] | 494 | // multiple meta boxes exist as BMFF siblings). Each time avifParseMetaBox() is called on an |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 495 | // avifMeta struct, this value is incremented. Any time an additional meta box is detected at |
| 496 | // the same "level" (root level, trak level, etc), this ID helps distinguish which meta box's |
Wan-Teh Chang | 4b331fb | 2020-07-07 14:10:02 -0700 | [diff] [blame] | 497 | // "idat" is which, as items implicitly reference idat boxes that exist in the same meta |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 498 | // box. |
| 499 | uint32_t idatID; |
| 500 | |
| 501 | // Contents of a pitm box, which signal which of the items in this file is the main image. For |
| 502 | // AVIF, this should point at an av01 type item containing color planes, and all other items |
| 503 | // are ignored unless they refer to this item in some way (alpha plane, EXIF/XMP metadata). |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 504 | uint32_t primaryItemID; |
| 505 | } avifMeta; |
| 506 | |
| 507 | static avifMeta * avifMetaCreate() |
| 508 | { |
| 509 | avifMeta * meta = (avifMeta *)avifAlloc(sizeof(avifMeta)); |
| 510 | memset(meta, 0, sizeof(avifMeta)); |
| 511 | avifArrayCreate(&meta->items, sizeof(avifDecoderItem), 8); |
| 512 | avifArrayCreate(&meta->properties, sizeof(avifProperty), 16); |
| 513 | avifArrayCreate(&meta->idats, sizeof(avifDecoderItemData), 1); |
| 514 | return meta; |
| 515 | } |
| 516 | |
| 517 | static void avifMetaDestroy(avifMeta * meta) |
| 518 | { |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 519 | for (uint32_t i = 0; i < meta->items.count; ++i) { |
| 520 | avifDecoderItem * item = &meta->items.item[i]; |
| 521 | avifArrayDestroy(&item->properties); |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 522 | avifArrayDestroy(&item->extents); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 523 | if (item->ownsMergedExtents) { |
| 524 | avifRWDataFree(&item->mergedExtents); |
| 525 | } |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 526 | } |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 527 | avifArrayDestroy(&meta->items); |
| 528 | avifArrayDestroy(&meta->properties); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 529 | for (uint32_t i = 0; i < meta->idats.count; ++i) { |
| 530 | avifDecoderItemData * idat = &meta->idats.idat[i]; |
| 531 | avifRWDataFree(&idat->data); |
| 532 | } |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 533 | avifArrayDestroy(&meta->idats); |
| 534 | avifFree(meta); |
| 535 | } |
| 536 | |
| 537 | static avifDecoderItem * avifMetaFindItem(avifMeta * meta, uint32_t itemID) |
| 538 | { |
| 539 | if (itemID == 0) { |
| 540 | return NULL; |
| 541 | } |
| 542 | |
| 543 | for (uint32_t i = 0; i < meta->items.count; ++i) { |
| 544 | if (meta->items.item[i].id == itemID) { |
| 545 | return &meta->items.item[i]; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | avifDecoderItem * item = (avifDecoderItem *)avifArrayPushPtr(&meta->items); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 550 | avifArrayCreate(&item->properties, sizeof(avifProperty), 16); |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 551 | avifArrayCreate(&item->extents, sizeof(avifExtent), 1); |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 552 | item->id = itemID; |
| 553 | item->meta = meta; |
| 554 | return item; |
| 555 | } |
| 556 | |
| 557 | typedef struct avifDecoderData |
| 558 | { |
Wan-Teh Chang | 6fc1758 | 2020-09-24 15:16:37 -0700 | [diff] [blame] | 559 | avifMeta * meta; // The root-level meta box |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 560 | avifTrackArray tracks; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 561 | avifTileArray tiles; |
| 562 | unsigned int colorTileCount; |
| 563 | unsigned int alphaTileCount; |
| 564 | avifImageGrid colorGrid; |
| 565 | avifImageGrid alphaGrid; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 566 | avifDecoderSource source; |
Wan-Teh Chang | 306c306 | 2020-04-05 12:17:33 -0700 | [diff] [blame] | 567 | const avifSampleTable * sourceSampleTable; // NULL unless (source == AVIF_DECODER_SOURCE_TRACKS), owned by an avifTrack |
Joe Drago | c00d583 | 2020-08-13 16:03:28 -0700 | [diff] [blame] | 568 | avifBool cicpSet; // True if avifDecoder's image has had its CICP set correctly yet. |
| 569 | // This allows nclx colr boxes to override AV1 CICP, as specified in the MIAF |
| 570 | // standard (ISO/IEC 23000-22:2019), section 7.3.6.4: |
| 571 | // |
| 572 | // "The colour information property takes precedence over any colour information in the image |
| 573 | // bitstream, i.e. if the property is present, colour information in the bitstream shall be ignored." |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 574 | } avifDecoderData; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 575 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 576 | static avifDecoderData * avifDecoderDataCreate() |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 577 | { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 578 | avifDecoderData * data = (avifDecoderData *)avifAlloc(sizeof(avifDecoderData)); |
| 579 | memset(data, 0, sizeof(avifDecoderData)); |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 580 | data->meta = avifMetaCreate(); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 581 | avifArrayCreate(&data->tracks, sizeof(avifTrack), 2); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 582 | avifArrayCreate(&data->tiles, sizeof(avifTile), 8); |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 583 | return data; |
| 584 | } |
| 585 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 586 | static void avifDecoderDataResetCodec(avifDecoderData * data) |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 587 | { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 588 | for (unsigned int i = 0; i < data->tiles.count; ++i) { |
| 589 | avifTile * tile = &data->tiles.tile[i]; |
Joe Drago | 9d36878 | 2020-03-04 17:53:17 -0800 | [diff] [blame] | 590 | if (tile->image) { |
| 591 | avifImageFreePlanes(tile->image, AVIF_PLANES_ALL); // forget any pointers into codec image buffers |
| 592 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 593 | if (tile->codec) { |
| 594 | avifCodecDestroy(tile->codec); |
| 595 | tile->codec = NULL; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 600 | static avifTile * avifDecoderDataCreateTile(avifDecoderData * data) |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 601 | { |
| 602 | avifTile * tile = (avifTile *)avifArrayPushPtr(&data->tiles); |
| 603 | tile->image = avifImageCreateEmpty(); |
| 604 | tile->input = avifCodecDecodeInputCreate(); |
| 605 | return tile; |
| 606 | } |
| 607 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 608 | static avifTrack * avifDecoderDataCreateTrack(avifDecoderData * data) |
| 609 | { |
| 610 | avifTrack * track = (avifTrack *)avifArrayPushPtr(&data->tracks); |
| 611 | track->meta = avifMetaCreate(); |
| 612 | return track; |
| 613 | } |
| 614 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 615 | static void avifDecoderDataClearTiles(avifDecoderData * data) |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 616 | { |
| 617 | for (unsigned int i = 0; i < data->tiles.count; ++i) { |
| 618 | avifTile * tile = &data->tiles.tile[i]; |
| 619 | if (tile->input) { |
| 620 | avifCodecDecodeInputDestroy(tile->input); |
| 621 | tile->input = NULL; |
| 622 | } |
| 623 | if (tile->codec) { |
| 624 | avifCodecDestroy(tile->codec); |
| 625 | tile->codec = NULL; |
| 626 | } |
| 627 | if (tile->image) { |
| 628 | avifImageDestroy(tile->image); |
| 629 | tile->image = NULL; |
| 630 | } |
| 631 | } |
| 632 | data->tiles.count = 0; |
| 633 | data->colorTileCount = 0; |
| 634 | data->alphaTileCount = 0; |
| 635 | } |
| 636 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 637 | static void avifDecoderDataDestroy(avifDecoderData * data) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 638 | { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 639 | avifMetaDestroy(data->meta); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 640 | for (uint32_t i = 0; i < data->tracks.count; ++i) { |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 641 | avifTrack * track = &data->tracks.track[i]; |
| 642 | if (track->sampleTable) { |
| 643 | avifSampleTableDestroy(track->sampleTable); |
| 644 | } |
| 645 | if (track->meta) { |
| 646 | avifMetaDestroy(track->meta); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | avifArrayDestroy(&data->tracks); |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 650 | avifDecoderDataClearTiles(data); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 651 | avifArrayDestroy(&data->tiles); |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 652 | avifFree(data); |
| 653 | } |
| 654 | |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 655 | // This returns the max extent that has to be read in order to decode this item. If |
| 656 | // the item is stored in an idat, the data has already been read during Parse() and |
| 657 | // this function will return AVIF_RESULT_OK with a 0-byte extent. |
| 658 | static avifResult avifDecoderItemMaxExtent(const avifDecoderItem * item, avifExtent * outExtent) |
| 659 | { |
| 660 | if (item->extents.count == 0) { |
| 661 | return AVIF_RESULT_TRUNCATED_DATA; |
| 662 | } |
| 663 | |
| 664 | if (item->idatID != 0) { |
| 665 | // construction_method: idat(1) |
| 666 | |
Wan-Teh Chang | 8027539 | 2020-11-17 12:35:11 -0800 | [diff] [blame] | 667 | // Find associated idat box |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 668 | for (uint32_t i = 0; i < item->meta->idats.count; ++i) { |
| 669 | if (item->meta->idats.idat[i].id == item->idatID) { |
| 670 | // Already read from a meta box during Parse() |
| 671 | memset(outExtent, 0, sizeof(avifExtent)); |
| 672 | return AVIF_RESULT_OK; |
| 673 | } |
| 674 | } |
| 675 | |
Wan-Teh Chang | 8027539 | 2020-11-17 12:35:11 -0800 | [diff] [blame] | 676 | // no associated idat box was found in the meta box, bail out |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 677 | return AVIF_RESULT_NO_CONTENT; |
| 678 | } |
| 679 | |
| 680 | // construction_method: file(0) |
| 681 | |
Wan-Teh Chang | 1558452 | 2020-11-17 14:11:12 -0800 | [diff] [blame] | 682 | // Assert that the for loop below will execute at least one iteration. |
| 683 | assert(item->extents.count != 0); |
| 684 | uint64_t minOffset = UINT64_MAX; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 685 | uint64_t maxOffset = 0; |
| 686 | for (uint32_t extentIter = 0; extentIter < item->extents.count; ++extentIter) { |
| 687 | avifExtent * extent = &item->extents.extent[extentIter]; |
| 688 | |
| 689 | if (extent->size > UINT64_MAX - extent->offset) { |
| 690 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 691 | } |
| 692 | const uint64_t endOffset = extent->offset + extent->size; |
| 693 | |
Wan-Teh Chang | 1558452 | 2020-11-17 14:11:12 -0800 | [diff] [blame] | 694 | if (minOffset > extent->offset) { |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 695 | minOffset = extent->offset; |
Wan-Teh Chang | 1558452 | 2020-11-17 14:11:12 -0800 | [diff] [blame] | 696 | } |
| 697 | if (maxOffset < endOffset) { |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 698 | maxOffset = endOffset; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
| 702 | outExtent->offset = minOffset; |
Wan-Teh Chang | d69958e | 2020-11-17 12:14:27 -0800 | [diff] [blame] | 703 | const uint64_t extentLength = maxOffset - minOffset; |
| 704 | if (extentLength > SIZE_MAX) { |
| 705 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 706 | } |
| 707 | outExtent->size = (size_t)extentLength; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 708 | return AVIF_RESULT_OK; |
| 709 | } |
| 710 | |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 711 | static avifResult avifDecoderItemRead(avifDecoderItem * item, avifIO * io, avifROData * outData, size_t partialByteCount) |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 712 | { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 713 | if (item->mergedExtents.data && !item->partialMergedExtents) { |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 714 | // Multiple extents have already been concatenated for this item, just return it |
Wan-Teh Chang | 610b0a1 | 2020-10-06 16:47:21 -0700 | [diff] [blame] | 715 | memcpy(outData, &item->mergedExtents, sizeof(avifROData)); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 716 | return AVIF_RESULT_OK; |
| 717 | } |
| 718 | |
| 719 | if (item->extents.count == 0) { |
| 720 | return AVIF_RESULT_TRUNCATED_DATA; |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | // Find this item's source of all extents' data, based on the construction method |
Wan-Teh Chang | 3c304dc | 2020-10-08 12:07:45 -0700 | [diff] [blame] | 724 | const avifRWData * idatBuffer = NULL; |
Wan-Teh Chang | 610b0a1 | 2020-10-06 16:47:21 -0700 | [diff] [blame] | 725 | if (item->idatID != 0) { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 726 | // construction_method: idat(1) |
| 727 | |
Wan-Teh Chang | 8027539 | 2020-11-17 12:35:11 -0800 | [diff] [blame] | 728 | // Find associated idat box |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 729 | for (uint32_t i = 0; i < item->meta->idats.count; ++i) { |
| 730 | if (item->meta->idats.idat[i].id == item->idatID) { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 731 | idatBuffer = &item->meta->idats.idat[i].data; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 732 | break; |
| 733 | } |
| 734 | } |
| 735 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 736 | if (idatBuffer == NULL) { |
Wan-Teh Chang | 8027539 | 2020-11-17 12:35:11 -0800 | [diff] [blame] | 737 | // no associated idat box was found in the meta box, bail out |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 738 | return AVIF_RESULT_NO_CONTENT; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 742 | // Merge extents into a single contiguous buffer |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 743 | if ((io->sizeHint > 0) && (item->size > io->sizeHint)) { |
Wan-Teh Chang | e500323 | 2020-08-28 18:26:58 -0700 | [diff] [blame] | 744 | // Sanity check: somehow the sum of extents for this item exceeds the entire file or idat |
| 745 | // size! |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 746 | return AVIF_RESULT_TRUNCATED_DATA; |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 747 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 748 | |
Wan-Teh Chang | 610b0a1 | 2020-10-06 16:47:21 -0700 | [diff] [blame] | 749 | size_t totalBytesToRead = item->size; |
| 750 | if (partialByteCount && (totalBytesToRead > partialByteCount)) { |
| 751 | totalBytesToRead = partialByteCount; |
| 752 | } |
| 753 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 754 | // If there is a single extent for this item and the source of the read buffer is going to be |
| 755 | // persistent for the lifetime of the avifDecoder (whether it comes from its own internal |
| 756 | // idatBuffer or from a known-persistent IO), we can avoid buffer duplication and just use the |
| 757 | // preexisting buffer. |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 758 | avifBool singlePersistentBuffer = ((item->extents.count == 1) && (idatBuffer || io->persistent)); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 759 | if (!singlePersistentBuffer) { |
Wan-Teh Chang | 610b0a1 | 2020-10-06 16:47:21 -0700 | [diff] [blame] | 760 | avifRWDataRealloc(&item->mergedExtents, totalBytesToRead); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 761 | item->ownsMergedExtents = AVIF_TRUE; |
| 762 | } |
| 763 | |
| 764 | // Set this until we manage to fill the entire mergedExtents buffer |
| 765 | item->partialMergedExtents = AVIF_TRUE; |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 766 | |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 767 | uint8_t * front = item->mergedExtents.data; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 768 | size_t remainingBytes = totalBytesToRead; |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 769 | for (uint32_t extentIter = 0; extentIter < item->extents.count; ++extentIter) { |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 770 | avifExtent * extent = &item->extents.extent[extentIter]; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 771 | |
| 772 | size_t bytesToRead = extent->size; |
| 773 | if (bytesToRead > remainingBytes) { |
| 774 | bytesToRead = remainingBytes; |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 777 | avifROData offsetBuffer; |
| 778 | if (idatBuffer) { |
Wan-Teh Chang | 610b0a1 | 2020-10-06 16:47:21 -0700 | [diff] [blame] | 779 | if (extent->offset > idatBuffer->size) { |
| 780 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 781 | } |
| 782 | if (extent->size > idatBuffer->size - extent->offset) { |
| 783 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 784 | } |
| 785 | offsetBuffer.data = idatBuffer->data + extent->offset; |
| 786 | offsetBuffer.size = idatBuffer->size - extent->offset; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 787 | } else { |
| 788 | // construction_method: file(0) |
| 789 | |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 790 | if ((io->sizeHint > 0) && (extent->offset > io->sizeHint)) { |
Wan-Teh Chang | b856dc2 | 2020-09-28 13:00:56 -0700 | [diff] [blame] | 791 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 792 | } |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 793 | avifResult readResult = io->read(io, 0, extent->offset, bytesToRead, &offsetBuffer); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 794 | if (readResult != AVIF_RESULT_OK) { |
| 795 | return readResult; |
| 796 | } |
Wan-Teh Chang | 3c304dc | 2020-10-08 12:07:45 -0700 | [diff] [blame] | 797 | if (bytesToRead != offsetBuffer.size) { |
| 798 | return AVIF_RESULT_TRUNCATED_DATA; |
| 799 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | if (singlePersistentBuffer) { |
| 803 | memcpy(&item->mergedExtents, &offsetBuffer, sizeof(avifRWData)); |
| 804 | item->mergedExtents.size = bytesToRead; |
| 805 | } else { |
Joe Drago | db1009a | 2021-03-16 15:26:14 -0700 | [diff] [blame] | 806 | assert(item->ownsMergedExtents); |
| 807 | assert(front); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 808 | memcpy(front, offsetBuffer.data, bytesToRead); |
| 809 | front += bytesToRead; |
| 810 | } |
| 811 | |
| 812 | remainingBytes -= bytesToRead; |
| 813 | if (remainingBytes == 0) { |
| 814 | // This happens when partialByteCount is set |
| 815 | break; |
| 816 | } |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 817 | } |
| 818 | if (remainingBytes != 0) { |
| 819 | // This should be impossible? |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 820 | return AVIF_RESULT_TRUNCATED_DATA; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 821 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 822 | |
| 823 | outData->data = item->mergedExtents.data; |
| 824 | outData->size = totalBytesToRead; |
| 825 | item->partialMergedExtents = (item->size != totalBytesToRead); |
| 826 | return AVIF_RESULT_OK; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 827 | } |
| 828 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 829 | static avifBool avifDecoderDataGenerateImageGridTiles(avifDecoderData * data, avifImageGrid * grid, avifDecoderItem * gridItem, avifBool alpha) |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 830 | { |
Wan-Teh Chang | 7ca3dd9 | 2020-11-20 12:50:44 -0800 | [diff] [blame] | 831 | unsigned int tilesRequested = grid->rows * grid->columns; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 832 | |
| 833 | // Count number of dimg for this item, bail out if it doesn't match perfectly |
| 834 | unsigned int tilesAvailable = 0; |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 835 | for (uint32_t i = 0; i < gridItem->meta->items.count; ++i) { |
| 836 | avifDecoderItem * item = &gridItem->meta->items.item[i]; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 837 | if (item->dimgForID == gridItem->id) { |
| 838 | if (memcmp(item->type, "av01", 4)) { |
| 839 | continue; |
| 840 | } |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 841 | if (item->hasUnsupportedEssentialProperty) { |
Wan-Teh Chang | 29aaade | 2020-08-10 16:14:16 -0700 | [diff] [blame] | 842 | // An essential property isn't supported by libavif; can't |
| 843 | // decode a grid image if any tile in the grid isn't supported. |
| 844 | return AVIF_FALSE; |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 845 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 846 | |
| 847 | ++tilesAvailable; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | if (tilesRequested != tilesAvailable) { |
| 852 | return AVIF_FALSE; |
| 853 | } |
| 854 | |
Joe Drago | 9c5f565 | 2020-06-29 15:13:44 -0700 | [diff] [blame] | 855 | avifBool firstTile = AVIF_TRUE; |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 856 | for (uint32_t i = 0; i < gridItem->meta->items.count; ++i) { |
| 857 | avifDecoderItem * item = &gridItem->meta->items.item[i]; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 858 | if (item->dimgForID == gridItem->id) { |
| 859 | if (memcmp(item->type, "av01", 4)) { |
| 860 | continue; |
| 861 | } |
| 862 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 863 | avifTile * tile = avifDecoderDataCreateTile(data); |
Joe Drago | e3e3bfa | 2020-06-02 16:33:53 -0700 | [diff] [blame] | 864 | avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&tile->input->samples); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 865 | sample->itemID = item->id; |
| 866 | sample->offset = 0; |
| 867 | sample->size = item->size; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 868 | sample->sync = AVIF_TRUE; |
| 869 | tile->input->alpha = alpha; |
Joe Drago | 9c5f565 | 2020-06-29 15:13:44 -0700 | [diff] [blame] | 870 | |
| 871 | if (firstTile) { |
| 872 | firstTile = AVIF_FALSE; |
| 873 | |
| 874 | // Adopt the av1C property of the first av01 tile, so that it can be queried from |
| 875 | // the top-level color/alpha item during avifDecoderReset(). |
| 876 | const avifProperty * srcProp = avifPropertyArrayFind(&item->properties, "av1C"); |
| 877 | if (!srcProp) { |
| 878 | return AVIF_FALSE; |
| 879 | } |
| 880 | avifProperty * dstProp = (avifProperty *)avifArrayPushPtr(&gridItem->properties); |
| 881 | memcpy(dstProp, srcProp, sizeof(avifProperty)); |
| 882 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | return AVIF_TRUE; |
| 886 | } |
| 887 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 888 | static avifBool avifDecoderDataFillImageGrid(avifDecoderData * data, |
| 889 | avifImageGrid * grid, |
| 890 | avifImage * dstImage, |
| 891 | unsigned int firstTileIndex, |
| 892 | unsigned int tileCount, |
| 893 | avifBool alpha) |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 894 | { |
| 895 | if (tileCount == 0) { |
| 896 | return AVIF_FALSE; |
| 897 | } |
| 898 | |
| 899 | avifTile * firstTile = &data->tiles.tile[firstTileIndex]; |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 900 | avifBool firstTileUVPresent = (firstTile->image->yuvPlanes[AVIF_CHAN_U] && firstTile->image->yuvPlanes[AVIF_CHAN_V]); |
psi / Ryo Hirafuji | 922d8a1 | 2020-03-10 03:24:57 +0900 | [diff] [blame] | 901 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 902 | // Check for tile consistency: All tiles in a grid image should match in the properties checked below. |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 903 | for (unsigned int i = 1; i < tileCount; ++i) { |
| 904 | avifTile * tile = &data->tiles.tile[firstTileIndex + i]; |
Joe Drago | 951a002 | 2020-03-09 16:19:44 -0700 | [diff] [blame] | 905 | avifBool uvPresent = (tile->image->yuvPlanes[AVIF_CHAN_U] && tile->image->yuvPlanes[AVIF_CHAN_V]); |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 906 | if ((tile->image->width != firstTile->image->width) || (tile->image->height != firstTile->image->height) || |
| 907 | (tile->image->depth != firstTile->image->depth) || (tile->image->yuvFormat != firstTile->image->yuvFormat) || |
| 908 | (tile->image->yuvRange != firstTile->image->yuvRange) || (uvPresent != firstTileUVPresent) || |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame] | 909 | (tile->image->colorPrimaries != firstTile->image->colorPrimaries) || |
| 910 | (tile->image->transferCharacteristics != firstTile->image->transferCharacteristics) || |
Wan-Teh Chang | 5bdead5 | 2020-08-12 17:02:22 -0700 | [diff] [blame] | 911 | (tile->image->matrixCoefficients != firstTile->image->matrixCoefficients) || |
| 912 | (tile->image->alphaRange != firstTile->image->alphaRange)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 913 | return AVIF_FALSE; |
| 914 | } |
| 915 | } |
| 916 | |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 917 | // Validate grid image size and tile size. |
| 918 | // |
| 919 | // HEIF (ISO/IEC 23008-12:2017), Section 6.6.2.3.1: |
TYTY | b587c59 | 2020-12-08 17:42:18 +0800 | [diff] [blame] | 920 | // The tiled input images shall completely "cover" the reconstructed image grid canvas, ... |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame] | 921 | if (((firstTile->image->width * grid->columns) < grid->outputWidth) || |
| 922 | ((firstTile->image->height * grid->rows) < grid->outputHeight)) { |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 923 | return AVIF_FALSE; |
| 924 | } |
| 925 | // Tiles in the rightmost column and bottommost row must overlap the reconstructed image grid canvas. See MIAF (ISO/IEC 23000-22:2019), Section 7.3.11.4.2, Figure 2. |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame] | 926 | if (((firstTile->image->width * (grid->columns - 1)) >= grid->outputWidth) || |
| 927 | ((firstTile->image->height * (grid->rows - 1)) >= grid->outputHeight)) { |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 928 | return AVIF_FALSE; |
| 929 | } |
| 930 | // Check the restrictions in MIAF (ISO/IEC 23000-22:2019), Section 7.3.11.4.2. |
| 931 | // |
| 932 | // The tile_width shall be greater than or equal to 64, and the tile_height shall be greater than or equal to 64. |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame] | 933 | if ((firstTile->image->width < 64) || (firstTile->image->height < 64)) { |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 934 | return AVIF_FALSE; |
| 935 | } |
| 936 | if (!alpha) { |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame] | 937 | if ((firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV422) || (firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV420)) { |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 938 | // The horizontal tile offsets and widths, and the output width, shall be even numbers. |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame] | 939 | if (((firstTile->image->width & 1) != 0) || ((grid->outputWidth & 1) != 0)) { |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 940 | return AVIF_FALSE; |
| 941 | } |
| 942 | } |
| 943 | if (firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV420) { |
| 944 | // The vertical tile offsets and heights, and the output height, shall be even numbers. |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame] | 945 | if (((firstTile->image->height & 1) != 0) || ((grid->outputHeight & 1) != 0)) { |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 946 | return AVIF_FALSE; |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 951 | // Lazily populate dstImage with the new frame's properties. If we're decoding alpha, |
| 952 | // these values must already match. |
| 953 | if ((dstImage->width != grid->outputWidth) || (dstImage->height != grid->outputHeight) || |
Wan-Teh Chang | 5bdead5 | 2020-08-12 17:02:22 -0700 | [diff] [blame] | 954 | (dstImage->depth != firstTile->image->depth) || (!alpha && (dstImage->yuvFormat != firstTile->image->yuvFormat))) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 955 | if (alpha) { |
| 956 | // Alpha doesn't match size, just bail out |
| 957 | return AVIF_FALSE; |
| 958 | } |
| 959 | |
| 960 | avifImageFreePlanes(dstImage, AVIF_PLANES_ALL); |
| 961 | dstImage->width = grid->outputWidth; |
| 962 | dstImage->height = grid->outputHeight; |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 963 | dstImage->depth = firstTile->image->depth; |
| 964 | dstImage->yuvFormat = firstTile->image->yuvFormat; |
Joe Drago | c00d583 | 2020-08-13 16:03:28 -0700 | [diff] [blame] | 965 | dstImage->yuvRange = firstTile->image->yuvRange; |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 966 | if (!data->cicpSet) { |
| 967 | data->cicpSet = AVIF_TRUE; |
Joe Drago | c00d583 | 2020-08-13 16:03:28 -0700 | [diff] [blame] | 968 | dstImage->colorPrimaries = firstTile->image->colorPrimaries; |
| 969 | dstImage->transferCharacteristics = firstTile->image->transferCharacteristics; |
| 970 | dstImage->matrixCoefficients = firstTile->image->matrixCoefficients; |
psi / Ryo Hirafuji | 922d8a1 | 2020-03-10 03:24:57 +0900 | [diff] [blame] | 971 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 972 | } |
Joe Drago | d0eeb18 | 2020-05-18 17:23:48 -0700 | [diff] [blame] | 973 | if (alpha) { |
| 974 | dstImage->alphaRange = firstTile->image->alphaRange; |
| 975 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 976 | |
| 977 | avifImageAllocatePlanes(dstImage, alpha ? AVIF_PLANES_A : AVIF_PLANES_YUV); |
| 978 | |
| 979 | avifPixelFormatInfo formatInfo; |
Joe Drago | 7a249f5 | 2020-08-13 12:58:03 -0700 | [diff] [blame] | 980 | avifGetPixelFormatInfo(firstTile->image->yuvFormat, &formatInfo); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 981 | |
| 982 | unsigned int tileIndex = firstTileIndex; |
| 983 | size_t pixelBytes = avifImageUsesU16(dstImage) ? 2 : 1; |
| 984 | for (unsigned int rowIndex = 0; rowIndex < grid->rows; ++rowIndex) { |
| 985 | for (unsigned int colIndex = 0; colIndex < grid->columns; ++colIndex, ++tileIndex) { |
| 986 | avifTile * tile = &data->tiles.tile[tileIndex]; |
| 987 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 988 | unsigned int widthToCopy = firstTile->image->width; |
| 989 | unsigned int maxX = firstTile->image->width * (colIndex + 1); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 990 | if (maxX > grid->outputWidth) { |
| 991 | widthToCopy -= maxX - grid->outputWidth; |
| 992 | } |
| 993 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 994 | unsigned int heightToCopy = firstTile->image->height; |
| 995 | unsigned int maxY = firstTile->image->height * (rowIndex + 1); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 996 | if (maxY > grid->outputHeight) { |
| 997 | heightToCopy -= maxY - grid->outputHeight; |
| 998 | } |
| 999 | |
| 1000 | // Y and A channels |
Joe Drago | db1009a | 2021-03-16 15:26:14 -0700 | [diff] [blame] | 1001 | size_t yaColOffset = (size_t)colIndex * firstTile->image->width; |
| 1002 | size_t yaRowOffset = (size_t)rowIndex * firstTile->image->height; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1003 | size_t yaRowBytes = widthToCopy * pixelBytes; |
| 1004 | |
| 1005 | if (alpha) { |
| 1006 | // A |
| 1007 | for (unsigned int j = 0; j < heightToCopy; ++j) { |
| 1008 | uint8_t * src = &tile->image->alphaPlane[j * tile->image->alphaRowBytes]; |
| 1009 | uint8_t * dst = &dstImage->alphaPlane[(yaColOffset * pixelBytes) + ((yaRowOffset + j) * dstImage->alphaRowBytes)]; |
| 1010 | memcpy(dst, src, yaRowBytes); |
| 1011 | } |
| 1012 | } else { |
| 1013 | // Y |
| 1014 | for (unsigned int j = 0; j < heightToCopy; ++j) { |
| 1015 | uint8_t * src = &tile->image->yuvPlanes[AVIF_CHAN_Y][j * tile->image->yuvRowBytes[AVIF_CHAN_Y]]; |
| 1016 | uint8_t * dst = |
| 1017 | &dstImage->yuvPlanes[AVIF_CHAN_Y][(yaColOffset * pixelBytes) + ((yaRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_Y])]; |
| 1018 | memcpy(dst, src, yaRowBytes); |
| 1019 | } |
| 1020 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1021 | if (!firstTileUVPresent) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1022 | continue; |
| 1023 | } |
| 1024 | |
| 1025 | // UV |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1026 | heightToCopy >>= formatInfo.chromaShiftY; |
| 1027 | size_t uvColOffset = yaColOffset >> formatInfo.chromaShiftX; |
| 1028 | size_t uvRowOffset = yaRowOffset >> formatInfo.chromaShiftY; |
| 1029 | size_t uvRowBytes = yaRowBytes >> formatInfo.chromaShiftX; |
| 1030 | for (unsigned int j = 0; j < heightToCopy; ++j) { |
| 1031 | uint8_t * srcU = &tile->image->yuvPlanes[AVIF_CHAN_U][j * tile->image->yuvRowBytes[AVIF_CHAN_U]]; |
| 1032 | uint8_t * dstU = |
| 1033 | &dstImage->yuvPlanes[AVIF_CHAN_U][(uvColOffset * pixelBytes) + ((uvRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_U])]; |
| 1034 | memcpy(dstU, srcU, uvRowBytes); |
| 1035 | |
| 1036 | uint8_t * srcV = &tile->image->yuvPlanes[AVIF_CHAN_V][j * tile->image->yuvRowBytes[AVIF_CHAN_V]]; |
| 1037 | uint8_t * dstV = |
| 1038 | &dstImage->yuvPlanes[AVIF_CHAN_V][(uvColOffset * pixelBytes) + ((uvRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_V])]; |
| 1039 | memcpy(dstV, srcV, uvRowBytes); |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | return AVIF_TRUE; |
| 1046 | } |
| 1047 | |
Joe Drago | 39267fd | 2020-06-19 15:21:14 -0700 | [diff] [blame] | 1048 | // If colorId == 0 (a sentinel value as item IDs must be nonzero), accept any found EXIF/XMP metadata. Passing in 0 |
| 1049 | // is used when finding metadata in a meta box embedded in a trak box, as any items inside of a meta box that is |
Joe Drago | fa99082 | 2020-06-19 15:23:55 -0700 | [diff] [blame] | 1050 | // inside of a trak box are implicitly associated to the track. |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1051 | static avifResult avifDecoderFindMetadata(avifDecoder * decoder, avifMeta * meta, avifImage * image, uint32_t colorId) |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1052 | { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1053 | if (decoder->ignoreExif && decoder->ignoreXMP) { |
| 1054 | // Nothing to do! |
| 1055 | return AVIF_RESULT_OK; |
| 1056 | } |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1057 | |
| 1058 | for (uint32_t itemIndex = 0; itemIndex < meta->items.count; ++itemIndex) { |
| 1059 | avifDecoderItem * item = &meta->items.item[itemIndex]; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 1060 | if (!item->size) { |
| 1061 | continue; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1062 | } |
| 1063 | if (item->hasUnsupportedEssentialProperty) { |
| 1064 | // An essential property isn't supported by libavif; ignore the item. |
| 1065 | continue; |
| 1066 | } |
| 1067 | |
| 1068 | if ((colorId > 0) && (item->descForID != colorId)) { |
| 1069 | // Not a content description (metadata) for the colorOBU, skip it |
| 1070 | continue; |
| 1071 | } |
| 1072 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1073 | if (!decoder->ignoreExif && !memcmp(item->type, "Exif", 4)) { |
| 1074 | avifROData exifContents; |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 1075 | avifResult readResult = avifDecoderItemRead(item, decoder->io, &exifContents, 0); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1076 | if (readResult != AVIF_RESULT_OK) { |
| 1077 | return readResult; |
| 1078 | } |
| 1079 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1080 | // Advance past Annex A.2.1's header |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1081 | BEGIN_STREAM(exifBoxStream, exifContents.data, exifContents.size); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1082 | uint32_t exifTiffHeaderOffset; |
Wan-Teh Chang | f1d6019 | 2020-10-07 16:23:08 -0700 | [diff] [blame] | 1083 | CHECKERR(avifROStreamReadU32(&exifBoxStream, &exifTiffHeaderOffset), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) exif_tiff_header_offset; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1084 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1085 | avifImageSetMetadataExif(image, avifROStreamCurrent(&exifBoxStream), avifROStreamRemainingBytes(&exifBoxStream)); |
| 1086 | } else if (!decoder->ignoreXMP && !memcmp(item->type, "mime", 4) && |
| 1087 | !memcmp(item->contentType.contentType, xmpContentType, xmpContentTypeSize)) { |
| 1088 | avifROData xmpContents; |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 1089 | avifResult readResult = avifDecoderItemRead(item, decoder->io, &xmpContents, 0); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1090 | if (readResult != AVIF_RESULT_OK) { |
| 1091 | return readResult; |
| 1092 | } |
| 1093 | |
| 1094 | avifImageSetMetadataXMP(image, xmpContents.data, xmpContents.size); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1095 | } |
| 1096 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1097 | return AVIF_RESULT_OK; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1100 | // --------------------------------------------------------------------------- |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1101 | // URN |
| 1102 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1103 | static avifBool isAlphaURN(const char * urn) |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1104 | { |
wantehchang | 3fde0d0 | 2020-03-10 23:58:32 -0700 | [diff] [blame] | 1105 | return !strcmp(urn, URN_ALPHA0) || !strcmp(urn, URN_ALPHA1); |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | // --------------------------------------------------------------------------- |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1109 | // BMFF Parsing |
| 1110 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1111 | static avifBool avifParseItemLocationBox(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1112 | { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1113 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1114 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1115 | uint8_t version; |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 1116 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1117 | if (version > 2) { |
| 1118 | return AVIF_FALSE; |
| 1119 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1120 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1121 | uint8_t offsetSizeAndLengthSize; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1122 | CHECK(avifROStreamRead(&s, &offsetSizeAndLengthSize, 1)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1123 | uint8_t offsetSize = (offsetSizeAndLengthSize >> 4) & 0xf; // unsigned int(4) offset_size; |
| 1124 | uint8_t lengthSize = (offsetSizeAndLengthSize >> 0) & 0xf; // unsigned int(4) length_size; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1125 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1126 | uint8_t baseOffsetSizeAndIndexSize; |
| 1127 | CHECK(avifROStreamRead(&s, &baseOffsetSizeAndIndexSize, 1)); |
| 1128 | uint8_t baseOffsetSize = (baseOffsetSizeAndIndexSize >> 4) & 0xf; // unsigned int(4) base_offset_size; |
| 1129 | uint8_t indexSize = 0; |
| 1130 | if ((version == 1) || (version == 2)) { |
| 1131 | indexSize = baseOffsetSizeAndIndexSize & 0xf; // unsigned int(4) index_size; |
| 1132 | if (indexSize != 0) { |
| 1133 | // extent_index unsupported |
| 1134 | return AVIF_FALSE; |
| 1135 | } |
| 1136 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1137 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1138 | uint16_t tmp16; |
| 1139 | uint32_t itemCount; |
| 1140 | if (version < 2) { |
| 1141 | CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_count; |
| 1142 | itemCount = tmp16; |
| 1143 | } else { |
| 1144 | CHECK(avifROStreamReadU32(&s, &itemCount)); // unsigned int(32) item_count; |
| 1145 | } |
| 1146 | for (uint32_t i = 0; i < itemCount; ++i) { |
| 1147 | uint32_t itemID; |
| 1148 | uint32_t idatID = 0; |
| 1149 | if (version < 2) { |
| 1150 | CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID; |
| 1151 | itemID = tmp16; |
| 1152 | } else { |
| 1153 | CHECK(avifROStreamReadU32(&s, &itemID)); // unsigned int(32) item_ID; |
| 1154 | } |
| 1155 | |
| 1156 | if ((version == 1) || (version == 2)) { |
| 1157 | uint8_t ignored; |
| 1158 | uint8_t constructionMethod; |
| 1159 | CHECK(avifROStreamRead(&s, &ignored, 1)); // unsigned int(12) reserved = 0; |
| 1160 | CHECK(avifROStreamRead(&s, &constructionMethod, 1)); // unsigned int(4) construction_method; |
| 1161 | constructionMethod = constructionMethod & 0xf; |
| 1162 | if ((constructionMethod != 0 /* file */) && (constructionMethod != 1 /* idat */)) { |
| 1163 | // construction method item(2) unsupported |
| 1164 | return AVIF_FALSE; |
| 1165 | } |
| 1166 | if (constructionMethod == 1) { |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 1167 | idatID = meta->idatID; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 1171 | avifDecoderItem * item = avifMetaFindItem(meta, itemID); |
| 1172 | if (!item) { |
| 1173 | return AVIF_FALSE; |
| 1174 | } |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 1175 | if (item->extents.count > 0) { |
| 1176 | // This item has already been given extents via this iloc box. This is invalid. |
| 1177 | return AVIF_FALSE; |
| 1178 | } |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 1179 | item->idatID = idatID; |
| 1180 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1181 | uint16_t dataReferenceIndex; // unsigned int(16) data_ref rence_index; |
| 1182 | CHECK(avifROStreamReadU16(&s, &dataReferenceIndex)); // |
| 1183 | uint64_t baseOffset; // unsigned int(base_offset_size*8) base_offset; |
| 1184 | CHECK(avifROStreamReadUX8(&s, &baseOffset, baseOffsetSize)); // |
| 1185 | uint16_t extentCount; // unsigned int(16) extent_count; |
| 1186 | CHECK(avifROStreamReadU16(&s, &extentCount)); // |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 1187 | for (int extentIter = 0; extentIter < extentCount; ++extentIter) { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1188 | // If extent_index is ever supported, this spec must be implemented here: |
| 1189 | // :: if (((version == 1) || (version == 2)) && (index_size > 0)) { |
| 1190 | // :: unsigned int(index_size*8) extent_index; |
| 1191 | // :: } |
| 1192 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1193 | uint64_t extentOffset; // unsigned int(offset_size*8) extent_offset; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1194 | CHECK(avifROStreamReadUX8(&s, &extentOffset, offsetSize)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1195 | uint64_t extentLength; // unsigned int(offset_size*8) extent_length; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1196 | CHECK(avifROStreamReadUX8(&s, &extentLength, lengthSize)); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1197 | |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 1198 | avifExtent * extent = (avifExtent *)avifArrayPushPtr(&item->extents); |
Wan-Teh Chang | b985369 | 2020-08-28 15:32:36 -0700 | [diff] [blame] | 1199 | if (extentOffset > UINT64_MAX - baseOffset) { |
| 1200 | return AVIF_FALSE; |
| 1201 | } |
| 1202 | uint64_t offset = baseOffset + extentOffset; |
Joe Drago | 217056b | 2020-11-13 16:19:35 -0800 | [diff] [blame] | 1203 | extent->offset = offset; |
| 1204 | if (extentLength > SIZE_MAX) { |
Wan-Teh Chang | b985369 | 2020-08-28 15:32:36 -0700 | [diff] [blame] | 1205 | return AVIF_FALSE; |
| 1206 | } |
Joe Drago | 217056b | 2020-11-13 16:19:35 -0800 | [diff] [blame] | 1207 | extent->size = (size_t)extentLength; |
| 1208 | if (extent->size > SIZE_MAX - item->size) { |
Wan-Teh Chang | b985369 | 2020-08-28 15:32:36 -0700 | [diff] [blame] | 1209 | return AVIF_FALSE; |
| 1210 | } |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 1211 | item->size += extent->size; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | return AVIF_TRUE; |
| 1215 | } |
| 1216 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1217 | static avifBool avifParseImageGridBox(avifImageGrid * grid, const uint8_t * raw, size_t rawLen) |
| 1218 | { |
| 1219 | BEGIN_STREAM(s, raw, rawLen); |
| 1220 | |
| 1221 | uint8_t version, flags; |
| 1222 | CHECK(avifROStreamRead(&s, &version, 1)); // unsigned int(8) version = 0; |
| 1223 | if (version != 0) { |
| 1224 | return AVIF_FALSE; |
| 1225 | } |
Joe Drago | 79ebcf3 | 2020-11-18 22:37:10 -0800 | [diff] [blame] | 1226 | uint8_t rowsMinusOne, columnsMinusOne; |
| 1227 | CHECK(avifROStreamRead(&s, &flags, 1)); // unsigned int(8) flags; |
| 1228 | CHECK(avifROStreamRead(&s, &rowsMinusOne, 1)); // unsigned int(8) rows_minus_one; |
| 1229 | CHECK(avifROStreamRead(&s, &columnsMinusOne, 1)); // unsigned int(8) columns_minus_one; |
| 1230 | grid->rows = (uint32_t)rowsMinusOne + 1; |
| 1231 | grid->columns = (uint32_t)columnsMinusOne + 1; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1232 | |
| 1233 | uint32_t fieldLength = ((flags & 1) + 1) * 16; |
| 1234 | if (fieldLength == 16) { |
| 1235 | uint16_t outputWidth16, outputHeight16; |
| 1236 | CHECK(avifROStreamReadU16(&s, &outputWidth16)); // unsigned int(FieldLength) output_width; |
| 1237 | CHECK(avifROStreamReadU16(&s, &outputHeight16)); // unsigned int(FieldLength) output_height; |
| 1238 | grid->outputWidth = outputWidth16; |
| 1239 | grid->outputHeight = outputHeight16; |
| 1240 | } else { |
| 1241 | if (fieldLength != 32) { |
| 1242 | // This should be impossible |
| 1243 | return AVIF_FALSE; |
| 1244 | } |
| 1245 | CHECK(avifROStreamReadU32(&s, &grid->outputWidth)); // unsigned int(FieldLength) output_width; |
| 1246 | CHECK(avifROStreamReadU32(&s, &grid->outputHeight)); // unsigned int(FieldLength) output_height; |
| 1247 | } |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame] | 1248 | if ((grid->outputWidth == 0) || (grid->outputHeight == 0) || (grid->outputWidth > (AVIF_MAX_IMAGE_SIZE / grid->outputHeight))) { |
Wan-Teh Chang | 0a8e724 | 2020-08-10 13:24:59 -0700 | [diff] [blame] | 1249 | return AVIF_FALSE; |
| 1250 | } |
wantehchang | ae2074b | 2020-08-12 13:02:27 -0700 | [diff] [blame] | 1251 | return avifROStreamRemainingBytes(&s) == 0; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1252 | } |
| 1253 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1254 | static avifBool avifParseImageSpatialExtentsProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1255 | { |
| 1256 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1257 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1258 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1259 | avifImageSpatialExtents * ispe = &prop->u.ispe; |
| 1260 | CHECK(avifROStreamReadU32(&s, &ispe->width)); |
| 1261 | CHECK(avifROStreamReadU32(&s, &ispe->height)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1262 | return AVIF_TRUE; |
| 1263 | } |
| 1264 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1265 | static avifBool avifParseAuxiliaryTypeProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1266 | { |
| 1267 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1268 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1269 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1270 | CHECK(avifROStreamReadString(&s, prop->u.auxC.auxType, AUXTYPE_SIZE)); |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1271 | return AVIF_TRUE; |
| 1272 | } |
| 1273 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1274 | static avifBool avifParseColourInformationBox(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 1275 | { |
| 1276 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | e7ce20d | 2019-02-11 16:37:38 -0800 | [diff] [blame] | 1277 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1278 | avifColourInformationBox * colr = &prop->u.colr; |
| 1279 | colr->hasICC = AVIF_FALSE; |
| 1280 | colr->hasNCLX = AVIF_FALSE; |
Joe Drago | e7ce20d | 2019-02-11 16:37:38 -0800 | [diff] [blame] | 1281 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1282 | uint8_t colorType[4]; // unsigned int(32) colour_type; |
| 1283 | CHECK(avifROStreamRead(&s, colorType, 4)); |
| 1284 | if (!memcmp(colorType, "rICC", 4) || !memcmp(colorType, "prof", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1285 | colr->hasICC = AVIF_TRUE; |
| 1286 | colr->icc = avifROStreamCurrent(&s); |
| 1287 | colr->iccSize = avifROStreamRemainingBytes(&s); |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1288 | } else if (!memcmp(colorType, "nclx", 4)) { |
Joe Drago | fb5a5f0 | 2021-01-31 20:43:57 -0800 | [diff] [blame] | 1289 | CHECK(avifROStreamReadU16(&s, &colr->colorPrimaries)); // unsigned int(16) colour_primaries; |
| 1290 | CHECK(avifROStreamReadU16(&s, &colr->transferCharacteristics)); // unsigned int(16) transfer_characteristics; |
| 1291 | CHECK(avifROStreamReadU16(&s, &colr->matrixCoefficients)); // unsigned int(16) matrix_coefficients; |
Joe Drago | 97b071c | 2019-07-17 14:24:56 -0700 | [diff] [blame] | 1292 | // unsigned int(1) full_range_flag; |
| 1293 | // unsigned int(7) reserved = 0; |
Joe Drago | 74cd1c9 | 2020-04-16 12:17:11 -0700 | [diff] [blame] | 1294 | uint8_t tmp8; |
| 1295 | CHECK(avifROStreamRead(&s, &tmp8, 1)); |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1296 | colr->range = (tmp8 & 0x80) ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED; |
| 1297 | colr->hasNCLX = AVIF_TRUE; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 1298 | } |
| 1299 | return AVIF_TRUE; |
| 1300 | } |
| 1301 | |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1302 | static avifBool avifParseAV1CodecConfigurationBox(const uint8_t * raw, size_t rawLen, avifCodecConfigurationBox * av1C) |
| 1303 | { |
| 1304 | BEGIN_STREAM(s, raw, rawLen); |
| 1305 | |
| 1306 | uint8_t markerAndVersion = 0; |
| 1307 | CHECK(avifROStreamRead(&s, &markerAndVersion, 1)); |
| 1308 | uint8_t seqProfileAndIndex = 0; |
| 1309 | CHECK(avifROStreamRead(&s, &seqProfileAndIndex, 1)); |
| 1310 | uint8_t rawFlags = 0; |
| 1311 | CHECK(avifROStreamRead(&s, &rawFlags, 1)); |
| 1312 | |
| 1313 | if (markerAndVersion != 0x81) { |
| 1314 | // Marker and version must both == 1 |
| 1315 | return AVIF_FALSE; |
| 1316 | } |
| 1317 | |
| 1318 | av1C->seqProfile = (seqProfileAndIndex >> 5) & 0x7; // unsigned int (3) seq_profile; |
| 1319 | av1C->seqLevelIdx0 = (seqProfileAndIndex >> 0) & 0x1f; // unsigned int (5) seq_level_idx_0; |
| 1320 | av1C->seqTier0 = (rawFlags >> 7) & 0x1; // unsigned int (1) seq_tier_0; |
| 1321 | av1C->highBitdepth = (rawFlags >> 6) & 0x1; // unsigned int (1) high_bitdepth; |
| 1322 | av1C->twelveBit = (rawFlags >> 5) & 0x1; // unsigned int (1) twelve_bit; |
| 1323 | av1C->monochrome = (rawFlags >> 4) & 0x1; // unsigned int (1) monochrome; |
| 1324 | av1C->chromaSubsamplingX = (rawFlags >> 3) & 0x1; // unsigned int (1) chroma_subsampling_x; |
| 1325 | av1C->chromaSubsamplingY = (rawFlags >> 2) & 0x1; // unsigned int (1) chroma_subsampling_y; |
| 1326 | av1C->chromaSamplePosition = (rawFlags >> 0) & 0x3; // unsigned int (2) chroma_sample_position; |
| 1327 | return AVIF_TRUE; |
| 1328 | } |
| 1329 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1330 | static avifBool avifParseAV1CodecConfigurationBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1331 | { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1332 | return avifParseAV1CodecConfigurationBox(raw, rawLen, &prop->u.av1C); |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1335 | static avifBool avifParsePixelAspectRatioBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1336 | { |
| 1337 | BEGIN_STREAM(s, raw, rawLen); |
| 1338 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1339 | avifPixelAspectRatioBox * pasp = &prop->u.pasp; |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1340 | CHECK(avifROStreamReadU32(&s, &pasp->hSpacing)); // unsigned int(32) hSpacing; |
| 1341 | CHECK(avifROStreamReadU32(&s, &pasp->vSpacing)); // unsigned int(32) vSpacing; |
| 1342 | return AVIF_TRUE; |
| 1343 | } |
| 1344 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1345 | static avifBool avifParseCleanApertureBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1346 | { |
| 1347 | BEGIN_STREAM(s, raw, rawLen); |
| 1348 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1349 | avifCleanApertureBox * clap = &prop->u.clap; |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1350 | CHECK(avifROStreamReadU32(&s, &clap->widthN)); // unsigned int(32) cleanApertureWidthN; |
| 1351 | CHECK(avifROStreamReadU32(&s, &clap->widthD)); // unsigned int(32) cleanApertureWidthD; |
| 1352 | CHECK(avifROStreamReadU32(&s, &clap->heightN)); // unsigned int(32) cleanApertureHeightN; |
| 1353 | CHECK(avifROStreamReadU32(&s, &clap->heightD)); // unsigned int(32) cleanApertureHeightD; |
| 1354 | CHECK(avifROStreamReadU32(&s, &clap->horizOffN)); // unsigned int(32) horizOffN; |
| 1355 | CHECK(avifROStreamReadU32(&s, &clap->horizOffD)); // unsigned int(32) horizOffD; |
| 1356 | CHECK(avifROStreamReadU32(&s, &clap->vertOffN)); // unsigned int(32) vertOffN; |
| 1357 | CHECK(avifROStreamReadU32(&s, &clap->vertOffD)); // unsigned int(32) vertOffD; |
| 1358 | return AVIF_TRUE; |
| 1359 | } |
| 1360 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1361 | static avifBool avifParseImageRotationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1362 | { |
| 1363 | BEGIN_STREAM(s, raw, rawLen); |
| 1364 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1365 | avifImageRotation * irot = &prop->u.irot; |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1366 | CHECK(avifROStreamRead(&s, &irot->angle, 1)); // unsigned int (6) reserved = 0; unsigned int (2) angle; |
| 1367 | if ((irot->angle & 0xfc) != 0) { |
| 1368 | // reserved bits must be 0 |
| 1369 | return AVIF_FALSE; |
| 1370 | } |
| 1371 | return AVIF_TRUE; |
| 1372 | } |
| 1373 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1374 | static avifBool avifParseImageMirrorProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1375 | { |
| 1376 | BEGIN_STREAM(s, raw, rawLen); |
| 1377 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1378 | avifImageMirror * imir = &prop->u.imir; |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1379 | CHECK(avifROStreamRead(&s, &imir->axis, 1)); // unsigned int (7) reserved = 0; unsigned int (1) axis; |
| 1380 | if ((imir->axis & 0xfe) != 0) { |
| 1381 | // reserved bits must be 0 |
| 1382 | return AVIF_FALSE; |
| 1383 | } |
| 1384 | return AVIF_TRUE; |
| 1385 | } |
| 1386 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1387 | static avifBool avifParsePixelInformationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 6042156 | 2020-04-23 11:32:26 -0700 | [diff] [blame] | 1388 | { |
| 1389 | BEGIN_STREAM(s, raw, rawLen); |
| 1390 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
| 1391 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1392 | avifPixelInformationProperty * pixi = &prop->u.pixi; |
Joe Drago | 6042156 | 2020-04-23 11:32:26 -0700 | [diff] [blame] | 1393 | CHECK(avifROStreamRead(&s, &pixi->planeCount, 1)); // unsigned int (8) num_channels; |
| 1394 | if (pixi->planeCount > MAX_PIXI_PLANE_DEPTHS) { |
| 1395 | return AVIF_FALSE; |
| 1396 | } |
| 1397 | for (uint8_t i = 0; i < pixi->planeCount; ++i) { |
| 1398 | CHECK(avifROStreamRead(&s, &pixi->planeDepths[i], 1)); // unsigned int (8) bits_per_channel; |
| 1399 | } |
| 1400 | return AVIF_TRUE; |
| 1401 | } |
| 1402 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1403 | static avifBool avifParseItemPropertyContainerBox(avifPropertyArray * properties, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1404 | { |
| 1405 | BEGIN_STREAM(s, raw, rawLen); |
| 1406 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1407 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1408 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1409 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1410 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1411 | int propertyIndex = avifArrayPushIndex(properties); |
| 1412 | avifProperty * prop = &properties->prop[propertyIndex]; |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1413 | memcpy(prop->type, header.type, 4); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1414 | if (!memcmp(header.type, "ispe", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1415 | CHECK(avifParseImageSpatialExtentsProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1416 | } else if (!memcmp(header.type, "auxC", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1417 | CHECK(avifParseAuxiliaryTypeProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1418 | } else if (!memcmp(header.type, "colr", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1419 | CHECK(avifParseColourInformationBox(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1420 | } else if (!memcmp(header.type, "av1C", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1421 | CHECK(avifParseAV1CodecConfigurationBoxProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1422 | } else if (!memcmp(header.type, "pasp", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1423 | CHECK(avifParsePixelAspectRatioBoxProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1424 | } else if (!memcmp(header.type, "clap", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1425 | CHECK(avifParseCleanApertureBoxProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1426 | } else if (!memcmp(header.type, "irot", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1427 | CHECK(avifParseImageRotationProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1428 | } else if (!memcmp(header.type, "imir", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1429 | CHECK(avifParseImageMirrorProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1430 | } else if (!memcmp(header.type, "pixi", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1431 | CHECK(avifParsePixelInformationProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 6042156 | 2020-04-23 11:32:26 -0700 | [diff] [blame] | 1432 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1433 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1434 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1435 | } |
| 1436 | return AVIF_TRUE; |
| 1437 | } |
| 1438 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1439 | static avifBool avifParseItemPropertyAssociation(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1440 | { |
| 1441 | BEGIN_STREAM(s, raw, rawLen); |
| 1442 | |
| 1443 | uint8_t version; |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 1444 | uint32_t flags; |
| 1445 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, &flags)); |
| 1446 | avifBool propertyIndexIsU16 = ((flags & 0x1) != 0); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1447 | |
| 1448 | uint32_t entryCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1449 | CHECK(avifROStreamReadU32(&s, &entryCount)); |
Wan-Teh Chang | f4c6538 | 2020-11-03 14:27:45 -0800 | [diff] [blame] | 1450 | unsigned int prevItemID = 0; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1451 | for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) { |
Wan-Teh Chang | f4c6538 | 2020-11-03 14:27:45 -0800 | [diff] [blame] | 1452 | // ISO/IEC 23008-12, First edition, 2017-12, Section 9.3.1: |
| 1453 | // Each ItemPropertyAssociation box shall be ordered by increasing item_ID, and there shall |
| 1454 | // be at most one association box for each item_ID, in any ItemPropertyAssociation box. |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1455 | unsigned int itemID; |
| 1456 | if (version < 1) { |
| 1457 | uint16_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1458 | CHECK(avifROStreamReadU16(&s, &tmp)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1459 | itemID = tmp; |
| 1460 | } else { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1461 | CHECK(avifROStreamReadU32(&s, &itemID)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1462 | } |
Wan-Teh Chang | f4c6538 | 2020-11-03 14:27:45 -0800 | [diff] [blame] | 1463 | if (itemID <= prevItemID) { |
| 1464 | return AVIF_FALSE; |
| 1465 | } |
| 1466 | prevItemID = itemID; |
Wan-Teh Chang | 750a192 | 2020-10-28 17:54:35 -0700 | [diff] [blame] | 1467 | |
| 1468 | avifDecoderItem * item = avifMetaFindItem(meta, itemID); |
| 1469 | if (!item) { |
| 1470 | return AVIF_FALSE; |
| 1471 | } |
Wan-Teh Chang | f4c6538 | 2020-11-03 14:27:45 -0800 | [diff] [blame] | 1472 | if (item->ipmaSeen) { |
| 1473 | return AVIF_FALSE; |
| 1474 | } |
| 1475 | item->ipmaSeen = AVIF_TRUE; |
Wan-Teh Chang | 750a192 | 2020-10-28 17:54:35 -0700 | [diff] [blame] | 1476 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1477 | uint8_t associationCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1478 | CHECK(avifROStreamRead(&s, &associationCount, 1)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1479 | for (uint8_t associationIndex = 0; associationIndex < associationCount; ++associationIndex) { |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 1480 | avifBool essential = AVIF_FALSE; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1481 | uint16_t propertyIndex = 0; |
| 1482 | if (propertyIndexIsU16) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1483 | CHECK(avifROStreamReadU16(&s, &propertyIndex)); |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 1484 | essential = ((propertyIndex & 0x8000) != 0); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1485 | propertyIndex &= 0x7fff; |
| 1486 | } else { |
| 1487 | uint8_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1488 | CHECK(avifROStreamRead(&s, &tmp, 1)); |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 1489 | essential = ((tmp & 0x80) != 0); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1490 | propertyIndex = tmp & 0x7f; |
| 1491 | } |
| 1492 | |
| 1493 | if (propertyIndex == 0) { |
| 1494 | // Not associated with any item |
| 1495 | continue; |
| 1496 | } |
| 1497 | --propertyIndex; // 1-indexed |
| 1498 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1499 | if (propertyIndex >= meta->properties.count) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1500 | return AVIF_FALSE; |
| 1501 | } |
| 1502 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1503 | // Copy property to item |
| 1504 | avifProperty * srcProp = &meta->properties.prop[propertyIndex]; |
| 1505 | |
| 1506 | static const char * supportedTypes[] = { "ispe", "auxC", "colr", "av1C", "pasp", "clap", "irot", "imir", "pixi" }; |
| 1507 | size_t supportedTypesCount = sizeof(supportedTypes) / sizeof(supportedTypes[0]); |
| 1508 | avifBool supportedType = AVIF_FALSE; |
| 1509 | for (size_t i = 0; i < supportedTypesCount; ++i) { |
| 1510 | if (!memcmp(srcProp->type, supportedTypes[i], 4)) { |
| 1511 | supportedType = AVIF_TRUE; |
| 1512 | break; |
| 1513 | } |
| 1514 | } |
| 1515 | if (supportedType) { |
| 1516 | avifProperty * dstProp = (avifProperty *)avifArrayPushPtr(&item->properties); |
| 1517 | memcpy(dstProp, srcProp, sizeof(avifProperty)); |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 1518 | } else { |
| 1519 | if (essential) { |
| 1520 | // Discovered an essential item property that libavif doesn't support! |
| 1521 | // Make a note to ignore this item later. |
| 1522 | item->hasUnsupportedEssentialProperty = AVIF_TRUE; |
| 1523 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | return AVIF_TRUE; |
| 1529 | } |
| 1530 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1531 | static avifBool avifParsePrimaryItemBox(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1532 | { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1533 | if (meta->primaryItemID > 0) { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1534 | // Illegal to have multiple pitm boxes, bail out |
| 1535 | return AVIF_FALSE; |
| 1536 | } |
| 1537 | |
| 1538 | BEGIN_STREAM(s, raw, rawLen); |
| 1539 | |
| 1540 | uint8_t version; |
| 1541 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
| 1542 | |
| 1543 | if (version == 0) { |
| 1544 | uint16_t tmp16; |
| 1545 | CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID; |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1546 | meta->primaryItemID = tmp16; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1547 | } else { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1548 | CHECK(avifROStreamReadU32(&s, &meta->primaryItemID)); // unsigned int(32) item_ID; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1549 | } |
| 1550 | return AVIF_TRUE; |
| 1551 | } |
| 1552 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1553 | static avifBool avifParseItemDataBox(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1554 | { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1555 | // Check to see if we've already seen an idat box for this meta box. If so, bail out |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1556 | for (uint32_t i = 0; i < meta->idats.count; ++i) { |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 1557 | if (meta->idats.idat[i].id == meta->idatID) { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1558 | return AVIF_FALSE; |
| 1559 | } |
| 1560 | } |
| 1561 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1562 | int index = avifArrayPushIndex(&meta->idats); |
| 1563 | avifDecoderItemData * idat = &meta->idats.idat[index]; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 1564 | idat->id = meta->idatID; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1565 | avifRWDataSet(&idat->data, raw, rawLen); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1566 | return AVIF_TRUE; |
| 1567 | } |
| 1568 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1569 | static avifBool avifParseItemPropertiesBox(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1570 | { |
| 1571 | BEGIN_STREAM(s, raw, rawLen); |
| 1572 | |
| 1573 | avifBoxHeader ipcoHeader; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1574 | CHECK(avifROStreamReadBoxHeader(&s, &ipcoHeader)); |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame] | 1575 | if (memcmp(ipcoHeader.type, "ipco", 4)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1576 | return AVIF_FALSE; |
| 1577 | } |
| 1578 | |
| 1579 | // Read all item properties inside of ItemPropertyContainerBox |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1580 | CHECK(avifParseItemPropertyContainerBox(&meta->properties, avifROStreamCurrent(&s), ipcoHeader.size)); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1581 | CHECK(avifROStreamSkip(&s, ipcoHeader.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1582 | |
| 1583 | // Now read all ItemPropertyAssociation until the end of the box, and make associations |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1584 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1585 | avifBoxHeader ipmaHeader; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1586 | CHECK(avifROStreamReadBoxHeader(&s, &ipmaHeader)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1587 | |
| 1588 | if (!memcmp(ipmaHeader.type, "ipma", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1589 | CHECK(avifParseItemPropertyAssociation(meta, avifROStreamCurrent(&s), ipmaHeader.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1590 | } else { |
| 1591 | // These must all be type ipma |
| 1592 | return AVIF_FALSE; |
| 1593 | } |
| 1594 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1595 | CHECK(avifROStreamSkip(&s, ipmaHeader.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1596 | } |
| 1597 | return AVIF_TRUE; |
| 1598 | } |
| 1599 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1600 | static avifBool avifParseItemInfoEntry(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1601 | { |
| 1602 | BEGIN_STREAM(s, raw, rawLen); |
| 1603 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1604 | CHECK(avifROStreamReadAndEnforceVersion(&s, 2)); // TODO: support version > 2? 2+ is required for item_type |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1605 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1606 | uint16_t itemID; // unsigned int(16) item_ID; |
| 1607 | CHECK(avifROStreamReadU16(&s, &itemID)); // |
| 1608 | uint16_t itemProtectionIndex; // unsigned int(16) item_protection_index; |
| 1609 | CHECK(avifROStreamReadU16(&s, &itemProtectionIndex)); // |
| 1610 | uint8_t itemType[4]; // unsigned int(32) item_type; |
| 1611 | CHECK(avifROStreamRead(&s, itemType, 4)); // |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1612 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1613 | avifContentType contentType; |
| 1614 | if (!memcmp(itemType, "mime", 4)) { |
| 1615 | CHECK(avifROStreamReadString(&s, NULL, 0)); // string item_name; (skipped) |
| 1616 | CHECK(avifROStreamReadString(&s, contentType.contentType, CONTENTTYPE_SIZE)); // string content_type; |
| 1617 | } else { |
| 1618 | memset(&contentType, 0, sizeof(contentType)); |
| 1619 | } |
| 1620 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1621 | avifDecoderItem * item = avifMetaFindItem(meta, itemID); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1622 | if (!item) { |
| 1623 | return AVIF_FALSE; |
| 1624 | } |
| 1625 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 1626 | memcpy(item->type, itemType, sizeof(itemType)); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1627 | memcpy(&item->contentType, &contentType, sizeof(contentType)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1628 | return AVIF_TRUE; |
| 1629 | } |
| 1630 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1631 | static avifBool avifParseItemInfoBox(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1632 | { |
| 1633 | BEGIN_STREAM(s, raw, rawLen); |
| 1634 | |
| 1635 | uint8_t version; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1636 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1637 | uint32_t entryCount; |
| 1638 | if (version == 0) { |
| 1639 | uint16_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1640 | CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) entry_count; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1641 | entryCount = tmp; |
| 1642 | } else if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1643 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1644 | } else { |
| 1645 | return AVIF_FALSE; |
| 1646 | } |
| 1647 | |
Joe Drago | 678b938 | 2019-02-09 03:17:47 -0800 | [diff] [blame] | 1648 | for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1649 | avifBoxHeader infeHeader; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1650 | CHECK(avifROStreamReadBoxHeader(&s, &infeHeader)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1651 | |
| 1652 | if (!memcmp(infeHeader.type, "infe", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1653 | CHECK(avifParseItemInfoEntry(meta, avifROStreamCurrent(&s), infeHeader.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1654 | } else { |
| 1655 | // These must all be type ipma |
| 1656 | return AVIF_FALSE; |
| 1657 | } |
| 1658 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1659 | CHECK(avifROStreamSkip(&s, infeHeader.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | return AVIF_TRUE; |
| 1663 | } |
| 1664 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1665 | static avifBool avifParseItemReferenceBox(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1666 | { |
| 1667 | BEGIN_STREAM(s, raw, rawLen); |
| 1668 | |
| 1669 | uint8_t version; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1670 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1671 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1672 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1673 | avifBoxHeader irefHeader; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1674 | CHECK(avifROStreamReadBoxHeader(&s, &irefHeader)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1675 | |
| 1676 | uint32_t fromID = 0; |
| 1677 | if (version == 0) { |
| 1678 | uint16_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1679 | CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) from_item_ID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1680 | fromID = tmp; |
| 1681 | } else if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1682 | CHECK(avifROStreamReadU32(&s, &fromID)); // unsigned int(32) from_item_ID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1683 | } else { |
| 1684 | // unsupported iref version, skip it |
| 1685 | break; |
| 1686 | } |
| 1687 | |
| 1688 | uint16_t referenceCount = 0; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1689 | CHECK(avifROStreamReadU16(&s, &referenceCount)); // unsigned int(16) reference_count; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1690 | |
| 1691 | for (uint16_t refIndex = 0; refIndex < referenceCount; ++refIndex) { |
| 1692 | uint32_t toID = 0; |
| 1693 | if (version == 0) { |
| 1694 | uint16_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1695 | CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) to_item_ID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1696 | toID = tmp; |
| 1697 | } else if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1698 | CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) to_item_ID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1699 | } else { |
| 1700 | // unsupported iref version, skip it |
| 1701 | break; |
| 1702 | } |
| 1703 | |
| 1704 | // Read this reference as "{fromID} is a {irefType} for {toID}" |
| 1705 | if (fromID && toID) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1706 | avifDecoderItem * item = avifMetaFindItem(meta, fromID); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1707 | if (!item) { |
| 1708 | return AVIF_FALSE; |
| 1709 | } |
| 1710 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1711 | if (!memcmp(irefHeader.type, "thmb", 4)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 1712 | item->thumbnailForID = toID; |
Wan-Teh Chang | b309e98 | 2021-02-20 16:59:36 -0800 | [diff] [blame] | 1713 | } else if (!memcmp(irefHeader.type, "auxl", 4)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 1714 | item->auxForID = toID; |
Wan-Teh Chang | b309e98 | 2021-02-20 16:59:36 -0800 | [diff] [blame] | 1715 | } else if (!memcmp(irefHeader.type, "cdsc", 4)) { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1716 | item->descForID = toID; |
Wan-Teh Chang | b309e98 | 2021-02-20 16:59:36 -0800 | [diff] [blame] | 1717 | } else if (!memcmp(irefHeader.type, "dimg", 4)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1718 | // derived images refer in the opposite direction |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1719 | avifDecoderItem * dimg = avifMetaFindItem(meta, toID); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1720 | if (!dimg) { |
| 1721 | return AVIF_FALSE; |
| 1722 | } |
| 1723 | |
| 1724 | dimg->dimgForID = fromID; |
Wan-Teh Chang | b309e98 | 2021-02-20 16:59:36 -0800 | [diff] [blame] | 1725 | } else if (!memcmp(irefHeader.type, "prem", 4)) { |
Yuan Tong | e4850be | 2021-01-22 14:21:25 +0800 | [diff] [blame] | 1726 | item->premByID = toID; |
| 1727 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1728 | } |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | return AVIF_TRUE; |
| 1733 | } |
| 1734 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1735 | static avifBool avifParseMetaBox(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1736 | { |
| 1737 | BEGIN_STREAM(s, raw, rawLen); |
| 1738 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1739 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1740 | |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 1741 | ++meta->idatID; // for tracking idat |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1742 | |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 1743 | uint32_t uniqueBoxFlags = 0; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1744 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1745 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1746 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1747 | |
| 1748 | if (!memcmp(header.type, "iloc", 4)) { |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 1749 | CHECK(uniqueBoxSeen(&uniqueBoxFlags, 0)); |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1750 | CHECK(avifParseItemLocationBox(meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1751 | } else if (!memcmp(header.type, "pitm", 4)) { |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 1752 | CHECK(uniqueBoxSeen(&uniqueBoxFlags, 1)); |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1753 | CHECK(avifParsePrimaryItemBox(meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1754 | } else if (!memcmp(header.type, "idat", 4)) { |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 1755 | CHECK(uniqueBoxSeen(&uniqueBoxFlags, 2)); |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1756 | CHECK(avifParseItemDataBox(meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1757 | } else if (!memcmp(header.type, "iprp", 4)) { |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 1758 | CHECK(uniqueBoxSeen(&uniqueBoxFlags, 3)); |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1759 | CHECK(avifParseItemPropertiesBox(meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1760 | } else if (!memcmp(header.type, "iinf", 4)) { |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 1761 | CHECK(uniqueBoxSeen(&uniqueBoxFlags, 4)); |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1762 | CHECK(avifParseItemInfoBox(meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1763 | } else if (!memcmp(header.type, "iref", 4)) { |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 1764 | CHECK(uniqueBoxSeen(&uniqueBoxFlags, 5)); |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1765 | CHECK(avifParseItemReferenceBox(meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1766 | } |
| 1767 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1768 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1769 | } |
| 1770 | return AVIF_TRUE; |
| 1771 | } |
| 1772 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1773 | static avifBool avifParseTrackHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1774 | { |
| 1775 | BEGIN_STREAM(s, raw, rawLen); |
| 1776 | |
| 1777 | uint8_t version; |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 1778 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1779 | |
| 1780 | uint32_t ignored32, trackID; |
| 1781 | uint64_t ignored64; |
| 1782 | if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1783 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time; |
| 1784 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time; |
| 1785 | CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID; |
Joe Drago | fc4144e | 2019-09-27 20:35:06 -0700 | [diff] [blame] | 1786 | CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0; |
| 1787 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) duration; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1788 | } else if (version == 0) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1789 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time; |
| 1790 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time; |
| 1791 | CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID; |
Joe Drago | fc4144e | 2019-09-27 20:35:06 -0700 | [diff] [blame] | 1792 | CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0; |
| 1793 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) duration; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1794 | } else { |
| 1795 | // Unsupported version |
| 1796 | return AVIF_FALSE; |
| 1797 | } |
| 1798 | |
Joe Drago | fc4144e | 2019-09-27 20:35:06 -0700 | [diff] [blame] | 1799 | // Skipping the following 52 bytes here: |
| 1800 | // ------------------------------------ |
| 1801 | // const unsigned int(32)[2] reserved = 0; |
| 1802 | // template int(16) layer = 0; |
| 1803 | // template int(16) alternate_group = 0; |
| 1804 | // template int(16) volume = {if track_is_audio 0x0100 else 0}; |
| 1805 | // const unsigned int(16) reserved = 0; |
| 1806 | // template int(32)[9] matrix= { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 }; // unity matrix |
| 1807 | CHECK(avifROStreamSkip(&s, 52)); |
| 1808 | |
| 1809 | uint32_t width, height; |
| 1810 | CHECK(avifROStreamReadU32(&s, &width)); // unsigned int(32) width; |
| 1811 | CHECK(avifROStreamReadU32(&s, &height)); // unsigned int(32) height; |
| 1812 | track->width = width >> 16; |
| 1813 | track->height = height >> 16; |
| 1814 | |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1815 | // TODO: support scaling based on width/height track header info? |
| 1816 | |
| 1817 | track->id = trackID; |
| 1818 | return AVIF_TRUE; |
| 1819 | } |
| 1820 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1821 | static avifBool avifParseMediaHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1822 | { |
| 1823 | BEGIN_STREAM(s, raw, rawLen); |
| 1824 | |
| 1825 | uint8_t version; |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 1826 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1827 | |
| 1828 | uint32_t ignored32, mediaTimescale, mediaDuration32; |
| 1829 | uint64_t ignored64, mediaDuration64; |
| 1830 | if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1831 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time; |
| 1832 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time; |
| 1833 | CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale; |
| 1834 | CHECK(avifROStreamReadU64(&s, &mediaDuration64)); // unsigned int(64) duration; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1835 | track->mediaDuration = mediaDuration64; |
| 1836 | } else if (version == 0) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1837 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time; |
| 1838 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time; |
| 1839 | CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale; |
| 1840 | CHECK(avifROStreamReadU32(&s, &mediaDuration32)); // unsigned int(32) duration; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1841 | track->mediaDuration = (uint64_t)mediaDuration32; |
| 1842 | } else { |
| 1843 | // Unsupported version |
| 1844 | return AVIF_FALSE; |
| 1845 | } |
| 1846 | |
| 1847 | track->mediaTimescale = mediaTimescale; |
| 1848 | return AVIF_TRUE; |
| 1849 | } |
| 1850 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1851 | static avifBool avifParseChunkOffsetBox(avifSampleTable * sampleTable, avifBool largeOffsets, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1852 | { |
| 1853 | BEGIN_STREAM(s, raw, rawLen); |
| 1854 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1855 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1856 | |
| 1857 | uint32_t entryCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1858 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1859 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 1860 | uint64_t offset; |
| 1861 | if (largeOffsets) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1862 | CHECK(avifROStreamReadU64(&s, &offset)); // unsigned int(32) chunk_offset; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1863 | } else { |
| 1864 | uint32_t offset32; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1865 | CHECK(avifROStreamReadU32(&s, &offset32)); // unsigned int(32) chunk_offset; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1866 | offset = (uint64_t)offset32; |
| 1867 | } |
| 1868 | |
| 1869 | avifSampleTableChunk * chunk = (avifSampleTableChunk *)avifArrayPushPtr(&sampleTable->chunks); |
| 1870 | chunk->offset = offset; |
| 1871 | } |
| 1872 | return AVIF_TRUE; |
| 1873 | } |
| 1874 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1875 | static avifBool avifParseSampleToChunkBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1876 | { |
| 1877 | BEGIN_STREAM(s, raw, rawLen); |
| 1878 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1879 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1880 | |
| 1881 | uint32_t entryCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1882 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
Joe Drago | 859c910 | 2021-03-11 18:06:40 -0800 | [diff] [blame] | 1883 | uint32_t prevFirstChunk = 0; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1884 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 1885 | avifSampleTableSampleToChunk * sampleToChunk = (avifSampleTableSampleToChunk *)avifArrayPushPtr(&sampleTable->sampleToChunks); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1886 | CHECK(avifROStreamReadU32(&s, &sampleToChunk->firstChunk)); // unsigned int(32) first_chunk; |
| 1887 | CHECK(avifROStreamReadU32(&s, &sampleToChunk->samplesPerChunk)); // unsigned int(32) samples_per_chunk; |
| 1888 | CHECK(avifROStreamReadU32(&s, &sampleToChunk->sampleDescriptionIndex)); // unsigned int(32) sample_description_index; |
Wan-Teh Chang | 2456d2f | 2021-03-11 17:31:03 -0800 | [diff] [blame] | 1889 | // The first_chunk fields should start with 1 and be strictly increasing. |
| 1890 | if (i == 0) { |
| 1891 | if (sampleToChunk->firstChunk != 1) { |
| 1892 | return AVIF_FALSE; |
| 1893 | } |
| 1894 | } else { |
| 1895 | if (sampleToChunk->firstChunk <= prevFirstChunk) { |
| 1896 | return AVIF_FALSE; |
| 1897 | } |
| 1898 | } |
| 1899 | prevFirstChunk = sampleToChunk->firstChunk; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1900 | } |
| 1901 | return AVIF_TRUE; |
| 1902 | } |
| 1903 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1904 | static avifBool avifParseSampleSizeBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1905 | { |
| 1906 | BEGIN_STREAM(s, raw, rawLen); |
| 1907 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1908 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1909 | |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 1910 | uint32_t allSamplesSize, sampleCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1911 | CHECK(avifROStreamReadU32(&s, &allSamplesSize)); // unsigned int(32) sample_size; |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 1912 | CHECK(avifROStreamReadU32(&s, &sampleCount)); // unsigned int(32) sample_count; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1913 | |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 1914 | if (allSamplesSize > 0) { |
| 1915 | sampleTable->allSamplesSize = allSamplesSize; |
| 1916 | } else { |
| 1917 | for (uint32_t i = 0; i < sampleCount; ++i) { |
| 1918 | avifSampleTableSampleSize * sampleSize = (avifSampleTableSampleSize *)avifArrayPushPtr(&sampleTable->sampleSizes); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1919 | CHECK(avifROStreamReadU32(&s, &sampleSize->size)); // unsigned int(32) entry_size; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1920 | } |
| 1921 | } |
| 1922 | return AVIF_TRUE; |
| 1923 | } |
| 1924 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1925 | static avifBool avifParseSyncSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen) |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 1926 | { |
| 1927 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 1928 | |
| 1929 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
| 1930 | |
| 1931 | uint32_t entryCount; |
| 1932 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
| 1933 | |
| 1934 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 1935 | uint32_t sampleNumber = 0; |
| 1936 | CHECK(avifROStreamReadU32(&s, &sampleNumber)); // unsigned int(32) sample_number; |
| 1937 | avifSyncSample * syncSample = (avifSyncSample *)avifArrayPushPtr(&sampleTable->syncSamples); |
| 1938 | syncSample->sampleNumber = sampleNumber; |
| 1939 | } |
| 1940 | return AVIF_TRUE; |
| 1941 | } |
| 1942 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1943 | static avifBool avifParseTimeToSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1944 | { |
| 1945 | BEGIN_STREAM(s, raw, rawLen); |
| 1946 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1947 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1948 | |
| 1949 | uint32_t entryCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1950 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1951 | |
| 1952 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 1953 | avifSampleTableTimeToSample * timeToSample = (avifSampleTableTimeToSample *)avifArrayPushPtr(&sampleTable->timeToSamples); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1954 | CHECK(avifROStreamReadU32(&s, &timeToSample->sampleCount)); // unsigned int(32) sample_count; |
| 1955 | CHECK(avifROStreamReadU32(&s, &timeToSample->sampleDelta)); // unsigned int(32) sample_delta; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1956 | } |
| 1957 | return AVIF_TRUE; |
| 1958 | } |
| 1959 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1960 | static avifBool avifParseSampleDescriptionBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen) |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 1961 | { |
| 1962 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 1963 | |
| 1964 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
| 1965 | |
| 1966 | uint32_t entryCount; |
| 1967 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
| 1968 | |
| 1969 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 1970 | avifBoxHeader sampleEntryHeader; |
| 1971 | CHECK(avifROStreamReadBoxHeader(&s, &sampleEntryHeader)); |
| 1972 | |
| 1973 | avifSampleDescription * description = (avifSampleDescription *)avifArrayPushPtr(&sampleTable->sampleDescriptions); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1974 | avifArrayCreate(&description->properties, sizeof(avifProperty), 16); |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 1975 | memcpy(description->format, sampleEntryHeader.type, sizeof(description->format)); |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1976 | size_t remainingBytes = avifROStreamRemainingBytes(&s); |
| 1977 | if (!memcmp(description->format, "av01", 4) && (remainingBytes > VISUALSAMPLEENTRY_SIZE)) { |
Joe Drago | 11d2359 | 2021-01-05 14:18:57 -0800 | [diff] [blame] | 1978 | CHECK(avifParseItemPropertyContainerBox(&description->properties, |
| 1979 | avifROStreamCurrent(&s) + VISUALSAMPLEENTRY_SIZE, |
| 1980 | remainingBytes - VISUALSAMPLEENTRY_SIZE)); |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1981 | } |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 1982 | |
| 1983 | CHECK(avifROStreamSkip(&s, sampleEntryHeader.size)); |
| 1984 | } |
| 1985 | return AVIF_TRUE; |
| 1986 | } |
| 1987 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1988 | static avifBool avifParseSampleTableBox(avifTrack * track, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1989 | { |
| 1990 | if (track->sampleTable) { |
| 1991 | // A TrackBox may only have one SampleTable |
| 1992 | return AVIF_FALSE; |
| 1993 | } |
| 1994 | track->sampleTable = avifSampleTableCreate(); |
| 1995 | |
| 1996 | BEGIN_STREAM(s, raw, rawLen); |
| 1997 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1998 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1999 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2000 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2001 | |
| 2002 | if (!memcmp(header.type, "stco", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2003 | CHECK(avifParseChunkOffsetBox(track->sampleTable, AVIF_FALSE, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2004 | } else if (!memcmp(header.type, "co64", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2005 | CHECK(avifParseChunkOffsetBox(track->sampleTable, AVIF_TRUE, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2006 | } else if (!memcmp(header.type, "stsc", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2007 | CHECK(avifParseSampleToChunkBox(track->sampleTable, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2008 | } else if (!memcmp(header.type, "stsz", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2009 | CHECK(avifParseSampleSizeBox(track->sampleTable, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2010 | } else if (!memcmp(header.type, "stss", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2011 | CHECK(avifParseSyncSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2012 | } else if (!memcmp(header.type, "stts", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2013 | CHECK(avifParseTimeToSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 2014 | } else if (!memcmp(header.type, "stsd", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2015 | CHECK(avifParseSampleDescriptionBox(track->sampleTable, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2016 | } |
| 2017 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2018 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2019 | } |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2020 | return AVIF_TRUE; |
| 2021 | } |
| 2022 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2023 | static avifBool avifParseMediaInformationBox(avifTrack * track, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2024 | { |
| 2025 | BEGIN_STREAM(s, raw, rawLen); |
| 2026 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2027 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2028 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2029 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2030 | |
| 2031 | if (!memcmp(header.type, "stbl", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2032 | CHECK(avifParseSampleTableBox(track, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2033 | } |
| 2034 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2035 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2036 | } |
| 2037 | return AVIF_TRUE; |
| 2038 | } |
| 2039 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2040 | static avifBool avifParseMediaBox(avifTrack * track, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2041 | { |
| 2042 | BEGIN_STREAM(s, raw, rawLen); |
| 2043 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2044 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2045 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2046 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2047 | |
| 2048 | if (!memcmp(header.type, "mdhd", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2049 | CHECK(avifParseMediaHeaderBox(track, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2050 | } else if (!memcmp(header.type, "minf", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2051 | CHECK(avifParseMediaInformationBox(track, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2052 | } |
| 2053 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2054 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2055 | } |
| 2056 | return AVIF_TRUE; |
| 2057 | } |
| 2058 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2059 | static avifBool avifTrackReferenceBox(avifTrack * track, const uint8_t * raw, size_t rawLen) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2060 | { |
| 2061 | BEGIN_STREAM(s, raw, rawLen); |
| 2062 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2063 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2064 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2065 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2066 | |
| 2067 | if (!memcmp(header.type, "auxl", 4)) { |
| 2068 | uint32_t toID; |
Joe Drago | ceb2fa0 | 2021-01-29 18:14:55 -0800 | [diff] [blame] | 2069 | CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) track_IDs[]; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2070 | CHECK(avifROStreamSkip(&s, header.size - sizeof(uint32_t))); // just take the first one |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2071 | track->auxForID = toID; |
Yuan Tong | e4850be | 2021-01-22 14:21:25 +0800 | [diff] [blame] | 2072 | } else if (!memcmp(header.type, "prem", 4)) { |
| 2073 | uint32_t byID; |
Joe Drago | ceb2fa0 | 2021-01-29 18:14:55 -0800 | [diff] [blame] | 2074 | CHECK(avifROStreamReadU32(&s, &byID)); // unsigned int(32) track_IDs[]; |
| 2075 | CHECK(avifROStreamSkip(&s, header.size - sizeof(uint32_t))); // just take the first one |
Yuan Tong | e4850be | 2021-01-22 14:21:25 +0800 | [diff] [blame] | 2076 | track->premByID = byID; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2077 | } else { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2078 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2079 | } |
| 2080 | } |
| 2081 | return AVIF_TRUE; |
| 2082 | } |
| 2083 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2084 | static avifBool avifParseTrackBox(avifDecoderData * data, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2085 | { |
| 2086 | BEGIN_STREAM(s, raw, rawLen); |
| 2087 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2088 | avifTrack * track = avifDecoderDataCreateTrack(data); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2089 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2090 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2091 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2092 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2093 | |
| 2094 | if (!memcmp(header.type, "tkhd", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2095 | CHECK(avifParseTrackHeaderBox(track, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2096 | } else if (!memcmp(header.type, "meta", 4)) { |
| 2097 | CHECK(avifParseMetaBox(track->meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2098 | } else if (!memcmp(header.type, "mdia", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2099 | CHECK(avifParseMediaBox(track, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2100 | } else if (!memcmp(header.type, "tref", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2101 | CHECK(avifTrackReferenceBox(track, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2102 | } |
| 2103 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2104 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2105 | } |
| 2106 | return AVIF_TRUE; |
| 2107 | } |
| 2108 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2109 | static avifBool avifParseMoovBox(avifDecoderData * data, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2110 | { |
| 2111 | BEGIN_STREAM(s, raw, rawLen); |
| 2112 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2113 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2114 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2115 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2116 | |
| 2117 | if (!memcmp(header.type, "trak", 4)) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2118 | CHECK(avifParseTrackBox(data, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2119 | } |
| 2120 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2121 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2122 | } |
| 2123 | return AVIF_TRUE; |
| 2124 | } |
| 2125 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2126 | static avifBool avifParseFileTypeBox(avifFileType * ftyp, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2127 | { |
| 2128 | BEGIN_STREAM(s, raw, rawLen); |
| 2129 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2130 | CHECK(avifROStreamRead(&s, ftyp->majorBrand, 4)); |
| 2131 | CHECK(avifROStreamReadU32(&s, &ftyp->minorVersion)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2132 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2133 | size_t compatibleBrandsBytes = avifROStreamRemainingBytes(&s); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2134 | if ((compatibleBrandsBytes % 4) != 0) { |
| 2135 | return AVIF_FALSE; |
| 2136 | } |
Wan-Teh Chang | 6da0a88 | 2020-07-01 12:19:31 -0700 | [diff] [blame] | 2137 | ftyp->compatibleBrands = avifROStreamCurrent(&s); |
| 2138 | CHECK(avifROStreamSkip(&s, compatibleBrandsBytes)); |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2139 | ftyp->compatibleBrandsCount = (int)compatibleBrandsBytes / 4; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2140 | |
| 2141 | return AVIF_TRUE; |
| 2142 | } |
| 2143 | |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2144 | static avifBool avifFileTypeHasBrand(avifFileType * ftyp, const char * brand); |
Wan-Teh Chang | 6fc1758 | 2020-09-24 15:16:37 -0700 | [diff] [blame] | 2145 | static avifBool avifFileTypeIsCompatible(avifFileType * ftyp); |
| 2146 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2147 | static avifResult avifParse(avifDecoder * decoder) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2148 | { |
Joe Drago | 9aa931f | 2020-09-24 13:10:11 -0700 | [diff] [blame] | 2149 | // Note: this top-level function is the only avifParse*() function that returns avifResult instead of avifBool. |
| 2150 | // Be sure to use CHECKERR() in this function with an explicit error result instead of simply using CHECK(). |
| 2151 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2152 | avifResult readResult; |
Wan-Teh Chang | 3b8b81c | 2020-09-28 10:35:08 -0700 | [diff] [blame] | 2153 | uint64_t parseOffset = 0; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2154 | avifDecoderData * data = decoder->data; |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2155 | avifBool ftypSeen = AVIF_FALSE; |
| 2156 | avifBool metaSeen = AVIF_FALSE; |
| 2157 | avifBool moovSeen = AVIF_FALSE; |
| 2158 | avifBool needsMeta = AVIF_FALSE; |
| 2159 | avifBool needsMoov = AVIF_FALSE; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2160 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2161 | for (;;) { |
| 2162 | // Read just enough to get the next box header (a max of 32 bytes) |
| 2163 | avifROData headerContents; |
Wan-Teh Chang | b856dc2 | 2020-09-28 13:00:56 -0700 | [diff] [blame] | 2164 | if ((decoder->io->sizeHint > 0) && (parseOffset > decoder->io->sizeHint)) { |
| 2165 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2166 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2167 | readResult = decoder->io->read(decoder->io, 0, parseOffset, 32, &headerContents); |
| 2168 | if (readResult != AVIF_RESULT_OK) { |
| 2169 | return readResult; |
| 2170 | } |
| 2171 | if (!headerContents.size) { |
| 2172 | // If we got AVIF_RESULT_OK from the reader but received 0 bytes, |
Wan-Teh Chang | 277d0d7 | 2020-10-08 10:24:41 -0700 | [diff] [blame] | 2173 | // we've reached the end of the file with no errors. Hooray! |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2174 | break; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2175 | } |
| 2176 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2177 | // Parse the header, and find out how many bytes it actually was |
| 2178 | BEGIN_STREAM(headerStream, headerContents.data, headerContents.size); |
| 2179 | avifBoxHeader header; |
Joe Drago | 468ded8 | 2020-09-24 12:52:51 -0700 | [diff] [blame] | 2180 | CHECKERR(avifROStreamReadBoxHeaderPartial(&headerStream, &header), AVIF_RESULT_BMFF_PARSE_FAILED); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2181 | parseOffset += headerStream.offset; |
Wan-Teh Chang | b856dc2 | 2020-09-28 13:00:56 -0700 | [diff] [blame] | 2182 | assert((decoder->io->sizeHint == 0) || (parseOffset <= decoder->io->sizeHint)); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2183 | |
| 2184 | // Try to get the remainder of the box, if necessary |
| 2185 | avifROData boxContents = AVIF_DATA_EMPTY; |
| 2186 | |
| 2187 | // TODO: reorg this code to only do these memcmps once each |
Wan-Teh Chang | b309e98 | 2021-02-20 16:59:36 -0800 | [diff] [blame] | 2188 | if (!memcmp(header.type, "ftyp", 4) || !memcmp(header.type, "meta", 4) || !memcmp(header.type, "moov", 4)) { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2189 | readResult = decoder->io->read(decoder->io, 0, parseOffset, header.size, &boxContents); |
| 2190 | if (readResult != AVIF_RESULT_OK) { |
| 2191 | return readResult; |
| 2192 | } |
| 2193 | if (boxContents.size != header.size) { |
| 2194 | // A truncated box, bail out |
Joe Drago | fe5d5e4 | 2020-09-24 13:07:58 -0700 | [diff] [blame] | 2195 | return AVIF_RESULT_TRUNCATED_DATA; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2196 | } |
Wan-Teh Chang | 3b8b81c | 2020-09-28 10:35:08 -0700 | [diff] [blame] | 2197 | } else if (header.size > (UINT64_MAX - parseOffset)) { |
| 2198 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2199 | } |
Wan-Teh Chang | 3b8b81c | 2020-09-28 10:35:08 -0700 | [diff] [blame] | 2200 | parseOffset += header.size; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2201 | |
| 2202 | if (!memcmp(header.type, "ftyp", 4)) { |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2203 | CHECKERR(!ftypSeen, AVIF_RESULT_BMFF_PARSE_FAILED); |
Wan-Teh Chang | 6fc1758 | 2020-09-24 15:16:37 -0700 | [diff] [blame] | 2204 | avifFileType ftyp; |
| 2205 | CHECKERR(avifParseFileTypeBox(&ftyp, boxContents.data, boxContents.size), AVIF_RESULT_BMFF_PARSE_FAILED); |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2206 | if (!avifFileTypeIsCompatible(&ftyp)) { |
Wan-Teh Chang | 6fc1758 | 2020-09-24 15:16:37 -0700 | [diff] [blame] | 2207 | return AVIF_RESULT_INVALID_FTYP; |
| 2208 | } |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2209 | ftypSeen = AVIF_TRUE; |
| 2210 | needsMeta = avifFileTypeHasBrand(&ftyp, "avif"); |
| 2211 | needsMoov = avifFileTypeHasBrand(&ftyp, "avis"); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2212 | } else if (!memcmp(header.type, "meta", 4)) { |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2213 | CHECKERR(!metaSeen, AVIF_RESULT_BMFF_PARSE_FAILED); |
Joe Drago | 468ded8 | 2020-09-24 12:52:51 -0700 | [diff] [blame] | 2214 | CHECKERR(avifParseMetaBox(data->meta, boxContents.data, boxContents.size), AVIF_RESULT_BMFF_PARSE_FAILED); |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2215 | metaSeen = AVIF_TRUE; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2216 | } else if (!memcmp(header.type, "moov", 4)) { |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2217 | CHECKERR(!moovSeen, AVIF_RESULT_BMFF_PARSE_FAILED); |
Joe Drago | 468ded8 | 2020-09-24 12:52:51 -0700 | [diff] [blame] | 2218 | CHECKERR(avifParseMoovBox(data, boxContents.data, boxContents.size), AVIF_RESULT_BMFF_PARSE_FAILED); |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2219 | moovSeen = AVIF_TRUE; |
| 2220 | } |
| 2221 | |
| 2222 | // See if there is enough information to consider Parse() a success and early-out: |
| 2223 | // * If the brand 'avif' is present, require a meta box |
| 2224 | // * If the brand 'avis' is present, require a moov box |
| 2225 | if (ftypSeen && (!needsMeta || metaSeen) && (!needsMoov || moovSeen)) { |
| 2226 | return AVIF_RESULT_OK; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2227 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2228 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2229 | return AVIF_RESULT_OK; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | // --------------------------------------------------------------------------- |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2233 | |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2234 | static avifBool avifFileTypeHasBrand(avifFileType * ftyp, const char * brand) |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2235 | { |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2236 | if (!memcmp(ftyp->majorBrand, brand, 4)) { |
| 2237 | return AVIF_TRUE; |
| 2238 | } |
| 2239 | |
| 2240 | for (int compatibleBrandIndex = 0; compatibleBrandIndex < ftyp->compatibleBrandsCount; ++compatibleBrandIndex) { |
| 2241 | const uint8_t * compatibleBrand = &ftyp->compatibleBrands[4 * compatibleBrandIndex]; |
| 2242 | if (!memcmp(compatibleBrand, brand, 4)) { |
| 2243 | return AVIF_TRUE; |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2244 | } |
| 2245 | } |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2246 | return AVIF_FALSE; |
| 2247 | } |
| 2248 | |
| 2249 | static avifBool avifFileTypeIsCompatible(avifFileType * ftyp) |
| 2250 | { |
| 2251 | return avifFileTypeHasBrand(ftyp, "avif") || avifFileTypeHasBrand(ftyp, "avis"); |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
Wan-Teh Chang | e184dc1 | 2020-05-11 12:47:21 -0700 | [diff] [blame] | 2254 | avifBool avifPeekCompatibleFileType(const avifROData * input) |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2255 | { |
| 2256 | BEGIN_STREAM(s, input->data, input->size); |
| 2257 | |
| 2258 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2259 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame] | 2260 | if (memcmp(header.type, "ftyp", 4)) { |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2261 | return AVIF_FALSE; |
| 2262 | } |
| 2263 | |
| 2264 | avifFileType ftyp; |
| 2265 | memset(&ftyp, 0, sizeof(avifFileType)); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2266 | avifBool parsed = avifParseFileTypeBox(&ftyp, avifROStreamCurrent(&s), header.size); |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2267 | if (!parsed) { |
| 2268 | return AVIF_FALSE; |
| 2269 | } |
| 2270 | return avifFileTypeIsCompatible(&ftyp); |
| 2271 | } |
| 2272 | |
| 2273 | // --------------------------------------------------------------------------- |
| 2274 | |
Joe Drago | 0b05eee | 2019-06-12 13:24:39 -0700 | [diff] [blame] | 2275 | avifDecoder * avifDecoderCreate(void) |
| 2276 | { |
| 2277 | avifDecoder * decoder = (avifDecoder *)avifAlloc(sizeof(avifDecoder)); |
| 2278 | memset(decoder, 0, sizeof(avifDecoder)); |
Joe Drago | ede5c20 | 2020-11-11 09:42:57 -0800 | [diff] [blame] | 2279 | decoder->maxThreads = 1; |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 2280 | decoder->imageCountLimit = AVIF_DEFAULT_IMAGE_COUNT_LIMIT; |
Joe Drago | 0b05eee | 2019-06-12 13:24:39 -0700 | [diff] [blame] | 2281 | return decoder; |
| 2282 | } |
| 2283 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2284 | static void avifDecoderCleanup(avifDecoder * decoder) |
| 2285 | { |
| 2286 | if (decoder->data) { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2287 | avifDecoderDataDestroy(decoder->data); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2288 | decoder->data = NULL; |
| 2289 | } |
| 2290 | |
| 2291 | if (decoder->image) { |
| 2292 | avifImageDestroy(decoder->image); |
| 2293 | decoder->image = NULL; |
| 2294 | } |
| 2295 | } |
| 2296 | |
Joe Drago | 0b05eee | 2019-06-12 13:24:39 -0700 | [diff] [blame] | 2297 | void avifDecoderDestroy(avifDecoder * decoder) |
| 2298 | { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2299 | avifDecoderCleanup(decoder); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2300 | avifIODestroy(decoder->io); |
Joe Drago | 0b05eee | 2019-06-12 13:24:39 -0700 | [diff] [blame] | 2301 | avifFree(decoder); |
| 2302 | } |
| 2303 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2304 | avifResult avifDecoderSetSource(avifDecoder * decoder, avifDecoderSource source) |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2305 | { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2306 | decoder->requestedSource = source; |
| 2307 | return avifDecoderReset(decoder); |
| 2308 | } |
Joe Drago | 33f1d36 | 2019-02-13 16:46:22 -0800 | [diff] [blame] | 2309 | |
Wan-Teh Chang | e67f936 | 2020-10-12 16:07:57 -0700 | [diff] [blame] | 2310 | void avifDecoderSetIO(avifDecoder * decoder, avifIO * io) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2311 | { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2312 | avifIODestroy(decoder->io); |
| 2313 | decoder->io = io; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2314 | } |
| 2315 | |
Wan-Teh Chang | 7de4445 | 2020-10-08 11:43:18 -0700 | [diff] [blame] | 2316 | avifResult avifDecoderSetIOMemory(avifDecoder * decoder, const uint8_t * data, size_t size) |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2317 | { |
Wan-Teh Chang | 7de4445 | 2020-10-08 11:43:18 -0700 | [diff] [blame] | 2318 | avifIO * io = avifIOCreateMemoryReader(data, size); |
Wan-Teh Chang | 31c7c1a | 2020-10-13 16:45:41 -0700 | [diff] [blame] | 2319 | assert(io); |
Wan-Teh Chang | e67f936 | 2020-10-12 16:07:57 -0700 | [diff] [blame] | 2320 | avifDecoderSetIO(decoder, io); |
| 2321 | return AVIF_RESULT_OK; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2322 | } |
| 2323 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2324 | avifResult avifDecoderSetIOFile(avifDecoder * decoder, const char * filename) |
| 2325 | { |
Wan-Teh Chang | 7de4445 | 2020-10-08 11:43:18 -0700 | [diff] [blame] | 2326 | avifIO * io = avifIOCreateFileReader(filename); |
| 2327 | if (!io) { |
Wan-Teh Chang | 31c7c1a | 2020-10-13 16:45:41 -0700 | [diff] [blame] | 2328 | return AVIF_RESULT_IO_ERROR; |
Wan-Teh Chang | 7de4445 | 2020-10-08 11:43:18 -0700 | [diff] [blame] | 2329 | } |
Wan-Teh Chang | e67f936 | 2020-10-12 16:07:57 -0700 | [diff] [blame] | 2330 | avifDecoderSetIO(decoder, io); |
| 2331 | return AVIF_RESULT_OK; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2332 | } |
| 2333 | |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 2334 | // 0-byte extents are ignored/overwritten during the merge, as they are the signal from helper |
| 2335 | // functions that no extent was necessary for this given sample. If both provided extents are |
| 2336 | // >0 bytes, this will set dst to be an extent that bounds both supplied extents. |
Wan-Teh Chang | d69958e | 2020-11-17 12:14:27 -0800 | [diff] [blame] | 2337 | static avifResult avifExtentMerge(avifExtent * dst, const avifExtent * src) |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 2338 | { |
| 2339 | if (!dst->size) { |
| 2340 | memcpy(dst, src, sizeof(avifExtent)); |
Wan-Teh Chang | d69958e | 2020-11-17 12:14:27 -0800 | [diff] [blame] | 2341 | return AVIF_RESULT_OK; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 2342 | } |
| 2343 | if (!src->size) { |
Wan-Teh Chang | d69958e | 2020-11-17 12:14:27 -0800 | [diff] [blame] | 2344 | return AVIF_RESULT_OK; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | const uint64_t minExtent1 = dst->offset; |
| 2348 | const uint64_t maxExtent1 = dst->offset + dst->size; |
| 2349 | const uint64_t minExtent2 = src->offset; |
| 2350 | const uint64_t maxExtent2 = src->offset + src->size; |
| 2351 | dst->offset = AVIF_MIN(minExtent1, minExtent2); |
Wan-Teh Chang | d69958e | 2020-11-17 12:14:27 -0800 | [diff] [blame] | 2352 | const uint64_t extentLength = AVIF_MAX(maxExtent1, maxExtent2) - dst->offset; |
| 2353 | if (extentLength > SIZE_MAX) { |
| 2354 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2355 | } |
| 2356 | dst->size = (size_t)extentLength; |
| 2357 | return AVIF_RESULT_OK; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 2358 | } |
| 2359 | |
Joe Drago | 93d5bf9 | 2020-11-17 14:31:57 -0800 | [diff] [blame] | 2360 | avifResult avifDecoderNthImageMaxExtent(const avifDecoder * decoder, uint32_t frameIndex, avifExtent * outExtent) |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 2361 | { |
| 2362 | if (!decoder->data) { |
| 2363 | // Nothing has been parsed yet |
| 2364 | return AVIF_RESULT_NO_CONTENT; |
| 2365 | } |
| 2366 | |
| 2367 | memset(outExtent, 0, sizeof(avifExtent)); |
| 2368 | |
Joe Drago | 93d5bf9 | 2020-11-17 14:31:57 -0800 | [diff] [blame] | 2369 | uint32_t startFrameIndex = avifDecoderNearestKeyframe(decoder, frameIndex); |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 2370 | uint32_t endFrameIndex = frameIndex; |
| 2371 | for (uint32_t currentFrameIndex = startFrameIndex; currentFrameIndex <= endFrameIndex; ++currentFrameIndex) { |
| 2372 | for (unsigned int tileIndex = 0; tileIndex < decoder->data->tiles.count; ++tileIndex) { |
| 2373 | avifTile * tile = &decoder->data->tiles.tile[tileIndex]; |
| 2374 | if (currentFrameIndex >= tile->input->samples.count) { |
| 2375 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
| 2376 | } |
| 2377 | |
| 2378 | avifDecodeSample * sample = &tile->input->samples.sample[currentFrameIndex]; |
| 2379 | avifExtent sampleExtent; |
| 2380 | if (sample->itemID) { |
| 2381 | // The data comes from an item. Let avifDecoderItemMaxExtent() do the heavy lifting. |
| 2382 | |
| 2383 | avifDecoderItem * item = avifMetaFindItem(decoder->data->meta, sample->itemID); |
| 2384 | avifResult maxExtentResult = avifDecoderItemMaxExtent(item, &sampleExtent); |
| 2385 | if (maxExtentResult != AVIF_RESULT_OK) { |
| 2386 | return maxExtentResult; |
| 2387 | } |
| 2388 | } else { |
| 2389 | // The data likely comes from a sample table. Use the sample position directly. |
| 2390 | |
| 2391 | sampleExtent.offset = sample->offset; |
| 2392 | sampleExtent.size = sample->size; |
| 2393 | } |
| 2394 | |
| 2395 | if (sampleExtent.size > UINT64_MAX - sampleExtent.offset) { |
| 2396 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2397 | } |
| 2398 | |
Wan-Teh Chang | d69958e | 2020-11-17 12:14:27 -0800 | [diff] [blame] | 2399 | avifResult extentMergeResult = avifExtentMerge(outExtent, &sampleExtent); |
| 2400 | if (extentMergeResult != AVIF_RESULT_OK) { |
| 2401 | return extentMergeResult; |
| 2402 | } |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 2403 | } |
| 2404 | } |
| 2405 | return AVIF_RESULT_OK; |
| 2406 | } |
| 2407 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2408 | static avifResult avifDecoderPrepareSample(avifDecoder * decoder, avifDecodeSample * sample, size_t partialByteCount) |
| 2409 | { |
| 2410 | if (!sample->data.size || sample->partialData) { |
| 2411 | // This sample hasn't been read from IO or had its extents fully merged yet. |
| 2412 | |
| 2413 | if (sample->itemID) { |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 2414 | // The data comes from an item. Let avifDecoderItemRead() do the heavy lifting. |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2415 | |
| 2416 | avifDecoderItem * item = avifMetaFindItem(decoder->data->meta, sample->itemID); |
| 2417 | avifROData itemContents; |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 2418 | avifResult readResult = avifDecoderItemRead(item, decoder->io, &itemContents, partialByteCount); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2419 | if (readResult != AVIF_RESULT_OK) { |
| 2420 | return readResult; |
| 2421 | } |
| 2422 | |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 2423 | // avifDecoderItemRead is guaranteed to already be persisted by either the underlying IO |
Wan-Teh Chang | 277d0d7 | 2020-10-08 10:24:41 -0700 | [diff] [blame] | 2424 | // or by mergedExtents; just reuse the buffer here. |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2425 | memcpy(&sample->data, &itemContents, sizeof(avifROData)); |
| 2426 | sample->ownsData = AVIF_FALSE; |
| 2427 | sample->partialData = item->partialMergedExtents; |
| 2428 | } else { |
| 2429 | // The data likely comes from a sample table. Pull the sample and make a copy if necessary. |
| 2430 | |
| 2431 | size_t bytesToRead = sample->size; |
| 2432 | if (partialByteCount && (bytesToRead > partialByteCount)) { |
| 2433 | bytesToRead = partialByteCount; |
| 2434 | } |
| 2435 | |
| 2436 | avifROData sampleContents; |
Wan-Teh Chang | b856dc2 | 2020-09-28 13:00:56 -0700 | [diff] [blame] | 2437 | if ((decoder->io->sizeHint > 0) && (sample->offset > decoder->io->sizeHint)) { |
| 2438 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2439 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2440 | avifResult readResult = decoder->io->read(decoder->io, 0, sample->offset, bytesToRead, &sampleContents); |
| 2441 | if (readResult != AVIF_RESULT_OK) { |
| 2442 | return readResult; |
| 2443 | } |
| 2444 | if (sampleContents.size != bytesToRead) { |
| 2445 | return AVIF_RESULT_TRUNCATED_DATA; |
| 2446 | } |
| 2447 | |
| 2448 | sample->ownsData = !decoder->io->persistent; |
| 2449 | sample->partialData = (bytesToRead != sample->size); |
| 2450 | if (decoder->io->persistent) { |
| 2451 | memcpy(&sample->data, &sampleContents, sizeof(avifROData)); |
| 2452 | } else { |
| 2453 | avifRWDataSet((avifRWData *)&sample->data, sampleContents.data, sampleContents.size); |
| 2454 | } |
| 2455 | } |
| 2456 | } |
| 2457 | return AVIF_RESULT_OK; |
| 2458 | } |
| 2459 | |
| 2460 | avifResult avifDecoderParse(avifDecoder * decoder) |
| 2461 | { |
| 2462 | if (!decoder->io || !decoder->io->read) { |
Wan-Teh Chang | 31c7c1a | 2020-10-13 16:45:41 -0700 | [diff] [blame] | 2463 | return AVIF_RESULT_IO_NOT_SET; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2464 | } |
| 2465 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2466 | // Cleanup anything lingering in the decoder |
| 2467 | avifDecoderCleanup(decoder); |
| 2468 | |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2469 | // ----------------------------------------------------------------------- |
| 2470 | // Parse BMFF boxes |
| 2471 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2472 | decoder->data = avifDecoderDataCreate(); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2473 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2474 | avifResult parseResult = avifParse(decoder); |
| 2475 | if (parseResult != AVIF_RESULT_OK) { |
| 2476 | return parseResult; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2477 | } |
| 2478 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2479 | return avifDecoderReset(decoder); |
| 2480 | } |
| 2481 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2482 | static avifCodec * avifCodecCreateInternal(avifCodecChoice choice) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2483 | { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2484 | return avifCodecCreate(choice, AVIF_CODEC_FLAG_CAN_DECODE); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2485 | } |
| 2486 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2487 | static avifResult avifDecoderFlush(avifDecoder * decoder) |
| 2488 | { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2489 | avifDecoderDataResetCodec(decoder->data); |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2490 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2491 | for (unsigned int i = 0; i < decoder->data->tiles.count; ++i) { |
| 2492 | avifTile * tile = &decoder->data->tiles.tile[i]; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2493 | tile->codec = avifCodecCreateInternal(decoder->codecChoice); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2494 | if (!tile->codec) { |
Joe Drago | 5335535 | 2019-10-28 19:04:51 -0700 | [diff] [blame] | 2495 | return AVIF_RESULT_NO_CODEC_AVAILABLE; |
| 2496 | } |
Joe Drago | ede5c20 | 2020-11-11 09:42:57 -0800 | [diff] [blame] | 2497 | if (!tile->codec->open(tile->codec, decoder)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2498 | return AVIF_RESULT_DECODE_COLOR_FAILED; |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2499 | } |
| 2500 | } |
| 2501 | return AVIF_RESULT_OK; |
| 2502 | } |
| 2503 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2504 | avifResult avifDecoderReset(avifDecoder * decoder) |
| 2505 | { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2506 | avifDecoderData * data = decoder->data; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2507 | if (!data) { |
| 2508 | // Nothing to reset. |
| 2509 | return AVIF_RESULT_OK; |
| 2510 | } |
| 2511 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2512 | memset(&data->colorGrid, 0, sizeof(data->colorGrid)); |
| 2513 | memset(&data->alphaGrid, 0, sizeof(data->alphaGrid)); |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2514 | avifDecoderDataClearTiles(data); |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 2515 | |
| 2516 | // Prepare / cleanup decoded image state |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 2517 | if (decoder->image) { |
| 2518 | avifImageDestroy(decoder->image); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2519 | } |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 2520 | decoder->image = avifImageCreateEmpty(); |
| 2521 | data->cicpSet = AVIF_FALSE; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2522 | |
Joe Drago | 70cbf60 | 2019-07-24 15:30:55 -0700 | [diff] [blame] | 2523 | memset(&decoder->ioStats, 0, sizeof(decoder->ioStats)); |
| 2524 | |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2525 | // ----------------------------------------------------------------------- |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2526 | // Build decode input |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2527 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2528 | data->sourceSampleTable = NULL; // Reset |
| 2529 | if (decoder->requestedSource == AVIF_DECODER_SOURCE_AUTO) { |
| 2530 | if (data->tracks.count > 0) { |
| 2531 | data->source = AVIF_DECODER_SOURCE_TRACKS; |
| 2532 | } else { |
| 2533 | data->source = AVIF_DECODER_SOURCE_PRIMARY_ITEM; |
Joe Drago | 7637023 | 2019-07-16 11:00:52 -0700 | [diff] [blame] | 2534 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2535 | } else { |
| 2536 | data->source = decoder->requestedSource; |
Joe Drago | 7637023 | 2019-07-16 11:00:52 -0700 | [diff] [blame] | 2537 | } |
| 2538 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2539 | const avifPropertyArray * colorProperties = NULL; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2540 | if (data->source == AVIF_DECODER_SOURCE_TRACKS) { |
| 2541 | avifTrack * colorTrack = NULL; |
| 2542 | avifTrack * alphaTrack = NULL; |
| 2543 | |
| 2544 | // Find primary track - this probably needs some better detection |
| 2545 | uint32_t colorTrackIndex = 0; |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2546 | for (; colorTrackIndex < data->tracks.count; ++colorTrackIndex) { |
| 2547 | avifTrack * track = &data->tracks.track[colorTrackIndex]; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2548 | if (!track->sampleTable) { |
| 2549 | continue; |
| 2550 | } |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 2551 | if (!track->id) { // trak box might be missing a tkhd box inside, skip it |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 2552 | continue; |
| 2553 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2554 | if (!track->sampleTable->chunks.count) { |
| 2555 | continue; |
| 2556 | } |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 2557 | if (!avifSampleTableHasFormat(track->sampleTable, "av01")) { |
| 2558 | continue; |
| 2559 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2560 | if (track->auxForID != 0) { |
| 2561 | continue; |
| 2562 | } |
| 2563 | |
| 2564 | // Found one! |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2565 | break; |
| 2566 | } |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2567 | if (colorTrackIndex == data->tracks.count) { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2568 | return AVIF_RESULT_NO_CONTENT; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2569 | } |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2570 | colorTrack = &data->tracks.track[colorTrackIndex]; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2571 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2572 | colorProperties = avifSampleTableGetProperties(colorTrack->sampleTable); |
| 2573 | if (!colorProperties) { |
| 2574 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2575 | } |
| 2576 | |
| 2577 | // Find Exif and/or XMP metadata, if any |
| 2578 | if (colorTrack->meta) { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2579 | // See the comment above avifDecoderFindMetadata() for the explanation of using 0 here |
| 2580 | avifResult findResult = avifDecoderFindMetadata(decoder, colorTrack->meta, decoder->image, 0); |
| 2581 | if (findResult != AVIF_RESULT_OK) { |
| 2582 | return findResult; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2583 | } |
| 2584 | } |
| 2585 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2586 | uint32_t alphaTrackIndex = 0; |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2587 | for (; alphaTrackIndex < data->tracks.count; ++alphaTrackIndex) { |
| 2588 | avifTrack * track = &data->tracks.track[alphaTrackIndex]; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2589 | if (!track->sampleTable) { |
| 2590 | continue; |
| 2591 | } |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 2592 | if (!track->id) { |
| 2593 | continue; |
| 2594 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2595 | if (!track->sampleTable->chunks.count) { |
| 2596 | continue; |
| 2597 | } |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 2598 | if (!avifSampleTableHasFormat(track->sampleTable, "av01")) { |
| 2599 | continue; |
| 2600 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2601 | if (track->auxForID == colorTrack->id) { |
| 2602 | // Found it! |
| 2603 | break; |
| 2604 | } |
| 2605 | } |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2606 | if (alphaTrackIndex != data->tracks.count) { |
| 2607 | alphaTrack = &data->tracks.track[alphaTrackIndex]; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2608 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2609 | |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2610 | avifTile * colorTile = avifDecoderDataCreateTile(data); |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 2611 | if (!avifCodecDecodeInputGetSamples(colorTile->input, colorTrack->sampleTable, decoder->imageCountLimit, decoder->io->sizeHint)) { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2612 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2613 | } |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2614 | data->colorTileCount = 1; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2615 | |
| 2616 | if (alphaTrack) { |
wantehchang | 76e16bf | 2020-08-12 13:01:31 -0700 | [diff] [blame] | 2617 | avifTile * alphaTile = avifDecoderDataCreateTile(data); |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 2618 | if (!avifCodecDecodeInputGetSamples(alphaTile->input, alphaTrack->sampleTable, decoder->imageCountLimit, decoder->io->sizeHint)) { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2619 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2620 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2621 | alphaTile->input->alpha = AVIF_TRUE; |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2622 | data->alphaTileCount = 1; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | // Stash off sample table for future timing information |
| 2626 | data->sourceSampleTable = colorTrack->sampleTable; |
| 2627 | |
| 2628 | // Image sequence timing |
| 2629 | decoder->imageIndex = -1; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2630 | decoder->imageCount = colorTile->input->samples.count; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2631 | decoder->timescale = colorTrack->mediaTimescale; |
| 2632 | decoder->durationInTimescales = colorTrack->mediaDuration; |
| 2633 | if (colorTrack->mediaTimescale) { |
| 2634 | decoder->duration = (double)decoder->durationInTimescales / (double)colorTrack->mediaTimescale; |
| 2635 | } else { |
| 2636 | decoder->duration = 0; |
| 2637 | } |
| 2638 | memset(&decoder->imageTiming, 0, sizeof(decoder->imageTiming)); // to be set in avifDecoderNextImage() |
Joe Drago | 4170085 | 2019-09-26 17:01:43 -0700 | [diff] [blame] | 2639 | |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2640 | decoder->image->width = colorTrack->width; |
| 2641 | decoder->image->height = colorTrack->height; |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2642 | decoder->alphaPresent = (alphaTrack != NULL); |
Joe Drago | ceb2fa0 | 2021-01-29 18:14:55 -0800 | [diff] [blame] | 2643 | decoder->image->alphaPremultiplied = decoder->alphaPresent && (colorTrack->premByID == alphaTrack->id); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2644 | } else { |
| 2645 | // Create from items |
| 2646 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2647 | avifDecoderItem * colorItem = NULL; |
| 2648 | avifDecoderItem * alphaItem = NULL; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2649 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2650 | // Find the colorOBU (primary) item |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2651 | for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) { |
| 2652 | avifDecoderItem * item = &data->meta->items.item[itemIndex]; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 2653 | if (!item->size) { |
| 2654 | continue; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2655 | } |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 2656 | if (item->hasUnsupportedEssentialProperty) { |
| 2657 | // An essential property isn't supported by libavif; ignore the item. |
| 2658 | continue; |
| 2659 | } |
Joe Drago | 951a002 | 2020-03-09 16:19:44 -0700 | [diff] [blame] | 2660 | avifBool isGrid = (memcmp(item->type, "grid", 4) == 0); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2661 | if (memcmp(item->type, "av01", 4) && !isGrid) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2662 | // probably exif or some other data |
| 2663 | continue; |
| 2664 | } |
| 2665 | if (item->thumbnailForID != 0) { |
| 2666 | // It's a thumbnail, skip it |
| 2667 | continue; |
| 2668 | } |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2669 | if ((data->meta->primaryItemID > 0) && (item->id != data->meta->primaryItemID)) { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2670 | // a primary item ID was specified, require it |
| 2671 | continue; |
| 2672 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2673 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2674 | if (isGrid) { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2675 | avifROData readData; |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 2676 | avifResult readResult = avifDecoderItemRead(item, decoder->io, &readData, 0); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2677 | if (readResult != AVIF_RESULT_OK) { |
| 2678 | return readResult; |
| 2679 | } |
Wan-Teh Chang | c443f14 | 2020-10-08 14:58:42 -0700 | [diff] [blame] | 2680 | if (!avifParseImageGridBox(&data->colorGrid, readData.data, readData.size)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2681 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 2682 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2683 | } |
| 2684 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2685 | colorItem = item; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2686 | break; |
| 2687 | } |
| 2688 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2689 | if (!colorItem) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2690 | return AVIF_RESULT_NO_AV1_ITEMS_FOUND; |
| 2691 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2692 | colorProperties = &colorItem->properties; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2693 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2694 | // Find the alphaOBU item, if any |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2695 | for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) { |
| 2696 | avifDecoderItem * item = &data->meta->items.item[itemIndex]; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 2697 | if (!item->size) { |
| 2698 | continue; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2699 | } |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 2700 | if (item->hasUnsupportedEssentialProperty) { |
| 2701 | // An essential property isn't supported by libavif; ignore the item. |
| 2702 | continue; |
| 2703 | } |
Joe Drago | 951a002 | 2020-03-09 16:19:44 -0700 | [diff] [blame] | 2704 | avifBool isGrid = (memcmp(item->type, "grid", 4) == 0); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2705 | if (memcmp(item->type, "av01", 4) && !isGrid) { |
| 2706 | // probably exif or some other data |
| 2707 | continue; |
| 2708 | } |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2709 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2710 | // Is this an alpha auxiliary item of whatever we chose for colorItem? |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2711 | const avifProperty * auxCProp = avifPropertyArrayFind(&item->properties, "auxC"); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2712 | if (auxCProp && isAlphaURN(auxCProp->u.auxC.auxType) && (item->auxForID == colorItem->id)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2713 | if (isGrid) { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2714 | avifROData readData; |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 2715 | avifResult readResult = avifDecoderItemRead(item, decoder->io, &readData, 0); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2716 | if (readResult != AVIF_RESULT_OK) { |
| 2717 | return readResult; |
| 2718 | } |
Wan-Teh Chang | c443f14 | 2020-10-08 14:58:42 -0700 | [diff] [blame] | 2719 | if (!avifParseImageGridBox(&data->alphaGrid, readData.data, readData.size)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2720 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 2721 | } |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2722 | } |
| 2723 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2724 | alphaItem = item; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2725 | break; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2726 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2727 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2728 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2729 | // Find Exif and/or XMP metadata, if any |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2730 | avifResult findResult = avifDecoderFindMetadata(decoder, data->meta, decoder->image, colorItem->id); |
| 2731 | if (findResult != AVIF_RESULT_OK) { |
| 2732 | return findResult; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2733 | } |
| 2734 | |
Wan-Teh Chang | 4295bcb | 2020-04-05 15:41:05 -0700 | [diff] [blame] | 2735 | if ((data->colorGrid.rows > 0) && (data->colorGrid.columns > 0)) { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2736 | if (!avifDecoderDataGenerateImageGridTiles(data, &data->colorGrid, colorItem, AVIF_FALSE)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2737 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 2738 | } |
| 2739 | data->colorTileCount = data->tiles.count; |
| 2740 | } else { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2741 | if (colorItem->size == 0) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2742 | return AVIF_RESULT_NO_AV1_ITEMS_FOUND; |
| 2743 | } |
| 2744 | |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2745 | avifTile * colorTile = avifDecoderDataCreateTile(data); |
Joe Drago | e3e3bfa | 2020-06-02 16:33:53 -0700 | [diff] [blame] | 2746 | avifDecodeSample * colorSample = (avifDecodeSample *)avifArrayPushPtr(&colorTile->input->samples); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2747 | colorSample->itemID = colorItem->id; |
| 2748 | colorSample->offset = 0; |
| 2749 | colorSample->size = colorItem->size; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2750 | colorSample->sync = AVIF_TRUE; |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2751 | data->colorTileCount = 1; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2752 | } |
| 2753 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2754 | if (alphaItem) { |
Wan-Teh Chang | 272aafe | 2020-08-12 17:31:38 -0700 | [diff] [blame] | 2755 | if ((data->alphaGrid.rows > 0) && (data->alphaGrid.columns > 0)) { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2756 | if (!avifDecoderDataGenerateImageGridTiles(data, &data->alphaGrid, alphaItem, AVIF_TRUE)) { |
Wan-Teh Chang | 272aafe | 2020-08-12 17:31:38 -0700 | [diff] [blame] | 2757 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 2758 | } |
| 2759 | data->alphaTileCount = data->tiles.count - data->colorTileCount; |
| 2760 | } else { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2761 | if (alphaItem->size == 0) { |
Wan-Teh Chang | 272aafe | 2020-08-12 17:31:38 -0700 | [diff] [blame] | 2762 | return AVIF_RESULT_NO_AV1_ITEMS_FOUND; |
| 2763 | } |
| 2764 | |
wantehchang | 76e16bf | 2020-08-12 13:01:31 -0700 | [diff] [blame] | 2765 | avifTile * alphaTile = avifDecoderDataCreateTile(data); |
Joe Drago | e3e3bfa | 2020-06-02 16:33:53 -0700 | [diff] [blame] | 2766 | avifDecodeSample * alphaSample = (avifDecodeSample *)avifArrayPushPtr(&alphaTile->input->samples); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2767 | alphaSample->itemID = alphaItem->id; |
| 2768 | alphaSample->offset = 0; |
| 2769 | alphaSample->size = alphaItem->size; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2770 | alphaSample->sync = AVIF_TRUE; |
| 2771 | alphaTile->input->alpha = AVIF_TRUE; |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2772 | data->alphaTileCount = 1; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2773 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2774 | } |
Joe Drago | 33f1d36 | 2019-02-13 16:46:22 -0800 | [diff] [blame] | 2775 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2776 | // Set all counts and timing to safe-but-uninteresting values |
| 2777 | decoder->imageIndex = -1; |
| 2778 | decoder->imageCount = 1; |
| 2779 | decoder->imageTiming.timescale = 1; |
| 2780 | decoder->imageTiming.pts = 0; |
| 2781 | decoder->imageTiming.ptsInTimescales = 0; |
| 2782 | decoder->imageTiming.duration = 1; |
| 2783 | decoder->imageTiming.durationInTimescales = 1; |
| 2784 | decoder->timescale = 1; |
| 2785 | decoder->duration = 1; |
| 2786 | decoder->durationInTimescales = 1; |
Joe Drago | 70cbf60 | 2019-07-24 15:30:55 -0700 | [diff] [blame] | 2787 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2788 | decoder->ioStats.colorOBUSize = colorItem->size; |
| 2789 | decoder->ioStats.alphaOBUSize = alphaItem ? alphaItem->size : 0; |
Joe Drago | 4170085 | 2019-09-26 17:01:43 -0700 | [diff] [blame] | 2790 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2791 | const avifProperty * ispeProp = avifPropertyArrayFind(colorProperties, "ispe"); |
| 2792 | if (ispeProp) { |
| 2793 | decoder->image->width = ispeProp->u.ispe.width; |
| 2794 | decoder->image->height = ispeProp->u.ispe.height; |
Joe Drago | 4170085 | 2019-09-26 17:01:43 -0700 | [diff] [blame] | 2795 | } else { |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2796 | decoder->image->width = 0; |
| 2797 | decoder->image->height = 0; |
Joe Drago | 4170085 | 2019-09-26 17:01:43 -0700 | [diff] [blame] | 2798 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2799 | decoder->alphaPresent = (alphaItem != NULL); |
Joe Drago | ceb2fa0 | 2021-01-29 18:14:55 -0800 | [diff] [blame] | 2800 | decoder->image->alphaPremultiplied = decoder->alphaPresent && (colorItem->premByID == alphaItem->id); |
Joe Drago | 00bcaaf | 2020-06-05 15:29:38 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
Joe Drago | 11f2a5e | 2020-07-06 10:49:00 -0700 | [diff] [blame] | 2803 | // Sanity check tiles |
| 2804 | for (uint32_t tileIndex = 0; tileIndex < data->tiles.count; ++tileIndex) { |
| 2805 | avifTile * tile = &data->tiles.tile[tileIndex]; |
| 2806 | for (uint32_t sampleIndex = 0; sampleIndex < tile->input->samples.count; ++sampleIndex) { |
Joe Drago | 043311b | 2020-07-06 16:48:41 -0700 | [diff] [blame] | 2807 | avifDecodeSample * sample = &tile->input->samples.sample[sampleIndex]; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2808 | if (!sample->size) { |
Joe Drago | 11f2a5e | 2020-07-06 10:49:00 -0700 | [diff] [blame] | 2809 | // Every sample must have some data |
| 2810 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2811 | } |
| 2812 | } |
| 2813 | } |
| 2814 | |
Joe Drago | bf58fe7 | 2020-11-05 13:25:14 -0800 | [diff] [blame] | 2815 | // Find and adopt all colr boxes "at most one for a given value of colour type" (HEIF 6.5.5.1, from Amendment 3) |
| 2816 | // Accept one of each type, and bail out if more than one of a given type is provided. |
| 2817 | avifBool colrICCSeen = AVIF_FALSE; |
| 2818 | avifBool colrNCLXSeen = AVIF_FALSE; |
| 2819 | for (uint32_t propertyIndex = 0; propertyIndex < colorProperties->count; ++propertyIndex) { |
| 2820 | avifProperty * prop = &colorProperties->prop[propertyIndex]; |
| 2821 | |
| 2822 | if (!memcmp(prop->type, "colr", 4)) { |
| 2823 | if (prop->u.colr.hasICC) { |
| 2824 | if (colrICCSeen) { |
| 2825 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2826 | } |
| 2827 | colrICCSeen = AVIF_TRUE; |
| 2828 | avifImageSetProfileICC(decoder->image, prop->u.colr.icc, prop->u.colr.iccSize); |
| 2829 | } |
| 2830 | if (prop->u.colr.hasNCLX) { |
| 2831 | if (colrNCLXSeen) { |
| 2832 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2833 | } |
| 2834 | colrNCLXSeen = AVIF_TRUE; |
| 2835 | data->cicpSet = AVIF_TRUE; |
| 2836 | decoder->image->colorPrimaries = prop->u.colr.colorPrimaries; |
| 2837 | decoder->image->transferCharacteristics = prop->u.colr.transferCharacteristics; |
| 2838 | decoder->image->matrixCoefficients = prop->u.colr.matrixCoefficients; |
| 2839 | decoder->image->yuvRange = prop->u.colr.range; |
| 2840 | } |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2841 | } |
| 2842 | } |
| 2843 | |
| 2844 | // Transformations |
| 2845 | const avifProperty * paspProp = avifPropertyArrayFind(colorProperties, "pasp"); |
| 2846 | if (paspProp) { |
| 2847 | decoder->image->transformFlags |= AVIF_TRANSFORM_PASP; |
| 2848 | memcpy(&decoder->image->pasp, &paspProp->u.pasp, sizeof(avifPixelAspectRatioBox)); |
| 2849 | } |
| 2850 | const avifProperty * clapProp = avifPropertyArrayFind(colorProperties, "clap"); |
| 2851 | if (clapProp) { |
| 2852 | decoder->image->transformFlags |= AVIF_TRANSFORM_CLAP; |
| 2853 | memcpy(&decoder->image->clap, &clapProp->u.clap, sizeof(avifCleanApertureBox)); |
| 2854 | } |
| 2855 | const avifProperty * irotProp = avifPropertyArrayFind(colorProperties, "irot"); |
| 2856 | if (irotProp) { |
| 2857 | decoder->image->transformFlags |= AVIF_TRANSFORM_IROT; |
| 2858 | memcpy(&decoder->image->irot, &irotProp->u.irot, sizeof(avifImageRotation)); |
| 2859 | } |
| 2860 | const avifProperty * imirProp = avifPropertyArrayFind(colorProperties, "imir"); |
| 2861 | if (imirProp) { |
| 2862 | decoder->image->transformFlags |= AVIF_TRANSFORM_IMIR; |
| 2863 | memcpy(&decoder->image->imir, &imirProp->u.imir, sizeof(avifImageMirror)); |
| 2864 | } |
| 2865 | |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2866 | if (!data->cicpSet && (data->tiles.count > 0)) { |
Joe Drago | da92a38 | 2020-06-09 17:08:45 -0700 | [diff] [blame] | 2867 | avifTile * firstTile = &data->tiles.tile[0]; |
| 2868 | if (firstTile->input->samples.count > 0) { |
| 2869 | avifDecodeSample * sample = &firstTile->input->samples.sample[0]; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2870 | |
| 2871 | // Harvest CICP from the AV1's sequence header, which should be very close to the front |
| 2872 | // of the first sample. Read in successively larger chunks until we successfully parse the sequence. |
| 2873 | static const size_t searchSampleChunkIncrement = 64; |
Wan-Teh Chang | ac14571 | 2021-02-08 15:53:44 -0800 | [diff] [blame] | 2874 | static const size_t searchSampleSizeMax = 4096; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2875 | size_t searchSampleSize = 0; |
Wan-Teh Chang | c6c2fe8 | 2020-10-08 10:44:04 -0700 | [diff] [blame] | 2876 | do { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2877 | searchSampleSize += searchSampleChunkIncrement; |
| 2878 | if (searchSampleSize > sample->size) { |
| 2879 | searchSampleSize = sample->size; |
| 2880 | } |
| 2881 | |
| 2882 | avifResult prepareResult = avifDecoderPrepareSample(decoder, sample, searchSampleSize); |
| 2883 | if (prepareResult != AVIF_RESULT_OK) { |
| 2884 | return prepareResult; |
| 2885 | } |
| 2886 | |
| 2887 | avifSequenceHeader sequenceHeader; |
| 2888 | if (avifSequenceHeaderParse(&sequenceHeader, &sample->data)) { |
| 2889 | data->cicpSet = AVIF_TRUE; |
| 2890 | decoder->image->colorPrimaries = sequenceHeader.colorPrimaries; |
| 2891 | decoder->image->transferCharacteristics = sequenceHeader.transferCharacteristics; |
| 2892 | decoder->image->matrixCoefficients = sequenceHeader.matrixCoefficients; |
| 2893 | decoder->image->yuvRange = sequenceHeader.range; |
| 2894 | break; |
| 2895 | } |
Wan-Teh Chang | ac14571 | 2021-02-08 15:53:44 -0800 | [diff] [blame] | 2896 | } while (searchSampleSize != sample->size && searchSampleSize < searchSampleSizeMax); |
Joe Drago | da92a38 | 2020-06-09 17:08:45 -0700 | [diff] [blame] | 2897 | } |
| 2898 | } |
| 2899 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2900 | const avifProperty * av1CProp = avifPropertyArrayFind(colorProperties, "av1C"); |
| 2901 | if (av1CProp) { |
Joe Drago | b840112 | 2020-06-19 11:45:49 -0700 | [diff] [blame] | 2902 | decoder->image->depth = avifCodecConfigurationBoxGetDepth(&av1CProp->u.av1C); |
| 2903 | if (av1CProp->u.av1C.monochrome) { |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2904 | decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV400; |
Joe Drago | 7b2cf80 | 2020-06-09 17:57:23 -0700 | [diff] [blame] | 2905 | } else { |
Joe Drago | b840112 | 2020-06-19 11:45:49 -0700 | [diff] [blame] | 2906 | if (av1CProp->u.av1C.chromaSubsamplingX && av1CProp->u.av1C.chromaSubsamplingY) { |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2907 | decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV420; |
Joe Drago | b840112 | 2020-06-19 11:45:49 -0700 | [diff] [blame] | 2908 | } else if (av1CProp->u.av1C.chromaSubsamplingX) { |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2909 | decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV422; |
Joe Drago | 7b2cf80 | 2020-06-09 17:57:23 -0700 | [diff] [blame] | 2910 | |
| 2911 | } else { |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2912 | decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV444; |
Joe Drago | 7b2cf80 | 2020-06-09 17:57:23 -0700 | [diff] [blame] | 2913 | } |
| 2914 | } |
Joe Drago | b840112 | 2020-06-19 11:45:49 -0700 | [diff] [blame] | 2915 | decoder->image->yuvChromaSamplePosition = (avifChromaSamplePosition)av1CProp->u.av1C.chromaSamplePosition; |
Joe Drago | 00bcaaf | 2020-06-05 15:29:38 -0700 | [diff] [blame] | 2916 | } else { |
Joe Drago | f48a338 | 2020-06-19 14:13:44 -0700 | [diff] [blame] | 2917 | // An av1C box is mandatory in all valid AVIF configurations. Bail out. |
| 2918 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2919 | } |
| 2920 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2921 | return avifDecoderFlush(decoder); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2922 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2923 | |
Joe Drago | 5998f59 | 2020-11-13 15:38:20 -0800 | [diff] [blame] | 2924 | avifResult avifDecoderNextImage(avifDecoder * decoder) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2925 | { |
Wan-Teh Chang | af0d484 | 2020-11-17 12:27:34 -0800 | [diff] [blame] | 2926 | if (!decoder->data) { |
| 2927 | // Nothing has been parsed yet |
| 2928 | return AVIF_RESULT_NO_CONTENT; |
| 2929 | } |
| 2930 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2931 | if (!decoder->io || !decoder->io->read) { |
Wan-Teh Chang | 31c7c1a | 2020-10-13 16:45:41 -0700 | [diff] [blame] | 2932 | return AVIF_RESULT_IO_NOT_SET; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2933 | } |
| 2934 | |
Wan-Teh Chang | af0d484 | 2020-11-17 12:27:34 -0800 | [diff] [blame] | 2935 | const uint32_t nextImageIndex = (uint32_t)(decoder->imageIndex + 1); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2936 | |
| 2937 | // Acquire all sample data for the current image first, allowing for any read call to bail out |
| 2938 | // with AVIF_RESULT_WAITING_ON_IO harmlessly / idempotently. |
| 2939 | for (unsigned int tileIndex = 0; tileIndex < decoder->data->tiles.count; ++tileIndex) { |
| 2940 | avifTile * tile = &decoder->data->tiles.tile[tileIndex]; |
Joe Drago | 5998f59 | 2020-11-13 15:38:20 -0800 | [diff] [blame] | 2941 | if (nextImageIndex >= tile->input->samples.count) { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2942 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
| 2943 | } |
| 2944 | |
Joe Drago | 5998f59 | 2020-11-13 15:38:20 -0800 | [diff] [blame] | 2945 | avifDecodeSample * sample = &tile->input->samples.sample[nextImageIndex]; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2946 | avifResult prepareResult = avifDecoderPrepareSample(decoder, sample, 0); |
| 2947 | if (prepareResult != AVIF_RESULT_OK) { |
| 2948 | return prepareResult; |
| 2949 | } |
| 2950 | } |
| 2951 | |
Wan-Teh Chang | af0d484 | 2020-11-17 12:27:34 -0800 | [diff] [blame] | 2952 | // Decode all tiles now that the sample data is ready. |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2953 | for (unsigned int tileIndex = 0; tileIndex < decoder->data->tiles.count; ++tileIndex) { |
| 2954 | avifTile * tile = &decoder->data->tiles.tile[tileIndex]; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 2955 | |
Wan-Teh Chang | b4977b3 | 2020-10-05 18:04:54 -0700 | [diff] [blame] | 2956 | const avifDecodeSample * sample = &tile->input->samples.sample[nextImageIndex]; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2957 | |
| 2958 | if (!tile->codec->getNextImage(tile->codec, sample, tile->input->alpha, tile->image)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2959 | if (tile->input->alpha) { |
| 2960 | return AVIF_RESULT_DECODE_ALPHA_FAILED; |
| 2961 | } else { |
| 2962 | if (tile->image->width) { |
| 2963 | // We've sent at least one image, but we've run out now. |
| 2964 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
| 2965 | } |
| 2966 | return AVIF_RESULT_DECODE_COLOR_FAILED; |
| 2967 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2968 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | if (decoder->data->tiles.count != (decoder->data->colorTileCount + decoder->data->alphaTileCount)) { |
| 2972 | // TODO: assert here? This should be impossible. |
| 2973 | return AVIF_RESULT_UNKNOWN_ERROR; |
| 2974 | } |
| 2975 | |
Wan-Teh Chang | ab8d9a5 | 2020-08-12 17:17:37 -0700 | [diff] [blame] | 2976 | if ((decoder->data->colorGrid.rows > 0) && (decoder->data->colorGrid.columns > 0)) { |
Joe Drago | 11d2359 | 2021-01-05 14:18:57 -0800 | [diff] [blame] | 2977 | if (!avifDecoderDataFillImageGrid(decoder->data, &decoder->data->colorGrid, decoder->image, 0, decoder->data->colorTileCount, AVIF_FALSE)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2978 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 2979 | } |
| 2980 | } else { |
| 2981 | // Normal (most common) non-grid path. Just steal the planes from the only "tile". |
| 2982 | |
| 2983 | if (decoder->data->colorTileCount != 1) { |
| 2984 | return AVIF_RESULT_DECODE_COLOR_FAILED; |
| 2985 | } |
| 2986 | |
| 2987 | avifImage * srcColor = decoder->data->tiles.tile[0].image; |
| 2988 | |
| 2989 | if ((decoder->image->width != srcColor->width) || (decoder->image->height != srcColor->height) || |
| 2990 | (decoder->image->depth != srcColor->depth)) { |
| 2991 | avifImageFreePlanes(decoder->image, AVIF_PLANES_ALL); |
| 2992 | |
| 2993 | decoder->image->width = srcColor->width; |
| 2994 | decoder->image->height = srcColor->height; |
| 2995 | decoder->image->depth = srcColor->depth; |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2996 | } |
psi / Ryo Hirafuji | 922d8a1 | 2020-03-10 03:24:57 +0900 | [diff] [blame] | 2997 | |
Joe Drago | c00d583 | 2020-08-13 16:03:28 -0700 | [diff] [blame] | 2998 | #if 0 |
| 2999 | // This code is currently unnecessary as the CICP is always set by the end of avifDecoderParse(). |
| 3000 | if (!decoder->data->cicpSet) { |
| 3001 | decoder->data->cicpSet = AVIF_TRUE; |
| 3002 | decoder->image->colorPrimaries = srcColor->colorPrimaries; |
| 3003 | decoder->image->transferCharacteristics = srcColor->transferCharacteristics; |
| 3004 | decoder->image->matrixCoefficients = srcColor->matrixCoefficients; |
| 3005 | } |
| 3006 | #endif |
| 3007 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3008 | avifImageStealPlanes(decoder->image, srcColor, AVIF_PLANES_YUV); |
| 3009 | } |
| 3010 | |
Wan-Teh Chang | ab8d9a5 | 2020-08-12 17:17:37 -0700 | [diff] [blame] | 3011 | if ((decoder->data->alphaGrid.rows > 0) && (decoder->data->alphaGrid.columns > 0)) { |
Joe Drago | 11d2359 | 2021-01-05 14:18:57 -0800 | [diff] [blame] | 3012 | if (!avifDecoderDataFillImageGrid(decoder->data, |
| 3013 | &decoder->data->alphaGrid, |
| 3014 | decoder->image, |
| 3015 | decoder->data->colorTileCount, |
| 3016 | decoder->data->alphaTileCount, |
| 3017 | AVIF_TRUE)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3018 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 3019 | } |
| 3020 | } else { |
| 3021 | // Normal (most common) non-grid path. Just steal the planes from the only "tile". |
| 3022 | |
| 3023 | if (decoder->data->alphaTileCount == 0) { |
| 3024 | avifImageFreePlanes(decoder->image, AVIF_PLANES_A); // no alpha |
| 3025 | } else { |
| 3026 | if (decoder->data->alphaTileCount != 1) { |
| 3027 | return AVIF_RESULT_DECODE_ALPHA_FAILED; |
| 3028 | } |
| 3029 | |
| 3030 | avifImage * srcAlpha = decoder->data->tiles.tile[decoder->data->colorTileCount].image; |
| 3031 | if ((decoder->image->width != srcAlpha->width) || (decoder->image->height != srcAlpha->height) || |
| 3032 | (decoder->image->depth != srcAlpha->depth)) { |
| 3033 | return AVIF_RESULT_DECODE_ALPHA_FAILED; |
| 3034 | } |
| 3035 | |
| 3036 | avifImageStealPlanes(decoder->image, srcAlpha, AVIF_PLANES_A); |
Joe Drago | 3fd2db2 | 2020-08-13 16:07:24 -0700 | [diff] [blame] | 3037 | decoder->image->alphaRange = srcAlpha->alphaRange; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3038 | } |
| 3039 | } |
Joe Drago | 7ad3ad6 | 2019-02-07 11:17:34 -0800 | [diff] [blame] | 3040 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3041 | decoder->imageIndex = nextImageIndex; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3042 | if (decoder->data->sourceSampleTable) { |
| 3043 | // Decoding from a track! Provide timing information. |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 3044 | |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3045 | avifResult timingResult = avifDecoderNthImageTiming(decoder, decoder->imageIndex, &decoder->imageTiming); |
| 3046 | if (timingResult != AVIF_RESULT_OK) { |
| 3047 | return timingResult; |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3048 | } |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3049 | } |
| 3050 | return AVIF_RESULT_OK; |
| 3051 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3052 | |
Wan-Teh Chang | e184dc1 | 2020-05-11 12:47:21 -0700 | [diff] [blame] | 3053 | avifResult avifDecoderNthImageTiming(const avifDecoder * decoder, uint32_t frameIndex, avifImageTiming * outTiming) |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3054 | { |
| 3055 | if (!decoder->data) { |
| 3056 | // Nothing has been parsed yet |
| 3057 | return AVIF_RESULT_NO_CONTENT; |
| 3058 | } |
| 3059 | |
Wan-Teh Chang | 25b9c70 | 2020-10-08 12:17:17 -0700 | [diff] [blame] | 3060 | if ((frameIndex > INT_MAX) || ((int)frameIndex >= decoder->imageCount)) { |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3061 | // Impossible index |
| 3062 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
| 3063 | } |
| 3064 | |
| 3065 | if (!decoder->data->sourceSampleTable) { |
| 3066 | // There isn't any real timing associated with this decode, so |
| 3067 | // just hand back the defaults chosen in avifDecoderReset(). |
| 3068 | memcpy(outTiming, &decoder->imageTiming, sizeof(avifImageTiming)); |
| 3069 | return AVIF_RESULT_OK; |
| 3070 | } |
| 3071 | |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 3072 | outTiming->timescale = decoder->timescale; |
| 3073 | outTiming->ptsInTimescales = 0; |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3074 | for (int imageIndex = 0; imageIndex < (int)frameIndex; ++imageIndex) { |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 3075 | outTiming->ptsInTimescales += avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, imageIndex); |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3076 | } |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 3077 | outTiming->durationInTimescales = avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, frameIndex); |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3078 | |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 3079 | if (outTiming->timescale > 0) { |
| 3080 | outTiming->pts = (double)outTiming->ptsInTimescales / (double)outTiming->timescale; |
| 3081 | outTiming->duration = (double)outTiming->durationInTimescales / (double)outTiming->timescale; |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3082 | } else { |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 3083 | outTiming->pts = 0.0; |
| 3084 | outTiming->duration = 0.0; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 3085 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3086 | return AVIF_RESULT_OK; |
| 3087 | } |
| 3088 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3089 | avifResult avifDecoderNthImage(avifDecoder * decoder, uint32_t frameIndex) |
| 3090 | { |
Wan-Teh Chang | 25b9c70 | 2020-10-08 12:17:17 -0700 | [diff] [blame] | 3091 | if (frameIndex > INT_MAX) { |
| 3092 | // Impossible index |
| 3093 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
| 3094 | } |
| 3095 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3096 | int requestedIndex = (int)frameIndex; |
| 3097 | if (requestedIndex == decoder->imageIndex) { |
| 3098 | // We're here already, nothing to do |
| 3099 | return AVIF_RESULT_OK; |
| 3100 | } |
| 3101 | |
| 3102 | if (requestedIndex == (decoder->imageIndex + 1)) { |
| 3103 | // it's just the next image, nothing special here |
| 3104 | return avifDecoderNextImage(decoder); |
| 3105 | } |
| 3106 | |
| 3107 | if (requestedIndex >= decoder->imageCount) { |
| 3108 | // Impossible index |
| 3109 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
| 3110 | } |
| 3111 | |
Wan-Teh Chang | 722fb7e | 2020-11-06 12:39:58 -0800 | [diff] [blame] | 3112 | int nearestKeyFrame = (int)avifDecoderNearestKeyframe(decoder, frameIndex); |
| 3113 | if ((nearestKeyFrame > (decoder->imageIndex + 1)) || (requestedIndex < decoder->imageIndex)) { |
| 3114 | // If we get here, a decoder flush is necessary |
| 3115 | decoder->imageIndex = nearestKeyFrame - 1; // prepare to read nearest keyframe |
| 3116 | avifDecoderFlush(decoder); |
| 3117 | } |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3118 | for (;;) { |
| 3119 | avifResult result = avifDecoderNextImage(decoder); |
| 3120 | if (result != AVIF_RESULT_OK) { |
| 3121 | return result; |
| 3122 | } |
| 3123 | |
| 3124 | if (requestedIndex == decoder->imageIndex) { |
| 3125 | break; |
| 3126 | } |
Joe Drago | fc4144e | 2019-09-27 20:35:06 -0700 | [diff] [blame] | 3127 | } |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3128 | return AVIF_RESULT_OK; |
| 3129 | } |
| 3130 | |
Wan-Teh Chang | e184dc1 | 2020-05-11 12:47:21 -0700 | [diff] [blame] | 3131 | avifBool avifDecoderIsKeyframe(const avifDecoder * decoder, uint32_t frameIndex) |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3132 | { |
Wan-Teh Chang | af0d484 | 2020-11-17 12:27:34 -0800 | [diff] [blame] | 3133 | if (!decoder->data) { |
| 3134 | // Nothing has been parsed yet |
| 3135 | return AVIF_FALSE; |
| 3136 | } |
| 3137 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3138 | if ((decoder->data->tiles.count > 0) && decoder->data->tiles.tile[0].input) { |
| 3139 | if (frameIndex < decoder->data->tiles.tile[0].input->samples.count) { |
| 3140 | return decoder->data->tiles.tile[0].input->samples.sample[frameIndex].sync; |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3141 | } |
| 3142 | } |
| 3143 | return AVIF_FALSE; |
| 3144 | } |
| 3145 | |
Wan-Teh Chang | e184dc1 | 2020-05-11 12:47:21 -0700 | [diff] [blame] | 3146 | uint32_t avifDecoderNearestKeyframe(const avifDecoder * decoder, uint32_t frameIndex) |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3147 | { |
Wan-Teh Chang | af0d484 | 2020-11-17 12:27:34 -0800 | [diff] [blame] | 3148 | if (!decoder->data) { |
| 3149 | // Nothing has been parsed yet |
| 3150 | return 0; |
| 3151 | } |
| 3152 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3153 | for (; frameIndex != 0; --frameIndex) { |
| 3154 | if (avifDecoderIsKeyframe(decoder, frameIndex)) { |
| 3155 | break; |
| 3156 | } |
| 3157 | } |
| 3158 | return frameIndex; |
| 3159 | } |
| 3160 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3161 | avifResult avifDecoderRead(avifDecoder * decoder, avifImage * image) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3162 | { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3163 | avifResult result = avifDecoderParse(decoder); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3164 | if (result != AVIF_RESULT_OK) { |
| 3165 | return result; |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 3166 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3167 | result = avifDecoderNextImage(decoder); |
| 3168 | if (result != AVIF_RESULT_OK) { |
| 3169 | return result; |
| 3170 | } |
Joe Drago | 250221a | 2020-06-01 11:11:06 -0700 | [diff] [blame] | 3171 | avifImageCopy(image, decoder->image, AVIF_PLANES_ALL); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3172 | return AVIF_RESULT_OK; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 3173 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3174 | |
Wan-Teh Chang | 7de4445 | 2020-10-08 11:43:18 -0700 | [diff] [blame] | 3175 | avifResult avifDecoderReadMemory(avifDecoder * decoder, avifImage * image, const uint8_t * data, size_t size) |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3176 | { |
Wan-Teh Chang | 7de4445 | 2020-10-08 11:43:18 -0700 | [diff] [blame] | 3177 | avifResult result = avifDecoderSetIOMemory(decoder, data, size); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3178 | if (result != AVIF_RESULT_OK) { |
| 3179 | return result; |
| 3180 | } |
| 3181 | return avifDecoderRead(decoder, image); |
| 3182 | } |
| 3183 | |
| 3184 | avifResult avifDecoderReadFile(avifDecoder * decoder, avifImage * image, const char * filename) |
| 3185 | { |
| 3186 | avifResult result = avifDecoderSetIOFile(decoder, filename); |
| 3187 | if (result != AVIF_RESULT_OK) { |
| 3188 | return result; |
| 3189 | } |
| 3190 | return avifDecoderRead(decoder, image); |
| 3191 | } |