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