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> |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 7 | #include <inttypes.h> |
Wan-Teh Chang | 25b9c70 | 2020-10-08 12:17:17 -0700 | [diff] [blame] | 8 | #include <limits.h> |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 9 | #include <string.h> |
| 10 | |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 11 | #define AUXTYPE_SIZE 64 |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 12 | #define CONTENTTYPE_SIZE 64 |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 13 | |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 14 | // class VisualSampleEntry(codingname) extends SampleEntry(codingname) { |
| 15 | // unsigned int(16) pre_defined = 0; |
| 16 | // const unsigned int(16) reserved = 0; |
| 17 | // unsigned int(32)[3] pre_defined = 0; |
| 18 | // unsigned int(16) width; |
| 19 | // unsigned int(16) height; |
| 20 | // template unsigned int(32) horizresolution = 0x00480000; // 72 dpi |
| 21 | // template unsigned int(32) vertresolution = 0x00480000; // 72 dpi |
| 22 | // const unsigned int(32) reserved = 0; |
| 23 | // template unsigned int(16) frame_count = 1; |
| 24 | // string[32] compressorname; |
| 25 | // template unsigned int(16) depth = 0x0018; |
| 26 | // int(16) pre_defined = -1; |
| 27 | // // other boxes from derived specifications |
| 28 | // CleanApertureBox clap; // optional |
| 29 | // PixelAspectRatioBox pasp; // optional |
| 30 | // } |
| 31 | static const size_t VISUALSAMPLEENTRY_SIZE = 78; |
| 32 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 33 | static const char xmpContentType[] = CONTENT_TYPE_XMP; |
| 34 | static const size_t xmpContentTypeSize = sizeof(xmpContentType); |
| 35 | |
Joe Drago | c60ccae | 2021-03-31 10:30:48 -0700 | [diff] [blame] | 36 | // The only supported ipma box values for both version and flags are [0,1], so there technically |
| 37 | // can't be more than 4 unique tuples right now. |
| 38 | #define MAX_IPMA_VERSION_AND_FLAGS_SEEN 4 |
| 39 | |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 40 | #define MAX_AV1_LAYER_COUNT 4 |
| 41 | |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 42 | // --------------------------------------------------------------------------- |
| 43 | // Box data structures |
| 44 | |
| 45 | // ftyp |
| 46 | typedef struct avifFileType |
| 47 | { |
| 48 | uint8_t majorBrand[4]; |
| 49 | uint32_t minorVersion; |
Wan-Teh Chang | 6da0a88 | 2020-07-01 12:19:31 -0700 | [diff] [blame] | 50 | // If not null, points to a memory block of 4 * compatibleBrandsCount bytes. |
| 51 | const uint8_t * compatibleBrands; |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 52 | int compatibleBrandsCount; |
| 53 | } avifFileType; |
| 54 | |
| 55 | // ispe |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 56 | typedef struct avifImageSpatialExtents |
| 57 | { |
| 58 | uint32_t width; |
| 59 | uint32_t height; |
| 60 | } avifImageSpatialExtents; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 61 | |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 62 | // auxC |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 63 | typedef struct avifAuxiliaryType |
| 64 | { |
| 65 | char auxType[AUXTYPE_SIZE]; |
| 66 | } avifAuxiliaryType; |
| 67 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 68 | // infe mime content_type |
| 69 | typedef struct avifContentType |
| 70 | { |
| 71 | char contentType[CONTENTTYPE_SIZE]; |
| 72 | } avifContentType; |
| 73 | |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 74 | // colr |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 75 | typedef struct avifColourInformationBox |
| 76 | { |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 77 | avifBool hasICC; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 78 | const uint8_t * icc; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 79 | size_t iccSize; |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 80 | |
| 81 | avifBool hasNCLX; |
Wan-Teh Chang | 559def5 | 2021-02-01 14:25:31 -0800 | [diff] [blame] | 82 | avifColorPrimaries colorPrimaries; |
| 83 | avifTransferCharacteristics transferCharacteristics; |
| 84 | avifMatrixCoefficients matrixCoefficients; |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 85 | avifRange range; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 86 | } avifColourInformationBox; |
| 87 | |
Joe Drago | 6042156 | 2020-04-23 11:32:26 -0700 | [diff] [blame] | 88 | #define MAX_PIXI_PLANE_DEPTHS 4 |
| 89 | typedef struct avifPixelInformationProperty |
| 90 | { |
| 91 | uint8_t planeDepths[MAX_PIXI_PLANE_DEPTHS]; |
| 92 | uint8_t planeCount; |
| 93 | } avifPixelInformationProperty; |
| 94 | |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 95 | typedef struct avifOperatingPointSelectorProperty |
| 96 | { |
| 97 | uint8_t opIndex; |
| 98 | } avifOperatingPointSelectorProperty; |
| 99 | |
| 100 | typedef struct avifLayerSelectorProperty |
| 101 | { |
| 102 | uint16_t layerID; |
| 103 | } avifLayerSelectorProperty; |
| 104 | |
| 105 | typedef struct avifAV1LayeredImageIndexingProperty |
| 106 | { |
| 107 | uint32_t layerSize[3]; |
| 108 | } avifAV1LayeredImageIndexingProperty; |
| 109 | |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 110 | // --------------------------------------------------------------------------- |
| 111 | // Top-level structures |
| 112 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 113 | struct avifMeta; |
| 114 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 115 | // 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] | 116 | typedef struct avifProperty |
| 117 | { |
| 118 | uint8_t type[4]; |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 119 | union |
| 120 | { |
| 121 | avifImageSpatialExtents ispe; |
| 122 | avifAuxiliaryType auxC; |
| 123 | avifColourInformationBox colr; |
| 124 | avifCodecConfigurationBox av1C; |
| 125 | avifPixelAspectRatioBox pasp; |
| 126 | avifCleanApertureBox clap; |
| 127 | avifImageRotation irot; |
| 128 | avifImageMirror imir; |
| 129 | avifPixelInformationProperty pixi; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 130 | avifOperatingPointSelectorProperty a1op; |
| 131 | avifLayerSelectorProperty lsel; |
| 132 | avifAV1LayeredImageIndexingProperty a1lx; |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 133 | } u; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 134 | } avifProperty; |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 135 | AVIF_ARRAY_DECLARE(avifPropertyArray, avifProperty, prop); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 136 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 137 | static const avifProperty * avifPropertyArrayFind(const avifPropertyArray * properties, const char * type) |
| 138 | { |
| 139 | for (uint32_t propertyIndex = 0; propertyIndex < properties->count; ++propertyIndex) { |
| 140 | avifProperty * prop = &properties->prop[propertyIndex]; |
| 141 | if (!memcmp(prop->type, type, 4)) { |
| 142 | return prop; |
| 143 | } |
| 144 | } |
| 145 | return NULL; |
| 146 | } |
| 147 | |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 148 | AVIF_ARRAY_DECLARE(avifExtentArray, avifExtent, extent); |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 149 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 150 | // one "item" worth for decoding (all iref, iloc, iprp, etc refer to one of these) |
| 151 | typedef struct avifDecoderItem |
| 152 | { |
| 153 | uint32_t id; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 154 | struct avifMeta * meta; // Unowned; A back-pointer for convenience |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 155 | uint8_t type[4]; |
Joe Drago | 217056b | 2020-11-13 16:19:35 -0800 | [diff] [blame] | 156 | size_t size; |
Joe Drago | 1dea33e | 2021-09-14 17:12:39 -0700 | [diff] [blame] | 157 | avifBool idatStored; // If true, offset is relative to the associated meta box's idat box (iloc construction_method==1) |
| 158 | uint32_t width; // Set from this item's ispe property, if present |
| 159 | uint32_t height; // Set from this item's ispe property, if present |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 160 | avifContentType contentType; |
| 161 | avifPropertyArray properties; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 162 | avifExtentArray extents; // All extent offsets/sizes |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 163 | avifRWData mergedExtents; // if set, is a single contiguous block of this item's extents (unused when extents.count == 1) |
| 164 | avifBool ownsMergedExtents; // if true, mergedExtents must be freed when this item is destroyed |
| 165 | avifBool partialMergedExtents; // If true, mergedExtents doesn't have all of the item data yet |
| 166 | uint32_t thumbnailForID; // if non-zero, this item is a thumbnail for Item #{thumbnailForID} |
| 167 | uint32_t auxForID; // if non-zero, this item is an auxC plane for Item #{auxForID} |
| 168 | uint32_t descForID; // if non-zero, this item is a content description for Item #{descForID} |
| 169 | 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] | 170 | 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] | 171 | avifBool hasUnsupportedEssentialProperty; // If true, this item cites a property flagged as 'essential' that libavif doesn't support (yet). Ignore the item, if so. |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 172 | avifBool ipmaSeen; // if true, this item already received a property association |
| 173 | avifBool progressive; // if true, this item has progressive layers (a1lx), but does not select a specific layer (lsel) |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 174 | } avifDecoderItem; |
| 175 | AVIF_ARRAY_DECLARE(avifDecoderItemArray, avifDecoderItem, item); |
| 176 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 177 | // grid storage |
| 178 | typedef struct avifImageGrid |
| 179 | { |
Joe Drago | 79ebcf3 | 2020-11-18 22:37:10 -0800 | [diff] [blame] | 180 | uint32_t rows; // Legal range: [1-256] |
| 181 | uint32_t columns; // Legal range: [1-256] |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 182 | uint32_t outputWidth; |
| 183 | uint32_t outputHeight; |
| 184 | } avifImageGrid; |
| 185 | |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 186 | // --------------------------------------------------------------------------- |
| 187 | // avifTrack |
| 188 | |
| 189 | typedef struct avifSampleTableChunk |
| 190 | { |
| 191 | uint64_t offset; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 192 | } avifSampleTableChunk; |
| 193 | AVIF_ARRAY_DECLARE(avifSampleTableChunkArray, avifSampleTableChunk, chunk); |
| 194 | |
| 195 | typedef struct avifSampleTableSampleToChunk |
| 196 | { |
| 197 | uint32_t firstChunk; |
| 198 | uint32_t samplesPerChunk; |
| 199 | uint32_t sampleDescriptionIndex; |
| 200 | } avifSampleTableSampleToChunk; |
| 201 | AVIF_ARRAY_DECLARE(avifSampleTableSampleToChunkArray, avifSampleTableSampleToChunk, sampleToChunk); |
| 202 | |
| 203 | typedef struct avifSampleTableSampleSize |
| 204 | { |
| 205 | uint32_t size; |
| 206 | } avifSampleTableSampleSize; |
| 207 | AVIF_ARRAY_DECLARE(avifSampleTableSampleSizeArray, avifSampleTableSampleSize, sampleSize); |
| 208 | |
| 209 | typedef struct avifSampleTableTimeToSample |
| 210 | { |
| 211 | uint32_t sampleCount; |
| 212 | uint32_t sampleDelta; |
| 213 | } avifSampleTableTimeToSample; |
| 214 | AVIF_ARRAY_DECLARE(avifSampleTableTimeToSampleArray, avifSampleTableTimeToSample, timeToSample); |
| 215 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 216 | typedef struct avifSyncSample |
| 217 | { |
| 218 | uint32_t sampleNumber; |
| 219 | } avifSyncSample; |
| 220 | AVIF_ARRAY_DECLARE(avifSyncSampleArray, avifSyncSample, syncSample); |
| 221 | |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 222 | typedef struct avifSampleDescription |
| 223 | { |
| 224 | uint8_t format[4]; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 225 | avifPropertyArray properties; |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 226 | } avifSampleDescription; |
| 227 | AVIF_ARRAY_DECLARE(avifSampleDescriptionArray, avifSampleDescription, description); |
| 228 | |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 229 | typedef struct avifSampleTable |
| 230 | { |
| 231 | avifSampleTableChunkArray chunks; |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 232 | avifSampleDescriptionArray sampleDescriptions; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 233 | avifSampleTableSampleToChunkArray sampleToChunks; |
| 234 | avifSampleTableSampleSizeArray sampleSizes; |
| 235 | avifSampleTableTimeToSampleArray timeToSamples; |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 236 | avifSyncSampleArray syncSamples; |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 237 | 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] | 238 | } avifSampleTable; |
| 239 | |
Wan-Teh Chang | f732a4d | 2022-01-21 15:56:35 -0800 | [diff] [blame] | 240 | static void avifSampleTableDestroy(avifSampleTable * sampleTable); |
| 241 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 242 | static avifSampleTable * avifSampleTableCreate() |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 243 | { |
| 244 | avifSampleTable * sampleTable = (avifSampleTable *)avifAlloc(sizeof(avifSampleTable)); |
| 245 | memset(sampleTable, 0, sizeof(avifSampleTable)); |
Wan-Teh Chang | f732a4d | 2022-01-21 15:56:35 -0800 | [diff] [blame] | 246 | if (!avifArrayCreate(&sampleTable->chunks, sizeof(avifSampleTableChunk), 16)) { |
| 247 | goto error; |
| 248 | } |
| 249 | if (!avifArrayCreate(&sampleTable->sampleDescriptions, sizeof(avifSampleDescription), 2)) { |
| 250 | goto error; |
| 251 | } |
| 252 | if (!avifArrayCreate(&sampleTable->sampleToChunks, sizeof(avifSampleTableSampleToChunk), 16)) { |
| 253 | goto error; |
| 254 | } |
| 255 | if (!avifArrayCreate(&sampleTable->sampleSizes, sizeof(avifSampleTableSampleSize), 16)) { |
| 256 | goto error; |
| 257 | } |
| 258 | if (!avifArrayCreate(&sampleTable->timeToSamples, sizeof(avifSampleTableTimeToSample), 16)) { |
| 259 | goto error; |
| 260 | } |
| 261 | if (!avifArrayCreate(&sampleTable->syncSamples, sizeof(avifSyncSample), 16)) { |
| 262 | goto error; |
| 263 | } |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 264 | return sampleTable; |
Wan-Teh Chang | f732a4d | 2022-01-21 15:56:35 -0800 | [diff] [blame] | 265 | |
| 266 | error: |
| 267 | avifSampleTableDestroy(sampleTable); |
| 268 | return NULL; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 271 | static void avifSampleTableDestroy(avifSampleTable * sampleTable) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 272 | { |
| 273 | avifArrayDestroy(&sampleTable->chunks); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 274 | for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) { |
| 275 | avifSampleDescription * description = &sampleTable->sampleDescriptions.description[i]; |
| 276 | avifArrayDestroy(&description->properties); |
| 277 | } |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 278 | avifArrayDestroy(&sampleTable->sampleDescriptions); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 279 | avifArrayDestroy(&sampleTable->sampleToChunks); |
| 280 | avifArrayDestroy(&sampleTable->sampleSizes); |
| 281 | avifArrayDestroy(&sampleTable->timeToSamples); |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 282 | avifArrayDestroy(&sampleTable->syncSamples); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 283 | avifFree(sampleTable); |
| 284 | } |
| 285 | |
Wan-Teh Chang | 306c306 | 2020-04-05 12:17:33 -0700 | [diff] [blame] | 286 | static uint32_t avifSampleTableGetImageDelta(const avifSampleTable * sampleTable, int imageIndex) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 287 | { |
| 288 | int maxSampleIndex = 0; |
| 289 | for (uint32_t i = 0; i < sampleTable->timeToSamples.count; ++i) { |
Wan-Teh Chang | 306c306 | 2020-04-05 12:17:33 -0700 | [diff] [blame] | 290 | const avifSampleTableTimeToSample * timeToSample = &sampleTable->timeToSamples.timeToSample[i]; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 291 | maxSampleIndex += timeToSample->sampleCount; |
| 292 | if ((imageIndex < maxSampleIndex) || (i == (sampleTable->timeToSamples.count - 1))) { |
| 293 | return timeToSample->sampleDelta; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // TODO: fail here? |
| 298 | return 1; |
| 299 | } |
| 300 | |
Wan-Teh Chang | 306c306 | 2020-04-05 12:17:33 -0700 | [diff] [blame] | 301 | static avifBool avifSampleTableHasFormat(const avifSampleTable * sampleTable, const char * format) |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 302 | { |
| 303 | for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) { |
| 304 | if (!memcmp(sampleTable->sampleDescriptions.description[i].format, format, 4)) { |
| 305 | return AVIF_TRUE; |
| 306 | } |
| 307 | } |
| 308 | return AVIF_FALSE; |
| 309 | } |
| 310 | |
Wan-Teh Chang | 306c306 | 2020-04-05 12:17:33 -0700 | [diff] [blame] | 311 | static uint32_t avifCodecConfigurationBoxGetDepth(const avifCodecConfigurationBox * av1C) |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 312 | { |
| 313 | if (av1C->twelveBit) { |
| 314 | return 12; |
| 315 | } else if (av1C->highBitdepth) { |
| 316 | return 10; |
| 317 | } |
| 318 | return 8; |
| 319 | } |
| 320 | |
Joe Drago | 4a70b47 | 2021-05-06 17:21:33 -0700 | [diff] [blame] | 321 | // This is used as a hint to validating the clap box in avifDecoderItemValidateAV1. |
| 322 | static avifPixelFormat avifCodecConfigurationBoxGetFormat(const avifCodecConfigurationBox * av1C) |
| 323 | { |
| 324 | if (av1C->monochrome) { |
| 325 | return AVIF_PIXEL_FORMAT_YUV400; |
Wan-Teh Chang | a1aa19c | 2021-05-28 14:29:25 -0700 | [diff] [blame] | 326 | } else if (av1C->chromaSubsamplingY == 1) { |
Joe Drago | 4a70b47 | 2021-05-06 17:21:33 -0700 | [diff] [blame] | 327 | return AVIF_PIXEL_FORMAT_YUV420; |
Wan-Teh Chang | a1aa19c | 2021-05-28 14:29:25 -0700 | [diff] [blame] | 328 | } else if (av1C->chromaSubsamplingX == 1) { |
Joe Drago | 4a70b47 | 2021-05-06 17:21:33 -0700 | [diff] [blame] | 329 | return AVIF_PIXEL_FORMAT_YUV422; |
| 330 | } |
| 331 | return AVIF_PIXEL_FORMAT_YUV444; |
| 332 | } |
| 333 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 334 | static const avifPropertyArray * avifSampleTableGetProperties(const avifSampleTable * sampleTable) |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 335 | { |
| 336 | for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) { |
Wan-Teh Chang | 306c306 | 2020-04-05 12:17:33 -0700 | [diff] [blame] | 337 | const avifSampleDescription * description = &sampleTable->sampleDescriptions.description[i]; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 338 | if (!memcmp(description->format, "av01", 4)) { |
| 339 | return &description->properties; |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
Joe Drago | 00bcaaf | 2020-06-05 15:29:38 -0700 | [diff] [blame] | 342 | return NULL; |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 345 | // one video track ("trak" contents) |
| 346 | typedef struct avifTrack |
| 347 | { |
| 348 | uint32_t id; |
Wan-Teh Chang | 53adb6a | 2021-02-20 16:58:43 -0800 | [diff] [blame] | 349 | uint32_t auxForID; // if non-zero, this track is an auxC plane for Track #{auxForID} |
| 350 | 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] | 351 | uint32_t mediaTimescale; |
| 352 | uint64_t mediaDuration; |
Joe Drago | fc4144e | 2019-09-27 20:35:06 -0700 | [diff] [blame] | 353 | uint32_t width; |
| 354 | uint32_t height; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 355 | avifSampleTable * sampleTable; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 356 | struct avifMeta * meta; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 357 | } avifTrack; |
| 358 | AVIF_ARRAY_DECLARE(avifTrackArray, avifTrack, track); |
| 359 | |
| 360 | // --------------------------------------------------------------------------- |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 361 | // avifCodecDecodeInput |
| 362 | |
Joe Drago | 399df4f | 2019-07-23 16:45:14 -0700 | [diff] [blame] | 363 | avifCodecDecodeInput * avifCodecDecodeInputCreate(void) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 364 | { |
| 365 | avifCodecDecodeInput * decodeInput = (avifCodecDecodeInput *)avifAlloc(sizeof(avifCodecDecodeInput)); |
| 366 | memset(decodeInput, 0, sizeof(avifCodecDecodeInput)); |
Wan-Teh Chang | f732a4d | 2022-01-21 15:56:35 -0800 | [diff] [blame] | 367 | if (!avifArrayCreate(&decodeInput->samples, sizeof(avifDecodeSample), 1)) { |
| 368 | goto error; |
| 369 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 370 | return decodeInput; |
Wan-Teh Chang | f732a4d | 2022-01-21 15:56:35 -0800 | [diff] [blame] | 371 | |
| 372 | error: |
| 373 | avifFree(decodeInput); |
| 374 | return NULL; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Joe Drago | 8b34ad7 | 2019-07-22 16:56:32 -0700 | [diff] [blame] | 377 | void avifCodecDecodeInputDestroy(avifCodecDecodeInput * decodeInput) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 378 | { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 379 | for (uint32_t sampleIndex = 0; sampleIndex < decodeInput->samples.count; ++sampleIndex) { |
| 380 | avifDecodeSample * sample = &decodeInput->samples.sample[sampleIndex]; |
| 381 | if (sample->ownsData) { |
| 382 | avifRWDataFree((avifRWData *)&sample->data); |
| 383 | } |
| 384 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 385 | avifArrayDestroy(&decodeInput->samples); |
| 386 | avifFree(decodeInput); |
| 387 | } |
| 388 | |
Wan-Teh Chang | b8c8944 | 2021-03-19 14:17:57 -0700 | [diff] [blame] | 389 | // Returns how many samples are in the chunk. |
| 390 | static uint32_t avifGetSampleCountOfChunk(const avifSampleTableSampleToChunkArray * sampleToChunks, uint32_t chunkIndex) |
| 391 | { |
| 392 | uint32_t sampleCount = 0; |
| 393 | for (int sampleToChunkIndex = sampleToChunks->count - 1; sampleToChunkIndex >= 0; --sampleToChunkIndex) { |
| 394 | const avifSampleTableSampleToChunk * sampleToChunk = &sampleToChunks->sampleToChunk[sampleToChunkIndex]; |
| 395 | if (sampleToChunk->firstChunk <= (chunkIndex + 1)) { |
| 396 | sampleCount = sampleToChunk->samplesPerChunk; |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | return sampleCount; |
| 401 | } |
| 402 | |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 403 | static avifBool avifCodecDecodeInputFillFromSampleTable(avifCodecDecodeInput * decodeInput, |
| 404 | avifSampleTable * sampleTable, |
| 405 | const uint32_t imageCountLimit, |
| 406 | const uint64_t sizeHint, |
| 407 | avifDiagnostics * diag) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 408 | { |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 409 | if (imageCountLimit) { |
| 410 | // Verify that the we're not about to exceed the frame count limit. |
| 411 | |
Wan-Teh Chang | b8c8944 | 2021-03-19 14:17:57 -0700 | [diff] [blame] | 412 | uint32_t imageCountLeft = imageCountLimit; |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 413 | for (uint32_t chunkIndex = 0; chunkIndex < sampleTable->chunks.count; ++chunkIndex) { |
| 414 | // First, figure out how many samples are in this chunk |
Wan-Teh Chang | b8c8944 | 2021-03-19 14:17:57 -0700 | [diff] [blame] | 415 | uint32_t sampleCount = avifGetSampleCountOfChunk(&sampleTable->sampleToChunks, chunkIndex); |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 416 | if (sampleCount == 0) { |
| 417 | // chunks with 0 samples are invalid |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 418 | avifDiagnosticsPrintf(diag, "Sample table contains a chunk with 0 samples"); |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 419 | return AVIF_FALSE; |
| 420 | } |
| 421 | |
Wan-Teh Chang | b8c8944 | 2021-03-19 14:17:57 -0700 | [diff] [blame] | 422 | if (sampleCount > imageCountLeft) { |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 423 | // This file exceeds the imageCountLimit, bail out |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 424 | avifDiagnosticsPrintf(diag, "Exceeded avifDecoder's imageCountLimit"); |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 425 | return AVIF_FALSE; |
| 426 | } |
Wan-Teh Chang | b8c8944 | 2021-03-19 14:17:57 -0700 | [diff] [blame] | 427 | imageCountLeft -= sampleCount; |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 431 | uint32_t sampleSizeIndex = 0; |
| 432 | for (uint32_t chunkIndex = 0; chunkIndex < sampleTable->chunks.count; ++chunkIndex) { |
| 433 | avifSampleTableChunk * chunk = &sampleTable->chunks.chunk[chunkIndex]; |
| 434 | |
| 435 | // First, figure out how many samples are in this chunk |
Wan-Teh Chang | b8c8944 | 2021-03-19 14:17:57 -0700 | [diff] [blame] | 436 | uint32_t sampleCount = avifGetSampleCountOfChunk(&sampleTable->sampleToChunks, chunkIndex); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 437 | if (sampleCount == 0) { |
| 438 | // chunks with 0 samples are invalid |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 439 | avifDiagnosticsPrintf(diag, "Sample table contains a chunk with 0 samples"); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 440 | return AVIF_FALSE; |
| 441 | } |
| 442 | |
| 443 | uint64_t sampleOffset = chunk->offset; |
| 444 | for (uint32_t sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex) { |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 445 | uint32_t sampleSize = sampleTable->allSamplesSize; |
| 446 | if (sampleSize == 0) { |
| 447 | if (sampleSizeIndex >= sampleTable->sampleSizes.count) { |
| 448 | // We've run out of samples to sum |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 449 | avifDiagnosticsPrintf(diag, "Truncated sample table"); |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 450 | return AVIF_FALSE; |
| 451 | } |
| 452 | avifSampleTableSampleSize * sampleSizePtr = &sampleTable->sampleSizes.sampleSize[sampleSizeIndex]; |
| 453 | sampleSize = sampleSizePtr->size; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Joe Drago | e3e3bfa | 2020-06-02 16:33:53 -0700 | [diff] [blame] | 456 | avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&decodeInput->samples); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 457 | sample->offset = sampleOffset; |
| 458 | sample->size = sampleSize; |
Joe Drago | 9d19546 | 2021-06-14 14:14:35 -0700 | [diff] [blame] | 459 | sample->spatialID = AVIF_SPATIAL_ID_UNSET; // Not filtering by spatial_id |
| 460 | 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] | 461 | |
Wan-Teh Chang | 3ca1424 | 2020-09-30 16:39:38 -0700 | [diff] [blame] | 462 | if (sampleSize > UINT64_MAX - sampleOffset) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 463 | avifDiagnosticsPrintf(diag, |
Joe Drago | 4886729 | 2021-05-04 13:23:08 -0700 | [diff] [blame] | 464 | "Sample table contains an offset/size pair which overflows: [%" PRIu64 " / %u]", |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 465 | sampleOffset, |
| 466 | sampleSize); |
Wan-Teh Chang | 3ca1424 | 2020-09-30 16:39:38 -0700 | [diff] [blame] | 467 | return AVIF_FALSE; |
| 468 | } |
Wan-Teh Chang | 136d757 | 2020-10-08 15:13:42 -0700 | [diff] [blame] | 469 | if (sizeHint && ((sampleOffset + sampleSize) > sizeHint)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 470 | avifDiagnosticsPrintf(diag, "Exceeded avifIO's sizeHint, possibly truncated data"); |
Joe Drago | 34c0d31 | 2020-04-30 15:23:03 -0700 | [diff] [blame] | 471 | return AVIF_FALSE; |
| 472 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 473 | |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 474 | sampleOffset += sampleSize; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 475 | ++sampleSizeIndex; |
| 476 | } |
| 477 | } |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 478 | |
| 479 | // Mark appropriate samples as sync |
| 480 | for (uint32_t syncSampleIndex = 0; syncSampleIndex < sampleTable->syncSamples.count; ++syncSampleIndex) { |
| 481 | uint32_t frameIndex = sampleTable->syncSamples.syncSample[syncSampleIndex].sampleNumber - 1; // sampleNumber is 1-based |
| 482 | if (frameIndex < decodeInput->samples.count) { |
| 483 | decodeInput->samples.sample[frameIndex].sync = AVIF_TRUE; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | // Assume frame 0 is sync, just in case the stss box is absent in the BMFF. (Unnecessary?) |
| 488 | if (decodeInput->samples.count > 0) { |
| 489 | decodeInput->samples.sample[0].sync = AVIF_TRUE; |
| 490 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 491 | return AVIF_TRUE; |
| 492 | } |
| 493 | |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 494 | static avifBool avifCodecDecodeInputFillFromDecoderItem(avifCodecDecodeInput * decodeInput, |
| 495 | avifDecoderItem * item, |
| 496 | avifBool allowProgressive, |
| 497 | const uint32_t imageCountLimit, |
| 498 | const uint64_t sizeHint, |
| 499 | avifDiagnostics * diag) |
| 500 | { |
| 501 | if (sizeHint && (item->size > sizeHint)) { |
| 502 | avifDiagnosticsPrintf(diag, "Exceeded avifIO's sizeHint, possibly truncated data"); |
| 503 | return AVIF_FALSE; |
| 504 | } |
| 505 | |
| 506 | uint8_t layerCount = 0; |
| 507 | size_t layerSizes[4] = { 0 }; |
| 508 | const avifProperty * a1lxProp = avifPropertyArrayFind(&item->properties, "a1lx"); |
| 509 | if (a1lxProp) { |
| 510 | // Calculate layer count and all layer sizes from the a1lx box, and then validate |
| 511 | |
| 512 | size_t remainingSize = item->size; |
| 513 | for (int i = 0; i < 3; ++i) { |
| 514 | ++layerCount; |
| 515 | |
| 516 | const size_t layerSize = (size_t)a1lxProp->u.a1lx.layerSize[i]; |
| 517 | if (layerSize) { |
| 518 | if (layerSize >= remainingSize) { // >= instead of > because there must be room for the last layer |
| 519 | avifDiagnosticsPrintf(diag, "a1lx layer index [%d] does not fit in item size", i); |
| 520 | return AVIF_FALSE; |
| 521 | } |
| 522 | layerSizes[i] = layerSize; |
| 523 | remainingSize -= layerSize; |
| 524 | } else { |
| 525 | layerSizes[i] = remainingSize; |
| 526 | remainingSize = 0; |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | if (remainingSize > 0) { |
| 531 | assert(layerCount == 3); |
| 532 | ++layerCount; |
| 533 | layerSizes[3] = remainingSize; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | const avifProperty * lselProp = avifPropertyArrayFind(&item->properties, "lsel"); |
| 538 | item->progressive = (a1lxProp && !lselProp); // Progressive images offer layers via the a1lxProp, but don't specify a layer selection with lsel. |
| 539 | if (lselProp) { |
| 540 | // Layer selection. This requires that the underlying AV1 codec decodes all layers, |
| 541 | // and then only returns the requested layer as a single frame. To the user of libavif, |
| 542 | // this appears to be a single frame. |
| 543 | |
| 544 | decodeInput->allLayers = AVIF_TRUE; |
| 545 | |
| 546 | size_t sampleSize = 0; |
| 547 | if (layerCount > 0) { |
| 548 | // Optimization: If we're selecting a layer that doesn't require the entire image's payload (hinted via the a1lx box) |
| 549 | |
| 550 | if (lselProp->u.lsel.layerID >= layerCount) { |
| 551 | avifDiagnosticsPrintf(diag, |
| 552 | "lsel property requests layer index [%u] which isn't present in a1lx property ([%u] layers)", |
| 553 | lselProp->u.lsel.layerID, |
| 554 | layerCount); |
| 555 | return AVIF_FALSE; |
| 556 | } |
| 557 | |
| 558 | for (uint8_t i = 0; i <= lselProp->u.lsel.layerID; ++i) { |
| 559 | sampleSize += layerSizes[i]; |
| 560 | } |
| 561 | } else { |
| 562 | // This layer's payload subsection is unknown, just use the whole payload |
| 563 | sampleSize = item->size; |
| 564 | } |
| 565 | |
| 566 | avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&decodeInput->samples); |
| 567 | sample->itemID = item->id; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 568 | sample->offset = 0; |
| 569 | sample->size = sampleSize; |
Wan-Teh Chang | 6ff9c4e | 2021-07-13 15:22:42 -0700 | [diff] [blame] | 570 | assert(lselProp->u.lsel.layerID < MAX_AV1_LAYER_COUNT); |
Joe Drago | 9d19546 | 2021-06-14 14:14:35 -0700 | [diff] [blame] | 571 | sample->spatialID = (uint8_t)lselProp->u.lsel.layerID; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 572 | sample->sync = AVIF_TRUE; |
Joe Drago | 8df04be | 2021-06-14 14:22:18 -0700 | [diff] [blame] | 573 | } else if (allowProgressive && item->progressive) { |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 574 | // Progressive image. Decode all layers and expose them all to the user. |
| 575 | |
| 576 | if (imageCountLimit && (layerCount > imageCountLimit)) { |
| 577 | avifDiagnosticsPrintf(diag, "Exceeded avifDecoder's imageCountLimit (progressive)"); |
| 578 | return AVIF_FALSE; |
| 579 | } |
| 580 | |
| 581 | decodeInput->allLayers = AVIF_TRUE; |
| 582 | |
| 583 | size_t offset = 0; |
| 584 | for (int i = 0; i < layerCount; ++i) { |
| 585 | avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&decodeInput->samples); |
| 586 | sample->itemID = item->id; |
| 587 | sample->offset = offset; |
| 588 | sample->size = layerSizes[i]; |
Joe Drago | 9d19546 | 2021-06-14 14:14:35 -0700 | [diff] [blame] | 589 | sample->spatialID = AVIF_SPATIAL_ID_UNSET; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 590 | sample->sync = (i == 0); // Assume all layers depend on the first layer |
| 591 | |
| 592 | offset += layerSizes[i]; |
| 593 | } |
| 594 | } else { |
| 595 | // Typical case: Use the entire item's payload for a single frame output |
| 596 | |
| 597 | avifDecodeSample * sample = (avifDecodeSample *)avifArrayPushPtr(&decodeInput->samples); |
| 598 | sample->itemID = item->id; |
| 599 | sample->offset = 0; |
| 600 | sample->size = item->size; |
Joe Drago | 9d19546 | 2021-06-14 14:14:35 -0700 | [diff] [blame] | 601 | sample->spatialID = AVIF_SPATIAL_ID_UNSET; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 602 | sample->sync = AVIF_TRUE; |
| 603 | } |
| 604 | return AVIF_TRUE; |
| 605 | } |
| 606 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 607 | // --------------------------------------------------------------------------- |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 608 | // Helper macros / functions |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 609 | |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 610 | #define BEGIN_STREAM(VARNAME, PTR, SIZE, DIAG, CONTEXT) \ |
| 611 | avifROStream VARNAME; \ |
| 612 | avifROData VARNAME##_roData; \ |
| 613 | VARNAME##_roData.data = PTR; \ |
| 614 | VARNAME##_roData.size = SIZE; \ |
| 615 | avifROStreamStart(&VARNAME, &VARNAME##_roData, DIAG, CONTEXT) |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 616 | |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 617 | // Use this to keep track of whether or not a child box that must be unique (0 or 1 present) has |
| 618 | // been seen yet, when parsing a parent box. If the "seen" bit is already set for a given box when |
| 619 | // it is encountered during parse, an error is thrown. Which bit corresponds to which box is |
| 620 | // dictated entirely by the calling function. |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 621 | static avifBool uniqueBoxSeen(uint32_t * uniqueBoxFlags, uint32_t whichFlag, const char * parentBoxType, const char * boxType, avifDiagnostics * diagnostics) |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 622 | { |
| 623 | const uint32_t flag = 1 << whichFlag; |
| 624 | if (*uniqueBoxFlags & flag) { |
| 625 | // This box has already been seen. Error! |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 626 | avifDiagnosticsPrintf(diagnostics, "Box[%s] contains a duplicate unique box of type '%s'", parentBoxType, boxType); |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 627 | return AVIF_FALSE; |
| 628 | } |
| 629 | |
| 630 | // Mark this box as seen. |
| 631 | *uniqueBoxFlags |= flag; |
| 632 | return AVIF_TRUE; |
| 633 | } |
| 634 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 635 | // --------------------------------------------------------------------------- |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 636 | // avifDecoderData |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 637 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 638 | typedef struct avifTile |
| 639 | { |
| 640 | avifCodecDecodeInput * input; |
| 641 | struct avifCodec * codec; |
| 642 | avifImage * image; |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 643 | uint32_t width; // Either avifTrack.width or avifDecoderItem.width |
| 644 | uint32_t height; // Either avifTrack.height or avifDecoderItem.height |
Joe Drago | e79bc37 | 2021-06-09 17:54:08 -0700 | [diff] [blame] | 645 | uint8_t operatingPoint; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 646 | } avifTile; |
| 647 | AVIF_ARRAY_DECLARE(avifTileArray, avifTile, tile); |
| 648 | |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 649 | // This holds one "meta" box (from the BMFF and HEIF standards) worth of relevant-to-AVIF information. |
| 650 | // * If a meta box is parsed from the root level of the BMFF, it can contain the information about |
| 651 | // "items" which might be color planes, alpha planes, or EXIF or XMP metadata. |
| 652 | // * If a meta box is parsed from inside of a track ("trak") box, any metadata (EXIF/XMP) items inside |
| 653 | // of that box are implicitly associated with that track. |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 654 | typedef struct avifMeta |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 655 | { |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 656 | // 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] | 657 | // (single image color planes, alpha planes, EXIF, XMP, etc). Each item has a unique integer ID >1, |
| 658 | // 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] | 659 | // * iloc - location: byte offset to item data, item size in bytes |
| 660 | // * iinf - information: type of item (color planes, alpha plane, EXIF, XMP) |
| 661 | // * ipco - properties: dimensions, aspect ratio, image transformations, references to other items |
| 662 | // * ipma - associations: Attaches an item in the properties list to a given item |
| 663 | // |
| 664 | // Items are lazily created in this array when any of the above boxes refer to one by a new (unseen) ID, |
| 665 | // 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] | 666 | avifDecoderItemArray items; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 667 | |
| 668 | // Any ipco boxes explained above are populated into this array as a staging area, which are |
| 669 | // then duplicated into the appropriate items upon encountering an item property association |
| 670 | // (ipma) box. |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 671 | avifPropertyArray properties; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 672 | |
Joe Drago | 1dea33e | 2021-09-14 17:12:39 -0700 | [diff] [blame] | 673 | // Filled with the contents of this meta box's "idat" box, which is raw data that an item can |
| 674 | // directly refer to in its item location box (iloc) instead of just giving an offset into the |
| 675 | // overall file. If all items' iloc boxes simply point at an offset/length in the file itself, |
| 676 | // this buffer will likely be empty. |
| 677 | avifRWData idat; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 678 | |
| 679 | // 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] | 680 | // 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] | 681 | // avifMeta struct, this value is incremented. Any time an additional meta box is detected at |
| 682 | // 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] | 683 | // "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] | 684 | // box. |
| 685 | uint32_t idatID; |
| 686 | |
| 687 | // Contents of a pitm box, which signal which of the items in this file is the main image. For |
| 688 | // AVIF, this should point at an av01 type item containing color planes, and all other items |
| 689 | // 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] | 690 | uint32_t primaryItemID; |
| 691 | } avifMeta; |
| 692 | |
Wan-Teh Chang | f732a4d | 2022-01-21 15:56:35 -0800 | [diff] [blame] | 693 | static void avifMetaDestroy(avifMeta * meta); |
| 694 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 695 | static avifMeta * avifMetaCreate() |
| 696 | { |
| 697 | avifMeta * meta = (avifMeta *)avifAlloc(sizeof(avifMeta)); |
| 698 | memset(meta, 0, sizeof(avifMeta)); |
Wan-Teh Chang | f732a4d | 2022-01-21 15:56:35 -0800 | [diff] [blame] | 699 | if (!avifArrayCreate(&meta->items, sizeof(avifDecoderItem), 8)) { |
| 700 | goto error; |
| 701 | } |
| 702 | if (!avifArrayCreate(&meta->properties, sizeof(avifProperty), 16)) { |
| 703 | goto error; |
| 704 | } |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 705 | return meta; |
Wan-Teh Chang | f732a4d | 2022-01-21 15:56:35 -0800 | [diff] [blame] | 706 | |
| 707 | error: |
| 708 | avifMetaDestroy(meta); |
| 709 | return NULL; |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | static void avifMetaDestroy(avifMeta * meta) |
| 713 | { |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 714 | for (uint32_t i = 0; i < meta->items.count; ++i) { |
| 715 | avifDecoderItem * item = &meta->items.item[i]; |
| 716 | avifArrayDestroy(&item->properties); |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 717 | avifArrayDestroy(&item->extents); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 718 | if (item->ownsMergedExtents) { |
| 719 | avifRWDataFree(&item->mergedExtents); |
| 720 | } |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 721 | } |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 722 | avifArrayDestroy(&meta->items); |
| 723 | avifArrayDestroy(&meta->properties); |
Joe Drago | 1dea33e | 2021-09-14 17:12:39 -0700 | [diff] [blame] | 724 | avifRWDataFree(&meta->idat); |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 725 | avifFree(meta); |
| 726 | } |
| 727 | |
| 728 | static avifDecoderItem * avifMetaFindItem(avifMeta * meta, uint32_t itemID) |
| 729 | { |
| 730 | if (itemID == 0) { |
| 731 | return NULL; |
| 732 | } |
| 733 | |
| 734 | for (uint32_t i = 0; i < meta->items.count; ++i) { |
| 735 | if (meta->items.item[i].id == itemID) { |
| 736 | return &meta->items.item[i]; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | avifDecoderItem * item = (avifDecoderItem *)avifArrayPushPtr(&meta->items); |
Wan-Teh Chang | f732a4d | 2022-01-21 15:56:35 -0800 | [diff] [blame] | 741 | if (!avifArrayCreate(&item->properties, sizeof(avifProperty), 16)) { |
| 742 | goto error; |
| 743 | } |
| 744 | if (!avifArrayCreate(&item->extents, sizeof(avifExtent), 1)) { |
| 745 | goto error; |
| 746 | } |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 747 | item->id = itemID; |
| 748 | item->meta = meta; |
| 749 | return item; |
Wan-Teh Chang | f732a4d | 2022-01-21 15:56:35 -0800 | [diff] [blame] | 750 | |
| 751 | error: |
| 752 | avifArrayDestroy(&item->extents); |
| 753 | avifArrayDestroy(&item->properties); |
| 754 | avifArrayPop(&meta->items); |
| 755 | return NULL; |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | typedef struct avifDecoderData |
| 759 | { |
Wan-Teh Chang | 6fc1758 | 2020-09-24 15:16:37 -0700 | [diff] [blame] | 760 | avifMeta * meta; // The root-level meta box |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 761 | avifTrackArray tracks; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 762 | avifTileArray tiles; |
| 763 | unsigned int colorTileCount; |
| 764 | unsigned int alphaTileCount; |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 765 | unsigned int decodedColorTileCount; |
| 766 | unsigned int decodedAlphaTileCount; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 767 | avifImageGrid colorGrid; |
| 768 | avifImageGrid alphaGrid; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 769 | avifDecoderSource source; |
Joe Drago | f131b78 | 2021-09-21 16:52:39 -0700 | [diff] [blame] | 770 | uint8_t majorBrand[4]; // From the file's ftyp, used by AVIF_DECODER_SOURCE_AUTO |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 771 | avifDiagnostics * diag; // Shallow copy; owned by avifDecoder |
Wan-Teh Chang | 306c306 | 2020-04-05 12:17:33 -0700 | [diff] [blame] | 772 | 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] | 773 | avifBool cicpSet; // True if avifDecoder's image has had its CICP set correctly yet. |
| 774 | // This allows nclx colr boxes to override AV1 CICP, as specified in the MIAF |
| 775 | // standard (ISO/IEC 23000-22:2019), section 7.3.6.4: |
| 776 | // |
| 777 | // "The colour information property takes precedence over any colour information in the image |
| 778 | // 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] | 779 | } avifDecoderData; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 780 | |
Wan-Teh Chang | f732a4d | 2022-01-21 15:56:35 -0800 | [diff] [blame] | 781 | static void avifDecoderDataDestroy(avifDecoderData * data); |
| 782 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 783 | static avifDecoderData * avifDecoderDataCreate() |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 784 | { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 785 | avifDecoderData * data = (avifDecoderData *)avifAlloc(sizeof(avifDecoderData)); |
| 786 | memset(data, 0, sizeof(avifDecoderData)); |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 787 | data->meta = avifMetaCreate(); |
Wan-Teh Chang | f732a4d | 2022-01-21 15:56:35 -0800 | [diff] [blame] | 788 | if (!avifArrayCreate(&data->tracks, sizeof(avifTrack), 2)) { |
| 789 | goto error; |
| 790 | } |
| 791 | if (!avifArrayCreate(&data->tiles, sizeof(avifTile), 8)) { |
| 792 | goto error; |
| 793 | } |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 794 | return data; |
Wan-Teh Chang | f732a4d | 2022-01-21 15:56:35 -0800 | [diff] [blame] | 795 | |
| 796 | error: |
| 797 | avifDecoderDataDestroy(data); |
| 798 | return NULL; |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 801 | static void avifDecoderDataResetCodec(avifDecoderData * data) |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 802 | { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 803 | for (unsigned int i = 0; i < data->tiles.count; ++i) { |
| 804 | avifTile * tile = &data->tiles.tile[i]; |
Joe Drago | 9d36878 | 2020-03-04 17:53:17 -0800 | [diff] [blame] | 805 | if (tile->image) { |
| 806 | avifImageFreePlanes(tile->image, AVIF_PLANES_ALL); // forget any pointers into codec image buffers |
| 807 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 808 | if (tile->codec) { |
| 809 | avifCodecDestroy(tile->codec); |
| 810 | tile->codec = NULL; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 811 | } |
| 812 | } |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 813 | data->decodedColorTileCount = 0; |
| 814 | data->decodedAlphaTileCount = 0; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Joe Drago | e79bc37 | 2021-06-09 17:54:08 -0700 | [diff] [blame] | 817 | static avifTile * avifDecoderDataCreateTile(avifDecoderData * data, uint32_t width, uint32_t height, uint8_t operatingPoint) |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 818 | { |
| 819 | avifTile * tile = (avifTile *)avifArrayPushPtr(&data->tiles); |
| 820 | tile->image = avifImageCreateEmpty(); |
Wan-Teh Chang | d891187 | 2022-02-14 14:15:00 -0800 | [diff] [blame] | 821 | if (!tile->image) { |
| 822 | goto error; |
| 823 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 824 | tile->input = avifCodecDecodeInputCreate(); |
Wan-Teh Chang | d891187 | 2022-02-14 14:15:00 -0800 | [diff] [blame] | 825 | if (!tile->input) { |
| 826 | goto error; |
| 827 | } |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 828 | tile->width = width; |
| 829 | tile->height = height; |
Joe Drago | e79bc37 | 2021-06-09 17:54:08 -0700 | [diff] [blame] | 830 | tile->operatingPoint = operatingPoint; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 831 | return tile; |
Wan-Teh Chang | d891187 | 2022-02-14 14:15:00 -0800 | [diff] [blame] | 832 | |
| 833 | error: |
| 834 | if (tile->input) { |
| 835 | avifCodecDecodeInputDestroy(tile->input); |
| 836 | } |
| 837 | if (tile->image) { |
| 838 | avifImageDestroy(tile->image); |
| 839 | } |
| 840 | avifArrayPop(&data->tiles); |
| 841 | return NULL; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 842 | } |
| 843 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 844 | static avifTrack * avifDecoderDataCreateTrack(avifDecoderData * data) |
| 845 | { |
| 846 | avifTrack * track = (avifTrack *)avifArrayPushPtr(&data->tracks); |
| 847 | track->meta = avifMetaCreate(); |
| 848 | return track; |
| 849 | } |
| 850 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 851 | static void avifDecoderDataClearTiles(avifDecoderData * data) |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 852 | { |
| 853 | for (unsigned int i = 0; i < data->tiles.count; ++i) { |
| 854 | avifTile * tile = &data->tiles.tile[i]; |
| 855 | if (tile->input) { |
| 856 | avifCodecDecodeInputDestroy(tile->input); |
| 857 | tile->input = NULL; |
| 858 | } |
| 859 | if (tile->codec) { |
| 860 | avifCodecDestroy(tile->codec); |
| 861 | tile->codec = NULL; |
| 862 | } |
| 863 | if (tile->image) { |
| 864 | avifImageDestroy(tile->image); |
| 865 | tile->image = NULL; |
| 866 | } |
| 867 | } |
| 868 | data->tiles.count = 0; |
| 869 | data->colorTileCount = 0; |
| 870 | data->alphaTileCount = 0; |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 871 | data->decodedColorTileCount = 0; |
| 872 | data->decodedAlphaTileCount = 0; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 873 | } |
| 874 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 875 | static void avifDecoderDataDestroy(avifDecoderData * data) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 876 | { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 877 | avifMetaDestroy(data->meta); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 878 | for (uint32_t i = 0; i < data->tracks.count; ++i) { |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 879 | avifTrack * track = &data->tracks.track[i]; |
| 880 | if (track->sampleTable) { |
| 881 | avifSampleTableDestroy(track->sampleTable); |
| 882 | } |
| 883 | if (track->meta) { |
| 884 | avifMetaDestroy(track->meta); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | avifArrayDestroy(&data->tracks); |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 888 | avifDecoderDataClearTiles(data); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 889 | avifArrayDestroy(&data->tiles); |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 890 | avifFree(data); |
| 891 | } |
| 892 | |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 893 | // This returns the max extent that has to be read in order to decode this item. If |
| 894 | // the item is stored in an idat, the data has already been read during Parse() and |
| 895 | // this function will return AVIF_RESULT_OK with a 0-byte extent. |
Joe Drago | c0758eb | 2021-06-23 17:52:47 -0700 | [diff] [blame] | 896 | static avifResult avifDecoderItemMaxExtent(const avifDecoderItem * item, const avifDecodeSample * sample, avifExtent * outExtent) |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 897 | { |
| 898 | if (item->extents.count == 0) { |
| 899 | return AVIF_RESULT_TRUNCATED_DATA; |
| 900 | } |
| 901 | |
Joe Drago | 1dea33e | 2021-09-14 17:12:39 -0700 | [diff] [blame] | 902 | if (item->idatStored) { |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 903 | // construction_method: idat(1) |
| 904 | |
Joe Drago | 1dea33e | 2021-09-14 17:12:39 -0700 | [diff] [blame] | 905 | if (item->meta->idat.size > 0) { |
| 906 | // Already read from a meta box during Parse() |
| 907 | memset(outExtent, 0, sizeof(avifExtent)); |
| 908 | return AVIF_RESULT_OK; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 909 | } |
| 910 | |
Wan-Teh Chang | 8027539 | 2020-11-17 12:35:11 -0800 | [diff] [blame] | 911 | // no associated idat box was found in the meta box, bail out |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 912 | return AVIF_RESULT_NO_CONTENT; |
| 913 | } |
| 914 | |
| 915 | // construction_method: file(0) |
| 916 | |
Joe Drago | c0758eb | 2021-06-23 17:52:47 -0700 | [diff] [blame] | 917 | if (sample->size == 0) { |
| 918 | return AVIF_RESULT_TRUNCATED_DATA; |
| 919 | } |
Joe Drago | 2dde71d | 2021-06-24 15:40:22 -0700 | [diff] [blame] | 920 | uint64_t remainingOffset = sample->offset; |
Joe Drago | c0758eb | 2021-06-23 17:52:47 -0700 | [diff] [blame] | 921 | size_t remainingBytes = sample->size; // This may be smaller than item->size if the item is progressive |
| 922 | |
Wan-Teh Chang | 1558452 | 2020-11-17 14:11:12 -0800 | [diff] [blame] | 923 | // Assert that the for loop below will execute at least one iteration. |
| 924 | assert(item->extents.count != 0); |
| 925 | uint64_t minOffset = UINT64_MAX; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 926 | uint64_t maxOffset = 0; |
| 927 | for (uint32_t extentIter = 0; extentIter < item->extents.count; ++extentIter) { |
| 928 | avifExtent * extent = &item->extents.extent[extentIter]; |
| 929 | |
Joe Drago | 2dde71d | 2021-06-24 15:40:22 -0700 | [diff] [blame] | 930 | // Make local copies of extent->offset and extent->size as they might need to be adjusted |
| 931 | // due to the sample's offset. |
| 932 | uint64_t startOffset = extent->offset; |
| 933 | size_t extentSize = extent->size; |
| 934 | if (remainingOffset) { |
| 935 | if (remainingOffset >= extentSize) { |
| 936 | remainingOffset -= extentSize; |
| 937 | continue; |
| 938 | } else { |
| 939 | if (remainingOffset > UINT64_MAX - startOffset) { |
| 940 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 941 | } |
| 942 | startOffset += remainingOffset; |
| 943 | extentSize -= remainingOffset; |
| 944 | remainingOffset = 0; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | const size_t usedExtentSize = (extentSize < remainingBytes) ? extentSize : remainingBytes; |
| 949 | |
| 950 | if (usedExtentSize > UINT64_MAX - startOffset) { |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 951 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 952 | } |
Joe Drago | 2dde71d | 2021-06-24 15:40:22 -0700 | [diff] [blame] | 953 | const uint64_t endOffset = startOffset + usedExtentSize; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 954 | |
Joe Drago | 2dde71d | 2021-06-24 15:40:22 -0700 | [diff] [blame] | 955 | if (minOffset > startOffset) { |
| 956 | minOffset = startOffset; |
Wan-Teh Chang | 1558452 | 2020-11-17 14:11:12 -0800 | [diff] [blame] | 957 | } |
| 958 | if (maxOffset < endOffset) { |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 959 | maxOffset = endOffset; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 960 | } |
Joe Drago | c0758eb | 2021-06-23 17:52:47 -0700 | [diff] [blame] | 961 | |
| 962 | remainingBytes -= usedExtentSize; |
| 963 | if (remainingBytes == 0) { |
| 964 | // We've got enough bytes for this sample. |
| 965 | break; |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | if (remainingBytes != 0) { |
| 970 | return AVIF_RESULT_TRUNCATED_DATA; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | outExtent->offset = minOffset; |
Wan-Teh Chang | d69958e | 2020-11-17 12:14:27 -0800 | [diff] [blame] | 974 | const uint64_t extentLength = maxOffset - minOffset; |
| 975 | if (extentLength > SIZE_MAX) { |
| 976 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 977 | } |
| 978 | outExtent->size = (size_t)extentLength; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 979 | return AVIF_RESULT_OK; |
| 980 | } |
| 981 | |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 982 | static uint8_t avifDecoderItemOperatingPoint(const avifDecoderItem * item) |
| 983 | { |
| 984 | const avifProperty * a1opProp = avifPropertyArrayFind(&item->properties, "a1op"); |
| 985 | if (a1opProp) { |
| 986 | return a1opProp->u.a1op.opIndex; |
| 987 | } |
| 988 | return 0; // default |
| 989 | } |
| 990 | |
Joe Drago | 9b942c2 | 2021-05-06 12:33:12 -0700 | [diff] [blame] | 991 | static avifResult avifDecoderItemValidateAV1(const avifDecoderItem * item, avifDiagnostics * diag, const avifStrictFlags strictFlags) |
Joe Drago | c10c6be | 2021-03-31 08:55:19 -0700 | [diff] [blame] | 992 | { |
| 993 | const avifProperty * av1CProp = avifPropertyArrayFind(&item->properties, "av1C"); |
| 994 | if (!av1CProp) { |
| 995 | // An av1C box is mandatory in all valid AVIF configurations. Bail out. |
Wan-Teh Chang | 455c9d6 | 2021-10-20 12:13:20 -0700 | [diff] [blame] | 996 | avifDiagnosticsPrintf(diag, "Item ID %u of type '%.4s' is missing mandatory av1C property", item->id, (const char *)item->type); |
Joe Drago | c10c6be | 2021-03-31 08:55:19 -0700 | [diff] [blame] | 997 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 998 | } |
Joe Drago | c10c6be | 2021-03-31 08:55:19 -0700 | [diff] [blame] | 999 | |
| 1000 | const avifProperty * pixiProp = avifPropertyArrayFind(&item->properties, "pixi"); |
Joe Drago | 9b942c2 | 2021-05-06 12:33:12 -0700 | [diff] [blame] | 1001 | if (!pixiProp && (strictFlags & AVIF_STRICT_PIXI_REQUIRED)) { |
Joe Drago | c10c6be | 2021-03-31 08:55:19 -0700 | [diff] [blame] | 1002 | // A pixi box is mandatory in all valid AVIF configurations. Bail out. |
Wan-Teh Chang | 455c9d6 | 2021-10-20 12:13:20 -0700 | [diff] [blame] | 1003 | avifDiagnosticsPrintf(diag, |
| 1004 | "[Strict] Item ID %u of type '%.4s' is missing mandatory pixi property", |
| 1005 | item->id, |
| 1006 | (const char *)item->type); |
Joe Drago | c10c6be | 2021-03-31 08:55:19 -0700 | [diff] [blame] | 1007 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 1008 | } |
| 1009 | |
Joe Drago | 9b942c2 | 2021-05-06 12:33:12 -0700 | [diff] [blame] | 1010 | if (pixiProp) { |
Joe Drago | 4a70b47 | 2021-05-06 17:21:33 -0700 | [diff] [blame] | 1011 | const uint32_t av1CDepth = avifCodecConfigurationBoxGetDepth(&av1CProp->u.av1C); |
Joe Drago | 9b942c2 | 2021-05-06 12:33:12 -0700 | [diff] [blame] | 1012 | for (uint8_t i = 0; i < pixiProp->u.pixi.planeCount; ++i) { |
| 1013 | if (pixiProp->u.pixi.planeDepths[i] != av1CDepth) { |
| 1014 | // pixi depth must match av1C depth |
| 1015 | avifDiagnosticsPrintf(diag, |
| 1016 | "Item ID %u depth specified by pixi property [%u] does not match av1C property depth [%u]", |
| 1017 | item->id, |
| 1018 | pixiProp->u.pixi.planeDepths[i], |
| 1019 | av1CDepth); |
| 1020 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 1021 | } |
Joe Drago | c10c6be | 2021-03-31 08:55:19 -0700 | [diff] [blame] | 1022 | } |
| 1023 | } |
Joe Drago | 4a70b47 | 2021-05-06 17:21:33 -0700 | [diff] [blame] | 1024 | |
| 1025 | if (strictFlags & AVIF_STRICT_CLAP_VALID) { |
| 1026 | const avifProperty * clapProp = avifPropertyArrayFind(&item->properties, "clap"); |
| 1027 | if (clapProp) { |
| 1028 | const avifProperty * ispeProp = avifPropertyArrayFind(&item->properties, "ispe"); |
| 1029 | if (!ispeProp) { |
Joe Drago | f5b98a6 | 2021-05-10 12:25:29 -0700 | [diff] [blame] | 1030 | avifDiagnosticsPrintf(diag, |
| 1031 | "[Strict] Item ID %u is missing an ispe property, so its clap property cannot be validated", |
| 1032 | item->id); |
Joe Drago | 4a70b47 | 2021-05-06 17:21:33 -0700 | [diff] [blame] | 1033 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 1034 | } |
| 1035 | |
| 1036 | avifCropRect cropRect; |
| 1037 | const uint32_t imageW = ispeProp->u.ispe.width; |
| 1038 | const uint32_t imageH = ispeProp->u.ispe.height; |
| 1039 | const avifPixelFormat av1CFormat = avifCodecConfigurationBoxGetFormat(&av1CProp->u.av1C); |
| 1040 | avifBool validClap = avifCropRectConvertCleanApertureBox(&cropRect, &clapProp->u.clap, imageW, imageH, av1CFormat, diag); |
| 1041 | if (!validClap) { |
| 1042 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 1043 | } |
| 1044 | } |
| 1045 | } |
Joe Drago | c10c6be | 2021-03-31 08:55:19 -0700 | [diff] [blame] | 1046 | return AVIF_RESULT_OK; |
| 1047 | } |
| 1048 | |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1049 | static avifResult avifDecoderItemRead(avifDecoderItem * item, |
| 1050 | avifIO * io, |
| 1051 | avifROData * outData, |
| 1052 | size_t offset, |
| 1053 | size_t partialByteCount, |
| 1054 | avifDiagnostics * diag) |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1055 | { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1056 | if (item->mergedExtents.data && !item->partialMergedExtents) { |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 1057 | // Multiple extents have already been concatenated for this item, just return it |
Joe Drago | 8df04be | 2021-06-14 14:22:18 -0700 | [diff] [blame] | 1058 | if (offset >= item->mergedExtents.size) { |
| 1059 | avifDiagnosticsPrintf(diag, "Item ID %u read has overflowing offset", item->id); |
| 1060 | return AVIF_RESULT_TRUNCATED_DATA; |
| 1061 | } |
| 1062 | outData->data = item->mergedExtents.data + offset; |
| 1063 | outData->size = item->mergedExtents.size - offset; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1064 | return AVIF_RESULT_OK; |
| 1065 | } |
| 1066 | |
| 1067 | if (item->extents.count == 0) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1068 | avifDiagnosticsPrintf(diag, "Item ID %u has zero extents", item->id); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1069 | return AVIF_RESULT_TRUNCATED_DATA; |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | // 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] | 1073 | const avifRWData * idatBuffer = NULL; |
Joe Drago | 1dea33e | 2021-09-14 17:12:39 -0700 | [diff] [blame] | 1074 | if (item->idatStored) { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1075 | // construction_method: idat(1) |
| 1076 | |
Joe Drago | 1dea33e | 2021-09-14 17:12:39 -0700 | [diff] [blame] | 1077 | if (item->meta->idat.size > 0) { |
| 1078 | idatBuffer = &item->meta->idat; |
| 1079 | } else { |
Wan-Teh Chang | 8027539 | 2020-11-17 12:35:11 -0800 | [diff] [blame] | 1080 | // no associated idat box was found in the meta box, bail out |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1081 | avifDiagnosticsPrintf(diag, "Item ID %u is stored in an idat, but no associated idat box was found", item->id); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1082 | return AVIF_RESULT_NO_CONTENT; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1086 | // Merge extents into a single contiguous buffer |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 1087 | if ((io->sizeHint > 0) && (item->size > io->sizeHint)) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1088 | // Sanity check: somehow the sum of extents exceeds the entire file or idat size! |
| 1089 | avifDiagnosticsPrintf(diag, "Item ID %u reported size failed size hint sanity check. Truncated data?", item->id); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1090 | return AVIF_RESULT_TRUNCATED_DATA; |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 1091 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1092 | |
Wan-Teh Chang | 905296e | 2021-07-13 16:55:45 -0700 | [diff] [blame] | 1093 | if (offset >= item->size) { |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1094 | avifDiagnosticsPrintf(diag, "Item ID %u read has overflowing offset", item->id); |
| 1095 | return AVIF_RESULT_TRUNCATED_DATA; |
| 1096 | } |
Wan-Teh Chang | 905296e | 2021-07-13 16:55:45 -0700 | [diff] [blame] | 1097 | const size_t maxOutputSize = item->size - offset; |
| 1098 | const size_t readOutputSize = (partialByteCount && (partialByteCount < maxOutputSize)) ? partialByteCount : maxOutputSize; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1099 | const size_t totalBytesToRead = offset + readOutputSize; |
Wan-Teh Chang | 610b0a1 | 2020-10-06 16:47:21 -0700 | [diff] [blame] | 1100 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1101 | // If there is a single extent for this item and the source of the read buffer is going to be |
| 1102 | // persistent for the lifetime of the avifDecoder (whether it comes from its own internal |
| 1103 | // idatBuffer or from a known-persistent IO), we can avoid buffer duplication and just use the |
| 1104 | // preexisting buffer. |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 1105 | avifBool singlePersistentBuffer = ((item->extents.count == 1) && (idatBuffer || io->persistent)); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1106 | if (!singlePersistentBuffer) { |
Joe Drago | cb2660e | 2021-08-19 02:24:31 -0700 | [diff] [blame] | 1107 | // Always allocate the item's full size here, as progressive image decodes will do partial |
| 1108 | // reads into this buffer and begin feeding the buffer to the underlying AV1 decoder, but |
| 1109 | // will then write more into this buffer without flushing the AV1 decoder (which is still |
| 1110 | // holding the address of the previous allocation of this buffer). This strategy avoids |
| 1111 | // use-after-free issues in the AV1 decoder and unnecessary reallocs as a typical |
| 1112 | // progressive decode use case will eventually decode the final layer anyway. |
| 1113 | avifRWDataRealloc(&item->mergedExtents, item->size); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1114 | item->ownsMergedExtents = AVIF_TRUE; |
| 1115 | } |
| 1116 | |
| 1117 | // Set this until we manage to fill the entire mergedExtents buffer |
| 1118 | item->partialMergedExtents = AVIF_TRUE; |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 1119 | |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 1120 | uint8_t * front = item->mergedExtents.data; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1121 | size_t remainingBytes = totalBytesToRead; |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 1122 | for (uint32_t extentIter = 0; extentIter < item->extents.count; ++extentIter) { |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 1123 | avifExtent * extent = &item->extents.extent[extentIter]; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1124 | |
| 1125 | size_t bytesToRead = extent->size; |
| 1126 | if (bytesToRead > remainingBytes) { |
| 1127 | bytesToRead = remainingBytes; |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1130 | avifROData offsetBuffer; |
| 1131 | if (idatBuffer) { |
Wan-Teh Chang | 610b0a1 | 2020-10-06 16:47:21 -0700 | [diff] [blame] | 1132 | if (extent->offset > idatBuffer->size) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1133 | avifDiagnosticsPrintf(diag, "Item ID %u has impossible extent offset in idat buffer", item->id); |
Wan-Teh Chang | 610b0a1 | 2020-10-06 16:47:21 -0700 | [diff] [blame] | 1134 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 1135 | } |
Wan-Teh Chang | a17693d | 2021-06-02 14:57:18 -0700 | [diff] [blame] | 1136 | // Since extent->offset (a uint64_t) is not bigger than idatBuffer->size (a size_t), |
| 1137 | // it is safe to cast extent->offset to size_t. |
| 1138 | const size_t extentOffset = (size_t)extent->offset; |
| 1139 | if (extent->size > idatBuffer->size - extentOffset) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1140 | avifDiagnosticsPrintf(diag, "Item ID %u has impossible extent size in idat buffer", item->id); |
Wan-Teh Chang | 610b0a1 | 2020-10-06 16:47:21 -0700 | [diff] [blame] | 1141 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 1142 | } |
Wan-Teh Chang | a17693d | 2021-06-02 14:57:18 -0700 | [diff] [blame] | 1143 | offsetBuffer.data = idatBuffer->data + extentOffset; |
| 1144 | offsetBuffer.size = idatBuffer->size - extentOffset; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1145 | } else { |
| 1146 | // construction_method: file(0) |
| 1147 | |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 1148 | if ((io->sizeHint > 0) && (extent->offset > io->sizeHint)) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1149 | avifDiagnosticsPrintf(diag, "Item ID %u extent offset failed size hint sanity check. Truncated data?", item->id); |
Wan-Teh Chang | b856dc2 | 2020-09-28 13:00:56 -0700 | [diff] [blame] | 1150 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 1151 | } |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 1152 | avifResult readResult = io->read(io, 0, extent->offset, bytesToRead, &offsetBuffer); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1153 | if (readResult != AVIF_RESULT_OK) { |
| 1154 | return readResult; |
| 1155 | } |
Wan-Teh Chang | 3c304dc | 2020-10-08 12:07:45 -0700 | [diff] [blame] | 1156 | if (bytesToRead != offsetBuffer.size) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1157 | avifDiagnosticsPrintf(diag, |
| 1158 | "Item ID %u tried to read %zu bytes, but only received %zu bytes", |
Joe Drago | 4886729 | 2021-05-04 13:23:08 -0700 | [diff] [blame] | 1159 | item->id, |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1160 | bytesToRead, |
| 1161 | offsetBuffer.size); |
Wan-Teh Chang | 3c304dc | 2020-10-08 12:07:45 -0700 | [diff] [blame] | 1162 | return AVIF_RESULT_TRUNCATED_DATA; |
| 1163 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | if (singlePersistentBuffer) { |
| 1167 | memcpy(&item->mergedExtents, &offsetBuffer, sizeof(avifRWData)); |
| 1168 | item->mergedExtents.size = bytesToRead; |
| 1169 | } else { |
Joe Drago | db1009a | 2021-03-16 15:26:14 -0700 | [diff] [blame] | 1170 | assert(item->ownsMergedExtents); |
| 1171 | assert(front); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1172 | memcpy(front, offsetBuffer.data, bytesToRead); |
| 1173 | front += bytesToRead; |
| 1174 | } |
| 1175 | |
| 1176 | remainingBytes -= bytesToRead; |
| 1177 | if (remainingBytes == 0) { |
| 1178 | // This happens when partialByteCount is set |
| 1179 | break; |
| 1180 | } |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 1181 | } |
| 1182 | if (remainingBytes != 0) { |
| 1183 | // This should be impossible? |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1184 | avifDiagnosticsPrintf(diag, "Item ID %u has %zu unexpected trailing bytes", item->id, remainingBytes); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1185 | return AVIF_RESULT_TRUNCATED_DATA; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1186 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1187 | |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1188 | outData->data = item->mergedExtents.data + offset; |
| 1189 | outData->size = readOutputSize; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1190 | item->partialMergedExtents = (item->size != totalBytesToRead); |
| 1191 | return AVIF_RESULT_OK; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1194 | static avifBool avifDecoderGenerateImageGridTiles(avifDecoder * decoder, avifImageGrid * grid, avifDecoderItem * gridItem, avifBool alpha) |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1195 | { |
Wan-Teh Chang | 7ca3dd9 | 2020-11-20 12:50:44 -0800 | [diff] [blame] | 1196 | unsigned int tilesRequested = grid->rows * grid->columns; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1197 | |
| 1198 | // Count number of dimg for this item, bail out if it doesn't match perfectly |
| 1199 | unsigned int tilesAvailable = 0; |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1200 | for (uint32_t i = 0; i < gridItem->meta->items.count; ++i) { |
| 1201 | avifDecoderItem * item = &gridItem->meta->items.item[i]; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1202 | if (item->dimgForID == gridItem->id) { |
| 1203 | if (memcmp(item->type, "av01", 4)) { |
| 1204 | continue; |
| 1205 | } |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 1206 | if (item->hasUnsupportedEssentialProperty) { |
Wan-Teh Chang | 29aaade | 2020-08-10 16:14:16 -0700 | [diff] [blame] | 1207 | // An essential property isn't supported by libavif; can't |
| 1208 | // decode a grid image if any tile in the grid isn't supported. |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1209 | avifDiagnosticsPrintf(&decoder->diag, "Grid image contains tile with an unsupported property marked as essential"); |
Wan-Teh Chang | 29aaade | 2020-08-10 16:14:16 -0700 | [diff] [blame] | 1210 | return AVIF_FALSE; |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 1211 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1212 | |
| 1213 | ++tilesAvailable; |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | if (tilesRequested != tilesAvailable) { |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1218 | avifDiagnosticsPrintf(&decoder->diag, |
Wan-Teh Chang | f01e225 | 2021-06-10 17:40:21 -0700 | [diff] [blame] | 1219 | "Grid image of dimensions %ux%u requires %u tiles, and only %u were found", |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1220 | grid->columns, |
| 1221 | grid->rows, |
| 1222 | tilesRequested, |
| 1223 | tilesAvailable); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1224 | return AVIF_FALSE; |
| 1225 | } |
| 1226 | |
Joe Drago | 9c5f565 | 2020-06-29 15:13:44 -0700 | [diff] [blame] | 1227 | avifBool firstTile = AVIF_TRUE; |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1228 | for (uint32_t i = 0; i < gridItem->meta->items.count; ++i) { |
| 1229 | avifDecoderItem * item = &gridItem->meta->items.item[i]; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1230 | if (item->dimgForID == gridItem->id) { |
| 1231 | if (memcmp(item->type, "av01", 4)) { |
| 1232 | continue; |
| 1233 | } |
| 1234 | |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 1235 | avifTile * tile = avifDecoderDataCreateTile(decoder->data, item->width, item->height, avifDecoderItemOperatingPoint(item)); |
Wan-Teh Chang | d891187 | 2022-02-14 14:15:00 -0800 | [diff] [blame] | 1236 | if (!tile) { |
| 1237 | return AVIF_FALSE; |
| 1238 | } |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1239 | if (!avifCodecDecodeInputFillFromDecoderItem(tile->input, |
| 1240 | item, |
| 1241 | decoder->allowProgressive, |
| 1242 | decoder->imageCountLimit, |
| 1243 | decoder->io->sizeHint, |
| 1244 | &decoder->diag)) { |
| 1245 | return AVIF_FALSE; |
| 1246 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1247 | tile->input->alpha = alpha; |
Joe Drago | 9c5f565 | 2020-06-29 15:13:44 -0700 | [diff] [blame] | 1248 | |
| 1249 | if (firstTile) { |
| 1250 | firstTile = AVIF_FALSE; |
| 1251 | |
| 1252 | // Adopt the av1C property of the first av01 tile, so that it can be queried from |
| 1253 | // the top-level color/alpha item during avifDecoderReset(). |
| 1254 | const avifProperty * srcProp = avifPropertyArrayFind(&item->properties, "av1C"); |
| 1255 | if (!srcProp) { |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1256 | avifDiagnosticsPrintf(&decoder->diag, "Grid image's first tile is missing an av1C property"); |
Joe Drago | 9c5f565 | 2020-06-29 15:13:44 -0700 | [diff] [blame] | 1257 | return AVIF_FALSE; |
| 1258 | } |
| 1259 | avifProperty * dstProp = (avifProperty *)avifArrayPushPtr(&gridItem->properties); |
Wan-Teh Chang | 2f225b0 | 2022-03-16 22:30:38 -0700 | [diff] [blame] | 1260 | *dstProp = *srcProp; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1261 | |
Wan-Teh Chang | 3461524 | 2021-07-13 17:26:31 -0700 | [diff] [blame] | 1262 | if (!alpha && item->progressive) { |
| 1263 | decoder->progressiveState = AVIF_PROGRESSIVE_STATE_AVAILABLE; |
| 1264 | if (tile->input->samples.count > 1) { |
| 1265 | decoder->progressiveState = AVIF_PROGRESSIVE_STATE_ACTIVE; |
| 1266 | decoder->imageCount = tile->input->samples.count; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1267 | } |
| 1268 | } |
Joe Drago | 9c5f565 | 2020-06-29 15:13:44 -0700 | [diff] [blame] | 1269 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | return AVIF_TRUE; |
| 1273 | } |
| 1274 | |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 1275 | // Checks the grid consistency and copies the pixels from the tiles to the |
| 1276 | // dstImage. Only the freshly decoded tiles are considered, skipping the already |
| 1277 | // copied or not-yet-decoded tiles. |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 1278 | static avifBool avifDecoderDataFillImageGrid(avifDecoderData * data, |
| 1279 | avifImageGrid * grid, |
| 1280 | avifImage * dstImage, |
| 1281 | unsigned int firstTileIndex, |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 1282 | unsigned int oldDecodedTileCount, |
| 1283 | unsigned int decodedTileCount, |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 1284 | avifBool alpha) |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1285 | { |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 1286 | assert(decodedTileCount > oldDecodedTileCount); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1287 | |
| 1288 | avifTile * firstTile = &data->tiles.tile[firstTileIndex]; |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1289 | 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] | 1290 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1291 | // Check for tile consistency: All tiles in a grid image should match in the properties checked below. |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 1292 | for (unsigned int i = AVIF_MAX(1, oldDecodedTileCount); i < decodedTileCount; ++i) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1293 | avifTile * tile = &data->tiles.tile[firstTileIndex + i]; |
Joe Drago | 951a002 | 2020-03-09 16:19:44 -0700 | [diff] [blame] | 1294 | 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] | 1295 | if ((tile->image->width != firstTile->image->width) || (tile->image->height != firstTile->image->height) || |
| 1296 | (tile->image->depth != firstTile->image->depth) || (tile->image->yuvFormat != firstTile->image->yuvFormat) || |
| 1297 | (tile->image->yuvRange != firstTile->image->yuvRange) || (uvPresent != firstTileUVPresent) || |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame] | 1298 | (tile->image->colorPrimaries != firstTile->image->colorPrimaries) || |
| 1299 | (tile->image->transferCharacteristics != firstTile->image->transferCharacteristics) || |
Wan-Teh Chang | 5bdead5 | 2020-08-12 17:02:22 -0700 | [diff] [blame] | 1300 | (tile->image->matrixCoefficients != firstTile->image->matrixCoefficients) || |
| 1301 | (tile->image->alphaRange != firstTile->image->alphaRange)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1302 | avifDiagnosticsPrintf(data->diag, "Grid image contains mismatched tiles"); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1303 | return AVIF_FALSE; |
| 1304 | } |
| 1305 | } |
| 1306 | |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 1307 | // Validate grid image size and tile size. |
| 1308 | // |
| 1309 | // HEIF (ISO/IEC 23008-12:2017), Section 6.6.2.3.1: |
TYTY | b587c59 | 2020-12-08 17:42:18 +0800 | [diff] [blame] | 1310 | // The tiled input images shall completely "cover" the reconstructed image grid canvas, ... |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame] | 1311 | if (((firstTile->image->width * grid->columns) < grid->outputWidth) || |
| 1312 | ((firstTile->image->height * grid->rows) < grid->outputHeight)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1313 | avifDiagnosticsPrintf(data->diag, |
| 1314 | "Grid image tiles do not completely cover the image (HEIF (ISO/IEC 23008-12:2017), Section 6.6.2.3.1)"); |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 1315 | return AVIF_FALSE; |
| 1316 | } |
| 1317 | // 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] | 1318 | if (((firstTile->image->width * (grid->columns - 1)) >= grid->outputWidth) || |
| 1319 | ((firstTile->image->height * (grid->rows - 1)) >= grid->outputHeight)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1320 | avifDiagnosticsPrintf(data->diag, |
| 1321 | "Grid image tiles in the rightmost column and bottommost row do not overlap the reconstructed image grid canvas. See MIAF (ISO/IEC 23000-22:2019), Section 7.3.11.4.2, Figure 2"); |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 1322 | return AVIF_FALSE; |
| 1323 | } |
Yannis Guyon | bf28a92 | 2022-02-09 23:19:32 +0100 | [diff] [blame] | 1324 | |
| 1325 | if (alpha) { |
Yannis Guyon | 7a6d13b | 2022-02-22 20:15:07 +0000 | [diff] [blame] | 1326 | // An alpha tile does not contain any YUV pixels. |
| 1327 | assert(firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_NONE); |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 1328 | } |
Yannis Guyon | bf28a92 | 2022-02-09 23:19:32 +0100 | [diff] [blame] | 1329 | if (!avifAreGridDimensionsValid(firstTile->image->yuvFormat, |
| 1330 | grid->outputWidth, |
| 1331 | grid->outputHeight, |
| 1332 | firstTile->image->width, |
| 1333 | firstTile->image->height, |
| 1334 | data->diag)) { |
| 1335 | return AVIF_FALSE; |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1338 | // Lazily populate dstImage with the new frame's properties. If we're decoding alpha, |
| 1339 | // these values must already match. |
| 1340 | if ((dstImage->width != grid->outputWidth) || (dstImage->height != grid->outputHeight) || |
Wan-Teh Chang | 5bdead5 | 2020-08-12 17:02:22 -0700 | [diff] [blame] | 1341 | (dstImage->depth != firstTile->image->depth) || (!alpha && (dstImage->yuvFormat != firstTile->image->yuvFormat))) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1342 | if (alpha) { |
| 1343 | // Alpha doesn't match size, just bail out |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1344 | avifDiagnosticsPrintf(data->diag, "Alpha plane dimensions do not match color plane dimensions"); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1345 | return AVIF_FALSE; |
| 1346 | } |
| 1347 | |
| 1348 | avifImageFreePlanes(dstImage, AVIF_PLANES_ALL); |
| 1349 | dstImage->width = grid->outputWidth; |
| 1350 | dstImage->height = grid->outputHeight; |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1351 | dstImage->depth = firstTile->image->depth; |
| 1352 | dstImage->yuvFormat = firstTile->image->yuvFormat; |
Joe Drago | c00d583 | 2020-08-13 16:03:28 -0700 | [diff] [blame] | 1353 | dstImage->yuvRange = firstTile->image->yuvRange; |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1354 | if (!data->cicpSet) { |
| 1355 | data->cicpSet = AVIF_TRUE; |
Joe Drago | c00d583 | 2020-08-13 16:03:28 -0700 | [diff] [blame] | 1356 | dstImage->colorPrimaries = firstTile->image->colorPrimaries; |
| 1357 | dstImage->transferCharacteristics = firstTile->image->transferCharacteristics; |
| 1358 | dstImage->matrixCoefficients = firstTile->image->matrixCoefficients; |
psi / Ryo Hirafuji | 922d8a1 | 2020-03-10 03:24:57 +0900 | [diff] [blame] | 1359 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1360 | } |
Joe Drago | d0eeb18 | 2020-05-18 17:23:48 -0700 | [diff] [blame] | 1361 | if (alpha) { |
| 1362 | dstImage->alphaRange = firstTile->image->alphaRange; |
| 1363 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1364 | |
| 1365 | avifImageAllocatePlanes(dstImage, alpha ? AVIF_PLANES_A : AVIF_PLANES_YUV); |
| 1366 | |
| 1367 | avifPixelFormatInfo formatInfo; |
Joe Drago | 7a249f5 | 2020-08-13 12:58:03 -0700 | [diff] [blame] | 1368 | avifGetPixelFormatInfo(firstTile->image->yuvFormat, &formatInfo); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1369 | |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 1370 | unsigned int tileIndex = oldDecodedTileCount; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1371 | size_t pixelBytes = avifImageUsesU16(dstImage) ? 2 : 1; |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 1372 | unsigned int rowIndex = oldDecodedTileCount / grid->columns; |
| 1373 | unsigned int colIndex = oldDecodedTileCount % grid->columns; |
| 1374 | // Only the first iteration of the outer for loop uses this initial value of colIndex. |
| 1375 | // Subsequent iterations of the outer for loop initializes colIndex to 0. |
| 1376 | for (; rowIndex < grid->rows; ++rowIndex, colIndex = 0) { |
| 1377 | for (; colIndex < grid->columns; ++colIndex, ++tileIndex) { |
| 1378 | if (tileIndex >= decodedTileCount) { |
| 1379 | // Tile is not ready yet. |
| 1380 | return AVIF_TRUE; |
| 1381 | } |
| 1382 | avifTile * tile = &data->tiles.tile[firstTileIndex + tileIndex]; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1383 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1384 | unsigned int widthToCopy = firstTile->image->width; |
| 1385 | unsigned int maxX = firstTile->image->width * (colIndex + 1); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1386 | if (maxX > grid->outputWidth) { |
| 1387 | widthToCopy -= maxX - grid->outputWidth; |
| 1388 | } |
| 1389 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1390 | unsigned int heightToCopy = firstTile->image->height; |
| 1391 | unsigned int maxY = firstTile->image->height * (rowIndex + 1); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1392 | if (maxY > grid->outputHeight) { |
| 1393 | heightToCopy -= maxY - grid->outputHeight; |
| 1394 | } |
| 1395 | |
| 1396 | // Y and A channels |
Joe Drago | db1009a | 2021-03-16 15:26:14 -0700 | [diff] [blame] | 1397 | size_t yaColOffset = (size_t)colIndex * firstTile->image->width; |
| 1398 | size_t yaRowOffset = (size_t)rowIndex * firstTile->image->height; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1399 | size_t yaRowBytes = widthToCopy * pixelBytes; |
| 1400 | |
| 1401 | if (alpha) { |
| 1402 | // A |
| 1403 | for (unsigned int j = 0; j < heightToCopy; ++j) { |
| 1404 | uint8_t * src = &tile->image->alphaPlane[j * tile->image->alphaRowBytes]; |
| 1405 | uint8_t * dst = &dstImage->alphaPlane[(yaColOffset * pixelBytes) + ((yaRowOffset + j) * dstImage->alphaRowBytes)]; |
| 1406 | memcpy(dst, src, yaRowBytes); |
| 1407 | } |
| 1408 | } else { |
| 1409 | // Y |
| 1410 | for (unsigned int j = 0; j < heightToCopy; ++j) { |
| 1411 | uint8_t * src = &tile->image->yuvPlanes[AVIF_CHAN_Y][j * tile->image->yuvRowBytes[AVIF_CHAN_Y]]; |
| 1412 | uint8_t * dst = |
| 1413 | &dstImage->yuvPlanes[AVIF_CHAN_Y][(yaColOffset * pixelBytes) + ((yaRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_Y])]; |
| 1414 | memcpy(dst, src, yaRowBytes); |
| 1415 | } |
| 1416 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1417 | if (!firstTileUVPresent) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1418 | continue; |
| 1419 | } |
| 1420 | |
| 1421 | // UV |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1422 | heightToCopy >>= formatInfo.chromaShiftY; |
| 1423 | size_t uvColOffset = yaColOffset >> formatInfo.chromaShiftX; |
| 1424 | size_t uvRowOffset = yaRowOffset >> formatInfo.chromaShiftY; |
| 1425 | size_t uvRowBytes = yaRowBytes >> formatInfo.chromaShiftX; |
| 1426 | for (unsigned int j = 0; j < heightToCopy; ++j) { |
| 1427 | uint8_t * srcU = &tile->image->yuvPlanes[AVIF_CHAN_U][j * tile->image->yuvRowBytes[AVIF_CHAN_U]]; |
| 1428 | uint8_t * dstU = |
| 1429 | &dstImage->yuvPlanes[AVIF_CHAN_U][(uvColOffset * pixelBytes) + ((uvRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_U])]; |
| 1430 | memcpy(dstU, srcU, uvRowBytes); |
| 1431 | |
| 1432 | uint8_t * srcV = &tile->image->yuvPlanes[AVIF_CHAN_V][j * tile->image->yuvRowBytes[AVIF_CHAN_V]]; |
| 1433 | uint8_t * dstV = |
| 1434 | &dstImage->yuvPlanes[AVIF_CHAN_V][(uvColOffset * pixelBytes) + ((uvRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_V])]; |
| 1435 | memcpy(dstV, srcV, uvRowBytes); |
| 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | return AVIF_TRUE; |
| 1442 | } |
| 1443 | |
Joe Drago | 39267fd | 2020-06-19 15:21:14 -0700 | [diff] [blame] | 1444 | // If colorId == 0 (a sentinel value as item IDs must be nonzero), accept any found EXIF/XMP metadata. Passing in 0 |
| 1445 | // 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] | 1446 | // inside of a trak box are implicitly associated to the track. |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1447 | static avifResult avifDecoderFindMetadata(avifDecoder * decoder, avifMeta * meta, avifImage * image, uint32_t colorId) |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1448 | { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1449 | if (decoder->ignoreExif && decoder->ignoreXMP) { |
| 1450 | // Nothing to do! |
| 1451 | return AVIF_RESULT_OK; |
| 1452 | } |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1453 | |
| 1454 | for (uint32_t itemIndex = 0; itemIndex < meta->items.count; ++itemIndex) { |
| 1455 | avifDecoderItem * item = &meta->items.item[itemIndex]; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 1456 | if (!item->size) { |
| 1457 | continue; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1458 | } |
| 1459 | if (item->hasUnsupportedEssentialProperty) { |
| 1460 | // An essential property isn't supported by libavif; ignore the item. |
| 1461 | continue; |
| 1462 | } |
| 1463 | |
| 1464 | if ((colorId > 0) && (item->descForID != colorId)) { |
| 1465 | // Not a content description (metadata) for the colorOBU, skip it |
| 1466 | continue; |
| 1467 | } |
| 1468 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1469 | if (!decoder->ignoreExif && !memcmp(item->type, "Exif", 4)) { |
| 1470 | avifROData exifContents; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1471 | avifResult readResult = avifDecoderItemRead(item, decoder->io, &exifContents, 0, 0, &decoder->diag); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1472 | if (readResult != AVIF_RESULT_OK) { |
| 1473 | return readResult; |
| 1474 | } |
| 1475 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1476 | // Advance past Annex A.2.1's header |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1477 | BEGIN_STREAM(exifBoxStream, exifContents.data, exifContents.size, &decoder->diag, "Exif header"); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1478 | uint32_t exifTiffHeaderOffset; |
Wan-Teh Chang | f1d6019 | 2020-10-07 16:23:08 -0700 | [diff] [blame] | 1479 | 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] | 1480 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1481 | avifImageSetMetadataExif(image, avifROStreamCurrent(&exifBoxStream), avifROStreamRemainingBytes(&exifBoxStream)); |
| 1482 | } else if (!decoder->ignoreXMP && !memcmp(item->type, "mime", 4) && |
| 1483 | !memcmp(item->contentType.contentType, xmpContentType, xmpContentTypeSize)) { |
| 1484 | avifROData xmpContents; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1485 | avifResult readResult = avifDecoderItemRead(item, decoder->io, &xmpContents, 0, 0, &decoder->diag); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1486 | if (readResult != AVIF_RESULT_OK) { |
| 1487 | return readResult; |
| 1488 | } |
| 1489 | |
| 1490 | avifImageSetMetadataXMP(image, xmpContents.data, xmpContents.size); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1491 | } |
| 1492 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 1493 | return AVIF_RESULT_OK; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1496 | // --------------------------------------------------------------------------- |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1497 | // URN |
| 1498 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1499 | static avifBool isAlphaURN(const char * urn) |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1500 | { |
wantehchang | 3fde0d0 | 2020-03-10 23:58:32 -0700 | [diff] [blame] | 1501 | return !strcmp(urn, URN_ALPHA0) || !strcmp(urn, URN_ALPHA1); |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | // --------------------------------------------------------------------------- |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1505 | // BMFF Parsing |
| 1506 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1507 | static avifBool avifParseHandlerBox(const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | e018518 | 2021-03-31 08:14:51 -0700 | [diff] [blame] | 1508 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1509 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[hdlr]"); |
Joe Drago | e018518 | 2021-03-31 08:14:51 -0700 | [diff] [blame] | 1510 | |
| 1511 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
| 1512 | |
| 1513 | uint32_t predefined; |
| 1514 | CHECK(avifROStreamReadU32(&s, &predefined)); // unsigned int(32) pre_defined = 0; |
| 1515 | if (predefined != 0) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1516 | avifDiagnosticsPrintf(diag, "Box[hdlr] contains a pre_defined value that is nonzero"); |
Joe Drago | e018518 | 2021-03-31 08:14:51 -0700 | [diff] [blame] | 1517 | return AVIF_FALSE; |
| 1518 | } |
| 1519 | |
| 1520 | uint8_t handlerType[4]; |
| 1521 | CHECK(avifROStreamRead(&s, handlerType, 4)); // unsigned int(32) handler_type; |
| 1522 | if (memcmp(handlerType, "pict", 4) != 0) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1523 | avifDiagnosticsPrintf(diag, "Box[hdlr] handler_type is not 'pict'"); |
Joe Drago | e018518 | 2021-03-31 08:14:51 -0700 | [diff] [blame] | 1524 | return AVIF_FALSE; |
| 1525 | } |
| 1526 | |
| 1527 | for (int i = 0; i < 3; ++i) { |
| 1528 | uint32_t reserved; |
| 1529 | CHECK(avifROStreamReadU32(&s, &reserved)); // const unsigned int(32)[3] reserved = 0; |
| 1530 | } |
| 1531 | |
| 1532 | // Verify that a valid string is here, but don't bother to store it |
| 1533 | CHECK(avifROStreamReadString(&s, NULL, 0)); // string name; |
| 1534 | return AVIF_TRUE; |
| 1535 | } |
| 1536 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1537 | static avifBool avifParseItemLocationBox(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1538 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1539 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[iloc]"); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1540 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1541 | uint8_t version; |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 1542 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1543 | if (version > 2) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1544 | avifDiagnosticsPrintf(diag, "Box[iloc] has an unsupported version [%u]", version); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1545 | return AVIF_FALSE; |
| 1546 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1547 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1548 | uint8_t offsetSizeAndLengthSize; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1549 | CHECK(avifROStreamRead(&s, &offsetSizeAndLengthSize, 1)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1550 | uint8_t offsetSize = (offsetSizeAndLengthSize >> 4) & 0xf; // unsigned int(4) offset_size; |
| 1551 | uint8_t lengthSize = (offsetSizeAndLengthSize >> 0) & 0xf; // unsigned int(4) length_size; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1552 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1553 | uint8_t baseOffsetSizeAndIndexSize; |
| 1554 | CHECK(avifROStreamRead(&s, &baseOffsetSizeAndIndexSize, 1)); |
| 1555 | uint8_t baseOffsetSize = (baseOffsetSizeAndIndexSize >> 4) & 0xf; // unsigned int(4) base_offset_size; |
| 1556 | uint8_t indexSize = 0; |
| 1557 | if ((version == 1) || (version == 2)) { |
| 1558 | indexSize = baseOffsetSizeAndIndexSize & 0xf; // unsigned int(4) index_size; |
| 1559 | if (indexSize != 0) { |
| 1560 | // extent_index unsupported |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1561 | avifDiagnosticsPrintf(diag, "Box[iloc] has an unsupported extent_index"); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1562 | return AVIF_FALSE; |
| 1563 | } |
| 1564 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1565 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1566 | uint16_t tmp16; |
| 1567 | uint32_t itemCount; |
| 1568 | if (version < 2) { |
| 1569 | CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_count; |
| 1570 | itemCount = tmp16; |
| 1571 | } else { |
| 1572 | CHECK(avifROStreamReadU32(&s, &itemCount)); // unsigned int(32) item_count; |
| 1573 | } |
| 1574 | for (uint32_t i = 0; i < itemCount; ++i) { |
| 1575 | uint32_t itemID; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1576 | if (version < 2) { |
| 1577 | CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID; |
| 1578 | itemID = tmp16; |
| 1579 | } else { |
| 1580 | CHECK(avifROStreamReadU32(&s, &itemID)); // unsigned int(32) item_ID; |
| 1581 | } |
| 1582 | |
Joe Drago | 1dea33e | 2021-09-14 17:12:39 -0700 | [diff] [blame] | 1583 | avifDecoderItem * item = avifMetaFindItem(meta, itemID); |
| 1584 | if (!item) { |
| 1585 | avifDiagnosticsPrintf(diag, "Box[iloc] has an invalid item ID [%u]", itemID); |
| 1586 | return AVIF_FALSE; |
| 1587 | } |
| 1588 | if (item->extents.count > 0) { |
| 1589 | // This item has already been given extents via this iloc box. This is invalid. |
| 1590 | avifDiagnosticsPrintf(diag, "Item ID [%u] contains duplicate sets of extents", itemID); |
| 1591 | return AVIF_FALSE; |
| 1592 | } |
| 1593 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1594 | if ((version == 1) || (version == 2)) { |
| 1595 | uint8_t ignored; |
| 1596 | uint8_t constructionMethod; |
| 1597 | CHECK(avifROStreamRead(&s, &ignored, 1)); // unsigned int(12) reserved = 0; |
| 1598 | CHECK(avifROStreamRead(&s, &constructionMethod, 1)); // unsigned int(4) construction_method; |
| 1599 | constructionMethod = constructionMethod & 0xf; |
| 1600 | if ((constructionMethod != 0 /* file */) && (constructionMethod != 1 /* idat */)) { |
| 1601 | // construction method item(2) unsupported |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1602 | avifDiagnosticsPrintf(diag, "Box[iloc] has an unsupported construction method [%u]", constructionMethod); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1603 | return AVIF_FALSE; |
| 1604 | } |
| 1605 | if (constructionMethod == 1) { |
Joe Drago | 1dea33e | 2021-09-14 17:12:39 -0700 | [diff] [blame] | 1606 | item->idatStored = AVIF_TRUE; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1607 | } |
| 1608 | } |
| 1609 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1610 | uint16_t dataReferenceIndex; // unsigned int(16) data_ref rence_index; |
| 1611 | CHECK(avifROStreamReadU16(&s, &dataReferenceIndex)); // |
| 1612 | uint64_t baseOffset; // unsigned int(base_offset_size*8) base_offset; |
| 1613 | CHECK(avifROStreamReadUX8(&s, &baseOffset, baseOffsetSize)); // |
| 1614 | uint16_t extentCount; // unsigned int(16) extent_count; |
| 1615 | CHECK(avifROStreamReadU16(&s, &extentCount)); // |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 1616 | for (int extentIter = 0; extentIter < extentCount; ++extentIter) { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1617 | // If extent_index is ever supported, this spec must be implemented here: |
| 1618 | // :: if (((version == 1) || (version == 2)) && (index_size > 0)) { |
| 1619 | // :: unsigned int(index_size*8) extent_index; |
| 1620 | // :: } |
| 1621 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1622 | uint64_t extentOffset; // unsigned int(offset_size*8) extent_offset; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1623 | CHECK(avifROStreamReadUX8(&s, &extentOffset, offsetSize)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1624 | uint64_t extentLength; // unsigned int(offset_size*8) extent_length; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1625 | CHECK(avifROStreamReadUX8(&s, &extentLength, lengthSize)); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1626 | |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 1627 | avifExtent * extent = (avifExtent *)avifArrayPushPtr(&item->extents); |
Wan-Teh Chang | b985369 | 2020-08-28 15:32:36 -0700 | [diff] [blame] | 1628 | if (extentOffset > UINT64_MAX - baseOffset) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1629 | avifDiagnosticsPrintf(diag, |
| 1630 | "Item ID [%u] contains an extent offset which overflows: [base: %" PRIu64 " offset:%" PRIu64 "]", |
| 1631 | itemID, |
| 1632 | baseOffset, |
| 1633 | extentOffset); |
Wan-Teh Chang | b985369 | 2020-08-28 15:32:36 -0700 | [diff] [blame] | 1634 | return AVIF_FALSE; |
| 1635 | } |
| 1636 | uint64_t offset = baseOffset + extentOffset; |
Joe Drago | 217056b | 2020-11-13 16:19:35 -0800 | [diff] [blame] | 1637 | extent->offset = offset; |
| 1638 | if (extentLength > SIZE_MAX) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1639 | avifDiagnosticsPrintf(diag, "Item ID [%u] contains an extent length which overflows: [%" PRIu64 "]", itemID, extentLength); |
Wan-Teh Chang | b985369 | 2020-08-28 15:32:36 -0700 | [diff] [blame] | 1640 | return AVIF_FALSE; |
| 1641 | } |
Joe Drago | 217056b | 2020-11-13 16:19:35 -0800 | [diff] [blame] | 1642 | extent->size = (size_t)extentLength; |
| 1643 | if (extent->size > SIZE_MAX - item->size) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1644 | avifDiagnosticsPrintf(diag, |
Joe Drago | 4886729 | 2021-05-04 13:23:08 -0700 | [diff] [blame] | 1645 | "Item ID [%u] contains an extent length which overflows the item size: [%zu, %zu]", |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1646 | itemID, |
| 1647 | extent->size, |
| 1648 | item->size); |
Wan-Teh Chang | b985369 | 2020-08-28 15:32:36 -0700 | [diff] [blame] | 1649 | return AVIF_FALSE; |
| 1650 | } |
Joe Drago | a495690 | 2020-08-28 01:24:35 -0700 | [diff] [blame] | 1651 | item->size += extent->size; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1652 | } |
| 1653 | } |
| 1654 | return AVIF_TRUE; |
| 1655 | } |
| 1656 | |
Wan-Teh Chang | 980d585 | 2021-08-03 20:02:38 -0700 | [diff] [blame] | 1657 | static avifBool avifParseImageGridBox(avifImageGrid * grid, const uint8_t * raw, size_t rawLen, uint32_t imageSizeLimit, avifDiagnostics * diag) |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1658 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1659 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[grid]"); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1660 | |
| 1661 | uint8_t version, flags; |
| 1662 | CHECK(avifROStreamRead(&s, &version, 1)); // unsigned int(8) version = 0; |
| 1663 | if (version != 0) { |
Joe Drago | ba4d67d | 2021-05-04 12:34:37 -0700 | [diff] [blame] | 1664 | avifDiagnosticsPrintf(diag, "Box[grid] has unsupported version [%u]", version); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1665 | return AVIF_FALSE; |
| 1666 | } |
Joe Drago | 79ebcf3 | 2020-11-18 22:37:10 -0800 | [diff] [blame] | 1667 | uint8_t rowsMinusOne, columnsMinusOne; |
| 1668 | CHECK(avifROStreamRead(&s, &flags, 1)); // unsigned int(8) flags; |
| 1669 | CHECK(avifROStreamRead(&s, &rowsMinusOne, 1)); // unsigned int(8) rows_minus_one; |
| 1670 | CHECK(avifROStreamRead(&s, &columnsMinusOne, 1)); // unsigned int(8) columns_minus_one; |
| 1671 | grid->rows = (uint32_t)rowsMinusOne + 1; |
| 1672 | grid->columns = (uint32_t)columnsMinusOne + 1; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1673 | |
| 1674 | uint32_t fieldLength = ((flags & 1) + 1) * 16; |
| 1675 | if (fieldLength == 16) { |
| 1676 | uint16_t outputWidth16, outputHeight16; |
| 1677 | CHECK(avifROStreamReadU16(&s, &outputWidth16)); // unsigned int(FieldLength) output_width; |
| 1678 | CHECK(avifROStreamReadU16(&s, &outputHeight16)); // unsigned int(FieldLength) output_height; |
| 1679 | grid->outputWidth = outputWidth16; |
| 1680 | grid->outputHeight = outputHeight16; |
| 1681 | } else { |
| 1682 | if (fieldLength != 32) { |
| 1683 | // This should be impossible |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1684 | avifDiagnosticsPrintf(diag, "Grid box contains illegal field length: [%u]", fieldLength); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1685 | return AVIF_FALSE; |
| 1686 | } |
| 1687 | CHECK(avifROStreamReadU32(&s, &grid->outputWidth)); // unsigned int(FieldLength) output_width; |
| 1688 | CHECK(avifROStreamReadU32(&s, &grid->outputHeight)); // unsigned int(FieldLength) output_height; |
| 1689 | } |
Wan-Teh Chang | bd1492e | 2021-08-06 16:08:14 -0700 | [diff] [blame] | 1690 | if ((grid->outputWidth == 0) || (grid->outputHeight == 0)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1691 | avifDiagnosticsPrintf(diag, "Grid box contains illegal dimensions: [%u x %u]", grid->outputWidth, grid->outputHeight); |
Wan-Teh Chang | 0a8e724 | 2020-08-10 13:24:59 -0700 | [diff] [blame] | 1692 | return AVIF_FALSE; |
| 1693 | } |
Wan-Teh Chang | bd1492e | 2021-08-06 16:08:14 -0700 | [diff] [blame] | 1694 | if (grid->outputWidth > (imageSizeLimit / grid->outputHeight)) { |
| 1695 | avifDiagnosticsPrintf(diag, "Grid box dimensions are too large: [%u x %u]", grid->outputWidth, grid->outputHeight); |
| 1696 | return AVIF_FALSE; |
| 1697 | } |
wantehchang | ae2074b | 2020-08-12 13:02:27 -0700 | [diff] [blame] | 1698 | return avifROStreamRemainingBytes(&s) == 0; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1699 | } |
| 1700 | |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1701 | static avifBool avifParseImageSpatialExtentsProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1702 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1703 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[ispe]"); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1704 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1705 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1706 | avifImageSpatialExtents * ispe = &prop->u.ispe; |
| 1707 | CHECK(avifROStreamReadU32(&s, &ispe->width)); |
| 1708 | CHECK(avifROStreamReadU32(&s, &ispe->height)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1709 | return AVIF_TRUE; |
| 1710 | } |
| 1711 | |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1712 | static avifBool avifParseAuxiliaryTypeProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1713 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1714 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[auxC]"); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1715 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1716 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1717 | CHECK(avifROStreamReadString(&s, prop->u.auxC.auxType, AUXTYPE_SIZE)); |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1718 | return AVIF_TRUE; |
| 1719 | } |
| 1720 | |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1721 | static avifBool avifParseColourInformationBox(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 1722 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1723 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[colr]"); |
Joe Drago | e7ce20d | 2019-02-11 16:37:38 -0800 | [diff] [blame] | 1724 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1725 | avifColourInformationBox * colr = &prop->u.colr; |
| 1726 | colr->hasICC = AVIF_FALSE; |
| 1727 | colr->hasNCLX = AVIF_FALSE; |
Joe Drago | e7ce20d | 2019-02-11 16:37:38 -0800 | [diff] [blame] | 1728 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1729 | uint8_t colorType[4]; // unsigned int(32) colour_type; |
| 1730 | CHECK(avifROStreamRead(&s, colorType, 4)); |
| 1731 | if (!memcmp(colorType, "rICC", 4) || !memcmp(colorType, "prof", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1732 | colr->hasICC = AVIF_TRUE; |
| 1733 | colr->icc = avifROStreamCurrent(&s); |
| 1734 | colr->iccSize = avifROStreamRemainingBytes(&s); |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1735 | } else if (!memcmp(colorType, "nclx", 4)) { |
Joe Drago | fb5a5f0 | 2021-01-31 20:43:57 -0800 | [diff] [blame] | 1736 | CHECK(avifROStreamReadU16(&s, &colr->colorPrimaries)); // unsigned int(16) colour_primaries; |
| 1737 | CHECK(avifROStreamReadU16(&s, &colr->transferCharacteristics)); // unsigned int(16) transfer_characteristics; |
| 1738 | CHECK(avifROStreamReadU16(&s, &colr->matrixCoefficients)); // unsigned int(16) matrix_coefficients; |
Joe Drago | 97b071c | 2019-07-17 14:24:56 -0700 | [diff] [blame] | 1739 | // unsigned int(1) full_range_flag; |
| 1740 | // unsigned int(7) reserved = 0; |
Joe Drago | 74cd1c9 | 2020-04-16 12:17:11 -0700 | [diff] [blame] | 1741 | uint8_t tmp8; |
| 1742 | CHECK(avifROStreamRead(&s, &tmp8, 1)); |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1743 | colr->range = (tmp8 & 0x80) ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED; |
| 1744 | colr->hasNCLX = AVIF_TRUE; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 1745 | } |
| 1746 | return AVIF_TRUE; |
| 1747 | } |
| 1748 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1749 | static avifBool avifParseAV1CodecConfigurationBox(const uint8_t * raw, size_t rawLen, avifCodecConfigurationBox * av1C, avifDiagnostics * diag) |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1750 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1751 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[av1C]"); |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1752 | |
| 1753 | uint8_t markerAndVersion = 0; |
| 1754 | CHECK(avifROStreamRead(&s, &markerAndVersion, 1)); |
| 1755 | uint8_t seqProfileAndIndex = 0; |
| 1756 | CHECK(avifROStreamRead(&s, &seqProfileAndIndex, 1)); |
| 1757 | uint8_t rawFlags = 0; |
| 1758 | CHECK(avifROStreamRead(&s, &rawFlags, 1)); |
| 1759 | |
| 1760 | if (markerAndVersion != 0x81) { |
| 1761 | // Marker and version must both == 1 |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1762 | avifDiagnosticsPrintf(diag, "av1C contains illegal marker and version pair: [%u]", markerAndVersion); |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1763 | return AVIF_FALSE; |
| 1764 | } |
| 1765 | |
| 1766 | av1C->seqProfile = (seqProfileAndIndex >> 5) & 0x7; // unsigned int (3) seq_profile; |
| 1767 | av1C->seqLevelIdx0 = (seqProfileAndIndex >> 0) & 0x1f; // unsigned int (5) seq_level_idx_0; |
| 1768 | av1C->seqTier0 = (rawFlags >> 7) & 0x1; // unsigned int (1) seq_tier_0; |
| 1769 | av1C->highBitdepth = (rawFlags >> 6) & 0x1; // unsigned int (1) high_bitdepth; |
| 1770 | av1C->twelveBit = (rawFlags >> 5) & 0x1; // unsigned int (1) twelve_bit; |
| 1771 | av1C->monochrome = (rawFlags >> 4) & 0x1; // unsigned int (1) monochrome; |
| 1772 | av1C->chromaSubsamplingX = (rawFlags >> 3) & 0x1; // unsigned int (1) chroma_subsampling_x; |
| 1773 | av1C->chromaSubsamplingY = (rawFlags >> 2) & 0x1; // unsigned int (1) chroma_subsampling_y; |
| 1774 | av1C->chromaSamplePosition = (rawFlags >> 0) & 0x3; // unsigned int (2) chroma_sample_position; |
| 1775 | return AVIF_TRUE; |
| 1776 | } |
| 1777 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1778 | static avifBool avifParseAV1CodecConfigurationBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1779 | { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1780 | return avifParseAV1CodecConfigurationBox(raw, rawLen, &prop->u.av1C, diag); |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1783 | static avifBool avifParsePixelAspectRatioBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1784 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1785 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[pasp]"); |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1786 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1787 | avifPixelAspectRatioBox * pasp = &prop->u.pasp; |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1788 | CHECK(avifROStreamReadU32(&s, &pasp->hSpacing)); // unsigned int(32) hSpacing; |
| 1789 | CHECK(avifROStreamReadU32(&s, &pasp->vSpacing)); // unsigned int(32) vSpacing; |
| 1790 | return AVIF_TRUE; |
| 1791 | } |
| 1792 | |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1793 | static avifBool avifParseCleanApertureBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1794 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1795 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[clap]"); |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1796 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1797 | avifCleanApertureBox * clap = &prop->u.clap; |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1798 | CHECK(avifROStreamReadU32(&s, &clap->widthN)); // unsigned int(32) cleanApertureWidthN; |
| 1799 | CHECK(avifROStreamReadU32(&s, &clap->widthD)); // unsigned int(32) cleanApertureWidthD; |
| 1800 | CHECK(avifROStreamReadU32(&s, &clap->heightN)); // unsigned int(32) cleanApertureHeightN; |
| 1801 | CHECK(avifROStreamReadU32(&s, &clap->heightD)); // unsigned int(32) cleanApertureHeightD; |
| 1802 | CHECK(avifROStreamReadU32(&s, &clap->horizOffN)); // unsigned int(32) horizOffN; |
| 1803 | CHECK(avifROStreamReadU32(&s, &clap->horizOffD)); // unsigned int(32) horizOffD; |
| 1804 | CHECK(avifROStreamReadU32(&s, &clap->vertOffN)); // unsigned int(32) vertOffN; |
| 1805 | CHECK(avifROStreamReadU32(&s, &clap->vertOffD)); // unsigned int(32) vertOffD; |
| 1806 | return AVIF_TRUE; |
| 1807 | } |
| 1808 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1809 | static avifBool avifParseImageRotationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1810 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1811 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[irot]"); |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1812 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1813 | avifImageRotation * irot = &prop->u.irot; |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1814 | CHECK(avifROStreamRead(&s, &irot->angle, 1)); // unsigned int (6) reserved = 0; unsigned int (2) angle; |
| 1815 | if ((irot->angle & 0xfc) != 0) { |
| 1816 | // reserved bits must be 0 |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1817 | avifDiagnosticsPrintf(diag, "Box[irot] contains nonzero reserved bits [%u]", irot->angle); |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1818 | return AVIF_FALSE; |
| 1819 | } |
| 1820 | return AVIF_TRUE; |
| 1821 | } |
| 1822 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1823 | static avifBool avifParseImageMirrorProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1824 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1825 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[imir]"); |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1826 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1827 | avifImageMirror * imir = &prop->u.imir; |
Joe Drago | b551bb3 | 2021-06-03 15:22:05 -0700 | [diff] [blame] | 1828 | CHECK(avifROStreamRead(&s, &imir->mode, 1)); // unsigned int (7) reserved = 0; unsigned int (1) mode; |
| 1829 | if ((imir->mode & 0xfe) != 0) { |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1830 | // reserved bits must be 0 |
Joe Drago | b551bb3 | 2021-06-03 15:22:05 -0700 | [diff] [blame] | 1831 | avifDiagnosticsPrintf(diag, "Box[imir] contains nonzero reserved bits [%u]", imir->mode); |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1832 | return AVIF_FALSE; |
| 1833 | } |
| 1834 | return AVIF_TRUE; |
| 1835 | } |
| 1836 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1837 | static avifBool avifParsePixelInformationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 6042156 | 2020-04-23 11:32:26 -0700 | [diff] [blame] | 1838 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1839 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[pixi]"); |
Joe Drago | 6042156 | 2020-04-23 11:32:26 -0700 | [diff] [blame] | 1840 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
| 1841 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1842 | avifPixelInformationProperty * pixi = &prop->u.pixi; |
Joe Drago | 6042156 | 2020-04-23 11:32:26 -0700 | [diff] [blame] | 1843 | CHECK(avifROStreamRead(&s, &pixi->planeCount, 1)); // unsigned int (8) num_channels; |
| 1844 | if (pixi->planeCount > MAX_PIXI_PLANE_DEPTHS) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1845 | avifDiagnosticsPrintf(diag, "Box[pixi] contains unsupported plane count [%u]", pixi->planeCount); |
Joe Drago | 6042156 | 2020-04-23 11:32:26 -0700 | [diff] [blame] | 1846 | return AVIF_FALSE; |
| 1847 | } |
| 1848 | for (uint8_t i = 0; i < pixi->planeCount; ++i) { |
| 1849 | CHECK(avifROStreamRead(&s, &pixi->planeDepths[i], 1)); // unsigned int (8) bits_per_channel; |
| 1850 | } |
| 1851 | return AVIF_TRUE; |
| 1852 | } |
| 1853 | |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1854 | static avifBool avifParseOperatingPointSelectorProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
| 1855 | { |
| 1856 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[a1op]"); |
| 1857 | |
| 1858 | avifOperatingPointSelectorProperty * a1op = &prop->u.a1op; |
| 1859 | CHECK(avifROStreamRead(&s, &a1op->opIndex, 1)); |
| 1860 | if (a1op->opIndex > 31) { // 31 is AV1's max operating point value |
| 1861 | avifDiagnosticsPrintf(diag, "Box[a1op] contains an unsupported operating point [%u]", a1op->opIndex); |
| 1862 | return AVIF_FALSE; |
| 1863 | } |
| 1864 | return AVIF_TRUE; |
| 1865 | } |
| 1866 | |
| 1867 | static avifBool avifParseLayerSelectorProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
| 1868 | { |
| 1869 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[lsel]"); |
| 1870 | |
| 1871 | avifLayerSelectorProperty * lsel = &prop->u.lsel; |
| 1872 | CHECK(avifROStreamReadU16(&s, &lsel->layerID)); |
| 1873 | if (lsel->layerID >= MAX_AV1_LAYER_COUNT) { |
| 1874 | avifDiagnosticsPrintf(diag, "Box[lsel] contains an unsupported layer [%u]", lsel->layerID); |
| 1875 | return AVIF_FALSE; |
| 1876 | } |
| 1877 | return AVIF_TRUE; |
| 1878 | } |
| 1879 | |
| 1880 | static avifBool avifParseAV1LayeredImageIndexingProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
| 1881 | { |
| 1882 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[a1lx]"); |
| 1883 | |
| 1884 | avifAV1LayeredImageIndexingProperty * a1lx = &prop->u.a1lx; |
| 1885 | |
| 1886 | uint8_t largeSize = 0; |
| 1887 | CHECK(avifROStreamRead(&s, &largeSize, 1)); |
| 1888 | if (largeSize & 0xFE) { |
| 1889 | avifDiagnosticsPrintf(diag, "Box[a1lx] has bits set in the reserved section [%u]", largeSize); |
| 1890 | return AVIF_FALSE; |
| 1891 | } |
| 1892 | |
| 1893 | for (int i = 0; i < 3; ++i) { |
| 1894 | if (largeSize) { |
| 1895 | CHECK(avifROStreamReadU32(&s, &a1lx->layerSize[i])); |
| 1896 | } else { |
| 1897 | uint16_t layerSize16; |
| 1898 | CHECK(avifROStreamReadU16(&s, &layerSize16)); |
| 1899 | a1lx->layerSize[i] = (uint32_t)layerSize16; |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | // Layer sizes will be validated layer (when the item's size is known) |
| 1904 | return AVIF_TRUE; |
| 1905 | } |
| 1906 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1907 | static avifBool avifParseItemPropertyContainerBox(avifPropertyArray * properties, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1908 | { |
Wan-Teh Chang | d8867d1 | 2021-05-27 16:08:55 -0700 | [diff] [blame] | 1909 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[ipco]"); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1910 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1911 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1912 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1913 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1914 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1915 | int propertyIndex = avifArrayPushIndex(properties); |
| 1916 | avifProperty * prop = &properties->prop[propertyIndex]; |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1917 | memcpy(prop->type, header.type, 4); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1918 | if (!memcmp(header.type, "ispe", 4)) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1919 | CHECK(avifParseImageSpatialExtentsProperty(prop, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1920 | } else if (!memcmp(header.type, "auxC", 4)) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1921 | CHECK(avifParseAuxiliaryTypeProperty(prop, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1922 | } else if (!memcmp(header.type, "colr", 4)) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1923 | CHECK(avifParseColourInformationBox(prop, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1924 | } else if (!memcmp(header.type, "av1C", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1925 | CHECK(avifParseAV1CodecConfigurationBoxProperty(prop, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1926 | } else if (!memcmp(header.type, "pasp", 4)) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1927 | CHECK(avifParsePixelAspectRatioBoxProperty(prop, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1928 | } else if (!memcmp(header.type, "clap", 4)) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1929 | CHECK(avifParseCleanApertureBoxProperty(prop, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1930 | } else if (!memcmp(header.type, "irot", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1931 | CHECK(avifParseImageRotationProperty(prop, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1932 | } else if (!memcmp(header.type, "imir", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1933 | CHECK(avifParseImageMirrorProperty(prop, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1934 | } else if (!memcmp(header.type, "pixi", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1935 | CHECK(avifParsePixelInformationProperty(prop, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 1936 | } else if (!memcmp(header.type, "a1op", 4)) { |
| 1937 | CHECK(avifParseOperatingPointSelectorProperty(prop, avifROStreamCurrent(&s), header.size, diag)); |
| 1938 | } else if (!memcmp(header.type, "lsel", 4)) { |
| 1939 | CHECK(avifParseLayerSelectorProperty(prop, avifROStreamCurrent(&s), header.size, diag)); |
| 1940 | } else if (!memcmp(header.type, "a1lx", 4)) { |
| 1941 | CHECK(avifParseAV1LayeredImageIndexingProperty(prop, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | 6042156 | 2020-04-23 11:32:26 -0700 | [diff] [blame] | 1942 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1943 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1944 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1945 | } |
| 1946 | return AVIF_TRUE; |
| 1947 | } |
| 1948 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 1949 | static avifBool avifParseItemPropertyAssociation(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag, uint32_t * outVersionAndFlags) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1950 | { |
Joe Drago | c60ccae | 2021-03-31 10:30:48 -0700 | [diff] [blame] | 1951 | // NOTE: If this function ever adds support for versions other than [0,1] or flags other than |
| 1952 | // [0,1], please increase the value of MAX_IPMA_VERSION_AND_FLAGS_SEEN accordingly. |
| 1953 | |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1954 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[ipma]"); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1955 | |
| 1956 | uint8_t version; |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 1957 | uint32_t flags; |
| 1958 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, &flags)); |
| 1959 | avifBool propertyIndexIsU16 = ((flags & 0x1) != 0); |
Wan-Teh Chang | 33d6462 | 2021-04-08 08:49:22 -0700 | [diff] [blame] | 1960 | *outVersionAndFlags = ((uint32_t)version << 24) | flags; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1961 | |
| 1962 | uint32_t entryCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1963 | CHECK(avifROStreamReadU32(&s, &entryCount)); |
Wan-Teh Chang | f4c6538 | 2020-11-03 14:27:45 -0800 | [diff] [blame] | 1964 | unsigned int prevItemID = 0; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1965 | for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) { |
Wan-Teh Chang | f4c6538 | 2020-11-03 14:27:45 -0800 | [diff] [blame] | 1966 | // ISO/IEC 23008-12, First edition, 2017-12, Section 9.3.1: |
| 1967 | // Each ItemPropertyAssociation box shall be ordered by increasing item_ID, and there shall |
| 1968 | // 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] | 1969 | unsigned int itemID; |
| 1970 | if (version < 1) { |
| 1971 | uint16_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1972 | CHECK(avifROStreamReadU16(&s, &tmp)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1973 | itemID = tmp; |
| 1974 | } else { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1975 | CHECK(avifROStreamReadU32(&s, &itemID)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1976 | } |
Wan-Teh Chang | f4c6538 | 2020-11-03 14:27:45 -0800 | [diff] [blame] | 1977 | if (itemID <= prevItemID) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1978 | avifDiagnosticsPrintf(diag, "Box[ipma] item IDs are not ordered by increasing ID"); |
Wan-Teh Chang | f4c6538 | 2020-11-03 14:27:45 -0800 | [diff] [blame] | 1979 | return AVIF_FALSE; |
| 1980 | } |
| 1981 | prevItemID = itemID; |
Wan-Teh Chang | 750a192 | 2020-10-28 17:54:35 -0700 | [diff] [blame] | 1982 | |
| 1983 | avifDecoderItem * item = avifMetaFindItem(meta, itemID); |
| 1984 | if (!item) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1985 | avifDiagnosticsPrintf(diag, "Box[ipma] has an invalid item ID [%u]", itemID); |
Wan-Teh Chang | 750a192 | 2020-10-28 17:54:35 -0700 | [diff] [blame] | 1986 | return AVIF_FALSE; |
| 1987 | } |
Wan-Teh Chang | f4c6538 | 2020-11-03 14:27:45 -0800 | [diff] [blame] | 1988 | if (item->ipmaSeen) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 1989 | avifDiagnosticsPrintf(diag, "Duplicate Box[ipma] for item ID [%u]", itemID); |
Wan-Teh Chang | f4c6538 | 2020-11-03 14:27:45 -0800 | [diff] [blame] | 1990 | return AVIF_FALSE; |
| 1991 | } |
| 1992 | item->ipmaSeen = AVIF_TRUE; |
Wan-Teh Chang | 750a192 | 2020-10-28 17:54:35 -0700 | [diff] [blame] | 1993 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1994 | uint8_t associationCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1995 | CHECK(avifROStreamRead(&s, &associationCount, 1)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1996 | for (uint8_t associationIndex = 0; associationIndex < associationCount; ++associationIndex) { |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 1997 | avifBool essential = AVIF_FALSE; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1998 | uint16_t propertyIndex = 0; |
| 1999 | if (propertyIndexIsU16) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2000 | CHECK(avifROStreamReadU16(&s, &propertyIndex)); |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 2001 | essential = ((propertyIndex & 0x8000) != 0); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2002 | propertyIndex &= 0x7fff; |
| 2003 | } else { |
| 2004 | uint8_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2005 | CHECK(avifROStreamRead(&s, &tmp, 1)); |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 2006 | essential = ((tmp & 0x80) != 0); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2007 | propertyIndex = tmp & 0x7f; |
| 2008 | } |
| 2009 | |
| 2010 | if (propertyIndex == 0) { |
| 2011 | // Not associated with any item |
| 2012 | continue; |
| 2013 | } |
| 2014 | --propertyIndex; // 1-indexed |
| 2015 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2016 | if (propertyIndex >= meta->properties.count) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2017 | avifDiagnosticsPrintf(diag, |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2018 | "Box[ipma] for item ID [%u] contains an illegal property index [%u] (out of [%u] properties)", |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2019 | itemID, |
| 2020 | propertyIndex, |
| 2021 | meta->properties.count); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2022 | return AVIF_FALSE; |
| 2023 | } |
| 2024 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2025 | // Copy property to item |
Wan-Teh Chang | 2f225b0 | 2022-03-16 22:30:38 -0700 | [diff] [blame] | 2026 | const avifProperty * srcProp = &meta->properties.prop[propertyIndex]; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2027 | |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 2028 | static const char * supportedTypes[] = { "ispe", "auxC", "colr", "av1C", "pasp", "clap", |
| 2029 | "irot", "imir", "pixi", "a1op", "lsel", "a1lx" }; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2030 | size_t supportedTypesCount = sizeof(supportedTypes) / sizeof(supportedTypes[0]); |
| 2031 | avifBool supportedType = AVIF_FALSE; |
| 2032 | for (size_t i = 0; i < supportedTypesCount; ++i) { |
| 2033 | if (!memcmp(srcProp->type, supportedTypes[i], 4)) { |
| 2034 | supportedType = AVIF_TRUE; |
| 2035 | break; |
| 2036 | } |
| 2037 | } |
| 2038 | if (supportedType) { |
Wan-Teh Chang | 37eefca | 2021-07-14 16:10:34 -0700 | [diff] [blame] | 2039 | if (essential) { |
| 2040 | // Verify that it is legal for this property to be flagged as essential. Any |
| 2041 | // types in this list are *required* in the spec to not be flagged as essential |
| 2042 | // when associated with an item. |
| 2043 | static const char * const nonessentialTypes[] = { |
| 2044 | |
| 2045 | // AVIF: Section 2.3.2.3.2: "If associated, it shall not be marked as essential." |
| 2046 | "a1lx" |
| 2047 | |
| 2048 | }; |
| 2049 | size_t nonessentialTypesCount = sizeof(nonessentialTypes) / sizeof(nonessentialTypes[0]); |
| 2050 | for (size_t i = 0; i < nonessentialTypesCount; ++i) { |
| 2051 | if (!memcmp(srcProp->type, nonessentialTypes[i], 4)) { |
| 2052 | avifDiagnosticsPrintf(diag, |
| 2053 | "Item ID [%u] has a %s property association which must not be marked essential, but is", |
| 2054 | itemID, |
| 2055 | nonessentialTypes[i]); |
| 2056 | return AVIF_FALSE; |
| 2057 | } |
| 2058 | } |
| 2059 | } else { |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 2060 | // Verify that it is legal for this property to not be flagged as essential. Any |
| 2061 | // types in this list are *required* in the spec to be flagged as essential when |
| 2062 | // associated with an item. |
| 2063 | static const char * const essentialTypes[] = { |
| 2064 | |
| 2065 | // AVIF: Section 2.3.2.1.1: "If associated, it shall be marked as essential." |
| 2066 | "a1op", |
| 2067 | |
| 2068 | // HEIF: Section 6.5.11.1: "essential shall be equal to 1 for an 'lsel' item property." |
| 2069 | "lsel" |
| 2070 | |
| 2071 | }; |
| 2072 | size_t essentialTypesCount = sizeof(essentialTypes) / sizeof(essentialTypes[0]); |
| 2073 | for (size_t i = 0; i < essentialTypesCount; ++i) { |
| 2074 | if (!memcmp(srcProp->type, essentialTypes[i], 4)) { |
| 2075 | avifDiagnosticsPrintf(diag, |
| 2076 | "Item ID [%u] has a %s property association which must be marked essential, but is not", |
| 2077 | itemID, |
| 2078 | essentialTypes[i]); |
| 2079 | return AVIF_FALSE; |
| 2080 | } |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | // Supported and valid; associate it with this item. |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2085 | avifProperty * dstProp = (avifProperty *)avifArrayPushPtr(&item->properties); |
Wan-Teh Chang | 2f225b0 | 2022-03-16 22:30:38 -0700 | [diff] [blame] | 2086 | *dstProp = *srcProp; |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 2087 | } else { |
| 2088 | if (essential) { |
| 2089 | // Discovered an essential item property that libavif doesn't support! |
| 2090 | // Make a note to ignore this item later. |
| 2091 | item->hasUnsupportedEssentialProperty = AVIF_TRUE; |
| 2092 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2093 | } |
| 2094 | } |
| 2095 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2096 | return AVIF_TRUE; |
| 2097 | } |
| 2098 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2099 | static avifBool avifParsePrimaryItemBox(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2100 | { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2101 | if (meta->primaryItemID > 0) { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2102 | // Illegal to have multiple pitm boxes, bail out |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2103 | avifDiagnosticsPrintf(diag, "Multiple boxes of unique Box[pitm] found"); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2104 | return AVIF_FALSE; |
| 2105 | } |
| 2106 | |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2107 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[pitm]"); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2108 | |
| 2109 | uint8_t version; |
| 2110 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
| 2111 | |
| 2112 | if (version == 0) { |
| 2113 | uint16_t tmp16; |
| 2114 | CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID; |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2115 | meta->primaryItemID = tmp16; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2116 | } else { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2117 | CHECK(avifROStreamReadU32(&s, &meta->primaryItemID)); // unsigned int(32) item_ID; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2118 | } |
| 2119 | return AVIF_TRUE; |
| 2120 | } |
| 2121 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2122 | static avifBool avifParseItemDataBox(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2123 | { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2124 | // Check to see if we've already seen an idat box for this meta box. If so, bail out |
Joe Drago | 1dea33e | 2021-09-14 17:12:39 -0700 | [diff] [blame] | 2125 | if (meta->idat.size > 0) { |
| 2126 | avifDiagnosticsPrintf(diag, "Meta box contains multiple idat boxes"); |
| 2127 | return AVIF_FALSE; |
| 2128 | } |
| 2129 | if (rawLen == 0) { |
| 2130 | avifDiagnosticsPrintf(diag, "idat box has a length of 0"); |
| 2131 | return AVIF_FALSE; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2132 | } |
| 2133 | |
Joe Drago | 1dea33e | 2021-09-14 17:12:39 -0700 | [diff] [blame] | 2134 | avifRWDataSet(&meta->idat, raw, rawLen); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2135 | return AVIF_TRUE; |
| 2136 | } |
| 2137 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2138 | static avifBool avifParseItemPropertiesBox(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2139 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2140 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[iprp]"); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2141 | |
| 2142 | avifBoxHeader ipcoHeader; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2143 | CHECK(avifROStreamReadBoxHeader(&s, &ipcoHeader)); |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame] | 2144 | if (memcmp(ipcoHeader.type, "ipco", 4)) { |
Joe Drago | ba4d67d | 2021-05-04 12:34:37 -0700 | [diff] [blame] | 2145 | avifDiagnosticsPrintf(diag, "Failed to find Box[ipco] as the first box in Box[iprp]"); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2146 | return AVIF_FALSE; |
| 2147 | } |
| 2148 | |
| 2149 | // Read all item properties inside of ItemPropertyContainerBox |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2150 | CHECK(avifParseItemPropertyContainerBox(&meta->properties, avifROStreamCurrent(&s), ipcoHeader.size, diag)); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2151 | CHECK(avifROStreamSkip(&s, ipcoHeader.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2152 | |
Joe Drago | c60ccae | 2021-03-31 10:30:48 -0700 | [diff] [blame] | 2153 | uint32_t versionAndFlagsSeen[MAX_IPMA_VERSION_AND_FLAGS_SEEN]; |
| 2154 | uint32_t versionAndFlagsSeenCount = 0; |
| 2155 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2156 | // 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] | 2157 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2158 | avifBoxHeader ipmaHeader; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2159 | CHECK(avifROStreamReadBoxHeader(&s, &ipmaHeader)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2160 | |
| 2161 | if (!memcmp(ipmaHeader.type, "ipma", 4)) { |
Joe Drago | c60ccae | 2021-03-31 10:30:48 -0700 | [diff] [blame] | 2162 | uint32_t versionAndFlags; |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2163 | CHECK(avifParseItemPropertyAssociation(meta, avifROStreamCurrent(&s), ipmaHeader.size, diag, &versionAndFlags)); |
Joe Drago | c60ccae | 2021-03-31 10:30:48 -0700 | [diff] [blame] | 2164 | for (uint32_t i = 0; i < versionAndFlagsSeenCount; ++i) { |
| 2165 | if (versionAndFlagsSeen[i] == versionAndFlags) { |
| 2166 | // HEIF (ISO 23008-12:2017) 9.3.1 - There shall be at most one |
| 2167 | // ItemPropertyAssociation box with a given pair of values of version and |
| 2168 | // flags. |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2169 | avifDiagnosticsPrintf(diag, "Multiple Box[ipma] with a given pair of values of version and flags. See HEIF (ISO 23008-12:2017) 9.3.1"); |
Joe Drago | c60ccae | 2021-03-31 10:30:48 -0700 | [diff] [blame] | 2170 | return AVIF_FALSE; |
| 2171 | } |
| 2172 | } |
| 2173 | if (versionAndFlagsSeenCount == MAX_IPMA_VERSION_AND_FLAGS_SEEN) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2174 | avifDiagnosticsPrintf(diag, "Exceeded possible count of unique ipma version and flags tuples"); |
Joe Drago | c60ccae | 2021-03-31 10:30:48 -0700 | [diff] [blame] | 2175 | return AVIF_FALSE; |
| 2176 | } |
| 2177 | versionAndFlagsSeen[versionAndFlagsSeenCount] = versionAndFlags; |
| 2178 | ++versionAndFlagsSeenCount; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2179 | } else { |
| 2180 | // These must all be type ipma |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2181 | avifDiagnosticsPrintf(diag, "Box[iprp] contains a box that isn't type 'ipma'"); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2182 | return AVIF_FALSE; |
| 2183 | } |
| 2184 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2185 | CHECK(avifROStreamSkip(&s, ipmaHeader.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2186 | } |
| 2187 | return AVIF_TRUE; |
| 2188 | } |
| 2189 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2190 | static avifBool avifParseItemInfoEntry(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2191 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2192 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[infe]"); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2193 | |
Wan-Teh Chang | 35a4bc9 | 2022-02-17 09:14:49 -0800 | [diff] [blame] | 2194 | uint8_t version; |
| 2195 | uint32_t flags; |
| 2196 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, &flags)); |
| 2197 | // Version 2+ is required for item_type |
| 2198 | if (version != 2 && version != 3) { |
| 2199 | avifDiagnosticsPrintf(s.diag, "%s: Expecting box version 2 or 3, got version %u", s.diagContext, version); |
| 2200 | return AVIF_FALSE; |
| 2201 | } |
| 2202 | // TODO: check flags. ISO/IEC 23008-12:2017, Section 9.2 says: |
| 2203 | // The flags field of ItemInfoEntry with version greater than or equal to 2 is specified as |
| 2204 | // follows: |
| 2205 | // |
| 2206 | // (flags & 1) equal to 1 indicates that the item is not intended to be a part of the |
| 2207 | // presentation. For example, when (flags & 1) is equal to 1 for an image item, the image |
| 2208 | // item should not be displayed. |
| 2209 | // (flags & 1) equal to 0 indicates that the item is intended to be a part of the |
| 2210 | // presentation. |
| 2211 | // |
| 2212 | // See also Section 6.4.2. |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2213 | |
Wan-Teh Chang | 35a4bc9 | 2022-02-17 09:14:49 -0800 | [diff] [blame] | 2214 | uint32_t itemID; |
| 2215 | if (version == 2) { |
| 2216 | uint16_t tmp; |
| 2217 | CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) item_ID; |
| 2218 | itemID = tmp; |
| 2219 | } else { |
| 2220 | assert(version == 3); |
| 2221 | CHECK(avifROStreamReadU32(&s, &itemID)); // unsigned int(32) item_ID; |
| 2222 | } |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2223 | uint16_t itemProtectionIndex; // unsigned int(16) item_protection_index; |
| 2224 | CHECK(avifROStreamReadU16(&s, &itemProtectionIndex)); // |
| 2225 | uint8_t itemType[4]; // unsigned int(32) item_type; |
| 2226 | CHECK(avifROStreamRead(&s, itemType, 4)); // |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2227 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2228 | avifContentType contentType; |
| 2229 | if (!memcmp(itemType, "mime", 4)) { |
| 2230 | CHECK(avifROStreamReadString(&s, NULL, 0)); // string item_name; (skipped) |
| 2231 | CHECK(avifROStreamReadString(&s, contentType.contentType, CONTENTTYPE_SIZE)); // string content_type; |
| 2232 | } else { |
| 2233 | memset(&contentType, 0, sizeof(contentType)); |
| 2234 | } |
| 2235 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2236 | avifDecoderItem * item = avifMetaFindItem(meta, itemID); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2237 | if (!item) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2238 | avifDiagnosticsPrintf(diag, "Box[infe] has an invalid item ID [%u]", itemID); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2239 | return AVIF_FALSE; |
| 2240 | } |
| 2241 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 2242 | memcpy(item->type, itemType, sizeof(itemType)); |
Wan-Teh Chang | 2f225b0 | 2022-03-16 22:30:38 -0700 | [diff] [blame] | 2243 | item->contentType = contentType; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2244 | return AVIF_TRUE; |
| 2245 | } |
| 2246 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2247 | static avifBool avifParseItemInfoBox(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2248 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2249 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[iinf]"); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2250 | |
| 2251 | uint8_t version; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2252 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2253 | uint32_t entryCount; |
| 2254 | if (version == 0) { |
| 2255 | uint16_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2256 | CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) entry_count; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2257 | entryCount = tmp; |
| 2258 | } else if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2259 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2260 | } else { |
Joe Drago | ba4d67d | 2021-05-04 12:34:37 -0700 | [diff] [blame] | 2261 | avifDiagnosticsPrintf(diag, "Box[iinf] has an unsupported version %u", version); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2262 | return AVIF_FALSE; |
| 2263 | } |
| 2264 | |
Joe Drago | 678b938 | 2019-02-09 03:17:47 -0800 | [diff] [blame] | 2265 | for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2266 | avifBoxHeader infeHeader; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2267 | CHECK(avifROStreamReadBoxHeader(&s, &infeHeader)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2268 | |
| 2269 | if (!memcmp(infeHeader.type, "infe", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2270 | CHECK(avifParseItemInfoEntry(meta, avifROStreamCurrent(&s), infeHeader.size, diag)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2271 | } else { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2272 | // These must all be type infe |
| 2273 | avifDiagnosticsPrintf(diag, "Box[iinf] contains a box that isn't type 'infe'"); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2274 | return AVIF_FALSE; |
| 2275 | } |
| 2276 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2277 | CHECK(avifROStreamSkip(&s, infeHeader.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | return AVIF_TRUE; |
| 2281 | } |
| 2282 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2283 | static avifBool avifParseItemReferenceBox(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2284 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2285 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[iref]"); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2286 | |
| 2287 | uint8_t version; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2288 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2289 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2290 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2291 | avifBoxHeader irefHeader; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2292 | CHECK(avifROStreamReadBoxHeader(&s, &irefHeader)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2293 | |
| 2294 | uint32_t fromID = 0; |
| 2295 | if (version == 0) { |
| 2296 | uint16_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2297 | CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) from_item_ID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2298 | fromID = tmp; |
| 2299 | } else if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2300 | CHECK(avifROStreamReadU32(&s, &fromID)); // unsigned int(32) from_item_ID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2301 | } else { |
| 2302 | // unsupported iref version, skip it |
| 2303 | break; |
| 2304 | } |
| 2305 | |
| 2306 | uint16_t referenceCount = 0; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2307 | CHECK(avifROStreamReadU16(&s, &referenceCount)); // unsigned int(16) reference_count; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2308 | |
| 2309 | for (uint16_t refIndex = 0; refIndex < referenceCount; ++refIndex) { |
| 2310 | uint32_t toID = 0; |
| 2311 | if (version == 0) { |
| 2312 | uint16_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2313 | CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) to_item_ID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2314 | toID = tmp; |
| 2315 | } else if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2316 | CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) to_item_ID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2317 | } else { |
| 2318 | // unsupported iref version, skip it |
| 2319 | break; |
| 2320 | } |
| 2321 | |
| 2322 | // Read this reference as "{fromID} is a {irefType} for {toID}" |
| 2323 | if (fromID && toID) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2324 | avifDecoderItem * item = avifMetaFindItem(meta, fromID); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2325 | if (!item) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2326 | avifDiagnosticsPrintf(diag, "Box[iref] has an invalid item ID [%u]", fromID); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2327 | return AVIF_FALSE; |
| 2328 | } |
| 2329 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2330 | if (!memcmp(irefHeader.type, "thmb", 4)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 2331 | item->thumbnailForID = toID; |
Wan-Teh Chang | b309e98 | 2021-02-20 16:59:36 -0800 | [diff] [blame] | 2332 | } else if (!memcmp(irefHeader.type, "auxl", 4)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 2333 | item->auxForID = toID; |
Wan-Teh Chang | b309e98 | 2021-02-20 16:59:36 -0800 | [diff] [blame] | 2334 | } else if (!memcmp(irefHeader.type, "cdsc", 4)) { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2335 | item->descForID = toID; |
Wan-Teh Chang | b309e98 | 2021-02-20 16:59:36 -0800 | [diff] [blame] | 2336 | } else if (!memcmp(irefHeader.type, "dimg", 4)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2337 | // derived images refer in the opposite direction |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2338 | avifDecoderItem * dimg = avifMetaFindItem(meta, toID); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2339 | if (!dimg) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2340 | avifDiagnosticsPrintf(diag, "Box[iref] has an invalid item ID dimg ref [%u]", toID); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2341 | return AVIF_FALSE; |
| 2342 | } |
| 2343 | |
| 2344 | dimg->dimgForID = fromID; |
Wan-Teh Chang | b309e98 | 2021-02-20 16:59:36 -0800 | [diff] [blame] | 2345 | } else if (!memcmp(irefHeader.type, "prem", 4)) { |
Yuan Tong | e4850be | 2021-01-22 14:21:25 +0800 | [diff] [blame] | 2346 | item->premByID = toID; |
| 2347 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2348 | } |
| 2349 | } |
| 2350 | } |
| 2351 | |
| 2352 | return AVIF_TRUE; |
| 2353 | } |
| 2354 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2355 | static avifBool avifParseMetaBox(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2356 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2357 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[meta]"); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2358 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2359 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2360 | |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 2361 | ++meta->idatID; // for tracking idat |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2362 | |
Joe Drago | e018518 | 2021-03-31 08:14:51 -0700 | [diff] [blame] | 2363 | avifBool firstBox = AVIF_TRUE; |
Joe Drago | 8f43909 | 2020-08-28 15:05:17 -0700 | [diff] [blame] | 2364 | uint32_t uniqueBoxFlags = 0; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2365 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2366 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2367 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2368 | |
Joe Drago | e018518 | 2021-03-31 08:14:51 -0700 | [diff] [blame] | 2369 | if (firstBox) { |
| 2370 | if (!memcmp(header.type, "hdlr", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2371 | CHECK(uniqueBoxSeen(&uniqueBoxFlags, 0, "meta", "hdlr", diag)); |
| 2372 | CHECK(avifParseHandlerBox(avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | e018518 | 2021-03-31 08:14:51 -0700 | [diff] [blame] | 2373 | firstBox = AVIF_FALSE; |
| 2374 | } else { |
| 2375 | // hdlr must be the first box! |
Joe Drago | ba4d67d | 2021-05-04 12:34:37 -0700 | [diff] [blame] | 2376 | avifDiagnosticsPrintf(diag, "Box[meta] does not have a Box[hdlr] as its first child box"); |
Joe Drago | e018518 | 2021-03-31 08:14:51 -0700 | [diff] [blame] | 2377 | return AVIF_FALSE; |
| 2378 | } |
| 2379 | } else if (!memcmp(header.type, "iloc", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2380 | CHECK(uniqueBoxSeen(&uniqueBoxFlags, 1, "meta", "iloc", diag)); |
| 2381 | CHECK(avifParseItemLocationBox(meta, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2382 | } else if (!memcmp(header.type, "pitm", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2383 | CHECK(uniqueBoxSeen(&uniqueBoxFlags, 2, "meta", "pitm", diag)); |
| 2384 | CHECK(avifParsePrimaryItemBox(meta, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2385 | } else if (!memcmp(header.type, "idat", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2386 | CHECK(uniqueBoxSeen(&uniqueBoxFlags, 3, "meta", "idat", diag)); |
| 2387 | CHECK(avifParseItemDataBox(meta, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2388 | } else if (!memcmp(header.type, "iprp", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2389 | CHECK(uniqueBoxSeen(&uniqueBoxFlags, 4, "meta", "iprp", diag)); |
| 2390 | CHECK(avifParseItemPropertiesBox(meta, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2391 | } else if (!memcmp(header.type, "iinf", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2392 | CHECK(uniqueBoxSeen(&uniqueBoxFlags, 5, "meta", "iinf", diag)); |
| 2393 | CHECK(avifParseItemInfoBox(meta, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2394 | } else if (!memcmp(header.type, "iref", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2395 | CHECK(uniqueBoxSeen(&uniqueBoxFlags, 6, "meta", "iref", diag)); |
| 2396 | CHECK(avifParseItemReferenceBox(meta, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2397 | } |
| 2398 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2399 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2400 | } |
Joe Drago | e018518 | 2021-03-31 08:14:51 -0700 | [diff] [blame] | 2401 | if (firstBox) { |
| 2402 | // The meta box must not be empty (it must contain at least a hdlr box) |
Joe Drago | ba4d67d | 2021-05-04 12:34:37 -0700 | [diff] [blame] | 2403 | avifDiagnosticsPrintf(diag, "Box[meta] has no child boxes"); |
Joe Drago | e018518 | 2021-03-31 08:14:51 -0700 | [diff] [blame] | 2404 | return AVIF_FALSE; |
| 2405 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2406 | return AVIF_TRUE; |
| 2407 | } |
| 2408 | |
Wan-Teh Chang | 980d585 | 2021-08-03 20:02:38 -0700 | [diff] [blame] | 2409 | static avifBool avifParseTrackHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen, uint32_t imageSizeLimit, avifDiagnostics * diag) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2410 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2411 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[tkhd]"); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2412 | |
| 2413 | uint8_t version; |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 2414 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2415 | |
| 2416 | uint32_t ignored32, trackID; |
| 2417 | uint64_t ignored64; |
| 2418 | if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2419 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time; |
| 2420 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time; |
| 2421 | CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID; |
Joe Drago | fc4144e | 2019-09-27 20:35:06 -0700 | [diff] [blame] | 2422 | CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0; |
| 2423 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) duration; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2424 | } else if (version == 0) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2425 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time; |
| 2426 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time; |
| 2427 | CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID; |
Joe Drago | fc4144e | 2019-09-27 20:35:06 -0700 | [diff] [blame] | 2428 | CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0; |
| 2429 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) duration; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2430 | } else { |
| 2431 | // Unsupported version |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2432 | avifDiagnosticsPrintf(diag, "Box[tkhd] has an unsupported version [%u]", version); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2433 | return AVIF_FALSE; |
| 2434 | } |
| 2435 | |
Joe Drago | fc4144e | 2019-09-27 20:35:06 -0700 | [diff] [blame] | 2436 | // Skipping the following 52 bytes here: |
| 2437 | // ------------------------------------ |
| 2438 | // const unsigned int(32)[2] reserved = 0; |
| 2439 | // template int(16) layer = 0; |
| 2440 | // template int(16) alternate_group = 0; |
| 2441 | // template int(16) volume = {if track_is_audio 0x0100 else 0}; |
| 2442 | // const unsigned int(16) reserved = 0; |
| 2443 | // template int(32)[9] matrix= { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 }; // unity matrix |
| 2444 | CHECK(avifROStreamSkip(&s, 52)); |
| 2445 | |
| 2446 | uint32_t width, height; |
| 2447 | CHECK(avifROStreamReadU32(&s, &width)); // unsigned int(32) width; |
| 2448 | CHECK(avifROStreamReadU32(&s, &height)); // unsigned int(32) height; |
| 2449 | track->width = width >> 16; |
| 2450 | track->height = height >> 16; |
| 2451 | |
Wan-Teh Chang | bd1492e | 2021-08-06 16:08:14 -0700 | [diff] [blame] | 2452 | if ((track->width == 0) || (track->height == 0)) { |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 2453 | avifDiagnosticsPrintf(diag, "Track ID [%u] has an invalid size [%ux%u]", track->id, track->width, track->height); |
| 2454 | return AVIF_FALSE; |
| 2455 | } |
Wan-Teh Chang | bd1492e | 2021-08-06 16:08:14 -0700 | [diff] [blame] | 2456 | if (track->width > (imageSizeLimit / track->height)) { |
| 2457 | avifDiagnosticsPrintf(diag, "Track ID [%u] size is too large [%ux%u]", track->id, track->width, track->height); |
| 2458 | return AVIF_FALSE; |
| 2459 | } |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 2460 | |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2461 | // TODO: support scaling based on width/height track header info? |
| 2462 | |
| 2463 | track->id = trackID; |
| 2464 | return AVIF_TRUE; |
| 2465 | } |
| 2466 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2467 | static avifBool avifParseMediaHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2468 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2469 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[mdhd]"); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2470 | |
| 2471 | uint8_t version; |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 2472 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2473 | |
| 2474 | uint32_t ignored32, mediaTimescale, mediaDuration32; |
| 2475 | uint64_t ignored64, mediaDuration64; |
| 2476 | if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2477 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time; |
| 2478 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time; |
| 2479 | CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale; |
| 2480 | CHECK(avifROStreamReadU64(&s, &mediaDuration64)); // unsigned int(64) duration; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2481 | track->mediaDuration = mediaDuration64; |
| 2482 | } else if (version == 0) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2483 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time; |
| 2484 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time; |
| 2485 | CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale; |
| 2486 | CHECK(avifROStreamReadU32(&s, &mediaDuration32)); // unsigned int(32) duration; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2487 | track->mediaDuration = (uint64_t)mediaDuration32; |
| 2488 | } else { |
| 2489 | // Unsupported version |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2490 | avifDiagnosticsPrintf(diag, "Box[mdhd] has an unsupported version [%u]", version); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2491 | return AVIF_FALSE; |
| 2492 | } |
| 2493 | |
| 2494 | track->mediaTimescale = mediaTimescale; |
| 2495 | return AVIF_TRUE; |
| 2496 | } |
| 2497 | |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2498 | static avifBool avifParseChunkOffsetBox(avifSampleTable * sampleTable, avifBool largeOffsets, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2499 | { |
Wan-Teh Chang | 72de166 | 2021-05-28 18:11:11 -0700 | [diff] [blame] | 2500 | BEGIN_STREAM(s, raw, rawLen, diag, largeOffsets ? "Box[co64]" : "Box[stco]"); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2501 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2502 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2503 | |
| 2504 | uint32_t entryCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2505 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2506 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 2507 | uint64_t offset; |
| 2508 | if (largeOffsets) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2509 | CHECK(avifROStreamReadU64(&s, &offset)); // unsigned int(32) chunk_offset; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2510 | } else { |
| 2511 | uint32_t offset32; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2512 | CHECK(avifROStreamReadU32(&s, &offset32)); // unsigned int(32) chunk_offset; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2513 | offset = (uint64_t)offset32; |
| 2514 | } |
| 2515 | |
| 2516 | avifSampleTableChunk * chunk = (avifSampleTableChunk *)avifArrayPushPtr(&sampleTable->chunks); |
| 2517 | chunk->offset = offset; |
| 2518 | } |
| 2519 | return AVIF_TRUE; |
| 2520 | } |
| 2521 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2522 | static avifBool avifParseSampleToChunkBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2523 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2524 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[stsc]"); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2525 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2526 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2527 | |
| 2528 | uint32_t entryCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2529 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
Joe Drago | 859c910 | 2021-03-11 18:06:40 -0800 | [diff] [blame] | 2530 | uint32_t prevFirstChunk = 0; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2531 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 2532 | avifSampleTableSampleToChunk * sampleToChunk = (avifSampleTableSampleToChunk *)avifArrayPushPtr(&sampleTable->sampleToChunks); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2533 | CHECK(avifROStreamReadU32(&s, &sampleToChunk->firstChunk)); // unsigned int(32) first_chunk; |
| 2534 | CHECK(avifROStreamReadU32(&s, &sampleToChunk->samplesPerChunk)); // unsigned int(32) samples_per_chunk; |
| 2535 | CHECK(avifROStreamReadU32(&s, &sampleToChunk->sampleDescriptionIndex)); // unsigned int(32) sample_description_index; |
Wan-Teh Chang | 2456d2f | 2021-03-11 17:31:03 -0800 | [diff] [blame] | 2536 | // The first_chunk fields should start with 1 and be strictly increasing. |
| 2537 | if (i == 0) { |
| 2538 | if (sampleToChunk->firstChunk != 1) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2539 | avifDiagnosticsPrintf(diag, "Box[stsc] does not begin with chunk 1 [%u]", sampleToChunk->firstChunk); |
Wan-Teh Chang | 2456d2f | 2021-03-11 17:31:03 -0800 | [diff] [blame] | 2540 | return AVIF_FALSE; |
| 2541 | } |
| 2542 | } else { |
| 2543 | if (sampleToChunk->firstChunk <= prevFirstChunk) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2544 | avifDiagnosticsPrintf(diag, "Box[stsc] chunks are not strictly increasing"); |
Wan-Teh Chang | 2456d2f | 2021-03-11 17:31:03 -0800 | [diff] [blame] | 2545 | return AVIF_FALSE; |
| 2546 | } |
| 2547 | } |
| 2548 | prevFirstChunk = sampleToChunk->firstChunk; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2549 | } |
| 2550 | return AVIF_TRUE; |
| 2551 | } |
| 2552 | |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2553 | static avifBool avifParseSampleSizeBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2554 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2555 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[stsz]"); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2556 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2557 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2558 | |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 2559 | uint32_t allSamplesSize, sampleCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2560 | CHECK(avifROStreamReadU32(&s, &allSamplesSize)); // unsigned int(32) sample_size; |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 2561 | CHECK(avifROStreamReadU32(&s, &sampleCount)); // unsigned int(32) sample_count; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2562 | |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 2563 | if (allSamplesSize > 0) { |
| 2564 | sampleTable->allSamplesSize = allSamplesSize; |
| 2565 | } else { |
| 2566 | for (uint32_t i = 0; i < sampleCount; ++i) { |
| 2567 | avifSampleTableSampleSize * sampleSize = (avifSampleTableSampleSize *)avifArrayPushPtr(&sampleTable->sampleSizes); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2568 | CHECK(avifROStreamReadU32(&s, &sampleSize->size)); // unsigned int(32) entry_size; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2569 | } |
| 2570 | } |
| 2571 | return AVIF_TRUE; |
| 2572 | } |
| 2573 | |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2574 | static avifBool avifParseSyncSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2575 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2576 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[stss]"); |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2577 | |
| 2578 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
| 2579 | |
| 2580 | uint32_t entryCount; |
| 2581 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
| 2582 | |
| 2583 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 2584 | uint32_t sampleNumber = 0; |
| 2585 | CHECK(avifROStreamReadU32(&s, &sampleNumber)); // unsigned int(32) sample_number; |
| 2586 | avifSyncSample * syncSample = (avifSyncSample *)avifArrayPushPtr(&sampleTable->syncSamples); |
| 2587 | syncSample->sampleNumber = sampleNumber; |
| 2588 | } |
| 2589 | return AVIF_TRUE; |
| 2590 | } |
| 2591 | |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2592 | static avifBool avifParseTimeToSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2593 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2594 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[stts]"); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2595 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2596 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2597 | |
| 2598 | uint32_t entryCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2599 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2600 | |
| 2601 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 2602 | avifSampleTableTimeToSample * timeToSample = (avifSampleTableTimeToSample *)avifArrayPushPtr(&sampleTable->timeToSamples); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2603 | CHECK(avifROStreamReadU32(&s, &timeToSample->sampleCount)); // unsigned int(32) sample_count; |
| 2604 | CHECK(avifROStreamReadU32(&s, &timeToSample->sampleDelta)); // unsigned int(32) sample_delta; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2605 | } |
| 2606 | return AVIF_TRUE; |
| 2607 | } |
| 2608 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2609 | static avifBool avifParseSampleDescriptionBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 2610 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2611 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[stsd]"); |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 2612 | |
| 2613 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
| 2614 | |
| 2615 | uint32_t entryCount; |
| 2616 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
| 2617 | |
| 2618 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 2619 | avifBoxHeader sampleEntryHeader; |
| 2620 | CHECK(avifROStreamReadBoxHeader(&s, &sampleEntryHeader)); |
| 2621 | |
| 2622 | avifSampleDescription * description = (avifSampleDescription *)avifArrayPushPtr(&sampleTable->sampleDescriptions); |
Wan-Teh Chang | f732a4d | 2022-01-21 15:56:35 -0800 | [diff] [blame] | 2623 | if (!avifArrayCreate(&description->properties, sizeof(avifProperty), 16)) { |
| 2624 | avifArrayPop(&sampleTable->sampleDescriptions); |
| 2625 | return AVIF_FALSE; |
| 2626 | } |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 2627 | memcpy(description->format, sampleEntryHeader.type, sizeof(description->format)); |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 2628 | size_t remainingBytes = avifROStreamRemainingBytes(&s); |
| 2629 | if (!memcmp(description->format, "av01", 4) && (remainingBytes > VISUALSAMPLEENTRY_SIZE)) { |
Joe Drago | 11d2359 | 2021-01-05 14:18:57 -0800 | [diff] [blame] | 2630 | CHECK(avifParseItemPropertyContainerBox(&description->properties, |
| 2631 | avifROStreamCurrent(&s) + VISUALSAMPLEENTRY_SIZE, |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2632 | remainingBytes - VISUALSAMPLEENTRY_SIZE, |
| 2633 | diag)); |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 2634 | } |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 2635 | |
| 2636 | CHECK(avifROStreamSkip(&s, sampleEntryHeader.size)); |
| 2637 | } |
| 2638 | return AVIF_TRUE; |
| 2639 | } |
| 2640 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2641 | static avifBool avifParseSampleTableBox(avifTrack * track, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2642 | { |
| 2643 | if (track->sampleTable) { |
| 2644 | // A TrackBox may only have one SampleTable |
Joe Drago | ba4d67d | 2021-05-04 12:34:37 -0700 | [diff] [blame] | 2645 | avifDiagnosticsPrintf(diag, "Duplicate Box[stbl] for a single track detected"); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2646 | return AVIF_FALSE; |
| 2647 | } |
| 2648 | track->sampleTable = avifSampleTableCreate(); |
| 2649 | |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2650 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[stbl]"); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2651 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2652 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2653 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2654 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2655 | |
| 2656 | if (!memcmp(header.type, "stco", 4)) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2657 | CHECK(avifParseChunkOffsetBox(track->sampleTable, AVIF_FALSE, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2658 | } else if (!memcmp(header.type, "co64", 4)) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2659 | CHECK(avifParseChunkOffsetBox(track->sampleTable, AVIF_TRUE, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2660 | } else if (!memcmp(header.type, "stsc", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2661 | CHECK(avifParseSampleToChunkBox(track->sampleTable, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2662 | } else if (!memcmp(header.type, "stsz", 4)) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2663 | CHECK(avifParseSampleSizeBox(track->sampleTable, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2664 | } else if (!memcmp(header.type, "stss", 4)) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2665 | CHECK(avifParseSyncSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2666 | } else if (!memcmp(header.type, "stts", 4)) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2667 | CHECK(avifParseTimeToSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 2668 | } else if (!memcmp(header.type, "stsd", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2669 | CHECK(avifParseSampleDescriptionBox(track->sampleTable, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2670 | } |
| 2671 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2672 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2673 | } |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2674 | return AVIF_TRUE; |
| 2675 | } |
| 2676 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2677 | static avifBool avifParseMediaInformationBox(avifTrack * track, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2678 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2679 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[minf]"); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2680 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2681 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2682 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2683 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2684 | |
| 2685 | if (!memcmp(header.type, "stbl", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2686 | CHECK(avifParseSampleTableBox(track, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2687 | } |
| 2688 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2689 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2690 | } |
| 2691 | return AVIF_TRUE; |
| 2692 | } |
| 2693 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2694 | static avifBool avifParseMediaBox(avifTrack * track, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2695 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2696 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[mdia]"); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2697 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2698 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2699 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2700 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2701 | |
| 2702 | if (!memcmp(header.type, "mdhd", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2703 | CHECK(avifParseMediaHeaderBox(track, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2704 | } else if (!memcmp(header.type, "minf", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2705 | CHECK(avifParseMediaInformationBox(track, avifROStreamCurrent(&s), header.size, diag)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2706 | } |
| 2707 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2708 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2709 | } |
| 2710 | return AVIF_TRUE; |
| 2711 | } |
| 2712 | |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2713 | static avifBool avifTrackReferenceBox(avifTrack * track, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2714 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2715 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[tref]"); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2716 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2717 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2718 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2719 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2720 | |
| 2721 | if (!memcmp(header.type, "auxl", 4)) { |
| 2722 | uint32_t toID; |
Joe Drago | ceb2fa0 | 2021-01-29 18:14:55 -0800 | [diff] [blame] | 2723 | CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) track_IDs[]; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2724 | 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] | 2725 | track->auxForID = toID; |
Yuan Tong | e4850be | 2021-01-22 14:21:25 +0800 | [diff] [blame] | 2726 | } else if (!memcmp(header.type, "prem", 4)) { |
| 2727 | uint32_t byID; |
Joe Drago | ceb2fa0 | 2021-01-29 18:14:55 -0800 | [diff] [blame] | 2728 | CHECK(avifROStreamReadU32(&s, &byID)); // unsigned int(32) track_IDs[]; |
| 2729 | 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] | 2730 | track->premByID = byID; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2731 | } else { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2732 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2733 | } |
| 2734 | } |
| 2735 | return AVIF_TRUE; |
| 2736 | } |
| 2737 | |
Wan-Teh Chang | 980d585 | 2021-08-03 20:02:38 -0700 | [diff] [blame] | 2738 | static avifBool avifParseTrackBox(avifDecoderData * data, const uint8_t * raw, size_t rawLen, uint32_t imageSizeLimit) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2739 | { |
Wan-Teh Chang | 5312181 | 2021-05-28 18:02:59 -0700 | [diff] [blame] | 2740 | BEGIN_STREAM(s, raw, rawLen, data->diag, "Box[trak]"); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2741 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2742 | avifTrack * track = avifDecoderDataCreateTrack(data); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2743 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2744 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2745 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2746 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2747 | |
| 2748 | if (!memcmp(header.type, "tkhd", 4)) { |
Wan-Teh Chang | 980d585 | 2021-08-03 20:02:38 -0700 | [diff] [blame] | 2749 | CHECK(avifParseTrackHeaderBox(track, avifROStreamCurrent(&s), header.size, imageSizeLimit, data->diag)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2750 | } else if (!memcmp(header.type, "meta", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2751 | CHECK(avifParseMetaBox(track->meta, avifROStreamCurrent(&s), header.size, data->diag)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2752 | } else if (!memcmp(header.type, "mdia", 4)) { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2753 | CHECK(avifParseMediaBox(track, avifROStreamCurrent(&s), header.size, data->diag)); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2754 | } else if (!memcmp(header.type, "tref", 4)) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2755 | CHECK(avifTrackReferenceBox(track, avifROStreamCurrent(&s), header.size, data->diag)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2756 | } |
| 2757 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2758 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2759 | } |
| 2760 | return AVIF_TRUE; |
| 2761 | } |
| 2762 | |
Joe Drago | b314e49 | 2021-09-14 16:57:00 -0700 | [diff] [blame] | 2763 | static avifBool avifParseMovieBox(avifDecoderData * data, const uint8_t * raw, size_t rawLen, uint32_t imageSizeLimit) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2764 | { |
Wan-Teh Chang | 5312181 | 2021-05-28 18:02:59 -0700 | [diff] [blame] | 2765 | BEGIN_STREAM(s, raw, rawLen, data->diag, "Box[moov]"); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2766 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2767 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2768 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2769 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2770 | |
| 2771 | if (!memcmp(header.type, "trak", 4)) { |
Wan-Teh Chang | 980d585 | 2021-08-03 20:02:38 -0700 | [diff] [blame] | 2772 | CHECK(avifParseTrackBox(data, avifROStreamCurrent(&s), header.size, imageSizeLimit)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2773 | } |
| 2774 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2775 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 2776 | } |
| 2777 | return AVIF_TRUE; |
| 2778 | } |
| 2779 | |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2780 | static avifBool avifParseFileTypeBox(avifFileType * ftyp, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2781 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2782 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[ftyp]"); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2783 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2784 | CHECK(avifROStreamRead(&s, ftyp->majorBrand, 4)); |
| 2785 | CHECK(avifROStreamReadU32(&s, &ftyp->minorVersion)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2786 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2787 | size_t compatibleBrandsBytes = avifROStreamRemainingBytes(&s); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2788 | if ((compatibleBrandsBytes % 4) != 0) { |
Joe Drago | 4886729 | 2021-05-04 13:23:08 -0700 | [diff] [blame] | 2789 | avifDiagnosticsPrintf(diag, "Box[ftyp] contains a compatible brands section that isn't divisible by 4 [%zu]", compatibleBrandsBytes); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2790 | return AVIF_FALSE; |
| 2791 | } |
Wan-Teh Chang | 6da0a88 | 2020-07-01 12:19:31 -0700 | [diff] [blame] | 2792 | ftyp->compatibleBrands = avifROStreamCurrent(&s); |
| 2793 | CHECK(avifROStreamSkip(&s, compatibleBrandsBytes)); |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2794 | ftyp->compatibleBrandsCount = (int)compatibleBrandsBytes / 4; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2795 | |
| 2796 | return AVIF_TRUE; |
| 2797 | } |
| 2798 | |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2799 | static avifBool avifFileTypeHasBrand(avifFileType * ftyp, const char * brand); |
Wan-Teh Chang | 6fc1758 | 2020-09-24 15:16:37 -0700 | [diff] [blame] | 2800 | static avifBool avifFileTypeIsCompatible(avifFileType * ftyp); |
| 2801 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2802 | static avifResult avifParse(avifDecoder * decoder) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2803 | { |
Joe Drago | 9aa931f | 2020-09-24 13:10:11 -0700 | [diff] [blame] | 2804 | // Note: this top-level function is the only avifParse*() function that returns avifResult instead of avifBool. |
| 2805 | // Be sure to use CHECKERR() in this function with an explicit error result instead of simply using CHECK(). |
| 2806 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2807 | avifResult readResult; |
Wan-Teh Chang | 3b8b81c | 2020-09-28 10:35:08 -0700 | [diff] [blame] | 2808 | uint64_t parseOffset = 0; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2809 | avifDecoderData * data = decoder->data; |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2810 | avifBool ftypSeen = AVIF_FALSE; |
| 2811 | avifBool metaSeen = AVIF_FALSE; |
| 2812 | avifBool moovSeen = AVIF_FALSE; |
| 2813 | avifBool needsMeta = AVIF_FALSE; |
| 2814 | avifBool needsMoov = AVIF_FALSE; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2815 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2816 | for (;;) { |
| 2817 | // Read just enough to get the next box header (a max of 32 bytes) |
| 2818 | avifROData headerContents; |
Wan-Teh Chang | b856dc2 | 2020-09-28 13:00:56 -0700 | [diff] [blame] | 2819 | if ((decoder->io->sizeHint > 0) && (parseOffset > decoder->io->sizeHint)) { |
| 2820 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2821 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2822 | readResult = decoder->io->read(decoder->io, 0, parseOffset, 32, &headerContents); |
| 2823 | if (readResult != AVIF_RESULT_OK) { |
| 2824 | return readResult; |
| 2825 | } |
| 2826 | if (!headerContents.size) { |
| 2827 | // 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] | 2828 | // we've reached the end of the file with no errors. Hooray! |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2829 | break; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2830 | } |
| 2831 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2832 | // Parse the header, and find out how many bytes it actually was |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2833 | BEGIN_STREAM(headerStream, headerContents.data, headerContents.size, &decoder->diag, "File-level box header"); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2834 | avifBoxHeader header; |
Joe Drago | 468ded8 | 2020-09-24 12:52:51 -0700 | [diff] [blame] | 2835 | CHECKERR(avifROStreamReadBoxHeaderPartial(&headerStream, &header), AVIF_RESULT_BMFF_PARSE_FAILED); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2836 | parseOffset += headerStream.offset; |
Wan-Teh Chang | b856dc2 | 2020-09-28 13:00:56 -0700 | [diff] [blame] | 2837 | assert((decoder->io->sizeHint == 0) || (parseOffset <= decoder->io->sizeHint)); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2838 | |
| 2839 | // Try to get the remainder of the box, if necessary |
| 2840 | avifROData boxContents = AVIF_DATA_EMPTY; |
| 2841 | |
| 2842 | // TODO: reorg this code to only do these memcmps once each |
Wan-Teh Chang | b309e98 | 2021-02-20 16:59:36 -0800 | [diff] [blame] | 2843 | 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] | 2844 | readResult = decoder->io->read(decoder->io, 0, parseOffset, header.size, &boxContents); |
| 2845 | if (readResult != AVIF_RESULT_OK) { |
| 2846 | return readResult; |
| 2847 | } |
| 2848 | if (boxContents.size != header.size) { |
| 2849 | // A truncated box, bail out |
Joe Drago | fe5d5e4 | 2020-09-24 13:07:58 -0700 | [diff] [blame] | 2850 | return AVIF_RESULT_TRUNCATED_DATA; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2851 | } |
Wan-Teh Chang | 3b8b81c | 2020-09-28 10:35:08 -0700 | [diff] [blame] | 2852 | } else if (header.size > (UINT64_MAX - parseOffset)) { |
| 2853 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2854 | } |
Wan-Teh Chang | 3b8b81c | 2020-09-28 10:35:08 -0700 | [diff] [blame] | 2855 | parseOffset += header.size; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2856 | |
| 2857 | if (!memcmp(header.type, "ftyp", 4)) { |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2858 | CHECKERR(!ftypSeen, AVIF_RESULT_BMFF_PARSE_FAILED); |
Wan-Teh Chang | 6fc1758 | 2020-09-24 15:16:37 -0700 | [diff] [blame] | 2859 | avifFileType ftyp; |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2860 | CHECKERR(avifParseFileTypeBox(&ftyp, boxContents.data, boxContents.size, data->diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2861 | if (!avifFileTypeIsCompatible(&ftyp)) { |
Wan-Teh Chang | 6fc1758 | 2020-09-24 15:16:37 -0700 | [diff] [blame] | 2862 | return AVIF_RESULT_INVALID_FTYP; |
| 2863 | } |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2864 | ftypSeen = AVIF_TRUE; |
Joe Drago | f131b78 | 2021-09-21 16:52:39 -0700 | [diff] [blame] | 2865 | memcpy(data->majorBrand, ftyp.majorBrand, 4); // Remember the major brand for future AVIF_DECODER_SOURCE_AUTO decisions |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2866 | needsMeta = avifFileTypeHasBrand(&ftyp, "avif"); |
| 2867 | needsMoov = avifFileTypeHasBrand(&ftyp, "avis"); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2868 | } else if (!memcmp(header.type, "meta", 4)) { |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2869 | CHECKERR(!metaSeen, AVIF_RESULT_BMFF_PARSE_FAILED); |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2870 | CHECKERR(avifParseMetaBox(data->meta, boxContents.data, boxContents.size, data->diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2871 | metaSeen = AVIF_TRUE; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2872 | } else if (!memcmp(header.type, "moov", 4)) { |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2873 | CHECKERR(!moovSeen, AVIF_RESULT_BMFF_PARSE_FAILED); |
Joe Drago | b314e49 | 2021-09-14 16:57:00 -0700 | [diff] [blame] | 2874 | CHECKERR(avifParseMovieBox(data, boxContents.data, boxContents.size, decoder->imageSizeLimit), AVIF_RESULT_BMFF_PARSE_FAILED); |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2875 | moovSeen = AVIF_TRUE; |
| 2876 | } |
| 2877 | |
| 2878 | // See if there is enough information to consider Parse() a success and early-out: |
| 2879 | // * If the brand 'avif' is present, require a meta box |
| 2880 | // * If the brand 'avis' is present, require a moov box |
| 2881 | if (ftypSeen && (!needsMeta || metaSeen) && (!needsMoov || moovSeen)) { |
| 2882 | return AVIF_RESULT_OK; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2883 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2884 | } |
Joe Drago | 029375a | 2021-04-28 11:57:56 -0700 | [diff] [blame] | 2885 | if (!ftypSeen) { |
| 2886 | return AVIF_RESULT_INVALID_FTYP; |
| 2887 | } |
| 2888 | if ((needsMeta && !metaSeen) || (needsMoov && !moovSeen)) { |
| 2889 | return AVIF_RESULT_TRUNCATED_DATA; |
| 2890 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2891 | return AVIF_RESULT_OK; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | // --------------------------------------------------------------------------- |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2895 | |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2896 | static avifBool avifFileTypeHasBrand(avifFileType * ftyp, const char * brand) |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2897 | { |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2898 | if (!memcmp(ftyp->majorBrand, brand, 4)) { |
| 2899 | return AVIF_TRUE; |
| 2900 | } |
| 2901 | |
| 2902 | for (int compatibleBrandIndex = 0; compatibleBrandIndex < ftyp->compatibleBrandsCount; ++compatibleBrandIndex) { |
| 2903 | const uint8_t * compatibleBrand = &ftyp->compatibleBrands[4 * compatibleBrandIndex]; |
| 2904 | if (!memcmp(compatibleBrand, brand, 4)) { |
| 2905 | return AVIF_TRUE; |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2906 | } |
| 2907 | } |
Joe Drago | bb39aab | 2020-11-03 19:23:40 -0800 | [diff] [blame] | 2908 | return AVIF_FALSE; |
| 2909 | } |
| 2910 | |
| 2911 | static avifBool avifFileTypeIsCompatible(avifFileType * ftyp) |
| 2912 | { |
| 2913 | return avifFileTypeHasBrand(ftyp, "avif") || avifFileTypeHasBrand(ftyp, "avis"); |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2914 | } |
| 2915 | |
Wan-Teh Chang | e184dc1 | 2020-05-11 12:47:21 -0700 | [diff] [blame] | 2916 | avifBool avifPeekCompatibleFileType(const avifROData * input) |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2917 | { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 2918 | BEGIN_STREAM(s, input->data, input->size, NULL, NULL); |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2919 | |
| 2920 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2921 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame] | 2922 | if (memcmp(header.type, "ftyp", 4)) { |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2923 | return AVIF_FALSE; |
| 2924 | } |
| 2925 | |
| 2926 | avifFileType ftyp; |
| 2927 | memset(&ftyp, 0, sizeof(avifFileType)); |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2928 | avifBool parsed = avifParseFileTypeBox(&ftyp, avifROStreamCurrent(&s), header.size, NULL); |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 2929 | if (!parsed) { |
| 2930 | return AVIF_FALSE; |
| 2931 | } |
| 2932 | return avifFileTypeIsCompatible(&ftyp); |
| 2933 | } |
| 2934 | |
| 2935 | // --------------------------------------------------------------------------- |
| 2936 | |
Joe Drago | 0b05eee | 2019-06-12 13:24:39 -0700 | [diff] [blame] | 2937 | avifDecoder * avifDecoderCreate(void) |
| 2938 | { |
| 2939 | avifDecoder * decoder = (avifDecoder *)avifAlloc(sizeof(avifDecoder)); |
| 2940 | memset(decoder, 0, sizeof(avifDecoder)); |
Joe Drago | ede5c20 | 2020-11-11 09:42:57 -0800 | [diff] [blame] | 2941 | decoder->maxThreads = 1; |
Wan-Teh Chang | 980d585 | 2021-08-03 20:02:38 -0700 | [diff] [blame] | 2942 | decoder->imageSizeLimit = AVIF_DEFAULT_IMAGE_SIZE_LIMIT; |
Joe Drago | d2340b4 | 2021-03-14 13:20:02 -0700 | [diff] [blame] | 2943 | decoder->imageCountLimit = AVIF_DEFAULT_IMAGE_COUNT_LIMIT; |
Joe Drago | 4d102ed | 2021-05-07 14:27:19 -0700 | [diff] [blame] | 2944 | decoder->strictFlags = AVIF_STRICT_ENABLED; |
Joe Drago | 0b05eee | 2019-06-12 13:24:39 -0700 | [diff] [blame] | 2945 | return decoder; |
| 2946 | } |
| 2947 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2948 | static void avifDecoderCleanup(avifDecoder * decoder) |
| 2949 | { |
| 2950 | if (decoder->data) { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2951 | avifDecoderDataDestroy(decoder->data); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2952 | decoder->data = NULL; |
| 2953 | } |
| 2954 | |
| 2955 | if (decoder->image) { |
| 2956 | avifImageDestroy(decoder->image); |
| 2957 | decoder->image = NULL; |
| 2958 | } |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 2959 | avifDiagnosticsClearError(&decoder->diag); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2960 | } |
| 2961 | |
Joe Drago | 0b05eee | 2019-06-12 13:24:39 -0700 | [diff] [blame] | 2962 | void avifDecoderDestroy(avifDecoder * decoder) |
| 2963 | { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2964 | avifDecoderCleanup(decoder); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2965 | avifIODestroy(decoder->io); |
Joe Drago | 0b05eee | 2019-06-12 13:24:39 -0700 | [diff] [blame] | 2966 | avifFree(decoder); |
| 2967 | } |
| 2968 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2969 | avifResult avifDecoderSetSource(avifDecoder * decoder, avifDecoderSource source) |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2970 | { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2971 | decoder->requestedSource = source; |
| 2972 | return avifDecoderReset(decoder); |
| 2973 | } |
Joe Drago | 33f1d36 | 2019-02-13 16:46:22 -0800 | [diff] [blame] | 2974 | |
Wan-Teh Chang | e67f936 | 2020-10-12 16:07:57 -0700 | [diff] [blame] | 2975 | void avifDecoderSetIO(avifDecoder * decoder, avifIO * io) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2976 | { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2977 | avifIODestroy(decoder->io); |
| 2978 | decoder->io = io; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2979 | } |
| 2980 | |
Wan-Teh Chang | 7de4445 | 2020-10-08 11:43:18 -0700 | [diff] [blame] | 2981 | avifResult avifDecoderSetIOMemory(avifDecoder * decoder, const uint8_t * data, size_t size) |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2982 | { |
Wan-Teh Chang | 7de4445 | 2020-10-08 11:43:18 -0700 | [diff] [blame] | 2983 | avifIO * io = avifIOCreateMemoryReader(data, size); |
Wan-Teh Chang | 31c7c1a | 2020-10-13 16:45:41 -0700 | [diff] [blame] | 2984 | assert(io); |
Wan-Teh Chang | e67f936 | 2020-10-12 16:07:57 -0700 | [diff] [blame] | 2985 | avifDecoderSetIO(decoder, io); |
| 2986 | return AVIF_RESULT_OK; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2987 | } |
| 2988 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2989 | avifResult avifDecoderSetIOFile(avifDecoder * decoder, const char * filename) |
| 2990 | { |
Wan-Teh Chang | 7de4445 | 2020-10-08 11:43:18 -0700 | [diff] [blame] | 2991 | avifIO * io = avifIOCreateFileReader(filename); |
| 2992 | if (!io) { |
Wan-Teh Chang | 31c7c1a | 2020-10-13 16:45:41 -0700 | [diff] [blame] | 2993 | return AVIF_RESULT_IO_ERROR; |
Wan-Teh Chang | 7de4445 | 2020-10-08 11:43:18 -0700 | [diff] [blame] | 2994 | } |
Wan-Teh Chang | e67f936 | 2020-10-12 16:07:57 -0700 | [diff] [blame] | 2995 | avifDecoderSetIO(decoder, io); |
| 2996 | return AVIF_RESULT_OK; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 2999 | // 0-byte extents are ignored/overwritten during the merge, as they are the signal from helper |
| 3000 | // functions that no extent was necessary for this given sample. If both provided extents are |
| 3001 | // >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] | 3002 | static avifResult avifExtentMerge(avifExtent * dst, const avifExtent * src) |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 3003 | { |
| 3004 | if (!dst->size) { |
Wan-Teh Chang | 2f225b0 | 2022-03-16 22:30:38 -0700 | [diff] [blame] | 3005 | *dst = *src; |
Wan-Teh Chang | d69958e | 2020-11-17 12:14:27 -0800 | [diff] [blame] | 3006 | return AVIF_RESULT_OK; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 3007 | } |
| 3008 | if (!src->size) { |
Wan-Teh Chang | d69958e | 2020-11-17 12:14:27 -0800 | [diff] [blame] | 3009 | return AVIF_RESULT_OK; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | const uint64_t minExtent1 = dst->offset; |
| 3013 | const uint64_t maxExtent1 = dst->offset + dst->size; |
| 3014 | const uint64_t minExtent2 = src->offset; |
| 3015 | const uint64_t maxExtent2 = src->offset + src->size; |
| 3016 | dst->offset = AVIF_MIN(minExtent1, minExtent2); |
Wan-Teh Chang | d69958e | 2020-11-17 12:14:27 -0800 | [diff] [blame] | 3017 | const uint64_t extentLength = AVIF_MAX(maxExtent1, maxExtent2) - dst->offset; |
| 3018 | if (extentLength > SIZE_MAX) { |
| 3019 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 3020 | } |
| 3021 | dst->size = (size_t)extentLength; |
| 3022 | return AVIF_RESULT_OK; |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 3023 | } |
| 3024 | |
Joe Drago | 93d5bf9 | 2020-11-17 14:31:57 -0800 | [diff] [blame] | 3025 | avifResult avifDecoderNthImageMaxExtent(const avifDecoder * decoder, uint32_t frameIndex, avifExtent * outExtent) |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 3026 | { |
| 3027 | if (!decoder->data) { |
| 3028 | // Nothing has been parsed yet |
| 3029 | return AVIF_RESULT_NO_CONTENT; |
| 3030 | } |
| 3031 | |
| 3032 | memset(outExtent, 0, sizeof(avifExtent)); |
| 3033 | |
Joe Drago | 93d5bf9 | 2020-11-17 14:31:57 -0800 | [diff] [blame] | 3034 | uint32_t startFrameIndex = avifDecoderNearestKeyframe(decoder, frameIndex); |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 3035 | uint32_t endFrameIndex = frameIndex; |
| 3036 | for (uint32_t currentFrameIndex = startFrameIndex; currentFrameIndex <= endFrameIndex; ++currentFrameIndex) { |
| 3037 | for (unsigned int tileIndex = 0; tileIndex < decoder->data->tiles.count; ++tileIndex) { |
| 3038 | avifTile * tile = &decoder->data->tiles.tile[tileIndex]; |
| 3039 | if (currentFrameIndex >= tile->input->samples.count) { |
| 3040 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
| 3041 | } |
| 3042 | |
| 3043 | avifDecodeSample * sample = &tile->input->samples.sample[currentFrameIndex]; |
| 3044 | avifExtent sampleExtent; |
| 3045 | if (sample->itemID) { |
| 3046 | // The data comes from an item. Let avifDecoderItemMaxExtent() do the heavy lifting. |
| 3047 | |
| 3048 | avifDecoderItem * item = avifMetaFindItem(decoder->data->meta, sample->itemID); |
Joe Drago | c0758eb | 2021-06-23 17:52:47 -0700 | [diff] [blame] | 3049 | avifResult maxExtentResult = avifDecoderItemMaxExtent(item, sample, &sampleExtent); |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 3050 | if (maxExtentResult != AVIF_RESULT_OK) { |
| 3051 | return maxExtentResult; |
| 3052 | } |
| 3053 | } else { |
| 3054 | // The data likely comes from a sample table. Use the sample position directly. |
| 3055 | |
| 3056 | sampleExtent.offset = sample->offset; |
| 3057 | sampleExtent.size = sample->size; |
| 3058 | } |
| 3059 | |
| 3060 | if (sampleExtent.size > UINT64_MAX - sampleExtent.offset) { |
| 3061 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 3062 | } |
| 3063 | |
Wan-Teh Chang | d69958e | 2020-11-17 12:14:27 -0800 | [diff] [blame] | 3064 | avifResult extentMergeResult = avifExtentMerge(outExtent, &sampleExtent); |
| 3065 | if (extentMergeResult != AVIF_RESULT_OK) { |
| 3066 | return extentMergeResult; |
| 3067 | } |
Joe Drago | 4bcdfde | 2020-11-13 17:50:55 -0800 | [diff] [blame] | 3068 | } |
| 3069 | } |
| 3070 | return AVIF_RESULT_OK; |
| 3071 | } |
| 3072 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3073 | static avifResult avifDecoderPrepareSample(avifDecoder * decoder, avifDecodeSample * sample, size_t partialByteCount) |
| 3074 | { |
| 3075 | if (!sample->data.size || sample->partialData) { |
| 3076 | // This sample hasn't been read from IO or had its extents fully merged yet. |
| 3077 | |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 3078 | size_t bytesToRead = sample->size; |
| 3079 | if (partialByteCount && (bytesToRead > partialByteCount)) { |
| 3080 | bytesToRead = partialByteCount; |
| 3081 | } |
| 3082 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3083 | if (sample->itemID) { |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 3084 | // The data comes from an item. Let avifDecoderItemRead() do the heavy lifting. |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3085 | |
| 3086 | avifDecoderItem * item = avifMetaFindItem(decoder->data->meta, sample->itemID); |
| 3087 | avifROData itemContents; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 3088 | avifResult readResult = avifDecoderItemRead(item, decoder->io, &itemContents, sample->offset, bytesToRead, &decoder->diag); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3089 | if (readResult != AVIF_RESULT_OK) { |
| 3090 | return readResult; |
| 3091 | } |
| 3092 | |
Wan-Teh Chang | 4548b16 | 2020-11-06 11:48:25 -0800 | [diff] [blame] | 3093 | // 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] | 3094 | // or by mergedExtents; just reuse the buffer here. |
Wan-Teh Chang | 2f225b0 | 2022-03-16 22:30:38 -0700 | [diff] [blame] | 3095 | sample->data = itemContents; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3096 | sample->ownsData = AVIF_FALSE; |
| 3097 | sample->partialData = item->partialMergedExtents; |
| 3098 | } else { |
| 3099 | // The data likely comes from a sample table. Pull the sample and make a copy if necessary. |
| 3100 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3101 | avifROData sampleContents; |
Wan-Teh Chang | b856dc2 | 2020-09-28 13:00:56 -0700 | [diff] [blame] | 3102 | if ((decoder->io->sizeHint > 0) && (sample->offset > decoder->io->sizeHint)) { |
| 3103 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 3104 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3105 | avifResult readResult = decoder->io->read(decoder->io, 0, sample->offset, bytesToRead, &sampleContents); |
| 3106 | if (readResult != AVIF_RESULT_OK) { |
| 3107 | return readResult; |
| 3108 | } |
| 3109 | if (sampleContents.size != bytesToRead) { |
| 3110 | return AVIF_RESULT_TRUNCATED_DATA; |
| 3111 | } |
| 3112 | |
| 3113 | sample->ownsData = !decoder->io->persistent; |
| 3114 | sample->partialData = (bytesToRead != sample->size); |
| 3115 | if (decoder->io->persistent) { |
Wan-Teh Chang | 2f225b0 | 2022-03-16 22:30:38 -0700 | [diff] [blame] | 3116 | sample->data = sampleContents; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3117 | } else { |
| 3118 | avifRWDataSet((avifRWData *)&sample->data, sampleContents.data, sampleContents.size); |
| 3119 | } |
| 3120 | } |
| 3121 | } |
| 3122 | return AVIF_RESULT_OK; |
| 3123 | } |
| 3124 | |
| 3125 | avifResult avifDecoderParse(avifDecoder * decoder) |
| 3126 | { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 3127 | avifDiagnosticsClearError(&decoder->diag); |
| 3128 | |
Wan-Teh Chang | 980d585 | 2021-08-03 20:02:38 -0700 | [diff] [blame] | 3129 | // An imageSizeLimit greater than AVIF_DEFAULT_IMAGE_SIZE_LIMIT and the special value of 0 to |
| 3130 | // disable the limit are not yet implemented. |
| 3131 | if ((decoder->imageSizeLimit > AVIF_DEFAULT_IMAGE_SIZE_LIMIT) || (decoder->imageSizeLimit == 0)) { |
| 3132 | return AVIF_RESULT_NOT_IMPLEMENTED; |
| 3133 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3134 | if (!decoder->io || !decoder->io->read) { |
Wan-Teh Chang | 31c7c1a | 2020-10-13 16:45:41 -0700 | [diff] [blame] | 3135 | return AVIF_RESULT_IO_NOT_SET; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3136 | } |
| 3137 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3138 | // Cleanup anything lingering in the decoder |
| 3139 | avifDecoderCleanup(decoder); |
| 3140 | |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 3141 | // ----------------------------------------------------------------------- |
| 3142 | // Parse BMFF boxes |
| 3143 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 3144 | decoder->data = avifDecoderDataCreate(); |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 3145 | decoder->data->diag = &decoder->diag; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3146 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3147 | avifResult parseResult = avifParse(decoder); |
| 3148 | if (parseResult != AVIF_RESULT_OK) { |
| 3149 | return parseResult; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 3150 | } |
| 3151 | |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 3152 | // Walk the decoded items (if any) and harvest ispe |
| 3153 | avifDecoderData * data = decoder->data; |
| 3154 | for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) { |
| 3155 | avifDecoderItem * item = &data->meta->items.item[itemIndex]; |
| 3156 | if (!item->size) { |
| 3157 | continue; |
| 3158 | } |
| 3159 | if (item->hasUnsupportedEssentialProperty) { |
| 3160 | // An essential property isn't supported by libavif; ignore the item. |
| 3161 | continue; |
| 3162 | } |
| 3163 | avifBool isGrid = (memcmp(item->type, "grid", 4) == 0); |
| 3164 | if (memcmp(item->type, "av01", 4) && !isGrid) { |
| 3165 | // probably exif or some other data |
| 3166 | continue; |
| 3167 | } |
| 3168 | |
| 3169 | const avifProperty * ispeProp = avifPropertyArrayFind(&item->properties, "ispe"); |
| 3170 | if (ispeProp) { |
| 3171 | item->width = ispeProp->u.ispe.width; |
| 3172 | item->height = ispeProp->u.ispe.height; |
| 3173 | |
Wan-Teh Chang | bd1492e | 2021-08-06 16:08:14 -0700 | [diff] [blame] | 3174 | if ((item->width == 0) || (item->height == 0)) { |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 3175 | avifDiagnosticsPrintf(data->diag, "Item ID [%u] has an invalid size [%ux%u]", item->id, item->width, item->height); |
| 3176 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 3177 | } |
Wan-Teh Chang | bd1492e | 2021-08-06 16:08:14 -0700 | [diff] [blame] | 3178 | if (item->width > (decoder->imageSizeLimit / item->height)) { |
| 3179 | avifDiagnosticsPrintf(data->diag, "Item ID [%u] size is too large [%ux%u]", item->id, item->width, item->height); |
| 3180 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 3181 | } |
Wan-Teh Chang | 2c1f627 | 2021-09-13 17:13:13 -0700 | [diff] [blame] | 3182 | } else { |
| 3183 | const avifProperty * auxCProp = avifPropertyArrayFind(&item->properties, "auxC"); |
| 3184 | if (auxCProp && isAlphaURN(auxCProp->u.auxC.auxType)) { |
| 3185 | if (decoder->strictFlags & AVIF_STRICT_ALPHA_ISPE_REQUIRED) { |
| 3186 | avifDiagnosticsPrintf(data->diag, |
| 3187 | "[Strict] Alpha auxiliary image item ID [%u] is missing a mandatory ispe property", |
| 3188 | item->id); |
| 3189 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 3190 | } |
| 3191 | } else { |
| 3192 | avifDiagnosticsPrintf(data->diag, "Item ID [%u] is missing a mandatory ispe property", item->id); |
| 3193 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 3194 | } |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 3195 | } |
| 3196 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3197 | return avifDecoderReset(decoder); |
| 3198 | } |
| 3199 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3200 | static avifCodec * avifCodecCreateInternal(avifCodecChoice choice) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3201 | { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3202 | return avifCodecCreate(choice, AVIF_CODEC_FLAG_CAN_DECODE); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3203 | } |
| 3204 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3205 | static avifResult avifDecoderFlush(avifDecoder * decoder) |
| 3206 | { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 3207 | avifDecoderDataResetCodec(decoder->data); |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3208 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3209 | for (unsigned int i = 0; i < decoder->data->tiles.count; ++i) { |
| 3210 | avifTile * tile = &decoder->data->tiles.tile[i]; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3211 | tile->codec = avifCodecCreateInternal(decoder->codecChoice); |
Joe Drago | 411221a | 2021-11-10 11:12:56 -0800 | [diff] [blame] | 3212 | if (tile->codec) { |
| 3213 | tile->codec->diag = &decoder->diag; |
| 3214 | tile->codec->operatingPoint = tile->operatingPoint; |
| 3215 | tile->codec->allLayers = tile->input->allLayers; |
Joe Drago | 5335535 | 2019-10-28 19:04:51 -0700 | [diff] [blame] | 3216 | } |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3217 | } |
| 3218 | return AVIF_RESULT_OK; |
| 3219 | } |
| 3220 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3221 | avifResult avifDecoderReset(avifDecoder * decoder) |
| 3222 | { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 3223 | avifDiagnosticsClearError(&decoder->diag); |
| 3224 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 3225 | avifDecoderData * data = decoder->data; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3226 | if (!data) { |
| 3227 | // Nothing to reset. |
| 3228 | return AVIF_RESULT_OK; |
| 3229 | } |
| 3230 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3231 | memset(&data->colorGrid, 0, sizeof(data->colorGrid)); |
| 3232 | memset(&data->alphaGrid, 0, sizeof(data->alphaGrid)); |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 3233 | avifDecoderDataClearTiles(data); |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 3234 | |
| 3235 | // Prepare / cleanup decoded image state |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 3236 | if (decoder->image) { |
| 3237 | avifImageDestroy(decoder->image); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 3238 | } |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 3239 | decoder->image = avifImageCreateEmpty(); |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 3240 | decoder->progressiveState = AVIF_PROGRESSIVE_STATE_UNAVAILABLE; |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 3241 | data->cicpSet = AVIF_FALSE; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 3242 | |
Joe Drago | 70cbf60 | 2019-07-24 15:30:55 -0700 | [diff] [blame] | 3243 | memset(&decoder->ioStats, 0, sizeof(decoder->ioStats)); |
| 3244 | |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 3245 | // ----------------------------------------------------------------------- |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3246 | // Build decode input |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 3247 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3248 | data->sourceSampleTable = NULL; // Reset |
| 3249 | if (decoder->requestedSource == AVIF_DECODER_SOURCE_AUTO) { |
Joe Drago | f131b78 | 2021-09-21 16:52:39 -0700 | [diff] [blame] | 3250 | // Honor the major brand (avif or avis) if present, otherwise prefer avis (tracks) if possible. |
| 3251 | if (!memcmp(data->majorBrand, "avis", 4)) { |
| 3252 | data->source = AVIF_DECODER_SOURCE_TRACKS; |
| 3253 | } else if (!memcmp(data->majorBrand, "avif", 4)) { |
| 3254 | data->source = AVIF_DECODER_SOURCE_PRIMARY_ITEM; |
| 3255 | } else if (data->tracks.count > 0) { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3256 | data->source = AVIF_DECODER_SOURCE_TRACKS; |
| 3257 | } else { |
| 3258 | data->source = AVIF_DECODER_SOURCE_PRIMARY_ITEM; |
Joe Drago | 7637023 | 2019-07-16 11:00:52 -0700 | [diff] [blame] | 3259 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3260 | } else { |
| 3261 | data->source = decoder->requestedSource; |
Joe Drago | 7637023 | 2019-07-16 11:00:52 -0700 | [diff] [blame] | 3262 | } |
| 3263 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 3264 | const avifPropertyArray * colorProperties = NULL; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3265 | if (data->source == AVIF_DECODER_SOURCE_TRACKS) { |
| 3266 | avifTrack * colorTrack = NULL; |
| 3267 | avifTrack * alphaTrack = NULL; |
| 3268 | |
| 3269 | // Find primary track - this probably needs some better detection |
| 3270 | uint32_t colorTrackIndex = 0; |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 3271 | for (; colorTrackIndex < data->tracks.count; ++colorTrackIndex) { |
| 3272 | avifTrack * track = &data->tracks.track[colorTrackIndex]; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3273 | if (!track->sampleTable) { |
| 3274 | continue; |
| 3275 | } |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 3276 | 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] | 3277 | continue; |
| 3278 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3279 | if (!track->sampleTable->chunks.count) { |
| 3280 | continue; |
| 3281 | } |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 3282 | if (!avifSampleTableHasFormat(track->sampleTable, "av01")) { |
| 3283 | continue; |
| 3284 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3285 | if (track->auxForID != 0) { |
| 3286 | continue; |
| 3287 | } |
| 3288 | |
| 3289 | // Found one! |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 3290 | break; |
| 3291 | } |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 3292 | if (colorTrackIndex == data->tracks.count) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 3293 | avifDiagnosticsPrintf(&decoder->diag, "Failed to find AV1 color track"); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3294 | return AVIF_RESULT_NO_CONTENT; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 3295 | } |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 3296 | colorTrack = &data->tracks.track[colorTrackIndex]; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3297 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 3298 | colorProperties = avifSampleTableGetProperties(colorTrack->sampleTable); |
| 3299 | if (!colorProperties) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 3300 | avifDiagnosticsPrintf(&decoder->diag, "Failed to find AV1 color track's color properties"); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 3301 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 3302 | } |
| 3303 | |
| 3304 | // Find Exif and/or XMP metadata, if any |
| 3305 | if (colorTrack->meta) { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3306 | // See the comment above avifDecoderFindMetadata() for the explanation of using 0 here |
| 3307 | avifResult findResult = avifDecoderFindMetadata(decoder, colorTrack->meta, decoder->image, 0); |
| 3308 | if (findResult != AVIF_RESULT_OK) { |
| 3309 | return findResult; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 3310 | } |
| 3311 | } |
| 3312 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3313 | uint32_t alphaTrackIndex = 0; |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 3314 | for (; alphaTrackIndex < data->tracks.count; ++alphaTrackIndex) { |
| 3315 | avifTrack * track = &data->tracks.track[alphaTrackIndex]; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3316 | if (!track->sampleTable) { |
| 3317 | continue; |
| 3318 | } |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 3319 | if (!track->id) { |
| 3320 | continue; |
| 3321 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3322 | if (!track->sampleTable->chunks.count) { |
| 3323 | continue; |
| 3324 | } |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 3325 | if (!avifSampleTableHasFormat(track->sampleTable, "av01")) { |
| 3326 | continue; |
| 3327 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3328 | if (track->auxForID == colorTrack->id) { |
| 3329 | // Found it! |
| 3330 | break; |
| 3331 | } |
| 3332 | } |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 3333 | if (alphaTrackIndex != data->tracks.count) { |
| 3334 | alphaTrack = &data->tracks.track[alphaTrackIndex]; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 3335 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 3336 | |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 3337 | avifTile * colorTile = avifDecoderDataCreateTile(data, colorTrack->width, colorTrack->height, 0); // No way to set operating point via tracks |
Wan-Teh Chang | d891187 | 2022-02-14 14:15:00 -0800 | [diff] [blame] | 3338 | if (!colorTile) { |
| 3339 | return AVIF_RESULT_OUT_OF_MEMORY; |
| 3340 | } |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 3341 | if (!avifCodecDecodeInputFillFromSampleTable(colorTile->input, |
| 3342 | colorTrack->sampleTable, |
| 3343 | decoder->imageCountLimit, |
| 3344 | decoder->io->sizeHint, |
| 3345 | data->diag)) { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3346 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 3347 | } |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 3348 | data->colorTileCount = 1; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3349 | |
| 3350 | if (alphaTrack) { |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 3351 | avifTile * alphaTile = avifDecoderDataCreateTile(data, alphaTrack->width, alphaTrack->height, 0); // No way to set operating point via tracks |
Wan-Teh Chang | d891187 | 2022-02-14 14:15:00 -0800 | [diff] [blame] | 3352 | if (!alphaTile) { |
| 3353 | return AVIF_RESULT_OUT_OF_MEMORY; |
| 3354 | } |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 3355 | if (!avifCodecDecodeInputFillFromSampleTable(alphaTile->input, |
| 3356 | alphaTrack->sampleTable, |
| 3357 | decoder->imageCountLimit, |
| 3358 | decoder->io->sizeHint, |
| 3359 | data->diag)) { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3360 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 3361 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3362 | alphaTile->input->alpha = AVIF_TRUE; |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 3363 | data->alphaTileCount = 1; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3364 | } |
| 3365 | |
| 3366 | // Stash off sample table for future timing information |
| 3367 | data->sourceSampleTable = colorTrack->sampleTable; |
| 3368 | |
| 3369 | // Image sequence timing |
| 3370 | decoder->imageIndex = -1; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3371 | decoder->imageCount = colorTile->input->samples.count; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3372 | decoder->timescale = colorTrack->mediaTimescale; |
| 3373 | decoder->durationInTimescales = colorTrack->mediaDuration; |
| 3374 | if (colorTrack->mediaTimescale) { |
| 3375 | decoder->duration = (double)decoder->durationInTimescales / (double)colorTrack->mediaTimescale; |
| 3376 | } else { |
| 3377 | decoder->duration = 0; |
| 3378 | } |
| 3379 | memset(&decoder->imageTiming, 0, sizeof(decoder->imageTiming)); // to be set in avifDecoderNextImage() |
Joe Drago | 4170085 | 2019-09-26 17:01:43 -0700 | [diff] [blame] | 3380 | |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 3381 | decoder->image->width = colorTrack->width; |
| 3382 | decoder->image->height = colorTrack->height; |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 3383 | decoder->alphaPresent = (alphaTrack != NULL); |
Joe Drago | ceb2fa0 | 2021-01-29 18:14:55 -0800 | [diff] [blame] | 3384 | decoder->image->alphaPremultiplied = decoder->alphaPresent && (colorTrack->premByID == alphaTrack->id); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3385 | } else { |
| 3386 | // Create from items |
| 3387 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3388 | avifDecoderItem * colorItem = NULL; |
| 3389 | avifDecoderItem * alphaItem = NULL; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3390 | |
Joe Drago | 51d1114 | 2021-03-31 07:46:30 -0700 | [diff] [blame] | 3391 | if (data->meta->primaryItemID == 0) { |
| 3392 | // A primary item is required |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 3393 | avifDiagnosticsPrintf(&decoder->diag, "Primary item not specified"); |
Joe Drago | 51d1114 | 2021-03-31 07:46:30 -0700 | [diff] [blame] | 3394 | return AVIF_RESULT_NO_AV1_ITEMS_FOUND; |
| 3395 | } |
| 3396 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 3397 | // Find the colorOBU (primary) item |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 3398 | for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) { |
| 3399 | avifDecoderItem * item = &data->meta->items.item[itemIndex]; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 3400 | if (!item->size) { |
| 3401 | continue; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 3402 | } |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 3403 | if (item->hasUnsupportedEssentialProperty) { |
| 3404 | // An essential property isn't supported by libavif; ignore the item. |
| 3405 | continue; |
| 3406 | } |
Joe Drago | 951a002 | 2020-03-09 16:19:44 -0700 | [diff] [blame] | 3407 | avifBool isGrid = (memcmp(item->type, "grid", 4) == 0); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3408 | if (memcmp(item->type, "av01", 4) && !isGrid) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 3409 | // probably exif or some other data |
| 3410 | continue; |
| 3411 | } |
| 3412 | if (item->thumbnailForID != 0) { |
| 3413 | // It's a thumbnail, skip it |
| 3414 | continue; |
| 3415 | } |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 3416 | if (item->id != data->meta->primaryItemID) { |
| 3417 | // This is not the primary item, skip it |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 3418 | continue; |
| 3419 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 3420 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3421 | if (isGrid) { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3422 | avifROData readData; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 3423 | avifResult readResult = avifDecoderItemRead(item, decoder->io, &readData, 0, 0, data->diag); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3424 | if (readResult != AVIF_RESULT_OK) { |
| 3425 | return readResult; |
| 3426 | } |
Wan-Teh Chang | 980d585 | 2021-08-03 20:02:38 -0700 | [diff] [blame] | 3427 | if (!avifParseImageGridBox(&data->colorGrid, readData.data, readData.size, decoder->imageSizeLimit, data->diag)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3428 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 3429 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3430 | } |
| 3431 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3432 | colorItem = item; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3433 | break; |
| 3434 | } |
| 3435 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3436 | if (!colorItem) { |
Joe Drago | 618ed5f | 2021-05-04 12:13:24 -0700 | [diff] [blame] | 3437 | avifDiagnosticsPrintf(&decoder->diag, "Primary item not found"); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3438 | return AVIF_RESULT_NO_AV1_ITEMS_FOUND; |
| 3439 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3440 | colorProperties = &colorItem->properties; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3441 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3442 | // Find the alphaOBU item, if any |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 3443 | for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) { |
| 3444 | avifDecoderItem * item = &data->meta->items.item[itemIndex]; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 3445 | if (!item->size) { |
| 3446 | continue; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3447 | } |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 3448 | if (item->hasUnsupportedEssentialProperty) { |
| 3449 | // An essential property isn't supported by libavif; ignore the item. |
| 3450 | continue; |
| 3451 | } |
Joe Drago | 951a002 | 2020-03-09 16:19:44 -0700 | [diff] [blame] | 3452 | avifBool isGrid = (memcmp(item->type, "grid", 4) == 0); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3453 | if (memcmp(item->type, "av01", 4) && !isGrid) { |
| 3454 | // probably exif or some other data |
| 3455 | continue; |
| 3456 | } |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 3457 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3458 | // Is this an alpha auxiliary item of whatever we chose for colorItem? |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 3459 | const avifProperty * auxCProp = avifPropertyArrayFind(&item->properties, "auxC"); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3460 | if (auxCProp && isAlphaURN(auxCProp->u.auxC.auxType) && (item->auxForID == colorItem->id)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3461 | if (isGrid) { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3462 | avifROData readData; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 3463 | avifResult readResult = avifDecoderItemRead(item, decoder->io, &readData, 0, 0, data->diag); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3464 | if (readResult != AVIF_RESULT_OK) { |
| 3465 | return readResult; |
| 3466 | } |
Wan-Teh Chang | 980d585 | 2021-08-03 20:02:38 -0700 | [diff] [blame] | 3467 | if (!avifParseImageGridBox(&data->alphaGrid, readData.data, readData.size, decoder->imageSizeLimit, data->diag)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3468 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 3469 | } |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 3470 | } |
| 3471 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3472 | alphaItem = item; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3473 | break; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 3474 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 3475 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 3476 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3477 | // Find Exif and/or XMP metadata, if any |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3478 | avifResult findResult = avifDecoderFindMetadata(decoder, data->meta, decoder->image, colorItem->id); |
| 3479 | if (findResult != AVIF_RESULT_OK) { |
| 3480 | return findResult; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3481 | } |
| 3482 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3483 | // Set all counts and timing to safe-but-uninteresting values |
| 3484 | decoder->imageIndex = -1; |
| 3485 | decoder->imageCount = 1; |
| 3486 | decoder->imageTiming.timescale = 1; |
| 3487 | decoder->imageTiming.pts = 0; |
| 3488 | decoder->imageTiming.ptsInTimescales = 0; |
| 3489 | decoder->imageTiming.duration = 1; |
| 3490 | decoder->imageTiming.durationInTimescales = 1; |
| 3491 | decoder->timescale = 1; |
| 3492 | decoder->duration = 1; |
| 3493 | decoder->durationInTimescales = 1; |
Joe Drago | 70cbf60 | 2019-07-24 15:30:55 -0700 | [diff] [blame] | 3494 | |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 3495 | if ((data->colorGrid.rows > 0) && (data->colorGrid.columns > 0)) { |
| 3496 | if (!avifDecoderGenerateImageGridTiles(decoder, &data->colorGrid, colorItem, AVIF_FALSE)) { |
| 3497 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 3498 | } |
| 3499 | data->colorTileCount = data->tiles.count; |
| 3500 | } else { |
| 3501 | if (colorItem->size == 0) { |
| 3502 | return AVIF_RESULT_NO_AV1_ITEMS_FOUND; |
| 3503 | } |
| 3504 | |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 3505 | avifTile * colorTile = |
| 3506 | avifDecoderDataCreateTile(data, colorItem->width, colorItem->height, avifDecoderItemOperatingPoint(colorItem)); |
Wan-Teh Chang | d891187 | 2022-02-14 14:15:00 -0800 | [diff] [blame] | 3507 | if (!colorTile) { |
| 3508 | return AVIF_RESULT_OUT_OF_MEMORY; |
| 3509 | } |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 3510 | if (!avifCodecDecodeInputFillFromDecoderItem(colorTile->input, |
| 3511 | colorItem, |
| 3512 | decoder->allowProgressive, |
| 3513 | decoder->imageCountLimit, |
| 3514 | decoder->io->sizeHint, |
| 3515 | &decoder->diag)) { |
Wan-Teh Chang | a8cd441 | 2022-02-10 14:30:48 -0800 | [diff] [blame] | 3516 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 3517 | } |
| 3518 | data->colorTileCount = 1; |
| 3519 | |
| 3520 | if (colorItem->progressive) { |
| 3521 | decoder->progressiveState = AVIF_PROGRESSIVE_STATE_AVAILABLE; |
| 3522 | if (colorTile->input->samples.count > 1) { |
| 3523 | decoder->progressiveState = AVIF_PROGRESSIVE_STATE_ACTIVE; |
| 3524 | decoder->imageCount = colorTile->input->samples.count; |
| 3525 | } |
| 3526 | } |
| 3527 | } |
| 3528 | |
| 3529 | if (alphaItem) { |
Joe Drago | 9c90c06 | 2021-09-01 13:34:42 -0700 | [diff] [blame] | 3530 | if (!alphaItem->width && !alphaItem->height) { |
| 3531 | // NON-STANDARD: Alpha subimage does not have an ispe property; adopt width/height from color item |
Wan-Teh Chang | 2c1f627 | 2021-09-13 17:13:13 -0700 | [diff] [blame] | 3532 | assert(!(decoder->strictFlags & AVIF_STRICT_ALPHA_ISPE_REQUIRED)); |
Joe Drago | 9c90c06 | 2021-09-01 13:34:42 -0700 | [diff] [blame] | 3533 | alphaItem->width = colorItem->width; |
| 3534 | alphaItem->height = colorItem->height; |
| 3535 | } |
| 3536 | |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 3537 | if ((data->alphaGrid.rows > 0) && (data->alphaGrid.columns > 0)) { |
| 3538 | if (!avifDecoderGenerateImageGridTiles(decoder, &data->alphaGrid, alphaItem, AVIF_TRUE)) { |
| 3539 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 3540 | } |
| 3541 | data->alphaTileCount = data->tiles.count - data->colorTileCount; |
| 3542 | } else { |
| 3543 | if (alphaItem->size == 0) { |
| 3544 | return AVIF_RESULT_NO_AV1_ITEMS_FOUND; |
| 3545 | } |
| 3546 | |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 3547 | avifTile * alphaTile = |
| 3548 | avifDecoderDataCreateTile(data, alphaItem->width, alphaItem->height, avifDecoderItemOperatingPoint(alphaItem)); |
Wan-Teh Chang | d891187 | 2022-02-14 14:15:00 -0800 | [diff] [blame] | 3549 | if (!alphaTile) { |
| 3550 | return AVIF_RESULT_OUT_OF_MEMORY; |
| 3551 | } |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 3552 | if (!avifCodecDecodeInputFillFromDecoderItem(alphaTile->input, |
| 3553 | alphaItem, |
| 3554 | decoder->allowProgressive, |
| 3555 | decoder->imageCountLimit, |
| 3556 | decoder->io->sizeHint, |
| 3557 | &decoder->diag)) { |
Wan-Teh Chang | a8cd441 | 2022-02-10 14:30:48 -0800 | [diff] [blame] | 3558 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
Joe Drago | bffba3b | 2021-05-26 15:46:10 -0700 | [diff] [blame] | 3559 | } |
| 3560 | alphaTile->input->alpha = AVIF_TRUE; |
| 3561 | data->alphaTileCount = 1; |
| 3562 | } |
| 3563 | } |
| 3564 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3565 | decoder->ioStats.colorOBUSize = colorItem->size; |
| 3566 | decoder->ioStats.alphaOBUSize = alphaItem ? alphaItem->size : 0; |
Joe Drago | 4170085 | 2019-09-26 17:01:43 -0700 | [diff] [blame] | 3567 | |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 3568 | decoder->image->width = colorItem->width; |
| 3569 | decoder->image->height = colorItem->height; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3570 | decoder->alphaPresent = (alphaItem != NULL); |
Joe Drago | ceb2fa0 | 2021-01-29 18:14:55 -0800 | [diff] [blame] | 3571 | decoder->image->alphaPremultiplied = decoder->alphaPresent && (colorItem->premByID == alphaItem->id); |
Joe Drago | c10c6be | 2021-03-31 08:55:19 -0700 | [diff] [blame] | 3572 | |
Joe Drago | 9b942c2 | 2021-05-06 12:33:12 -0700 | [diff] [blame] | 3573 | avifResult colorItemValidationResult = avifDecoderItemValidateAV1(colorItem, &decoder->diag, decoder->strictFlags); |
Joe Drago | c10c6be | 2021-03-31 08:55:19 -0700 | [diff] [blame] | 3574 | if (colorItemValidationResult != AVIF_RESULT_OK) { |
| 3575 | return colorItemValidationResult; |
| 3576 | } |
| 3577 | if (alphaItem) { |
Joe Drago | 9b942c2 | 2021-05-06 12:33:12 -0700 | [diff] [blame] | 3578 | avifResult alphaItemValidationResult = avifDecoderItemValidateAV1(alphaItem, &decoder->diag, decoder->strictFlags); |
Joe Drago | c10c6be | 2021-03-31 08:55:19 -0700 | [diff] [blame] | 3579 | if (alphaItemValidationResult != AVIF_RESULT_OK) { |
| 3580 | return alphaItemValidationResult; |
| 3581 | } |
| 3582 | } |
Joe Drago | 00bcaaf | 2020-06-05 15:29:38 -0700 | [diff] [blame] | 3583 | } |
| 3584 | |
Joe Drago | 11f2a5e | 2020-07-06 10:49:00 -0700 | [diff] [blame] | 3585 | // Sanity check tiles |
| 3586 | for (uint32_t tileIndex = 0; tileIndex < data->tiles.count; ++tileIndex) { |
| 3587 | avifTile * tile = &data->tiles.tile[tileIndex]; |
| 3588 | for (uint32_t sampleIndex = 0; sampleIndex < tile->input->samples.count; ++sampleIndex) { |
Joe Drago | 043311b | 2020-07-06 16:48:41 -0700 | [diff] [blame] | 3589 | avifDecodeSample * sample = &tile->input->samples.sample[sampleIndex]; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3590 | if (!sample->size) { |
Joe Drago | 11f2a5e | 2020-07-06 10:49:00 -0700 | [diff] [blame] | 3591 | // Every sample must have some data |
| 3592 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 3593 | } |
| 3594 | } |
| 3595 | } |
| 3596 | |
Joe Drago | bf58fe7 | 2020-11-05 13:25:14 -0800 | [diff] [blame] | 3597 | // Find and adopt all colr boxes "at most one for a given value of colour type" (HEIF 6.5.5.1, from Amendment 3) |
| 3598 | // Accept one of each type, and bail out if more than one of a given type is provided. |
| 3599 | avifBool colrICCSeen = AVIF_FALSE; |
| 3600 | avifBool colrNCLXSeen = AVIF_FALSE; |
| 3601 | for (uint32_t propertyIndex = 0; propertyIndex < colorProperties->count; ++propertyIndex) { |
| 3602 | avifProperty * prop = &colorProperties->prop[propertyIndex]; |
| 3603 | |
| 3604 | if (!memcmp(prop->type, "colr", 4)) { |
| 3605 | if (prop->u.colr.hasICC) { |
| 3606 | if (colrICCSeen) { |
| 3607 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 3608 | } |
| 3609 | colrICCSeen = AVIF_TRUE; |
| 3610 | avifImageSetProfileICC(decoder->image, prop->u.colr.icc, prop->u.colr.iccSize); |
| 3611 | } |
| 3612 | if (prop->u.colr.hasNCLX) { |
| 3613 | if (colrNCLXSeen) { |
| 3614 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 3615 | } |
| 3616 | colrNCLXSeen = AVIF_TRUE; |
| 3617 | data->cicpSet = AVIF_TRUE; |
| 3618 | decoder->image->colorPrimaries = prop->u.colr.colorPrimaries; |
| 3619 | decoder->image->transferCharacteristics = prop->u.colr.transferCharacteristics; |
| 3620 | decoder->image->matrixCoefficients = prop->u.colr.matrixCoefficients; |
| 3621 | decoder->image->yuvRange = prop->u.colr.range; |
| 3622 | } |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 3623 | } |
| 3624 | } |
| 3625 | |
| 3626 | // Transformations |
| 3627 | const avifProperty * paspProp = avifPropertyArrayFind(colorProperties, "pasp"); |
| 3628 | if (paspProp) { |
| 3629 | decoder->image->transformFlags |= AVIF_TRANSFORM_PASP; |
Wan-Teh Chang | 2f225b0 | 2022-03-16 22:30:38 -0700 | [diff] [blame] | 3630 | decoder->image->pasp = paspProp->u.pasp; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 3631 | } |
| 3632 | const avifProperty * clapProp = avifPropertyArrayFind(colorProperties, "clap"); |
| 3633 | if (clapProp) { |
| 3634 | decoder->image->transformFlags |= AVIF_TRANSFORM_CLAP; |
Wan-Teh Chang | 2f225b0 | 2022-03-16 22:30:38 -0700 | [diff] [blame] | 3635 | decoder->image->clap = clapProp->u.clap; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 3636 | } |
| 3637 | const avifProperty * irotProp = avifPropertyArrayFind(colorProperties, "irot"); |
| 3638 | if (irotProp) { |
| 3639 | decoder->image->transformFlags |= AVIF_TRANSFORM_IROT; |
Wan-Teh Chang | 2f225b0 | 2022-03-16 22:30:38 -0700 | [diff] [blame] | 3640 | decoder->image->irot = irotProp->u.irot; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 3641 | } |
| 3642 | const avifProperty * imirProp = avifPropertyArrayFind(colorProperties, "imir"); |
| 3643 | if (imirProp) { |
| 3644 | decoder->image->transformFlags |= AVIF_TRANSFORM_IMIR; |
Wan-Teh Chang | 2f225b0 | 2022-03-16 22:30:38 -0700 | [diff] [blame] | 3645 | decoder->image->imir = imirProp->u.imir; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 3646 | } |
| 3647 | |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 3648 | if (!data->cicpSet && (data->tiles.count > 0)) { |
Joe Drago | da92a38 | 2020-06-09 17:08:45 -0700 | [diff] [blame] | 3649 | avifTile * firstTile = &data->tiles.tile[0]; |
| 3650 | if (firstTile->input->samples.count > 0) { |
| 3651 | avifDecodeSample * sample = &firstTile->input->samples.sample[0]; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3652 | |
| 3653 | // Harvest CICP from the AV1's sequence header, which should be very close to the front |
| 3654 | // of the first sample. Read in successively larger chunks until we successfully parse the sequence. |
| 3655 | static const size_t searchSampleChunkIncrement = 64; |
Wan-Teh Chang | ac14571 | 2021-02-08 15:53:44 -0800 | [diff] [blame] | 3656 | static const size_t searchSampleSizeMax = 4096; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3657 | size_t searchSampleSize = 0; |
Wan-Teh Chang | c6c2fe8 | 2020-10-08 10:44:04 -0700 | [diff] [blame] | 3658 | do { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3659 | searchSampleSize += searchSampleChunkIncrement; |
| 3660 | if (searchSampleSize > sample->size) { |
| 3661 | searchSampleSize = sample->size; |
| 3662 | } |
| 3663 | |
| 3664 | avifResult prepareResult = avifDecoderPrepareSample(decoder, sample, searchSampleSize); |
| 3665 | if (prepareResult != AVIF_RESULT_OK) { |
| 3666 | return prepareResult; |
| 3667 | } |
| 3668 | |
| 3669 | avifSequenceHeader sequenceHeader; |
| 3670 | if (avifSequenceHeaderParse(&sequenceHeader, &sample->data)) { |
| 3671 | data->cicpSet = AVIF_TRUE; |
| 3672 | decoder->image->colorPrimaries = sequenceHeader.colorPrimaries; |
| 3673 | decoder->image->transferCharacteristics = sequenceHeader.transferCharacteristics; |
| 3674 | decoder->image->matrixCoefficients = sequenceHeader.matrixCoefficients; |
| 3675 | decoder->image->yuvRange = sequenceHeader.range; |
| 3676 | break; |
| 3677 | } |
Wan-Teh Chang | ac14571 | 2021-02-08 15:53:44 -0800 | [diff] [blame] | 3678 | } while (searchSampleSize != sample->size && searchSampleSize < searchSampleSizeMax); |
Joe Drago | da92a38 | 2020-06-09 17:08:45 -0700 | [diff] [blame] | 3679 | } |
| 3680 | } |
| 3681 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 3682 | const avifProperty * av1CProp = avifPropertyArrayFind(colorProperties, "av1C"); |
| 3683 | if (av1CProp) { |
Joe Drago | b840112 | 2020-06-19 11:45:49 -0700 | [diff] [blame] | 3684 | decoder->image->depth = avifCodecConfigurationBoxGetDepth(&av1CProp->u.av1C); |
| 3685 | if (av1CProp->u.av1C.monochrome) { |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 3686 | decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV400; |
Joe Drago | 7b2cf80 | 2020-06-09 17:57:23 -0700 | [diff] [blame] | 3687 | } else { |
Joe Drago | b840112 | 2020-06-19 11:45:49 -0700 | [diff] [blame] | 3688 | if (av1CProp->u.av1C.chromaSubsamplingX && av1CProp->u.av1C.chromaSubsamplingY) { |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 3689 | decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV420; |
Joe Drago | b840112 | 2020-06-19 11:45:49 -0700 | [diff] [blame] | 3690 | } else if (av1CProp->u.av1C.chromaSubsamplingX) { |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 3691 | decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV422; |
Joe Drago | 7b2cf80 | 2020-06-09 17:57:23 -0700 | [diff] [blame] | 3692 | |
| 3693 | } else { |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 3694 | decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV444; |
Joe Drago | 7b2cf80 | 2020-06-09 17:57:23 -0700 | [diff] [blame] | 3695 | } |
| 3696 | } |
Joe Drago | b840112 | 2020-06-19 11:45:49 -0700 | [diff] [blame] | 3697 | decoder->image->yuvChromaSamplePosition = (avifChromaSamplePosition)av1CProp->u.av1C.chromaSamplePosition; |
Joe Drago | 00bcaaf | 2020-06-05 15:29:38 -0700 | [diff] [blame] | 3698 | } else { |
Joe Drago | f48a338 | 2020-06-19 14:13:44 -0700 | [diff] [blame] | 3699 | // An av1C box is mandatory in all valid AVIF configurations. Bail out. |
| 3700 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3701 | } |
| 3702 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3703 | return avifDecoderFlush(decoder); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3704 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 3705 | |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3706 | static avifResult avifDecoderPrepareTiles(avifDecoder * decoder, |
| 3707 | uint32_t nextImageIndex, |
| 3708 | unsigned int firstTileIndex, |
| 3709 | unsigned int tileCount, |
| 3710 | unsigned int decodedTileCount) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3711 | { |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3712 | for (unsigned int tileIndex = decodedTileCount; tileIndex < tileCount; ++tileIndex) { |
| 3713 | avifTile * tile = &decoder->data->tiles.tile[firstTileIndex + tileIndex]; |
Joe Drago | 411221a | 2021-11-10 11:12:56 -0800 | [diff] [blame] | 3714 | |
| 3715 | // Ensure there's an AV1 codec available before doing anything else |
| 3716 | if (!tile->codec) { |
| 3717 | return AVIF_RESULT_NO_CODEC_AVAILABLE; |
| 3718 | } |
| 3719 | |
Joe Drago | 5998f59 | 2020-11-13 15:38:20 -0800 | [diff] [blame] | 3720 | if (nextImageIndex >= tile->input->samples.count) { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3721 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
| 3722 | } |
| 3723 | |
Joe Drago | 5998f59 | 2020-11-13 15:38:20 -0800 | [diff] [blame] | 3724 | avifDecodeSample * sample = &tile->input->samples.sample[nextImageIndex]; |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3725 | avifResult prepareResult = avifDecoderPrepareSample(decoder, sample, 0); |
| 3726 | if (prepareResult != AVIF_RESULT_OK) { |
| 3727 | return prepareResult; |
| 3728 | } |
| 3729 | } |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3730 | return AVIF_RESULT_OK; |
| 3731 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3732 | |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3733 | static avifResult avifDecoderDecodeTiles(avifDecoder * decoder, |
| 3734 | uint32_t nextImageIndex, |
| 3735 | unsigned int firstTileIndex, |
| 3736 | unsigned int tileCount, |
Wan-Teh Chang | a7743c8 | 2022-03-24 16:30:47 -0700 | [diff] [blame^] | 3737 | unsigned int * decodedTileCount) |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3738 | { |
Wan-Teh Chang | a7743c8 | 2022-03-24 16:30:47 -0700 | [diff] [blame^] | 3739 | const unsigned int oldDecodedTileCount = *decodedTileCount; |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3740 | for (unsigned int tileIndex = oldDecodedTileCount; tileIndex < tileCount; ++tileIndex) { |
| 3741 | avifTile * tile = &decoder->data->tiles.tile[firstTileIndex + tileIndex]; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 3742 | |
Wan-Teh Chang | b4977b3 | 2020-10-05 18:04:54 -0700 | [diff] [blame] | 3743 | const avifDecodeSample * sample = &tile->input->samples.sample[nextImageIndex]; |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3744 | if (!sample->data.data) { |
| 3745 | assert(decoder->allowIncremental); |
| 3746 | // Data is missing but there is no error yet. Output available pixel rows. |
| 3747 | return AVIF_RESULT_OK; |
| 3748 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3749 | |
Joe Drago | 405e872 | 2021-05-25 15:08:00 -0700 | [diff] [blame] | 3750 | if (!tile->codec->getNextImage(tile->codec, decoder, sample, tile->input->alpha, tile->image)) { |
Wan-Teh Chang | 9b68bf6 | 2021-06-23 15:46:37 -0700 | [diff] [blame] | 3751 | avifDiagnosticsPrintf(&decoder->diag, "tile->codec->getNextImage() failed"); |
Wan-Teh Chang | 365bd5e | 2021-03-23 18:28:40 -0700 | [diff] [blame] | 3752 | return tile->input->alpha ? AVIF_RESULT_DECODE_ALPHA_FAILED : AVIF_RESULT_DECODE_COLOR_FAILED; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3753 | } |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 3754 | |
| 3755 | // Scale the decoded image so that it corresponds to this tile's output dimensions |
| 3756 | if ((tile->width != tile->image->width) || (tile->height != tile->image->height)) { |
Wan-Teh Chang | 980d585 | 2021-08-03 20:02:38 -0700 | [diff] [blame] | 3757 | if (!avifImageScale(tile->image, tile->width, tile->height, decoder->imageSizeLimit, &decoder->diag)) { |
Wan-Teh Chang | 9b68bf6 | 2021-06-23 15:46:37 -0700 | [diff] [blame] | 3758 | avifDiagnosticsPrintf(&decoder->diag, "avifImageScale() failed"); |
Joe Drago | 46104d6 | 2021-05-27 18:17:29 -0700 | [diff] [blame] | 3759 | return tile->input->alpha ? AVIF_RESULT_DECODE_ALPHA_FAILED : AVIF_RESULT_DECODE_COLOR_FAILED; |
| 3760 | } |
| 3761 | } |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3762 | |
| 3763 | ++*decodedTileCount; |
| 3764 | } |
| 3765 | return AVIF_RESULT_OK; |
| 3766 | } |
| 3767 | |
| 3768 | avifResult avifDecoderNextImage(avifDecoder * decoder) |
| 3769 | { |
| 3770 | avifDiagnosticsClearError(&decoder->diag); |
| 3771 | |
| 3772 | if (!decoder->data) { |
| 3773 | // Nothing has been parsed yet |
| 3774 | return AVIF_RESULT_NO_CONTENT; |
| 3775 | } |
| 3776 | |
| 3777 | if (!decoder->io || !decoder->io->read) { |
| 3778 | return AVIF_RESULT_IO_NOT_SET; |
| 3779 | } |
| 3780 | |
Wan-Teh Chang | adfa25b | 2022-03-17 15:33:18 -0700 | [diff] [blame] | 3781 | if ((decoder->data->decodedColorTileCount == decoder->data->colorTileCount) && |
| 3782 | (decoder->data->decodedAlphaTileCount == decoder->data->alphaTileCount)) { |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3783 | // A frame was decoded during the last avifDecoderNextImage() call. |
| 3784 | decoder->data->decodedColorTileCount = 0; |
| 3785 | decoder->data->decodedAlphaTileCount = 0; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3786 | } |
| 3787 | |
| 3788 | if (decoder->data->tiles.count != (decoder->data->colorTileCount + decoder->data->alphaTileCount)) { |
| 3789 | // TODO: assert here? This should be impossible. |
| 3790 | return AVIF_RESULT_UNKNOWN_ERROR; |
| 3791 | } |
| 3792 | |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3793 | const uint32_t nextImageIndex = (uint32_t)(decoder->imageIndex + 1); |
| 3794 | const unsigned int firstColorTileIndex = 0; |
| 3795 | const unsigned int firstAlphaTileIndex = decoder->data->colorTileCount; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3796 | |
Wan-Teh Chang | a7743c8 | 2022-03-24 16:30:47 -0700 | [diff] [blame^] | 3797 | // Acquire all sample data for the current image first, allowing for any read call to bail out |
| 3798 | // with AVIF_RESULT_WAITING_ON_IO harmlessly / idempotently, unless decoder->allowIncremental. |
| 3799 | // Start with color tiles. |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3800 | const avifResult prepareColorTileResult = |
| 3801 | avifDecoderPrepareTiles(decoder, nextImageIndex, firstColorTileIndex, decoder->data->colorTileCount, decoder->data->decodedColorTileCount); |
Wan-Teh Chang | adfa25b | 2022-03-17 15:33:18 -0700 | [diff] [blame] | 3802 | if ((prepareColorTileResult != AVIF_RESULT_OK) && |
| 3803 | (!decoder->allowIncremental || (prepareColorTileResult != AVIF_RESULT_WAITING_ON_IO))) { |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3804 | return prepareColorTileResult; |
| 3805 | } |
| 3806 | // Do the same with alpha tiles. They are handled separately because their |
| 3807 | // order of appearance relative to the color tiles in the bitstream is left |
| 3808 | // to the encoder's choice, and decoding as many as possible of each |
| 3809 | // category in parallel is beneficial for incremental decoding, as pixel |
| 3810 | // rows need all channels to be decoded before being accessible to the user. |
| 3811 | const avifResult prepareAlphaTileResult = |
| 3812 | avifDecoderPrepareTiles(decoder, nextImageIndex, firstAlphaTileIndex, decoder->data->alphaTileCount, decoder->data->decodedAlphaTileCount); |
Wan-Teh Chang | adfa25b | 2022-03-17 15:33:18 -0700 | [diff] [blame] | 3813 | if ((prepareAlphaTileResult != AVIF_RESULT_OK) && |
| 3814 | (!decoder->allowIncremental || (prepareAlphaTileResult != AVIF_RESULT_WAITING_ON_IO))) { |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3815 | return prepareAlphaTileResult; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3816 | } |
| 3817 | |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3818 | // Decode all available color tiles now, then all available alpha tiles. |
| 3819 | const unsigned int oldDecodedColorTileCount = decoder->data->decodedColorTileCount; |
| 3820 | const avifResult decodeColorTileResult = |
| 3821 | avifDecoderDecodeTiles(decoder, nextImageIndex, firstColorTileIndex, decoder->data->colorTileCount, &decoder->data->decodedColorTileCount); |
| 3822 | if (decodeColorTileResult != AVIF_RESULT_OK) { |
| 3823 | return decodeColorTileResult; |
| 3824 | } |
| 3825 | const unsigned int oldDecodedAlphaTileCount = decoder->data->decodedAlphaTileCount; |
| 3826 | const avifResult decodeAlphaTileResult = |
| 3827 | avifDecoderDecodeTiles(decoder, nextImageIndex, firstAlphaTileIndex, decoder->data->alphaTileCount, &decoder->data->decodedAlphaTileCount); |
| 3828 | if (decodeAlphaTileResult != AVIF_RESULT_OK) { |
| 3829 | return decodeAlphaTileResult; |
| 3830 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3831 | |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3832 | if (decoder->data->decodedColorTileCount > oldDecodedColorTileCount) { |
| 3833 | // There is at least one newly decoded color tile. |
| 3834 | if ((decoder->data->colorGrid.rows > 0) && (decoder->data->colorGrid.columns > 0)) { |
| 3835 | if (!avifDecoderDataFillImageGrid(decoder->data, |
| 3836 | &decoder->data->colorGrid, |
| 3837 | decoder->image, |
| 3838 | firstColorTileIndex, |
| 3839 | oldDecodedColorTileCount, |
| 3840 | decoder->data->decodedColorTileCount, |
| 3841 | AVIF_FALSE)) { |
| 3842 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 3843 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3844 | } else { |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3845 | // Normal (most common) non-grid path. Just steal the planes from the only "tile". |
| 3846 | |
| 3847 | if (decoder->data->colorTileCount != 1) { |
| 3848 | avifDiagnosticsPrintf(&decoder->diag, "decoder->data->colorTileCount should be 1 but is %u", decoder->data->colorTileCount); |
| 3849 | return AVIF_RESULT_DECODE_COLOR_FAILED; |
| 3850 | } |
| 3851 | |
| 3852 | avifImage * srcColor = decoder->data->tiles.tile[0].image; |
| 3853 | |
| 3854 | if ((decoder->image->width != srcColor->width) || (decoder->image->height != srcColor->height) || |
| 3855 | (decoder->image->depth != srcColor->depth)) { |
| 3856 | avifImageFreePlanes(decoder->image, AVIF_PLANES_ALL); |
| 3857 | |
| 3858 | decoder->image->width = srcColor->width; |
| 3859 | decoder->image->height = srcColor->height; |
| 3860 | decoder->image->depth = srcColor->depth; |
| 3861 | } |
| 3862 | |
| 3863 | #if 0 |
| 3864 | // This code is currently unnecessary as the CICP is always set by the end of avifDecoderParse(). |
| 3865 | if (!decoder->data->cicpSet) { |
| 3866 | decoder->data->cicpSet = AVIF_TRUE; |
| 3867 | decoder->image->colorPrimaries = srcColor->colorPrimaries; |
| 3868 | decoder->image->transferCharacteristics = srcColor->transferCharacteristics; |
| 3869 | decoder->image->matrixCoefficients = srcColor->matrixCoefficients; |
| 3870 | } |
| 3871 | #endif |
| 3872 | |
| 3873 | avifImageStealPlanes(decoder->image, srcColor, AVIF_PLANES_YUV); |
| 3874 | } |
| 3875 | } |
| 3876 | |
| 3877 | if (decoder->data->alphaTileCount == 0) { |
| 3878 | avifImageFreePlanes(decoder->image, AVIF_PLANES_A); // no alpha |
| 3879 | } else if (decoder->data->decodedAlphaTileCount > oldDecodedAlphaTileCount) { |
| 3880 | // There is at least one newly decoded alpha tile. |
| 3881 | if ((decoder->data->alphaGrid.rows > 0) && (decoder->data->alphaGrid.columns > 0)) { |
| 3882 | if (!avifDecoderDataFillImageGrid(decoder->data, |
| 3883 | &decoder->data->alphaGrid, |
| 3884 | decoder->image, |
| 3885 | firstAlphaTileIndex, |
| 3886 | oldDecodedAlphaTileCount, |
| 3887 | decoder->data->decodedAlphaTileCount, |
| 3888 | AVIF_TRUE)) { |
| 3889 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 3890 | } |
| 3891 | } else { |
| 3892 | // Normal (most common) non-grid path. Just steal the planes from the only "tile". |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3893 | if (decoder->data->alphaTileCount != 1) { |
Wan-Teh Chang | 9b68bf6 | 2021-06-23 15:46:37 -0700 | [diff] [blame] | 3894 | avifDiagnosticsPrintf(&decoder->diag, "decoder->data->alphaTileCount should be 1 but is %u", decoder->data->alphaTileCount); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3895 | return AVIF_RESULT_DECODE_ALPHA_FAILED; |
| 3896 | } |
| 3897 | |
| 3898 | avifImage * srcAlpha = decoder->data->tiles.tile[decoder->data->colorTileCount].image; |
| 3899 | if ((decoder->image->width != srcAlpha->width) || (decoder->image->height != srcAlpha->height) || |
| 3900 | (decoder->image->depth != srcAlpha->depth)) { |
Wan-Teh Chang | 9b68bf6 | 2021-06-23 15:46:37 -0700 | [diff] [blame] | 3901 | avifDiagnosticsPrintf(&decoder->diag, "decoder->image does not match srcAlpha in width, height, or bit depth"); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3902 | return AVIF_RESULT_DECODE_ALPHA_FAILED; |
| 3903 | } |
| 3904 | |
| 3905 | avifImageStealPlanes(decoder->image, srcAlpha, AVIF_PLANES_A); |
Joe Drago | 3fd2db2 | 2020-08-13 16:07:24 -0700 | [diff] [blame] | 3906 | decoder->image->alphaRange = srcAlpha->alphaRange; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 3907 | } |
| 3908 | } |
Joe Drago | 7ad3ad6 | 2019-02-07 11:17:34 -0800 | [diff] [blame] | 3909 | |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3910 | if (avifDecoderDecodedRowCount(decoder) < decoder->image->height) { |
| 3911 | assert(decoder->allowIncremental); |
| 3912 | // Rows are missing. There should be no error unrelated to missing bytes, and at least some missing bytes. |
Wan-Teh Chang | adfa25b | 2022-03-17 15:33:18 -0700 | [diff] [blame] | 3913 | assert((prepareColorTileResult == AVIF_RESULT_OK) || (prepareColorTileResult == AVIF_RESULT_WAITING_ON_IO)); |
| 3914 | assert((prepareAlphaTileResult == AVIF_RESULT_OK) || (prepareAlphaTileResult == AVIF_RESULT_WAITING_ON_IO)); |
| 3915 | assert((prepareColorTileResult != AVIF_RESULT_OK) || (prepareAlphaTileResult != AVIF_RESULT_OK)); |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3916 | // Return the "not enough bytes" status now instead of moving on to the next frame. |
| 3917 | return AVIF_RESULT_WAITING_ON_IO; |
| 3918 | } |
Wan-Teh Chang | adfa25b | 2022-03-17 15:33:18 -0700 | [diff] [blame] | 3919 | assert((prepareColorTileResult == AVIF_RESULT_OK) && (prepareAlphaTileResult == AVIF_RESULT_OK)); |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3920 | |
| 3921 | // Only advance decoder->imageIndex once the image is completely decoded, so that |
| 3922 | // avifDecoderNthImage(decoder, decoder->imageIndex + 1) is equivalent to avifDecoderNextImage(decoder) |
| 3923 | // if the previous call to avifDecoderNextImage() returned AVIF_RESULT_WAITING_ON_IO. |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 3924 | decoder->imageIndex = nextImageIndex; |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3925 | // The decoded tile counts will be reset to 0 the next time avifDecoderNextImage() is called, |
| 3926 | // for avifDecoderDecodedRowCount() to work until then. |
| 3927 | assert(decoder->data->decodedColorTileCount == decoder->data->colorTileCount); |
| 3928 | assert(decoder->data->decodedAlphaTileCount == decoder->data->alphaTileCount); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3929 | if (decoder->data->sourceSampleTable) { |
| 3930 | // Decoding from a track! Provide timing information. |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 3931 | |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3932 | avifResult timingResult = avifDecoderNthImageTiming(decoder, decoder->imageIndex, &decoder->imageTiming); |
| 3933 | if (timingResult != AVIF_RESULT_OK) { |
| 3934 | return timingResult; |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3935 | } |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3936 | } |
| 3937 | return AVIF_RESULT_OK; |
| 3938 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3939 | |
Wan-Teh Chang | e184dc1 | 2020-05-11 12:47:21 -0700 | [diff] [blame] | 3940 | avifResult avifDecoderNthImageTiming(const avifDecoder * decoder, uint32_t frameIndex, avifImageTiming * outTiming) |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3941 | { |
| 3942 | if (!decoder->data) { |
| 3943 | // Nothing has been parsed yet |
| 3944 | return AVIF_RESULT_NO_CONTENT; |
| 3945 | } |
| 3946 | |
Wan-Teh Chang | 25b9c70 | 2020-10-08 12:17:17 -0700 | [diff] [blame] | 3947 | if ((frameIndex > INT_MAX) || ((int)frameIndex >= decoder->imageCount)) { |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3948 | // Impossible index |
| 3949 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
| 3950 | } |
| 3951 | |
| 3952 | if (!decoder->data->sourceSampleTable) { |
| 3953 | // There isn't any real timing associated with this decode, so |
| 3954 | // just hand back the defaults chosen in avifDecoderReset(). |
Wan-Teh Chang | 2f225b0 | 2022-03-16 22:30:38 -0700 | [diff] [blame] | 3955 | *outTiming = decoder->imageTiming; |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3956 | return AVIF_RESULT_OK; |
| 3957 | } |
| 3958 | |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 3959 | outTiming->timescale = decoder->timescale; |
| 3960 | outTiming->ptsInTimescales = 0; |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3961 | for (int imageIndex = 0; imageIndex < (int)frameIndex; ++imageIndex) { |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 3962 | outTiming->ptsInTimescales += avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, imageIndex); |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3963 | } |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 3964 | outTiming->durationInTimescales = avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, frameIndex); |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3965 | |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 3966 | if (outTiming->timescale > 0) { |
| 3967 | outTiming->pts = (double)outTiming->ptsInTimescales / (double)outTiming->timescale; |
| 3968 | outTiming->duration = (double)outTiming->durationInTimescales / (double)outTiming->timescale; |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 3969 | } else { |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 3970 | outTiming->pts = 0.0; |
| 3971 | outTiming->duration = 0.0; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 3972 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 3973 | return AVIF_RESULT_OK; |
| 3974 | } |
| 3975 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3976 | avifResult avifDecoderNthImage(avifDecoder * decoder, uint32_t frameIndex) |
| 3977 | { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 3978 | avifDiagnosticsClearError(&decoder->diag); |
| 3979 | |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3980 | if (!decoder->data) { |
| 3981 | // Nothing has been parsed yet |
| 3982 | return AVIF_RESULT_NO_CONTENT; |
| 3983 | } |
| 3984 | |
Wan-Teh Chang | 25b9c70 | 2020-10-08 12:17:17 -0700 | [diff] [blame] | 3985 | if (frameIndex > INT_MAX) { |
| 3986 | // Impossible index |
| 3987 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
| 3988 | } |
| 3989 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 3990 | int requestedIndex = (int)frameIndex; |
| 3991 | if (requestedIndex == decoder->imageIndex) { |
Wan-Teh Chang | adfa25b | 2022-03-17 15:33:18 -0700 | [diff] [blame] | 3992 | if ((decoder->data->decodedColorTileCount == decoder->data->colorTileCount) && |
| 3993 | (decoder->data->decodedAlphaTileCount == decoder->data->alphaTileCount)) { |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 3994 | // The current fully decoded image (decoder->imageIndex) is requested, nothing to do |
| 3995 | return AVIF_RESULT_OK; |
| 3996 | } |
| 3997 | // The next image (decoder->imageIndex + 1) is partially decoded but |
| 3998 | // the previous image (decoder->imageIndex) is requested. |
| 3999 | // Fall through to flush and start decoding from the nearest key frame. |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 4000 | } |
| 4001 | |
| 4002 | if (requestedIndex == (decoder->imageIndex + 1)) { |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 4003 | // It's just the next image (already partially decoded or not at all), nothing special here |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 4004 | return avifDecoderNextImage(decoder); |
| 4005 | } |
| 4006 | |
| 4007 | if (requestedIndex >= decoder->imageCount) { |
| 4008 | // Impossible index |
| 4009 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
| 4010 | } |
| 4011 | |
Wan-Teh Chang | 722fb7e | 2020-11-06 12:39:58 -0800 | [diff] [blame] | 4012 | int nearestKeyFrame = (int)avifDecoderNearestKeyframe(decoder, frameIndex); |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 4013 | if ((nearestKeyFrame > (decoder->imageIndex + 1)) || (requestedIndex <= decoder->imageIndex)) { |
Wan-Teh Chang | 722fb7e | 2020-11-06 12:39:58 -0800 | [diff] [blame] | 4014 | // If we get here, a decoder flush is necessary |
| 4015 | decoder->imageIndex = nearestKeyFrame - 1; // prepare to read nearest keyframe |
| 4016 | avifDecoderFlush(decoder); |
| 4017 | } |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 4018 | for (;;) { |
| 4019 | avifResult result = avifDecoderNextImage(decoder); |
| 4020 | if (result != AVIF_RESULT_OK) { |
| 4021 | return result; |
| 4022 | } |
| 4023 | |
| 4024 | if (requestedIndex == decoder->imageIndex) { |
| 4025 | break; |
| 4026 | } |
Joe Drago | fc4144e | 2019-09-27 20:35:06 -0700 | [diff] [blame] | 4027 | } |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 4028 | return AVIF_RESULT_OK; |
| 4029 | } |
| 4030 | |
Wan-Teh Chang | e184dc1 | 2020-05-11 12:47:21 -0700 | [diff] [blame] | 4031 | avifBool avifDecoderIsKeyframe(const avifDecoder * decoder, uint32_t frameIndex) |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 4032 | { |
Joe Drago | 91bc33a | 2022-02-08 17:02:30 -0800 | [diff] [blame] | 4033 | if (!decoder->data || (decoder->data->tiles.count == 0)) { |
Wan-Teh Chang | af0d484 | 2020-11-17 12:27:34 -0800 | [diff] [blame] | 4034 | // Nothing has been parsed yet |
| 4035 | return AVIF_FALSE; |
| 4036 | } |
| 4037 | |
Joe Drago | 91bc33a | 2022-02-08 17:02:30 -0800 | [diff] [blame] | 4038 | // *All* tiles for the requested frameIndex must be keyframes in order for |
| 4039 | // avifDecoderIsKeyframe() to return true, otherwise we may seek to a frame in which the color |
| 4040 | // planes are a keyframe but the alpha plane isn't a keyframe, which will cause an alpha plane |
| 4041 | // decode failure. |
| 4042 | for (unsigned int i = 0; i < decoder->data->tiles.count; ++i) { |
| 4043 | const avifTile * tile = &decoder->data->tiles.tile[i]; |
| 4044 | if ((frameIndex >= tile->input->samples.count) || !tile->input->samples.sample[frameIndex].sync) { |
| 4045 | return AVIF_FALSE; |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 4046 | } |
| 4047 | } |
Joe Drago | 91bc33a | 2022-02-08 17:02:30 -0800 | [diff] [blame] | 4048 | return AVIF_TRUE; |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 4049 | } |
| 4050 | |
Wan-Teh Chang | e184dc1 | 2020-05-11 12:47:21 -0700 | [diff] [blame] | 4051 | uint32_t avifDecoderNearestKeyframe(const avifDecoder * decoder, uint32_t frameIndex) |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 4052 | { |
Wan-Teh Chang | af0d484 | 2020-11-17 12:27:34 -0800 | [diff] [blame] | 4053 | if (!decoder->data) { |
| 4054 | // Nothing has been parsed yet |
| 4055 | return 0; |
| 4056 | } |
| 4057 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 4058 | for (; frameIndex != 0; --frameIndex) { |
| 4059 | if (avifDecoderIsKeyframe(decoder, frameIndex)) { |
| 4060 | break; |
| 4061 | } |
| 4062 | } |
| 4063 | return frameIndex; |
| 4064 | } |
| 4065 | |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 4066 | // Returns the number of available rows in decoder->image given a color or alpha subimage. |
Wan-Teh Chang | ca72883 | 2022-03-16 22:38:51 -0700 | [diff] [blame] | 4067 | static uint32_t avifGetDecodedRowCount(const avifDecoder * decoder, |
| 4068 | const avifImageGrid * grid, |
Wan-Teh Chang | a7743c8 | 2022-03-24 16:30:47 -0700 | [diff] [blame^] | 4069 | unsigned int firstTileIndex, |
| 4070 | unsigned int tileCount, |
| 4071 | unsigned int decodedTileCount) |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 4072 | { |
| 4073 | if (decodedTileCount == tileCount) { |
| 4074 | return decoder->image->height; |
| 4075 | } |
| 4076 | if (decodedTileCount == 0) { |
| 4077 | return 0; |
| 4078 | } |
| 4079 | |
Wan-Teh Chang | adfa25b | 2022-03-17 15:33:18 -0700 | [diff] [blame] | 4080 | if ((grid->rows > 0) && (grid->columns > 0)) { |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 4081 | // Grid of AVIF tiles (not to be confused with AV1 tiles). |
| 4082 | const uint32_t tileHeight = decoder->data->tiles.tile[firstTileIndex].height; |
| 4083 | return AVIF_MIN((decodedTileCount / grid->columns) * tileHeight, decoder->image->height); |
| 4084 | } else { |
| 4085 | // Non-grid image. |
| 4086 | return decoder->image->height; |
| 4087 | } |
| 4088 | } |
| 4089 | |
| 4090 | uint32_t avifDecoderDecodedRowCount(const avifDecoder * decoder) |
| 4091 | { |
Wan-Teh Chang | ca72883 | 2022-03-16 22:38:51 -0700 | [diff] [blame] | 4092 | const uint32_t colorRowCount = avifGetDecodedRowCount(decoder, |
| 4093 | &decoder->data->colorGrid, |
| 4094 | /*firstTileIndex=*/0, |
| 4095 | decoder->data->colorTileCount, |
| 4096 | decoder->data->decodedColorTileCount); |
Yannis Guyon | d3e85ff | 2022-03-17 05:10:29 +0000 | [diff] [blame] | 4097 | const uint32_t alphaRowCount = avifGetDecodedRowCount(decoder, |
| 4098 | &decoder->data->alphaGrid, |
| 4099 | /*firstTileIndex=*/decoder->data->colorTileCount, |
| 4100 | decoder->data->alphaTileCount, |
| 4101 | decoder->data->decodedAlphaTileCount); |
| 4102 | return AVIF_MIN(colorRowCount, alphaRowCount); |
| 4103 | } |
| 4104 | |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 4105 | avifResult avifDecoderRead(avifDecoder * decoder, avifImage * image) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 4106 | { |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 4107 | avifResult result = avifDecoderParse(decoder); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 4108 | if (result != AVIF_RESULT_OK) { |
| 4109 | return result; |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 4110 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 4111 | result = avifDecoderNextImage(decoder); |
| 4112 | if (result != AVIF_RESULT_OK) { |
| 4113 | return result; |
| 4114 | } |
Joe Drago | 250221a | 2020-06-01 11:11:06 -0700 | [diff] [blame] | 4115 | avifImageCopy(image, decoder->image, AVIF_PLANES_ALL); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 4116 | return AVIF_RESULT_OK; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 4117 | } |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 4118 | |
Wan-Teh Chang | 7de4445 | 2020-10-08 11:43:18 -0700 | [diff] [blame] | 4119 | 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] | 4120 | { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 4121 | avifDiagnosticsClearError(&decoder->diag); |
Wan-Teh Chang | 7de4445 | 2020-10-08 11:43:18 -0700 | [diff] [blame] | 4122 | avifResult result = avifDecoderSetIOMemory(decoder, data, size); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 4123 | if (result != AVIF_RESULT_OK) { |
| 4124 | return result; |
| 4125 | } |
| 4126 | return avifDecoderRead(decoder, image); |
| 4127 | } |
| 4128 | |
| 4129 | avifResult avifDecoderReadFile(avifDecoder * decoder, avifImage * image, const char * filename) |
| 4130 | { |
Joe Drago | 4d5f4a4 | 2021-04-28 13:11:45 -0700 | [diff] [blame] | 4131 | avifDiagnosticsClearError(&decoder->diag); |
Joe Drago | be4cbb9 | 2020-09-21 12:14:05 -0700 | [diff] [blame] | 4132 | avifResult result = avifDecoderSetIOFile(decoder, filename); |
| 4133 | if (result != AVIF_RESULT_OK) { |
| 4134 | return result; |
| 4135 | } |
| 4136 | return avifDecoderRead(decoder, image); |
| 4137 | } |