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