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) || |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame^] | 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 | |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 702 | // Validate grid image size and tile size. |
| 703 | // |
| 704 | // HEIF (ISO/IEC 23008-12:2017), Section 6.6.2.3.1: |
| 705 | // The tiled input images shall completely “cover” the reconstructed image grid canvas, ... |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame^] | 706 | if (((firstTile->image->width * grid->columns) < grid->outputWidth) || |
| 707 | ((firstTile->image->height * grid->rows) < grid->outputHeight)) { |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 708 | return AVIF_FALSE; |
| 709 | } |
| 710 | // Tiles in the rightmost column and bottommost row must overlap the reconstructed image grid canvas. See MIAF (ISO/IEC 23000-22:2019), Section 7.3.11.4.2, Figure 2. |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame^] | 711 | if (((firstTile->image->width * (grid->columns - 1)) >= grid->outputWidth) || |
| 712 | ((firstTile->image->height * (grid->rows - 1)) >= grid->outputHeight)) { |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 713 | return AVIF_FALSE; |
| 714 | } |
| 715 | // Check the restrictions in MIAF (ISO/IEC 23000-22:2019), Section 7.3.11.4.2. |
| 716 | // |
| 717 | // The tile_width shall be greater than or equal to 64, and the tile_height shall be greater than or equal to 64. |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame^] | 718 | if ((firstTile->image->width < 64) || (firstTile->image->height < 64)) { |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 719 | return AVIF_FALSE; |
| 720 | } |
| 721 | if (!alpha) { |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame^] | 722 | if ((firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV422) || (firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV420)) { |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 723 | // The horizontal tile offsets and widths, and the output width, shall be even numbers. |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame^] | 724 | if (((firstTile->image->width & 1) != 0) || ((grid->outputWidth & 1) != 0)) { |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 725 | return AVIF_FALSE; |
| 726 | } |
| 727 | } |
| 728 | if (firstTile->image->yuvFormat == AVIF_PIXEL_FORMAT_YUV420) { |
| 729 | // The vertical tile offsets and heights, and the output height, shall be even numbers. |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame^] | 730 | if (((firstTile->image->height & 1) != 0) || ((grid->outputHeight & 1) != 0)) { |
wantehchang | df586a8 | 2020-08-12 13:06:08 -0700 | [diff] [blame] | 731 | return AVIF_FALSE; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 736 | // Lazily populate dstImage with the new frame's properties. If we're decoding alpha, |
| 737 | // these values must already match. |
| 738 | if ((dstImage->width != grid->outputWidth) || (dstImage->height != grid->outputHeight) || |
| 739 | (dstImage->depth != firstTile->image->depth) || (dstImage->yuvFormat != firstTile->image->yuvFormat)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 740 | if (alpha) { |
| 741 | // Alpha doesn't match size, just bail out |
| 742 | return AVIF_FALSE; |
| 743 | } |
| 744 | |
| 745 | avifImageFreePlanes(dstImage, AVIF_PLANES_ALL); |
| 746 | dstImage->width = grid->outputWidth; |
| 747 | dstImage->height = grid->outputHeight; |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 748 | dstImage->depth = firstTile->image->depth; |
| 749 | dstImage->yuvFormat = firstTile->image->yuvFormat; |
| 750 | dstImage->yuvRange = firstTile->image->yuvRange; |
| 751 | if (!data->cicpSet) { |
| 752 | data->cicpSet = AVIF_TRUE; |
| 753 | dstImage->colorPrimaries = firstTile->image->colorPrimaries; |
| 754 | dstImage->transferCharacteristics = firstTile->image->transferCharacteristics; |
| 755 | dstImage->matrixCoefficients = firstTile->image->matrixCoefficients; |
psi / Ryo Hirafuji | 922d8a1 | 2020-03-10 03:24:57 +0900 | [diff] [blame] | 756 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 757 | } |
Joe Drago | d0eeb18 | 2020-05-18 17:23:48 -0700 | [diff] [blame] | 758 | if (alpha) { |
| 759 | dstImage->alphaRange = firstTile->image->alphaRange; |
| 760 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 761 | |
| 762 | avifImageAllocatePlanes(dstImage, alpha ? AVIF_PLANES_A : AVIF_PLANES_YUV); |
| 763 | |
| 764 | avifPixelFormatInfo formatInfo; |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 765 | avifGetPixelFormatInfo(firstTile->image->yuvFormat, &formatInfo); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 766 | |
| 767 | unsigned int tileIndex = firstTileIndex; |
| 768 | size_t pixelBytes = avifImageUsesU16(dstImage) ? 2 : 1; |
| 769 | for (unsigned int rowIndex = 0; rowIndex < grid->rows; ++rowIndex) { |
| 770 | for (unsigned int colIndex = 0; colIndex < grid->columns; ++colIndex, ++tileIndex) { |
| 771 | avifTile * tile = &data->tiles.tile[tileIndex]; |
| 772 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 773 | unsigned int widthToCopy = firstTile->image->width; |
| 774 | unsigned int maxX = firstTile->image->width * (colIndex + 1); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 775 | if (maxX > grid->outputWidth) { |
| 776 | widthToCopy -= maxX - grid->outputWidth; |
| 777 | } |
| 778 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 779 | unsigned int heightToCopy = firstTile->image->height; |
| 780 | unsigned int maxY = firstTile->image->height * (rowIndex + 1); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 781 | if (maxY > grid->outputHeight) { |
| 782 | heightToCopy -= maxY - grid->outputHeight; |
| 783 | } |
| 784 | |
| 785 | // Y and A channels |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 786 | size_t yaColOffset = colIndex * firstTile->image->width; |
| 787 | size_t yaRowOffset = rowIndex * firstTile->image->height; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 788 | size_t yaRowBytes = widthToCopy * pixelBytes; |
| 789 | |
| 790 | if (alpha) { |
| 791 | // A |
| 792 | for (unsigned int j = 0; j < heightToCopy; ++j) { |
| 793 | uint8_t * src = &tile->image->alphaPlane[j * tile->image->alphaRowBytes]; |
| 794 | uint8_t * dst = &dstImage->alphaPlane[(yaColOffset * pixelBytes) + ((yaRowOffset + j) * dstImage->alphaRowBytes)]; |
| 795 | memcpy(dst, src, yaRowBytes); |
| 796 | } |
| 797 | } else { |
| 798 | // Y |
| 799 | for (unsigned int j = 0; j < heightToCopy; ++j) { |
| 800 | uint8_t * src = &tile->image->yuvPlanes[AVIF_CHAN_Y][j * tile->image->yuvRowBytes[AVIF_CHAN_Y]]; |
| 801 | uint8_t * dst = |
| 802 | &dstImage->yuvPlanes[AVIF_CHAN_Y][(yaColOffset * pixelBytes) + ((yaRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_Y])]; |
| 803 | memcpy(dst, src, yaRowBytes); |
| 804 | } |
| 805 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 806 | if (!firstTileUVPresent) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 807 | continue; |
| 808 | } |
| 809 | |
| 810 | // UV |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 811 | heightToCopy >>= formatInfo.chromaShiftY; |
| 812 | size_t uvColOffset = yaColOffset >> formatInfo.chromaShiftX; |
| 813 | size_t uvRowOffset = yaRowOffset >> formatInfo.chromaShiftY; |
| 814 | size_t uvRowBytes = yaRowBytes >> formatInfo.chromaShiftX; |
| 815 | for (unsigned int j = 0; j < heightToCopy; ++j) { |
| 816 | uint8_t * srcU = &tile->image->yuvPlanes[AVIF_CHAN_U][j * tile->image->yuvRowBytes[AVIF_CHAN_U]]; |
| 817 | uint8_t * dstU = |
| 818 | &dstImage->yuvPlanes[AVIF_CHAN_U][(uvColOffset * pixelBytes) + ((uvRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_U])]; |
| 819 | memcpy(dstU, srcU, uvRowBytes); |
| 820 | |
| 821 | uint8_t * srcV = &tile->image->yuvPlanes[AVIF_CHAN_V][j * tile->image->yuvRowBytes[AVIF_CHAN_V]]; |
| 822 | uint8_t * dstV = |
| 823 | &dstImage->yuvPlanes[AVIF_CHAN_V][(uvColOffset * pixelBytes) + ((uvRowOffset + j) * dstImage->yuvRowBytes[AVIF_CHAN_V])]; |
| 824 | memcpy(dstV, srcV, uvRowBytes); |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | return AVIF_TRUE; |
| 831 | } |
| 832 | |
Joe Drago | 39267fd | 2020-06-19 15:21:14 -0700 | [diff] [blame] | 833 | // If colorId == 0 (a sentinel value as item IDs must be nonzero), accept any found EXIF/XMP metadata. Passing in 0 |
| 834 | // is used when finding metadata in a meta box embedded in a trak box, as any items inside of a meta box that is |
Joe Drago | fa99082 | 2020-06-19 15:23:55 -0700 | [diff] [blame] | 835 | // inside of a trak box are implicitly associated to the track. |
Joe Drago | 85050a3 | 2020-06-15 20:05:37 -0700 | [diff] [blame] | 836 | static avifBool avifDecoderDataFindMetadata(avifDecoderData * data, avifMeta * meta, avifImage * image, uint32_t colorId) |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 837 | { |
| 838 | avifROData exifData = AVIF_DATA_EMPTY; |
| 839 | avifROData xmpData = AVIF_DATA_EMPTY; |
| 840 | |
| 841 | for (uint32_t itemIndex = 0; itemIndex < meta->items.count; ++itemIndex) { |
| 842 | avifDecoderItem * item = &meta->items.item[itemIndex]; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 843 | if (!item->size) { |
| 844 | continue; |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 845 | } |
| 846 | if (item->hasUnsupportedEssentialProperty) { |
| 847 | // An essential property isn't supported by libavif; ignore the item. |
| 848 | continue; |
| 849 | } |
| 850 | |
| 851 | if ((colorId > 0) && (item->descForID != colorId)) { |
| 852 | // Not a content description (metadata) for the colorOBU, skip it |
| 853 | continue; |
| 854 | } |
| 855 | |
| 856 | if (!memcmp(item->type, "Exif", 4)) { |
| 857 | // Advance past Annex A.2.1's header |
| 858 | const uint8_t * boxPtr = avifDecoderDataCalcItemPtr(data, item); |
| 859 | BEGIN_STREAM(exifBoxStream, boxPtr, item->size); |
| 860 | uint32_t exifTiffHeaderOffset; |
| 861 | CHECK(avifROStreamReadU32(&exifBoxStream, &exifTiffHeaderOffset)); // unsigned int(32) exif_tiff_header_offset; |
| 862 | |
| 863 | exifData.data = avifROStreamCurrent(&exifBoxStream); |
| 864 | exifData.size = avifROStreamRemainingBytes(&exifBoxStream); |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 865 | } 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] | 866 | xmpData.data = avifDecoderDataCalcItemPtr(data, item); |
| 867 | xmpData.size = item->size; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | if (exifData.data && exifData.size) { |
| 872 | avifImageSetMetadataExif(image, exifData.data, exifData.size); |
| 873 | } |
| 874 | if (xmpData.data && xmpData.size) { |
| 875 | avifImageSetMetadataXMP(image, xmpData.data, xmpData.size); |
| 876 | } |
| 877 | return AVIF_TRUE; |
| 878 | } |
| 879 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 880 | // --------------------------------------------------------------------------- |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 881 | // URN |
| 882 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 883 | static avifBool isAlphaURN(const char * urn) |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 884 | { |
wantehchang | 3fde0d0 | 2020-03-10 23:58:32 -0700 | [diff] [blame] | 885 | return !strcmp(urn, URN_ALPHA0) || !strcmp(urn, URN_ALPHA1); |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | // --------------------------------------------------------------------------- |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 889 | // BMFF Parsing |
| 890 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 891 | static avifBool avifParseItemLocationBox(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 892 | { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 893 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 894 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 895 | uint8_t version; |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 896 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 897 | if (version > 2) { |
| 898 | return AVIF_FALSE; |
| 899 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 900 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 901 | uint8_t offsetSizeAndLengthSize; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 902 | CHECK(avifROStreamRead(&s, &offsetSizeAndLengthSize, 1)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 903 | uint8_t offsetSize = (offsetSizeAndLengthSize >> 4) & 0xf; // unsigned int(4) offset_size; |
| 904 | uint8_t lengthSize = (offsetSizeAndLengthSize >> 0) & 0xf; // unsigned int(4) length_size; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 905 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 906 | uint8_t baseOffsetSizeAndIndexSize; |
| 907 | CHECK(avifROStreamRead(&s, &baseOffsetSizeAndIndexSize, 1)); |
| 908 | uint8_t baseOffsetSize = (baseOffsetSizeAndIndexSize >> 4) & 0xf; // unsigned int(4) base_offset_size; |
| 909 | uint8_t indexSize = 0; |
| 910 | if ((version == 1) || (version == 2)) { |
| 911 | indexSize = baseOffsetSizeAndIndexSize & 0xf; // unsigned int(4) index_size; |
| 912 | if (indexSize != 0) { |
| 913 | // extent_index unsupported |
| 914 | return AVIF_FALSE; |
| 915 | } |
| 916 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 917 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 918 | uint16_t tmp16; |
| 919 | uint32_t itemCount; |
| 920 | if (version < 2) { |
| 921 | CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_count; |
| 922 | itemCount = tmp16; |
| 923 | } else { |
| 924 | CHECK(avifROStreamReadU32(&s, &itemCount)); // unsigned int(32) item_count; |
| 925 | } |
| 926 | for (uint32_t i = 0; i < itemCount; ++i) { |
| 927 | uint32_t itemID; |
| 928 | uint32_t idatID = 0; |
| 929 | if (version < 2) { |
| 930 | CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID; |
| 931 | itemID = tmp16; |
| 932 | } else { |
| 933 | CHECK(avifROStreamReadU32(&s, &itemID)); // unsigned int(32) item_ID; |
| 934 | } |
| 935 | |
| 936 | if ((version == 1) || (version == 2)) { |
| 937 | uint8_t ignored; |
| 938 | uint8_t constructionMethod; |
| 939 | CHECK(avifROStreamRead(&s, &ignored, 1)); // unsigned int(12) reserved = 0; |
| 940 | CHECK(avifROStreamRead(&s, &constructionMethod, 1)); // unsigned int(4) construction_method; |
| 941 | constructionMethod = constructionMethod & 0xf; |
| 942 | if ((constructionMethod != 0 /* file */) && (constructionMethod != 1 /* idat */)) { |
| 943 | // construction method item(2) unsupported |
| 944 | return AVIF_FALSE; |
| 945 | } |
| 946 | if (constructionMethod == 1) { |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 947 | idatID = meta->idatID; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 951 | uint16_t dataReferenceIndex; // unsigned int(16) data_ref rence_index; |
| 952 | CHECK(avifROStreamReadU16(&s, &dataReferenceIndex)); // |
| 953 | uint64_t baseOffset; // unsigned int(base_offset_size*8) base_offset; |
| 954 | CHECK(avifROStreamReadUX8(&s, &baseOffset, baseOffsetSize)); // |
| 955 | uint16_t extentCount; // unsigned int(16) extent_count; |
| 956 | CHECK(avifROStreamReadU16(&s, &extentCount)); // |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 957 | if (extentCount == 1) { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 958 | // If extent_index is ever supported, this spec must be implemented here: |
| 959 | // :: if (((version == 1) || (version == 2)) && (index_size > 0)) { |
| 960 | // :: unsigned int(index_size*8) extent_index; |
| 961 | // :: } |
| 962 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 963 | uint64_t extentOffset; // unsigned int(offset_size*8) extent_offset; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 964 | CHECK(avifROStreamReadUX8(&s, &extentOffset, offsetSize)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 965 | uint64_t extentLength; // unsigned int(offset_size*8) extent_length; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 966 | CHECK(avifROStreamReadUX8(&s, &extentLength, lengthSize)); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 967 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 968 | avifDecoderItem * item = avifMetaFindItem(meta, itemID); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 969 | if (!item) { |
| 970 | return AVIF_FALSE; |
| 971 | } |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 972 | item->id = itemID; |
| 973 | item->offset = (uint32_t)(baseOffset + extentOffset); |
| 974 | item->size = (uint32_t)extentLength; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 975 | item->idatID = idatID; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 976 | } else { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 977 | // TODO: support more than one extent |
| 978 | return AVIF_FALSE; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | return AVIF_TRUE; |
| 982 | } |
| 983 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 984 | static avifBool avifParseImageGridBox(avifImageGrid * grid, const uint8_t * raw, size_t rawLen) |
| 985 | { |
| 986 | BEGIN_STREAM(s, raw, rawLen); |
| 987 | |
| 988 | uint8_t version, flags; |
| 989 | CHECK(avifROStreamRead(&s, &version, 1)); // unsigned int(8) version = 0; |
| 990 | if (version != 0) { |
| 991 | return AVIF_FALSE; |
| 992 | } |
| 993 | CHECK(avifROStreamRead(&s, &flags, 1)); // unsigned int(8) flags; |
| 994 | CHECK(avifROStreamRead(&s, &grid->rows, 1)); // unsigned int(8) rows_minus_one; |
| 995 | CHECK(avifROStreamRead(&s, &grid->columns, 1)); // unsigned int(8) columns_minus_one; |
| 996 | ++grid->rows; |
| 997 | ++grid->columns; |
| 998 | |
| 999 | uint32_t fieldLength = ((flags & 1) + 1) * 16; |
| 1000 | if (fieldLength == 16) { |
| 1001 | uint16_t outputWidth16, outputHeight16; |
| 1002 | CHECK(avifROStreamReadU16(&s, &outputWidth16)); // unsigned int(FieldLength) output_width; |
| 1003 | CHECK(avifROStreamReadU16(&s, &outputHeight16)); // unsigned int(FieldLength) output_height; |
| 1004 | grid->outputWidth = outputWidth16; |
| 1005 | grid->outputHeight = outputHeight16; |
| 1006 | } else { |
| 1007 | if (fieldLength != 32) { |
| 1008 | // This should be impossible |
| 1009 | return AVIF_FALSE; |
| 1010 | } |
| 1011 | CHECK(avifROStreamReadU32(&s, &grid->outputWidth)); // unsigned int(FieldLength) output_width; |
| 1012 | CHECK(avifROStreamReadU32(&s, &grid->outputHeight)); // unsigned int(FieldLength) output_height; |
| 1013 | } |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame^] | 1014 | if ((grid->outputWidth == 0) || (grid->outputHeight == 0) || (grid->outputWidth > (AVIF_MAX_IMAGE_SIZE / grid->outputHeight))) { |
Wan-Teh Chang | 0a8e724 | 2020-08-10 13:24:59 -0700 | [diff] [blame] | 1015 | return AVIF_FALSE; |
| 1016 | } |
wantehchang | ae2074b | 2020-08-12 13:02:27 -0700 | [diff] [blame] | 1017 | return avifROStreamRemainingBytes(&s) == 0; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1018 | } |
| 1019 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1020 | static avifBool avifParseImageSpatialExtentsProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1021 | { |
| 1022 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1023 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1024 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1025 | avifImageSpatialExtents * ispe = &prop->u.ispe; |
| 1026 | CHECK(avifROStreamReadU32(&s, &ispe->width)); |
| 1027 | CHECK(avifROStreamReadU32(&s, &ispe->height)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1028 | return AVIF_TRUE; |
| 1029 | } |
| 1030 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1031 | static avifBool avifParseAuxiliaryTypeProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1032 | { |
| 1033 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1034 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1035 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1036 | CHECK(avifROStreamReadString(&s, prop->u.auxC.auxType, AUXTYPE_SIZE)); |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1037 | return AVIF_TRUE; |
| 1038 | } |
| 1039 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1040 | static avifBool avifParseColourInformationBox(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 1041 | { |
| 1042 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | e7ce20d | 2019-02-11 16:37:38 -0800 | [diff] [blame] | 1043 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1044 | avifColourInformationBox * colr = &prop->u.colr; |
| 1045 | colr->hasICC = AVIF_FALSE; |
| 1046 | colr->hasNCLX = AVIF_FALSE; |
Joe Drago | e7ce20d | 2019-02-11 16:37:38 -0800 | [diff] [blame] | 1047 | |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1048 | uint8_t colorType[4]; // unsigned int(32) colour_type; |
| 1049 | CHECK(avifROStreamRead(&s, colorType, 4)); |
| 1050 | if (!memcmp(colorType, "rICC", 4) || !memcmp(colorType, "prof", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1051 | colr->hasICC = AVIF_TRUE; |
| 1052 | colr->icc = avifROStreamCurrent(&s); |
| 1053 | colr->iccSize = avifROStreamRemainingBytes(&s); |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 1054 | } else if (!memcmp(colorType, "nclx", 4)) { |
Joe Drago | 74cd1c9 | 2020-04-16 12:17:11 -0700 | [diff] [blame] | 1055 | uint16_t tmp16; |
Joe Drago | 97b071c | 2019-07-17 14:24:56 -0700 | [diff] [blame] | 1056 | // unsigned int(16) colour_primaries; |
Joe Drago | 74cd1c9 | 2020-04-16 12:17:11 -0700 | [diff] [blame] | 1057 | CHECK(avifROStreamReadU16(&s, &tmp16)); |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1058 | colr->colorPrimaries = (avifColorPrimaries)tmp16; |
Joe Drago | 97b071c | 2019-07-17 14:24:56 -0700 | [diff] [blame] | 1059 | // unsigned int(16) transfer_characteristics; |
Joe Drago | 74cd1c9 | 2020-04-16 12:17:11 -0700 | [diff] [blame] | 1060 | CHECK(avifROStreamReadU16(&s, &tmp16)); |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1061 | colr->transferCharacteristics = (avifTransferCharacteristics)tmp16; |
Joe Drago | 97b071c | 2019-07-17 14:24:56 -0700 | [diff] [blame] | 1062 | // unsigned int(16) matrix_coefficients; |
Joe Drago | 74cd1c9 | 2020-04-16 12:17:11 -0700 | [diff] [blame] | 1063 | CHECK(avifROStreamReadU16(&s, &tmp16)); |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1064 | colr->matrixCoefficients = (avifMatrixCoefficients)tmp16; |
Joe Drago | 97b071c | 2019-07-17 14:24:56 -0700 | [diff] [blame] | 1065 | // unsigned int(1) full_range_flag; |
| 1066 | // unsigned int(7) reserved = 0; |
Joe Drago | 74cd1c9 | 2020-04-16 12:17:11 -0700 | [diff] [blame] | 1067 | uint8_t tmp8; |
| 1068 | CHECK(avifROStreamRead(&s, &tmp8, 1)); |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1069 | colr->range = (tmp8 & 0x80) ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED; |
| 1070 | colr->hasNCLX = AVIF_TRUE; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 1071 | } |
| 1072 | return AVIF_TRUE; |
| 1073 | } |
| 1074 | |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1075 | static avifBool avifParseAV1CodecConfigurationBox(const uint8_t * raw, size_t rawLen, avifCodecConfigurationBox * av1C) |
| 1076 | { |
| 1077 | BEGIN_STREAM(s, raw, rawLen); |
| 1078 | |
| 1079 | uint8_t markerAndVersion = 0; |
| 1080 | CHECK(avifROStreamRead(&s, &markerAndVersion, 1)); |
| 1081 | uint8_t seqProfileAndIndex = 0; |
| 1082 | CHECK(avifROStreamRead(&s, &seqProfileAndIndex, 1)); |
| 1083 | uint8_t rawFlags = 0; |
| 1084 | CHECK(avifROStreamRead(&s, &rawFlags, 1)); |
| 1085 | |
| 1086 | if (markerAndVersion != 0x81) { |
| 1087 | // Marker and version must both == 1 |
| 1088 | return AVIF_FALSE; |
| 1089 | } |
| 1090 | |
| 1091 | av1C->seqProfile = (seqProfileAndIndex >> 5) & 0x7; // unsigned int (3) seq_profile; |
| 1092 | av1C->seqLevelIdx0 = (seqProfileAndIndex >> 0) & 0x1f; // unsigned int (5) seq_level_idx_0; |
| 1093 | av1C->seqTier0 = (rawFlags >> 7) & 0x1; // unsigned int (1) seq_tier_0; |
| 1094 | av1C->highBitdepth = (rawFlags >> 6) & 0x1; // unsigned int (1) high_bitdepth; |
| 1095 | av1C->twelveBit = (rawFlags >> 5) & 0x1; // unsigned int (1) twelve_bit; |
| 1096 | av1C->monochrome = (rawFlags >> 4) & 0x1; // unsigned int (1) monochrome; |
| 1097 | av1C->chromaSubsamplingX = (rawFlags >> 3) & 0x1; // unsigned int (1) chroma_subsampling_x; |
| 1098 | av1C->chromaSubsamplingY = (rawFlags >> 2) & 0x1; // unsigned int (1) chroma_subsampling_y; |
| 1099 | av1C->chromaSamplePosition = (rawFlags >> 0) & 0x3; // unsigned int (2) chroma_sample_position; |
| 1100 | return AVIF_TRUE; |
| 1101 | } |
| 1102 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1103 | static avifBool avifParseAV1CodecConfigurationBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1104 | { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1105 | return avifParseAV1CodecConfigurationBox(raw, rawLen, &prop->u.av1C); |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1108 | static avifBool avifParsePixelAspectRatioBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1109 | { |
| 1110 | BEGIN_STREAM(s, raw, rawLen); |
| 1111 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1112 | avifPixelAspectRatioBox * pasp = &prop->u.pasp; |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1113 | CHECK(avifROStreamReadU32(&s, &pasp->hSpacing)); // unsigned int(32) hSpacing; |
| 1114 | CHECK(avifROStreamReadU32(&s, &pasp->vSpacing)); // unsigned int(32) vSpacing; |
| 1115 | return AVIF_TRUE; |
| 1116 | } |
| 1117 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1118 | static avifBool avifParseCleanApertureBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1119 | { |
| 1120 | BEGIN_STREAM(s, raw, rawLen); |
| 1121 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1122 | avifCleanApertureBox * clap = &prop->u.clap; |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1123 | CHECK(avifROStreamReadU32(&s, &clap->widthN)); // unsigned int(32) cleanApertureWidthN; |
| 1124 | CHECK(avifROStreamReadU32(&s, &clap->widthD)); // unsigned int(32) cleanApertureWidthD; |
| 1125 | CHECK(avifROStreamReadU32(&s, &clap->heightN)); // unsigned int(32) cleanApertureHeightN; |
| 1126 | CHECK(avifROStreamReadU32(&s, &clap->heightD)); // unsigned int(32) cleanApertureHeightD; |
| 1127 | CHECK(avifROStreamReadU32(&s, &clap->horizOffN)); // unsigned int(32) horizOffN; |
| 1128 | CHECK(avifROStreamReadU32(&s, &clap->horizOffD)); // unsigned int(32) horizOffD; |
| 1129 | CHECK(avifROStreamReadU32(&s, &clap->vertOffN)); // unsigned int(32) vertOffN; |
| 1130 | CHECK(avifROStreamReadU32(&s, &clap->vertOffD)); // unsigned int(32) vertOffD; |
| 1131 | return AVIF_TRUE; |
| 1132 | } |
| 1133 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1134 | static avifBool avifParseImageRotationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1135 | { |
| 1136 | BEGIN_STREAM(s, raw, rawLen); |
| 1137 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1138 | avifImageRotation * irot = &prop->u.irot; |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1139 | CHECK(avifROStreamRead(&s, &irot->angle, 1)); // unsigned int (6) reserved = 0; unsigned int (2) angle; |
| 1140 | if ((irot->angle & 0xfc) != 0) { |
| 1141 | // reserved bits must be 0 |
| 1142 | return AVIF_FALSE; |
| 1143 | } |
| 1144 | return AVIF_TRUE; |
| 1145 | } |
| 1146 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1147 | static avifBool avifParseImageMirrorProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1148 | { |
| 1149 | BEGIN_STREAM(s, raw, rawLen); |
| 1150 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1151 | avifImageMirror * imir = &prop->u.imir; |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 1152 | CHECK(avifROStreamRead(&s, &imir->axis, 1)); // unsigned int (7) reserved = 0; unsigned int (1) axis; |
| 1153 | if ((imir->axis & 0xfe) != 0) { |
| 1154 | // reserved bits must be 0 |
| 1155 | return AVIF_FALSE; |
| 1156 | } |
| 1157 | return AVIF_TRUE; |
| 1158 | } |
| 1159 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1160 | static avifBool avifParsePixelInformationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen) |
Joe Drago | 6042156 | 2020-04-23 11:32:26 -0700 | [diff] [blame] | 1161 | { |
| 1162 | BEGIN_STREAM(s, raw, rawLen); |
| 1163 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
| 1164 | |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1165 | avifPixelInformationProperty * pixi = &prop->u.pixi; |
Joe Drago | 6042156 | 2020-04-23 11:32:26 -0700 | [diff] [blame] | 1166 | CHECK(avifROStreamRead(&s, &pixi->planeCount, 1)); // unsigned int (8) num_channels; |
| 1167 | if (pixi->planeCount > MAX_PIXI_PLANE_DEPTHS) { |
| 1168 | return AVIF_FALSE; |
| 1169 | } |
| 1170 | for (uint8_t i = 0; i < pixi->planeCount; ++i) { |
| 1171 | CHECK(avifROStreamRead(&s, &pixi->planeDepths[i], 1)); // unsigned int (8) bits_per_channel; |
| 1172 | } |
| 1173 | return AVIF_TRUE; |
| 1174 | } |
| 1175 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1176 | static avifBool avifParseItemPropertyContainerBox(avifPropertyArray * properties, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1177 | { |
| 1178 | BEGIN_STREAM(s, raw, rawLen); |
| 1179 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1180 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1181 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1182 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1183 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1184 | int propertyIndex = avifArrayPushIndex(properties); |
| 1185 | avifProperty * prop = &properties->prop[propertyIndex]; |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1186 | memcpy(prop->type, header.type, 4); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1187 | if (!memcmp(header.type, "ispe", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1188 | CHECK(avifParseImageSpatialExtentsProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1189 | } else if (!memcmp(header.type, "auxC", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1190 | CHECK(avifParseAuxiliaryTypeProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1191 | } else if (!memcmp(header.type, "colr", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1192 | CHECK(avifParseColourInformationBox(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1193 | } else if (!memcmp(header.type, "av1C", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1194 | CHECK(avifParseAV1CodecConfigurationBoxProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1195 | } else if (!memcmp(header.type, "pasp", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1196 | CHECK(avifParsePixelAspectRatioBoxProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1197 | } else if (!memcmp(header.type, "clap", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1198 | CHECK(avifParseCleanApertureBoxProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1199 | } else if (!memcmp(header.type, "irot", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1200 | CHECK(avifParseImageRotationProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1201 | } else if (!memcmp(header.type, "imir", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1202 | CHECK(avifParseImageMirrorProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1203 | } else if (!memcmp(header.type, "pixi", 4)) { |
Wan-Teh Chang | 2031dc1 | 2020-05-22 09:24:46 -0700 | [diff] [blame] | 1204 | CHECK(avifParsePixelInformationProperty(prop, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 6042156 | 2020-04-23 11:32:26 -0700 | [diff] [blame] | 1205 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1206 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1207 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1208 | } |
| 1209 | return AVIF_TRUE; |
| 1210 | } |
| 1211 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1212 | static avifBool avifParseItemPropertyAssociation(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1213 | { |
| 1214 | BEGIN_STREAM(s, raw, rawLen); |
| 1215 | |
| 1216 | uint8_t version; |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 1217 | uint32_t flags; |
| 1218 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, &flags)); |
| 1219 | avifBool propertyIndexIsU16 = ((flags & 0x1) != 0); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1220 | |
| 1221 | uint32_t entryCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1222 | CHECK(avifROStreamReadU32(&s, &entryCount)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1223 | for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) { |
| 1224 | unsigned int itemID; |
| 1225 | if (version < 1) { |
| 1226 | uint16_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1227 | CHECK(avifROStreamReadU16(&s, &tmp)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1228 | itemID = tmp; |
| 1229 | } else { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1230 | CHECK(avifROStreamReadU32(&s, &itemID)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1231 | } |
| 1232 | uint8_t associationCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1233 | CHECK(avifROStreamRead(&s, &associationCount, 1)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1234 | for (uint8_t associationIndex = 0; associationIndex < associationCount; ++associationIndex) { |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 1235 | avifBool essential = AVIF_FALSE; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1236 | uint16_t propertyIndex = 0; |
| 1237 | if (propertyIndexIsU16) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1238 | CHECK(avifROStreamReadU16(&s, &propertyIndex)); |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 1239 | essential = ((propertyIndex & 0x8000) != 0); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1240 | propertyIndex &= 0x7fff; |
| 1241 | } else { |
| 1242 | uint8_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1243 | CHECK(avifROStreamRead(&s, &tmp, 1)); |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 1244 | essential = ((tmp & 0x80) != 0); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1245 | propertyIndex = tmp & 0x7f; |
| 1246 | } |
| 1247 | |
| 1248 | if (propertyIndex == 0) { |
| 1249 | // Not associated with any item |
| 1250 | continue; |
| 1251 | } |
| 1252 | --propertyIndex; // 1-indexed |
| 1253 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1254 | if (propertyIndex >= meta->properties.count) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1255 | return AVIF_FALSE; |
| 1256 | } |
| 1257 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1258 | avifDecoderItem * item = avifMetaFindItem(meta, itemID); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1259 | if (!item) { |
| 1260 | return AVIF_FALSE; |
| 1261 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1262 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1263 | // Copy property to item |
| 1264 | avifProperty * srcProp = &meta->properties.prop[propertyIndex]; |
| 1265 | |
| 1266 | static const char * supportedTypes[] = { "ispe", "auxC", "colr", "av1C", "pasp", "clap", "irot", "imir", "pixi" }; |
| 1267 | size_t supportedTypesCount = sizeof(supportedTypes) / sizeof(supportedTypes[0]); |
| 1268 | avifBool supportedType = AVIF_FALSE; |
| 1269 | for (size_t i = 0; i < supportedTypesCount; ++i) { |
| 1270 | if (!memcmp(srcProp->type, supportedTypes[i], 4)) { |
| 1271 | supportedType = AVIF_TRUE; |
| 1272 | break; |
| 1273 | } |
| 1274 | } |
| 1275 | if (supportedType) { |
| 1276 | avifProperty * dstProp = (avifProperty *)avifArrayPushPtr(&item->properties); |
| 1277 | memcpy(dstProp, srcProp, sizeof(avifProperty)); |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 1278 | } else { |
| 1279 | if (essential) { |
| 1280 | // Discovered an essential item property that libavif doesn't support! |
| 1281 | // Make a note to ignore this item later. |
| 1282 | item->hasUnsupportedEssentialProperty = AVIF_TRUE; |
| 1283 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | return AVIF_TRUE; |
| 1289 | } |
| 1290 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1291 | static avifBool avifParsePrimaryItemBox(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1292 | { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1293 | if (meta->primaryItemID > 0) { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1294 | // Illegal to have multiple pitm boxes, bail out |
| 1295 | return AVIF_FALSE; |
| 1296 | } |
| 1297 | |
| 1298 | BEGIN_STREAM(s, raw, rawLen); |
| 1299 | |
| 1300 | uint8_t version; |
| 1301 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
| 1302 | |
| 1303 | if (version == 0) { |
| 1304 | uint16_t tmp16; |
| 1305 | CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID; |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1306 | meta->primaryItemID = tmp16; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1307 | } else { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1308 | CHECK(avifROStreamReadU32(&s, &meta->primaryItemID)); // unsigned int(32) item_ID; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1309 | } |
| 1310 | return AVIF_TRUE; |
| 1311 | } |
| 1312 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1313 | static avifBool avifParseItemDataBox(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1314 | { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1315 | // 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] | 1316 | for (uint32_t i = 0; i < meta->idats.count; ++i) { |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 1317 | if (meta->idats.idat[i].id == meta->idatID) { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1318 | return AVIF_FALSE; |
| 1319 | } |
| 1320 | } |
| 1321 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1322 | int index = avifArrayPushIndex(&meta->idats); |
| 1323 | avifDecoderItemData * idat = &meta->idats.idat[index]; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 1324 | idat->id = meta->idatID; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1325 | idat->data.data = raw; |
| 1326 | idat->data.size = rawLen; |
| 1327 | return AVIF_TRUE; |
| 1328 | } |
| 1329 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1330 | static avifBool avifParseItemPropertiesBox(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1331 | { |
| 1332 | BEGIN_STREAM(s, raw, rawLen); |
| 1333 | |
| 1334 | avifBoxHeader ipcoHeader; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1335 | CHECK(avifROStreamReadBoxHeader(&s, &ipcoHeader)); |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame^] | 1336 | if (memcmp(ipcoHeader.type, "ipco", 4)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1337 | return AVIF_FALSE; |
| 1338 | } |
| 1339 | |
| 1340 | // Read all item properties inside of ItemPropertyContainerBox |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1341 | CHECK(avifParseItemPropertyContainerBox(&meta->properties, avifROStreamCurrent(&s), ipcoHeader.size)); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1342 | CHECK(avifROStreamSkip(&s, ipcoHeader.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1343 | |
| 1344 | // 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] | 1345 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1346 | avifBoxHeader ipmaHeader; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1347 | CHECK(avifROStreamReadBoxHeader(&s, &ipmaHeader)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1348 | |
| 1349 | if (!memcmp(ipmaHeader.type, "ipma", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1350 | CHECK(avifParseItemPropertyAssociation(meta, avifROStreamCurrent(&s), ipmaHeader.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1351 | } else { |
| 1352 | // These must all be type ipma |
| 1353 | return AVIF_FALSE; |
| 1354 | } |
| 1355 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1356 | CHECK(avifROStreamSkip(&s, ipmaHeader.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1357 | } |
| 1358 | return AVIF_TRUE; |
| 1359 | } |
| 1360 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1361 | static avifBool avifParseItemInfoEntry(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1362 | { |
| 1363 | BEGIN_STREAM(s, raw, rawLen); |
| 1364 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1365 | 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] | 1366 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1367 | uint16_t itemID; // unsigned int(16) item_ID; |
| 1368 | CHECK(avifROStreamReadU16(&s, &itemID)); // |
| 1369 | uint16_t itemProtectionIndex; // unsigned int(16) item_protection_index; |
| 1370 | CHECK(avifROStreamReadU16(&s, &itemProtectionIndex)); // |
| 1371 | uint8_t itemType[4]; // unsigned int(32) item_type; |
| 1372 | CHECK(avifROStreamRead(&s, itemType, 4)); // |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1373 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1374 | avifContentType contentType; |
| 1375 | if (!memcmp(itemType, "mime", 4)) { |
| 1376 | CHECK(avifROStreamReadString(&s, NULL, 0)); // string item_name; (skipped) |
| 1377 | CHECK(avifROStreamReadString(&s, contentType.contentType, CONTENTTYPE_SIZE)); // string content_type; |
| 1378 | } else { |
| 1379 | memset(&contentType, 0, sizeof(contentType)); |
| 1380 | } |
| 1381 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1382 | avifDecoderItem * item = avifMetaFindItem(meta, itemID); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1383 | if (!item) { |
| 1384 | return AVIF_FALSE; |
| 1385 | } |
| 1386 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 1387 | memcpy(item->type, itemType, sizeof(itemType)); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1388 | memcpy(&item->contentType, &contentType, sizeof(contentType)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1389 | return AVIF_TRUE; |
| 1390 | } |
| 1391 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1392 | static avifBool avifParseItemInfoBox(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 | uint32_t entryCount; |
| 1399 | if (version == 0) { |
| 1400 | uint16_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1401 | CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) entry_count; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1402 | entryCount = tmp; |
| 1403 | } else if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1404 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1405 | } else { |
| 1406 | return AVIF_FALSE; |
| 1407 | } |
| 1408 | |
Joe Drago | 678b938 | 2019-02-09 03:17:47 -0800 | [diff] [blame] | 1409 | for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1410 | avifBoxHeader infeHeader; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1411 | CHECK(avifROStreamReadBoxHeader(&s, &infeHeader)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1412 | |
| 1413 | if (!memcmp(infeHeader.type, "infe", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1414 | CHECK(avifParseItemInfoEntry(meta, avifROStreamCurrent(&s), infeHeader.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1415 | } else { |
| 1416 | // These must all be type ipma |
| 1417 | return AVIF_FALSE; |
| 1418 | } |
| 1419 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1420 | CHECK(avifROStreamSkip(&s, infeHeader.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | return AVIF_TRUE; |
| 1424 | } |
| 1425 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1426 | static avifBool avifParseItemReferenceBox(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1427 | { |
| 1428 | BEGIN_STREAM(s, raw, rawLen); |
| 1429 | |
| 1430 | uint8_t version; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1431 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1432 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1433 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1434 | avifBoxHeader irefHeader; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1435 | CHECK(avifROStreamReadBoxHeader(&s, &irefHeader)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1436 | |
| 1437 | uint32_t fromID = 0; |
| 1438 | if (version == 0) { |
| 1439 | uint16_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1440 | CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) from_item_ID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1441 | fromID = tmp; |
| 1442 | } else if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1443 | CHECK(avifROStreamReadU32(&s, &fromID)); // unsigned int(32) from_item_ID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1444 | } else { |
| 1445 | // unsupported iref version, skip it |
| 1446 | break; |
| 1447 | } |
| 1448 | |
| 1449 | uint16_t referenceCount = 0; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1450 | CHECK(avifROStreamReadU16(&s, &referenceCount)); // unsigned int(16) reference_count; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1451 | |
| 1452 | for (uint16_t refIndex = 0; refIndex < referenceCount; ++refIndex) { |
| 1453 | uint32_t toID = 0; |
| 1454 | if (version == 0) { |
| 1455 | uint16_t tmp; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1456 | CHECK(avifROStreamReadU16(&s, &tmp)); // unsigned int(16) to_item_ID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1457 | toID = tmp; |
| 1458 | } else if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1459 | CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) to_item_ID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1460 | } else { |
| 1461 | // unsupported iref version, skip it |
| 1462 | break; |
| 1463 | } |
| 1464 | |
| 1465 | // Read this reference as "{fromID} is a {irefType} for {toID}" |
| 1466 | if (fromID && toID) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1467 | avifDecoderItem * item = avifMetaFindItem(meta, fromID); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1468 | if (!item) { |
| 1469 | return AVIF_FALSE; |
| 1470 | } |
| 1471 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1472 | if (!memcmp(irefHeader.type, "thmb", 4)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 1473 | item->thumbnailForID = toID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1474 | } |
| 1475 | if (!memcmp(irefHeader.type, "auxl", 4)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 1476 | item->auxForID = toID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1477 | } |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1478 | if (!memcmp(irefHeader.type, "cdsc", 4)) { |
| 1479 | item->descForID = toID; |
| 1480 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1481 | if (!memcmp(irefHeader.type, "dimg", 4)) { |
| 1482 | // derived images refer in the opposite direction |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1483 | avifDecoderItem * dimg = avifMetaFindItem(meta, toID); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 1484 | if (!dimg) { |
| 1485 | return AVIF_FALSE; |
| 1486 | } |
| 1487 | |
| 1488 | dimg->dimgForID = fromID; |
| 1489 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | return AVIF_TRUE; |
| 1495 | } |
| 1496 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1497 | static avifBool avifParseMetaBox(avifMeta * meta, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1498 | { |
| 1499 | BEGIN_STREAM(s, raw, rawLen); |
| 1500 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1501 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1502 | |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 1503 | ++meta->idatID; // for tracking idat |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1504 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1505 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1506 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1507 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1508 | |
| 1509 | if (!memcmp(header.type, "iloc", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1510 | CHECK(avifParseItemLocationBox(meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1511 | } else if (!memcmp(header.type, "pitm", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1512 | CHECK(avifParsePrimaryItemBox(meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 1513 | } else if (!memcmp(header.type, "idat", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1514 | CHECK(avifParseItemDataBox(meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1515 | } else if (!memcmp(header.type, "iprp", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1516 | CHECK(avifParseItemPropertiesBox(meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1517 | } else if (!memcmp(header.type, "iinf", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1518 | CHECK(avifParseItemInfoBox(meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1519 | } else if (!memcmp(header.type, "iref", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1520 | CHECK(avifParseItemReferenceBox(meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1523 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1524 | } |
| 1525 | return AVIF_TRUE; |
| 1526 | } |
| 1527 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1528 | static avifBool avifParseTrackHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1529 | { |
| 1530 | BEGIN_STREAM(s, raw, rawLen); |
| 1531 | |
| 1532 | uint8_t version; |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 1533 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1534 | |
| 1535 | uint32_t ignored32, trackID; |
| 1536 | uint64_t ignored64; |
| 1537 | if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1538 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time; |
| 1539 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time; |
| 1540 | CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID; |
Joe Drago | fc4144e | 2019-09-27 20:35:06 -0700 | [diff] [blame] | 1541 | CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0; |
| 1542 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) duration; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1543 | } else if (version == 0) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1544 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time; |
| 1545 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time; |
| 1546 | CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID; |
Joe Drago | fc4144e | 2019-09-27 20:35:06 -0700 | [diff] [blame] | 1547 | CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0; |
| 1548 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) duration; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1549 | } else { |
| 1550 | // Unsupported version |
| 1551 | return AVIF_FALSE; |
| 1552 | } |
| 1553 | |
Joe Drago | fc4144e | 2019-09-27 20:35:06 -0700 | [diff] [blame] | 1554 | // Skipping the following 52 bytes here: |
| 1555 | // ------------------------------------ |
| 1556 | // const unsigned int(32)[2] reserved = 0; |
| 1557 | // template int(16) layer = 0; |
| 1558 | // template int(16) alternate_group = 0; |
| 1559 | // template int(16) volume = {if track_is_audio 0x0100 else 0}; |
| 1560 | // const unsigned int(16) reserved = 0; |
| 1561 | // template int(32)[9] matrix= { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 }; // unity matrix |
| 1562 | CHECK(avifROStreamSkip(&s, 52)); |
| 1563 | |
| 1564 | uint32_t width, height; |
| 1565 | CHECK(avifROStreamReadU32(&s, &width)); // unsigned int(32) width; |
| 1566 | CHECK(avifROStreamReadU32(&s, &height)); // unsigned int(32) height; |
| 1567 | track->width = width >> 16; |
| 1568 | track->height = height >> 16; |
| 1569 | |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1570 | // TODO: support scaling based on width/height track header info? |
| 1571 | |
| 1572 | track->id = trackID; |
| 1573 | return AVIF_TRUE; |
| 1574 | } |
| 1575 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1576 | static avifBool avifParseMediaHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1577 | { |
| 1578 | BEGIN_STREAM(s, raw, rawLen); |
| 1579 | |
| 1580 | uint8_t version; |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 1581 | CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1582 | |
| 1583 | uint32_t ignored32, mediaTimescale, mediaDuration32; |
| 1584 | uint64_t ignored64, mediaDuration64; |
| 1585 | if (version == 1) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1586 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time; |
| 1587 | CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time; |
| 1588 | CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale; |
| 1589 | CHECK(avifROStreamReadU64(&s, &mediaDuration64)); // unsigned int(64) duration; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1590 | track->mediaDuration = mediaDuration64; |
| 1591 | } else if (version == 0) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1592 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time; |
| 1593 | CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time; |
| 1594 | CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale; |
| 1595 | CHECK(avifROStreamReadU32(&s, &mediaDuration32)); // unsigned int(32) duration; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1596 | track->mediaDuration = (uint64_t)mediaDuration32; |
| 1597 | } else { |
| 1598 | // Unsupported version |
| 1599 | return AVIF_FALSE; |
| 1600 | } |
| 1601 | |
| 1602 | track->mediaTimescale = mediaTimescale; |
| 1603 | return AVIF_TRUE; |
| 1604 | } |
| 1605 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1606 | 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] | 1607 | { |
| 1608 | BEGIN_STREAM(s, raw, rawLen); |
| 1609 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1610 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1611 | |
| 1612 | uint32_t entryCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1613 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1614 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 1615 | uint64_t offset; |
| 1616 | if (largeOffsets) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1617 | CHECK(avifROStreamReadU64(&s, &offset)); // unsigned int(32) chunk_offset; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1618 | } else { |
| 1619 | uint32_t offset32; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1620 | CHECK(avifROStreamReadU32(&s, &offset32)); // unsigned int(32) chunk_offset; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1621 | offset = (uint64_t)offset32; |
| 1622 | } |
| 1623 | |
| 1624 | avifSampleTableChunk * chunk = (avifSampleTableChunk *)avifArrayPushPtr(&sampleTable->chunks); |
| 1625 | chunk->offset = offset; |
| 1626 | } |
| 1627 | return AVIF_TRUE; |
| 1628 | } |
| 1629 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1630 | static avifBool avifParseSampleToChunkBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1631 | { |
| 1632 | BEGIN_STREAM(s, raw, rawLen); |
| 1633 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1634 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1635 | |
| 1636 | uint32_t entryCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1637 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1638 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 1639 | avifSampleTableSampleToChunk * sampleToChunk = (avifSampleTableSampleToChunk *)avifArrayPushPtr(&sampleTable->sampleToChunks); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1640 | CHECK(avifROStreamReadU32(&s, &sampleToChunk->firstChunk)); // unsigned int(32) first_chunk; |
| 1641 | CHECK(avifROStreamReadU32(&s, &sampleToChunk->samplesPerChunk)); // unsigned int(32) samples_per_chunk; |
| 1642 | CHECK(avifROStreamReadU32(&s, &sampleToChunk->sampleDescriptionIndex)); // unsigned int(32) sample_description_index; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1643 | } |
| 1644 | return AVIF_TRUE; |
| 1645 | } |
| 1646 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1647 | static avifBool avifParseSampleSizeBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1648 | { |
| 1649 | BEGIN_STREAM(s, raw, rawLen); |
| 1650 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1651 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1652 | |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 1653 | uint32_t allSamplesSize, sampleCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1654 | CHECK(avifROStreamReadU32(&s, &allSamplesSize)); // unsigned int(32) sample_size; |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 1655 | CHECK(avifROStreamReadU32(&s, &sampleCount)); // unsigned int(32) sample_count; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1656 | |
Joe Drago | 370be3f | 2020-02-07 15:59:42 -0800 | [diff] [blame] | 1657 | if (allSamplesSize > 0) { |
| 1658 | sampleTable->allSamplesSize = allSamplesSize; |
| 1659 | } else { |
| 1660 | for (uint32_t i = 0; i < sampleCount; ++i) { |
| 1661 | avifSampleTableSampleSize * sampleSize = (avifSampleTableSampleSize *)avifArrayPushPtr(&sampleTable->sampleSizes); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1662 | CHECK(avifROStreamReadU32(&s, &sampleSize->size)); // unsigned int(32) entry_size; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | return AVIF_TRUE; |
| 1666 | } |
| 1667 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1668 | static avifBool avifParseSyncSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen) |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 1669 | { |
| 1670 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 1671 | |
| 1672 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
| 1673 | |
| 1674 | uint32_t entryCount; |
| 1675 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
| 1676 | |
| 1677 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 1678 | uint32_t sampleNumber = 0; |
| 1679 | CHECK(avifROStreamReadU32(&s, &sampleNumber)); // unsigned int(32) sample_number; |
| 1680 | avifSyncSample * syncSample = (avifSyncSample *)avifArrayPushPtr(&sampleTable->syncSamples); |
| 1681 | syncSample->sampleNumber = sampleNumber; |
| 1682 | } |
| 1683 | return AVIF_TRUE; |
| 1684 | } |
| 1685 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1686 | static avifBool avifParseTimeToSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1687 | { |
| 1688 | BEGIN_STREAM(s, raw, rawLen); |
| 1689 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1690 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1691 | |
| 1692 | uint32_t entryCount; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1693 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1694 | |
| 1695 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 1696 | avifSampleTableTimeToSample * timeToSample = (avifSampleTableTimeToSample *)avifArrayPushPtr(&sampleTable->timeToSamples); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1697 | CHECK(avifROStreamReadU32(&s, &timeToSample->sampleCount)); // unsigned int(32) sample_count; |
| 1698 | CHECK(avifROStreamReadU32(&s, &timeToSample->sampleDelta)); // unsigned int(32) sample_delta; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1699 | } |
| 1700 | return AVIF_TRUE; |
| 1701 | } |
| 1702 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1703 | static avifBool avifParseSampleDescriptionBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen) |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 1704 | { |
| 1705 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 1706 | |
| 1707 | CHECK(avifROStreamReadAndEnforceVersion(&s, 0)); |
| 1708 | |
| 1709 | uint32_t entryCount; |
| 1710 | CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
| 1711 | |
| 1712 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 1713 | avifBoxHeader sampleEntryHeader; |
| 1714 | CHECK(avifROStreamReadBoxHeader(&s, &sampleEntryHeader)); |
| 1715 | |
| 1716 | avifSampleDescription * description = (avifSampleDescription *)avifArrayPushPtr(&sampleTable->sampleDescriptions); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1717 | avifArrayCreate(&description->properties, sizeof(avifProperty), 16); |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 1718 | memcpy(description->format, sampleEntryHeader.type, sizeof(description->format)); |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1719 | size_t remainingBytes = avifROStreamRemainingBytes(&s); |
| 1720 | if (!memcmp(description->format, "av01", 4) && (remainingBytes > VISUALSAMPLEENTRY_SIZE)) { |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1721 | CHECK(avifParseItemPropertyContainerBox( |
| 1722 | &description->properties, avifROStreamCurrent(&s) + VISUALSAMPLEENTRY_SIZE, remainingBytes - VISUALSAMPLEENTRY_SIZE)); |
Joe Drago | 6500fd6 | 2019-10-08 17:17:34 -0700 | [diff] [blame] | 1723 | } |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 1724 | |
| 1725 | CHECK(avifROStreamSkip(&s, sampleEntryHeader.size)); |
| 1726 | } |
| 1727 | return AVIF_TRUE; |
| 1728 | } |
| 1729 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1730 | static avifBool avifParseSampleTableBox(avifTrack * track, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1731 | { |
| 1732 | if (track->sampleTable) { |
| 1733 | // A TrackBox may only have one SampleTable |
| 1734 | return AVIF_FALSE; |
| 1735 | } |
| 1736 | track->sampleTable = avifSampleTableCreate(); |
| 1737 | |
| 1738 | BEGIN_STREAM(s, raw, rawLen); |
| 1739 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1740 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1741 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1742 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1743 | |
| 1744 | if (!memcmp(header.type, "stco", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1745 | CHECK(avifParseChunkOffsetBox(track->sampleTable, AVIF_FALSE, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1746 | } else if (!memcmp(header.type, "co64", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1747 | CHECK(avifParseChunkOffsetBox(track->sampleTable, AVIF_TRUE, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1748 | } else if (!memcmp(header.type, "stsc", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1749 | CHECK(avifParseSampleToChunkBox(track->sampleTable, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1750 | } else if (!memcmp(header.type, "stsz", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1751 | CHECK(avifParseSampleSizeBox(track->sampleTable, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 1752 | } else if (!memcmp(header.type, "stss", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1753 | CHECK(avifParseSyncSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1754 | } else if (!memcmp(header.type, "stts", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1755 | CHECK(avifParseTimeToSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 1756 | } else if (!memcmp(header.type, "stsd", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1757 | CHECK(avifParseSampleDescriptionBox(track->sampleTable, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1758 | } |
| 1759 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1760 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1761 | } |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1762 | return AVIF_TRUE; |
| 1763 | } |
| 1764 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1765 | static avifBool avifParseMediaInformationBox(avifTrack * track, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1766 | { |
| 1767 | BEGIN_STREAM(s, raw, rawLen); |
| 1768 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1769 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1770 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1771 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1772 | |
| 1773 | if (!memcmp(header.type, "stbl", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1774 | CHECK(avifParseSampleTableBox(track, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1775 | } |
| 1776 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1777 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1778 | } |
| 1779 | return AVIF_TRUE; |
| 1780 | } |
| 1781 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1782 | static avifBool avifParseMediaBox(avifTrack * track, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1783 | { |
| 1784 | BEGIN_STREAM(s, raw, rawLen); |
| 1785 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1786 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1787 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1788 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1789 | |
| 1790 | if (!memcmp(header.type, "mdhd", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1791 | CHECK(avifParseMediaHeaderBox(track, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1792 | } else if (!memcmp(header.type, "minf", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1793 | CHECK(avifParseMediaInformationBox(track, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1794 | } |
| 1795 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1796 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1797 | } |
| 1798 | return AVIF_TRUE; |
| 1799 | } |
| 1800 | |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1801 | static avifBool avifTrackReferenceBox(avifTrack * track, const uint8_t * raw, size_t rawLen) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1802 | { |
| 1803 | BEGIN_STREAM(s, raw, rawLen); |
| 1804 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1805 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1806 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1807 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1808 | |
| 1809 | if (!memcmp(header.type, "auxl", 4)) { |
| 1810 | uint32_t toID; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1811 | CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) track_IDs[] |
| 1812 | 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] | 1813 | track->auxForID = toID; |
| 1814 | } else { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1815 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1816 | } |
| 1817 | } |
| 1818 | return AVIF_TRUE; |
| 1819 | } |
| 1820 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 1821 | static avifBool avifParseTrackBox(avifDecoderData * data, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1822 | { |
| 1823 | BEGIN_STREAM(s, raw, rawLen); |
| 1824 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1825 | avifTrack * track = avifDecoderDataCreateTrack(data); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1826 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1827 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1828 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1829 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1830 | |
| 1831 | if (!memcmp(header.type, "tkhd", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1832 | CHECK(avifParseTrackHeaderBox(track, avifROStreamCurrent(&s), header.size)); |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 1833 | } else if (!memcmp(header.type, "meta", 4)) { |
| 1834 | CHECK(avifParseMetaBox(track->meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1835 | } else if (!memcmp(header.type, "mdia", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1836 | CHECK(avifParseMediaBox(track, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1837 | } else if (!memcmp(header.type, "tref", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1838 | CHECK(avifTrackReferenceBox(track, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1841 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1842 | } |
| 1843 | return AVIF_TRUE; |
| 1844 | } |
| 1845 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 1846 | static avifBool avifParseMoovBox(avifDecoderData * data, const uint8_t * raw, size_t rawLen) |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1847 | { |
| 1848 | BEGIN_STREAM(s, raw, rawLen); |
| 1849 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1850 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1851 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1852 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1853 | |
| 1854 | if (!memcmp(header.type, "trak", 4)) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1855 | CHECK(avifParseTrackBox(data, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1858 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1859 | } |
| 1860 | return AVIF_TRUE; |
| 1861 | } |
| 1862 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1863 | static avifBool avifParseFileTypeBox(avifFileType * ftyp, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1864 | { |
| 1865 | BEGIN_STREAM(s, raw, rawLen); |
| 1866 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1867 | CHECK(avifROStreamRead(&s, ftyp->majorBrand, 4)); |
| 1868 | CHECK(avifROStreamReadU32(&s, &ftyp->minorVersion)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1869 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1870 | size_t compatibleBrandsBytes = avifROStreamRemainingBytes(&s); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1871 | if ((compatibleBrandsBytes % 4) != 0) { |
| 1872 | return AVIF_FALSE; |
| 1873 | } |
Wan-Teh Chang | 6da0a88 | 2020-07-01 12:19:31 -0700 | [diff] [blame] | 1874 | ftyp->compatibleBrands = avifROStreamCurrent(&s); |
| 1875 | CHECK(avifROStreamSkip(&s, compatibleBrandsBytes)); |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 1876 | ftyp->compatibleBrandsCount = (int)compatibleBrandsBytes / 4; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1877 | |
| 1878 | return AVIF_TRUE; |
| 1879 | } |
| 1880 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 1881 | static avifBool avifParse(avifDecoderData * data, const uint8_t * raw, size_t rawLen) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1882 | { |
| 1883 | BEGIN_STREAM(s, raw, rawLen); |
| 1884 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1885 | while (avifROStreamHasBytesLeft(&s, 1)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1886 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1887 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1888 | |
| 1889 | if (!memcmp(header.type, "ftyp", 4)) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1890 | CHECK(avifParseFileTypeBox(&data->ftyp, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1891 | } else if (!memcmp(header.type, "meta", 4)) { |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1892 | CHECK(avifParseMetaBox(data->meta, avifROStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame] | 1893 | } else if (!memcmp(header.type, "moov", 4)) { |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1894 | CHECK(avifParseMoovBox(data, avifROStreamCurrent(&s), header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1895 | } |
| 1896 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1897 | CHECK(avifROStreamSkip(&s, header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1898 | } |
| 1899 | return AVIF_TRUE; |
| 1900 | } |
| 1901 | |
| 1902 | // --------------------------------------------------------------------------- |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1903 | |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 1904 | static avifBool avifFileTypeIsCompatible(avifFileType * ftyp) |
| 1905 | { |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame^] | 1906 | avifBool avifCompatible = (!memcmp(ftyp->majorBrand, "avif", 4) || !memcmp(ftyp->majorBrand, "avis", 4)); |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 1907 | if (!avifCompatible) { |
| 1908 | for (int compatibleBrandIndex = 0; compatibleBrandIndex < ftyp->compatibleBrandsCount; ++compatibleBrandIndex) { |
Wan-Teh Chang | 6da0a88 | 2020-07-01 12:19:31 -0700 | [diff] [blame] | 1909 | const uint8_t * compatibleBrand = &ftyp->compatibleBrands[4 * compatibleBrandIndex]; |
Wan-Teh Chang | bb8e2a8 | 2020-06-29 18:13:28 -0700 | [diff] [blame] | 1910 | if (!memcmp(compatibleBrand, "avif", 4) || !memcmp(compatibleBrand, "avis", 4)) { |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 1911 | avifCompatible = AVIF_TRUE; |
| 1912 | break; |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | return avifCompatible; |
| 1917 | } |
| 1918 | |
Wan-Teh Chang | e184dc1 | 2020-05-11 12:47:21 -0700 | [diff] [blame] | 1919 | avifBool avifPeekCompatibleFileType(const avifROData * input) |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 1920 | { |
| 1921 | BEGIN_STREAM(s, input->data, input->size); |
| 1922 | |
| 1923 | avifBoxHeader header; |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1924 | CHECK(avifROStreamReadBoxHeader(&s, &header)); |
wantehchang | bc35a5f | 2020-08-12 15:27:39 -0700 | [diff] [blame^] | 1925 | if (memcmp(header.type, "ftyp", 4)) { |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 1926 | return AVIF_FALSE; |
| 1927 | } |
| 1928 | |
| 1929 | avifFileType ftyp; |
| 1930 | memset(&ftyp, 0, sizeof(avifFileType)); |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1931 | avifBool parsed = avifParseFileTypeBox(&ftyp, avifROStreamCurrent(&s), header.size); |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 1932 | if (!parsed) { |
| 1933 | return AVIF_FALSE; |
| 1934 | } |
| 1935 | return avifFileTypeIsCompatible(&ftyp); |
| 1936 | } |
| 1937 | |
| 1938 | // --------------------------------------------------------------------------- |
| 1939 | |
Joe Drago | 0b05eee | 2019-06-12 13:24:39 -0700 | [diff] [blame] | 1940 | avifDecoder * avifDecoderCreate(void) |
| 1941 | { |
| 1942 | avifDecoder * decoder = (avifDecoder *)avifAlloc(sizeof(avifDecoder)); |
| 1943 | memset(decoder, 0, sizeof(avifDecoder)); |
| 1944 | return decoder; |
| 1945 | } |
| 1946 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1947 | static void avifDecoderCleanup(avifDecoder * decoder) |
| 1948 | { |
| 1949 | if (decoder->data) { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 1950 | avifDecoderDataDestroy(decoder->data); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1951 | decoder->data = NULL; |
| 1952 | } |
| 1953 | |
| 1954 | if (decoder->image) { |
| 1955 | avifImageDestroy(decoder->image); |
| 1956 | decoder->image = NULL; |
| 1957 | } |
| 1958 | } |
| 1959 | |
Joe Drago | 0b05eee | 2019-06-12 13:24:39 -0700 | [diff] [blame] | 1960 | void avifDecoderDestroy(avifDecoder * decoder) |
| 1961 | { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1962 | avifDecoderCleanup(decoder); |
Joe Drago | 0b05eee | 2019-06-12 13:24:39 -0700 | [diff] [blame] | 1963 | avifFree(decoder); |
| 1964 | } |
| 1965 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1966 | avifResult avifDecoderSetSource(avifDecoder * decoder, avifDecoderSource source) |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1967 | { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1968 | decoder->requestedSource = source; |
| 1969 | return avifDecoderReset(decoder); |
| 1970 | } |
Joe Drago | 33f1d36 | 2019-02-13 16:46:22 -0800 | [diff] [blame] | 1971 | |
Wan-Teh Chang | e184dc1 | 2020-05-11 12:47:21 -0700 | [diff] [blame] | 1972 | avifResult avifDecoderParse(avifDecoder * decoder, const avifROData * rawInput) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1973 | { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1974 | // Cleanup anything lingering in the decoder |
| 1975 | avifDecoderCleanup(decoder); |
| 1976 | |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1977 | // ----------------------------------------------------------------------- |
| 1978 | // Parse BMFF boxes |
| 1979 | |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 1980 | decoder->data = avifDecoderDataCreate(); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1981 | |
| 1982 | // Shallow copy, on purpose |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 1983 | memcpy(&decoder->data->rawInput, rawInput, sizeof(avifROData)); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1984 | |
| 1985 | if (!avifParse(decoder->data, decoder->data->rawInput.data, decoder->data->rawInput.size)) { |
| 1986 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1987 | } |
| 1988 | |
Joe Drago | 7e37b97 | 2019-07-24 12:44:47 -0700 | [diff] [blame] | 1989 | avifBool avifCompatible = avifFileTypeIsCompatible(&decoder->data->ftyp); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1990 | if (!avifCompatible) { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 1991 | return AVIF_RESULT_INVALID_FTYP; |
| 1992 | } |
| 1993 | |
| 1994 | // Sanity check items |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 1995 | for (uint32_t itemIndex = 0; itemIndex < decoder->data->meta->items.count; ++itemIndex) { |
| 1996 | avifDecoderItem * item = &decoder->data->meta->items.item[itemIndex]; |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 1997 | if (item->hasUnsupportedEssentialProperty) { |
| 1998 | // An essential property isn't supported by libavif; ignore the item. |
| 1999 | continue; |
| 2000 | } |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2001 | const uint8_t * p = avifDecoderDataCalcItemPtr(decoder->data, item); |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2002 | if (p == NULL) { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2003 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | // Sanity check tracks |
| 2008 | for (uint32_t trackIndex = 0; trackIndex < decoder->data->tracks.count; ++trackIndex) { |
| 2009 | avifTrack * track = &decoder->data->tracks.track[trackIndex]; |
| 2010 | if (!track->sampleTable) { |
| 2011 | continue; |
| 2012 | } |
| 2013 | |
| 2014 | for (uint32_t chunkIndex = 0; chunkIndex < track->sampleTable->chunks.count; ++chunkIndex) { |
| 2015 | avifSampleTableChunk * chunk = &track->sampleTable->chunks.chunk[chunkIndex]; |
| 2016 | if (chunk->offset > decoder->data->rawInput.size) { |
| 2017 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2018 | } |
| 2019 | } |
| 2020 | } |
| 2021 | return avifDecoderReset(decoder); |
| 2022 | } |
| 2023 | |
Joe Drago | 5335535 | 2019-10-28 19:04:51 -0700 | [diff] [blame] | 2024 | static avifCodec * avifCodecCreateInternal(avifCodecChoice choice, avifCodecDecodeInput * decodeInput) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2025 | { |
Joe Drago | 5335535 | 2019-10-28 19:04:51 -0700 | [diff] [blame] | 2026 | avifCodec * codec = avifCodecCreate(choice, AVIF_CODEC_FLAG_CAN_DECODE); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2027 | if (codec) { |
| 2028 | codec->decodeInput = decodeInput; |
| 2029 | } |
| 2030 | return codec; |
| 2031 | } |
| 2032 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2033 | static avifResult avifDecoderFlush(avifDecoder * decoder) |
| 2034 | { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2035 | avifDecoderDataResetCodec(decoder->data); |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2036 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2037 | for (unsigned int i = 0; i < decoder->data->tiles.count; ++i) { |
| 2038 | avifTile * tile = &decoder->data->tiles.tile[i]; |
| 2039 | tile->codec = avifCodecCreateInternal(decoder->codecChoice, tile->input); |
| 2040 | if (!tile->codec) { |
Joe Drago | 5335535 | 2019-10-28 19:04:51 -0700 | [diff] [blame] | 2041 | return AVIF_RESULT_NO_CODEC_AVAILABLE; |
| 2042 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2043 | if (!tile->codec->open(tile->codec, decoder->imageIndex + 1)) { |
| 2044 | return AVIF_RESULT_DECODE_COLOR_FAILED; |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2045 | } |
| 2046 | } |
| 2047 | return AVIF_RESULT_OK; |
| 2048 | } |
| 2049 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2050 | avifResult avifDecoderReset(avifDecoder * decoder) |
| 2051 | { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2052 | avifDecoderData * data = decoder->data; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2053 | if (!data) { |
| 2054 | // Nothing to reset. |
| 2055 | return AVIF_RESULT_OK; |
| 2056 | } |
| 2057 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2058 | memset(&data->colorGrid, 0, sizeof(data->colorGrid)); |
| 2059 | memset(&data->alphaGrid, 0, sizeof(data->alphaGrid)); |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2060 | avifDecoderDataClearTiles(data); |
Joe Drago | 89f0cc8 | 2020-03-09 16:13:27 -0700 | [diff] [blame] | 2061 | |
| 2062 | // Prepare / cleanup decoded image state |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 2063 | if (decoder->image) { |
| 2064 | avifImageDestroy(decoder->image); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2065 | } |
Joe Drago | a0da4a4 | 2020-05-08 14:27:40 -0700 | [diff] [blame] | 2066 | decoder->image = avifImageCreateEmpty(); |
| 2067 | data->cicpSet = AVIF_FALSE; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2068 | |
Joe Drago | 70cbf60 | 2019-07-24 15:30:55 -0700 | [diff] [blame] | 2069 | memset(&decoder->ioStats, 0, sizeof(decoder->ioStats)); |
| 2070 | |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2071 | // ----------------------------------------------------------------------- |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2072 | // Build decode input |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2073 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2074 | data->sourceSampleTable = NULL; // Reset |
| 2075 | if (decoder->requestedSource == AVIF_DECODER_SOURCE_AUTO) { |
| 2076 | if (data->tracks.count > 0) { |
| 2077 | data->source = AVIF_DECODER_SOURCE_TRACKS; |
| 2078 | } else { |
| 2079 | data->source = AVIF_DECODER_SOURCE_PRIMARY_ITEM; |
Joe Drago | 7637023 | 2019-07-16 11:00:52 -0700 | [diff] [blame] | 2080 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2081 | } else { |
| 2082 | data->source = decoder->requestedSource; |
Joe Drago | 7637023 | 2019-07-16 11:00:52 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2085 | const avifPropertyArray * colorProperties = NULL; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2086 | if (data->source == AVIF_DECODER_SOURCE_TRACKS) { |
| 2087 | avifTrack * colorTrack = NULL; |
| 2088 | avifTrack * alphaTrack = NULL; |
| 2089 | |
| 2090 | // Find primary track - this probably needs some better detection |
| 2091 | uint32_t colorTrackIndex = 0; |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2092 | for (; colorTrackIndex < data->tracks.count; ++colorTrackIndex) { |
| 2093 | avifTrack * track = &data->tracks.track[colorTrackIndex]; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2094 | if (!track->sampleTable) { |
| 2095 | continue; |
| 2096 | } |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 2097 | 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] | 2098 | continue; |
| 2099 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2100 | if (!track->sampleTable->chunks.count) { |
| 2101 | continue; |
| 2102 | } |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 2103 | if (!avifSampleTableHasFormat(track->sampleTable, "av01")) { |
| 2104 | continue; |
| 2105 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2106 | if (track->auxForID != 0) { |
| 2107 | continue; |
| 2108 | } |
| 2109 | |
| 2110 | // Found one! |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2111 | break; |
| 2112 | } |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2113 | if (colorTrackIndex == data->tracks.count) { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2114 | return AVIF_RESULT_NO_CONTENT; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2115 | } |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2116 | colorTrack = &data->tracks.track[colorTrackIndex]; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2117 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2118 | colorProperties = avifSampleTableGetProperties(colorTrack->sampleTable); |
| 2119 | if (!colorProperties) { |
| 2120 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2121 | } |
| 2122 | |
| 2123 | // Find Exif and/or XMP metadata, if any |
| 2124 | if (colorTrack->meta) { |
Joe Drago | 39267fd | 2020-06-19 15:21:14 -0700 | [diff] [blame] | 2125 | // See the comment above avifDecoderDataFindMetadata() for the explanation of using 0 here |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2126 | if (!avifDecoderDataFindMetadata(data, colorTrack->meta, decoder->image, 0)) { |
| 2127 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2128 | } |
| 2129 | } |
| 2130 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2131 | uint32_t alphaTrackIndex = 0; |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2132 | for (; alphaTrackIndex < data->tracks.count; ++alphaTrackIndex) { |
| 2133 | avifTrack * track = &data->tracks.track[alphaTrackIndex]; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2134 | if (!track->sampleTable) { |
| 2135 | continue; |
| 2136 | } |
Joe Drago | 4a25c19 | 2020-06-03 16:29:58 -0700 | [diff] [blame] | 2137 | if (!track->id) { |
| 2138 | continue; |
| 2139 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2140 | if (!track->sampleTable->chunks.count) { |
| 2141 | continue; |
| 2142 | } |
Joe Drago | 2c0924c | 2019-09-26 17:41:01 -0700 | [diff] [blame] | 2143 | if (!avifSampleTableHasFormat(track->sampleTable, "av01")) { |
| 2144 | continue; |
| 2145 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2146 | if (track->auxForID == colorTrack->id) { |
| 2147 | // Found it! |
| 2148 | break; |
| 2149 | } |
| 2150 | } |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2151 | if (alphaTrackIndex != data->tracks.count) { |
| 2152 | alphaTrack = &data->tracks.track[alphaTrackIndex]; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2153 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2154 | |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2155 | avifTile * colorTile = avifDecoderDataCreateTile(data); |
| 2156 | if (!avifCodecDecodeInputGetSamples(colorTile->input, colorTrack->sampleTable, &data->rawInput)) { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2157 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2158 | } |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2159 | data->colorTileCount = 1; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2160 | |
| 2161 | if (alphaTrack) { |
wantehchang | 76e16bf | 2020-08-12 13:01:31 -0700 | [diff] [blame] | 2162 | avifTile * alphaTile = avifDecoderDataCreateTile(data); |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2163 | if (!avifCodecDecodeInputGetSamples(alphaTile->input, alphaTrack->sampleTable, &data->rawInput)) { |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2164 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2165 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2166 | alphaTile->input->alpha = AVIF_TRUE; |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2167 | data->alphaTileCount = 1; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | // Stash off sample table for future timing information |
| 2171 | data->sourceSampleTable = colorTrack->sampleTable; |
| 2172 | |
| 2173 | // Image sequence timing |
| 2174 | decoder->imageIndex = -1; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2175 | decoder->imageCount = colorTile->input->samples.count; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2176 | decoder->timescale = colorTrack->mediaTimescale; |
| 2177 | decoder->durationInTimescales = colorTrack->mediaDuration; |
| 2178 | if (colorTrack->mediaTimescale) { |
| 2179 | decoder->duration = (double)decoder->durationInTimescales / (double)colorTrack->mediaTimescale; |
| 2180 | } else { |
| 2181 | decoder->duration = 0; |
| 2182 | } |
| 2183 | memset(&decoder->imageTiming, 0, sizeof(decoder->imageTiming)); // to be set in avifDecoderNextImage() |
Joe Drago | 4170085 | 2019-09-26 17:01:43 -0700 | [diff] [blame] | 2184 | |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2185 | decoder->image->width = colorTrack->width; |
| 2186 | decoder->image->height = colorTrack->height; |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2187 | decoder->alphaPresent = (alphaTrack != NULL); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2188 | } else { |
| 2189 | // Create from items |
| 2190 | |
Joe Drago | 345aaa1 | 2019-09-25 13:42:12 -0700 | [diff] [blame] | 2191 | avifROData colorOBU = AVIF_DATA_EMPTY; |
| 2192 | avifROData alphaOBU = AVIF_DATA_EMPTY; |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2193 | avifDecoderItem * colorOBUItem = NULL; |
| 2194 | avifDecoderItem * alphaOBUItem = NULL; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2195 | |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2196 | // Find the colorOBU (primary) item |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2197 | for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) { |
| 2198 | avifDecoderItem * item = &data->meta->items.item[itemIndex]; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 2199 | if (!item->size) { |
| 2200 | continue; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2201 | } |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 2202 | if (item->hasUnsupportedEssentialProperty) { |
| 2203 | // An essential property isn't supported by libavif; ignore the item. |
| 2204 | continue; |
| 2205 | } |
Joe Drago | 951a002 | 2020-03-09 16:19:44 -0700 | [diff] [blame] | 2206 | avifBool isGrid = (memcmp(item->type, "grid", 4) == 0); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2207 | if (memcmp(item->type, "av01", 4) && !isGrid) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2208 | // probably exif or some other data |
| 2209 | continue; |
| 2210 | } |
| 2211 | if (item->thumbnailForID != 0) { |
| 2212 | // It's a thumbnail, skip it |
| 2213 | continue; |
| 2214 | } |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2215 | if ((data->meta->primaryItemID > 0) && (item->id != data->meta->primaryItemID)) { |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2216 | // a primary item ID was specified, require it |
| 2217 | continue; |
| 2218 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2219 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2220 | if (isGrid) { |
Wan-Teh Chang | cb16494 | 2020-08-10 15:47:29 -0700 | [diff] [blame] | 2221 | if (decoder->disableGridImages) { |
| 2222 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2223 | } |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2224 | const uint8_t * itemPtr = avifDecoderDataCalcItemPtr(data, item); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2225 | if (itemPtr == NULL) { |
| 2226 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2227 | } |
| 2228 | if (!avifParseImageGridBox(&data->colorGrid, itemPtr, item->size)) { |
| 2229 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 2230 | } |
| 2231 | } else { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2232 | colorOBU.data = avifDecoderDataCalcItemPtr(data, item); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2233 | colorOBU.size = item->size; |
| 2234 | } |
| 2235 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2236 | colorOBUItem = item; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2237 | break; |
| 2238 | } |
| 2239 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2240 | if (!colorOBUItem) { |
| 2241 | return AVIF_RESULT_NO_AV1_ITEMS_FOUND; |
| 2242 | } |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2243 | colorProperties = &colorOBUItem->properties; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2244 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2245 | // Find the alphaOBU item, if any |
Joe Drago | 9f2b87b | 2020-06-03 19:36:38 -0700 | [diff] [blame] | 2246 | for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) { |
| 2247 | avifDecoderItem * item = &data->meta->items.item[itemIndex]; |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 2248 | if (!item->size) { |
| 2249 | continue; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2250 | } |
Joe Drago | 3320e5f | 2020-04-21 17:36:27 -0700 | [diff] [blame] | 2251 | if (item->hasUnsupportedEssentialProperty) { |
| 2252 | // An essential property isn't supported by libavif; ignore the item. |
| 2253 | continue; |
| 2254 | } |
Joe Drago | 951a002 | 2020-03-09 16:19:44 -0700 | [diff] [blame] | 2255 | avifBool isGrid = (memcmp(item->type, "grid", 4) == 0); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2256 | if (memcmp(item->type, "av01", 4) && !isGrid) { |
| 2257 | // probably exif or some other data |
| 2258 | continue; |
| 2259 | } |
| 2260 | if (item->thumbnailForID != 0) { |
| 2261 | // It's a thumbnail, skip it |
| 2262 | continue; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 2263 | } |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2264 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2265 | const avifProperty * auxCProp = avifPropertyArrayFind(&item->properties, "auxC"); |
| 2266 | if (auxCProp && isAlphaURN(auxCProp->u.auxC.auxType) && (item->auxForID == colorOBUItem->id)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2267 | if (isGrid) { |
Wan-Teh Chang | cb16494 | 2020-08-10 15:47:29 -0700 | [diff] [blame] | 2268 | if (decoder->disableGridImages) { |
| 2269 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2270 | } |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2271 | const uint8_t * itemPtr = avifDecoderDataCalcItemPtr(data, item); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2272 | if (itemPtr == NULL) { |
| 2273 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2274 | } |
| 2275 | if (!avifParseImageGridBox(&data->alphaGrid, itemPtr, item->size)) { |
| 2276 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 2277 | } |
| 2278 | } else { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2279 | alphaOBU.data = avifDecoderDataCalcItemPtr(data, item); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2280 | alphaOBU.size = item->size; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2281 | } |
| 2282 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2283 | alphaOBUItem = item; |
| 2284 | break; |
Joe Drago | f6a4227 | 2019-11-21 15:21:41 -0800 | [diff] [blame] | 2285 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2286 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2287 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2288 | // Find Exif and/or XMP metadata, if any |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2289 | if (!avifDecoderDataFindMetadata(data, data->meta, decoder->image, colorOBUItem->id)) { |
| 2290 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2291 | } |
| 2292 | |
Wan-Teh Chang | 4295bcb | 2020-04-05 15:41:05 -0700 | [diff] [blame] | 2293 | if ((data->colorGrid.rows > 0) && (data->colorGrid.columns > 0)) { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2294 | if (!avifDecoderDataGenerateImageGridTiles(data, &data->colorGrid, colorOBUItem, AVIF_FALSE)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2295 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 2296 | } |
| 2297 | data->colorTileCount = data->tiles.count; |
| 2298 | } else { |
| 2299 | if (colorOBU.size == 0) { |
| 2300 | return AVIF_RESULT_NO_AV1_ITEMS_FOUND; |
| 2301 | } |
| 2302 | |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2303 | avifTile * colorTile = avifDecoderDataCreateTile(data); |
Joe Drago | e3e3bfa | 2020-06-02 16:33:53 -0700 | [diff] [blame] | 2304 | avifDecodeSample * colorSample = (avifDecodeSample *)avifArrayPushPtr(&colorTile->input->samples); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2305 | memcpy(&colorSample->data, &colorOBU, sizeof(avifROData)); |
| 2306 | colorSample->sync = AVIF_TRUE; |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2307 | data->colorTileCount = 1; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2308 | } |
| 2309 | |
Benbuck Nason | 78e3c9d | 2020-03-27 14:35:27 -0700 | [diff] [blame] | 2310 | if ((data->alphaGrid.rows > 0) && (data->alphaGrid.columns > 0) && alphaOBUItem) { |
Wan-Teh Chang | 5b99216 | 2020-08-10 14:57:05 -0700 | [diff] [blame] | 2311 | if (!avifDecoderDataGenerateImageGridTiles(data, &data->alphaGrid, alphaOBUItem, AVIF_TRUE)) { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2312 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 2313 | } |
| 2314 | data->alphaTileCount = data->tiles.count - data->colorTileCount; |
| 2315 | } else { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2316 | if (alphaOBU.size > 0) { |
wantehchang | 76e16bf | 2020-08-12 13:01:31 -0700 | [diff] [blame] | 2317 | avifTile * alphaTile = avifDecoderDataCreateTile(data); |
Joe Drago | e3e3bfa | 2020-06-02 16:33:53 -0700 | [diff] [blame] | 2318 | avifDecodeSample * alphaSample = (avifDecodeSample *)avifArrayPushPtr(&alphaTile->input->samples); |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2319 | memcpy(&alphaSample->data, &alphaOBU, sizeof(avifROData)); |
| 2320 | alphaSample->sync = AVIF_TRUE; |
| 2321 | alphaTile->input->alpha = AVIF_TRUE; |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2322 | data->alphaTileCount = 1; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2323 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2324 | } |
Joe Drago | 33f1d36 | 2019-02-13 16:46:22 -0800 | [diff] [blame] | 2325 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2326 | // Set all counts and timing to safe-but-uninteresting values |
| 2327 | decoder->imageIndex = -1; |
| 2328 | decoder->imageCount = 1; |
| 2329 | decoder->imageTiming.timescale = 1; |
| 2330 | decoder->imageTiming.pts = 0; |
| 2331 | decoder->imageTiming.ptsInTimescales = 0; |
| 2332 | decoder->imageTiming.duration = 1; |
| 2333 | decoder->imageTiming.durationInTimescales = 1; |
| 2334 | decoder->timescale = 1; |
| 2335 | decoder->duration = 1; |
| 2336 | decoder->durationInTimescales = 1; |
Joe Drago | 70cbf60 | 2019-07-24 15:30:55 -0700 | [diff] [blame] | 2337 | |
| 2338 | decoder->ioStats.colorOBUSize = colorOBU.size; |
| 2339 | decoder->ioStats.alphaOBUSize = alphaOBU.size; |
Joe Drago | 4170085 | 2019-09-26 17:01:43 -0700 | [diff] [blame] | 2340 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2341 | const avifProperty * ispeProp = avifPropertyArrayFind(colorProperties, "ispe"); |
| 2342 | if (ispeProp) { |
| 2343 | decoder->image->width = ispeProp->u.ispe.width; |
| 2344 | decoder->image->height = ispeProp->u.ispe.height; |
Joe Drago | 4170085 | 2019-09-26 17:01:43 -0700 | [diff] [blame] | 2345 | } else { |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2346 | decoder->image->width = 0; |
| 2347 | decoder->image->height = 0; |
Joe Drago | 4170085 | 2019-09-26 17:01:43 -0700 | [diff] [blame] | 2348 | } |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2349 | decoder->alphaPresent = (alphaOBUItem != NULL); |
Joe Drago | 00bcaaf | 2020-06-05 15:29:38 -0700 | [diff] [blame] | 2350 | } |
| 2351 | |
Joe Drago | 11f2a5e | 2020-07-06 10:49:00 -0700 | [diff] [blame] | 2352 | // Sanity check tiles |
| 2353 | for (uint32_t tileIndex = 0; tileIndex < data->tiles.count; ++tileIndex) { |
| 2354 | avifTile * tile = &data->tiles.tile[tileIndex]; |
| 2355 | for (uint32_t sampleIndex = 0; sampleIndex < tile->input->samples.count; ++sampleIndex) { |
Joe Drago | 043311b | 2020-07-06 16:48:41 -0700 | [diff] [blame] | 2356 | avifDecodeSample * sample = &tile->input->samples.sample[sampleIndex]; |
Joe Drago | 11f2a5e | 2020-07-06 10:49:00 -0700 | [diff] [blame] | 2357 | if (!sample->data.data || !sample->data.size) { |
| 2358 | // Every sample must have some data |
| 2359 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
| 2360 | } |
| 2361 | } |
| 2362 | } |
| 2363 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2364 | const avifProperty * colrProp = avifPropertyArrayFind(colorProperties, "colr"); |
| 2365 | if (colrProp) { |
| 2366 | if (colrProp->u.colr.hasICC) { |
| 2367 | avifImageSetProfileICC(decoder->image, colrProp->u.colr.icc, colrProp->u.colr.iccSize); |
| 2368 | } else if (colrProp->u.colr.hasNCLX) { |
| 2369 | data->cicpSet = AVIF_TRUE; |
| 2370 | decoder->image->colorPrimaries = colrProp->u.colr.colorPrimaries; |
| 2371 | decoder->image->transferCharacteristics = colrProp->u.colr.transferCharacteristics; |
| 2372 | decoder->image->matrixCoefficients = colrProp->u.colr.matrixCoefficients; |
| 2373 | decoder->image->yuvRange = colrProp->u.colr.range; |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | // Transformations |
| 2378 | const avifProperty * paspProp = avifPropertyArrayFind(colorProperties, "pasp"); |
| 2379 | if (paspProp) { |
| 2380 | decoder->image->transformFlags |= AVIF_TRANSFORM_PASP; |
| 2381 | memcpy(&decoder->image->pasp, &paspProp->u.pasp, sizeof(avifPixelAspectRatioBox)); |
| 2382 | } |
| 2383 | const avifProperty * clapProp = avifPropertyArrayFind(colorProperties, "clap"); |
| 2384 | if (clapProp) { |
| 2385 | decoder->image->transformFlags |= AVIF_TRANSFORM_CLAP; |
| 2386 | memcpy(&decoder->image->clap, &clapProp->u.clap, sizeof(avifCleanApertureBox)); |
| 2387 | } |
| 2388 | const avifProperty * irotProp = avifPropertyArrayFind(colorProperties, "irot"); |
| 2389 | if (irotProp) { |
| 2390 | decoder->image->transformFlags |= AVIF_TRANSFORM_IROT; |
| 2391 | memcpy(&decoder->image->irot, &irotProp->u.irot, sizeof(avifImageRotation)); |
| 2392 | } |
| 2393 | const avifProperty * imirProp = avifPropertyArrayFind(colorProperties, "imir"); |
| 2394 | if (imirProp) { |
| 2395 | decoder->image->transformFlags |= AVIF_TRANSFORM_IMIR; |
| 2396 | memcpy(&decoder->image->imir, &imirProp->u.imir, sizeof(avifImageMirror)); |
| 2397 | } |
| 2398 | |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2399 | if (!data->cicpSet && (data->tiles.count > 0)) { |
Joe Drago | da92a38 | 2020-06-09 17:08:45 -0700 | [diff] [blame] | 2400 | avifTile * firstTile = &data->tiles.tile[0]; |
| 2401 | if (firstTile->input->samples.count > 0) { |
| 2402 | avifDecodeSample * sample = &firstTile->input->samples.sample[0]; |
| 2403 | avifSequenceHeader sequenceHeader; |
| 2404 | if (avifSequenceHeaderParse(&sequenceHeader, &sample->data)) { |
wantehchang | b207b4d | 2020-08-11 17:50:22 -0700 | [diff] [blame] | 2405 | data->cicpSet = AVIF_TRUE; |
Joe Drago | da92a38 | 2020-06-09 17:08:45 -0700 | [diff] [blame] | 2406 | decoder->image->colorPrimaries = sequenceHeader.colorPrimaries; |
| 2407 | decoder->image->transferCharacteristics = sequenceHeader.transferCharacteristics; |
| 2408 | decoder->image->matrixCoefficients = sequenceHeader.matrixCoefficients; |
Joe Drago | c1d1895 | 2020-06-09 18:24:36 -0700 | [diff] [blame] | 2409 | decoder->image->yuvRange = sequenceHeader.range; |
Joe Drago | da92a38 | 2020-06-09 17:08:45 -0700 | [diff] [blame] | 2410 | } |
| 2411 | } |
| 2412 | } |
| 2413 | |
Joe Drago | a72da5b | 2020-06-15 19:40:17 -0700 | [diff] [blame] | 2414 | const avifProperty * av1CProp = avifPropertyArrayFind(colorProperties, "av1C"); |
| 2415 | if (av1CProp) { |
Joe Drago | b840112 | 2020-06-19 11:45:49 -0700 | [diff] [blame] | 2416 | decoder->image->depth = avifCodecConfigurationBoxGetDepth(&av1CProp->u.av1C); |
| 2417 | if (av1CProp->u.av1C.monochrome) { |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2418 | decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV400; |
Joe Drago | 7b2cf80 | 2020-06-09 17:57:23 -0700 | [diff] [blame] | 2419 | } else { |
Joe Drago | b840112 | 2020-06-19 11:45:49 -0700 | [diff] [blame] | 2420 | if (av1CProp->u.av1C.chromaSubsamplingX && av1CProp->u.av1C.chromaSubsamplingY) { |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2421 | decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV420; |
Joe Drago | b840112 | 2020-06-19 11:45:49 -0700 | [diff] [blame] | 2422 | } else if (av1CProp->u.av1C.chromaSubsamplingX) { |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2423 | decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV422; |
Joe Drago | 7b2cf80 | 2020-06-09 17:57:23 -0700 | [diff] [blame] | 2424 | |
| 2425 | } else { |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2426 | decoder->image->yuvFormat = AVIF_PIXEL_FORMAT_YUV444; |
Joe Drago | 7b2cf80 | 2020-06-09 17:57:23 -0700 | [diff] [blame] | 2427 | } |
| 2428 | } |
Joe Drago | b840112 | 2020-06-19 11:45:49 -0700 | [diff] [blame] | 2429 | decoder->image->yuvChromaSamplePosition = (avifChromaSamplePosition)av1CProp->u.av1C.chromaSamplePosition; |
Joe Drago | 00bcaaf | 2020-06-05 15:29:38 -0700 | [diff] [blame] | 2430 | } else { |
Joe Drago | f48a338 | 2020-06-19 14:13:44 -0700 | [diff] [blame] | 2431 | // An av1C box is mandatory in all valid AVIF configurations. Bail out. |
| 2432 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2433 | } |
| 2434 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2435 | return avifDecoderFlush(decoder); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2436 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2437 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2438 | avifResult avifDecoderNextImage(avifDecoder * decoder) |
| 2439 | { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2440 | for (unsigned int tileIndex = 0; tileIndex < decoder->data->tiles.count; ++tileIndex) { |
| 2441 | avifTile * tile = &decoder->data->tiles.tile[tileIndex]; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 2442 | |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2443 | if (!tile->codec->getNextImage(tile->codec, tile->image)) { |
| 2444 | if (tile->input->alpha) { |
| 2445 | return AVIF_RESULT_DECODE_ALPHA_FAILED; |
| 2446 | } else { |
| 2447 | if (tile->image->width) { |
| 2448 | // We've sent at least one image, but we've run out now. |
| 2449 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
| 2450 | } |
| 2451 | return AVIF_RESULT_DECODE_COLOR_FAILED; |
| 2452 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2453 | } |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | if (decoder->data->tiles.count != (decoder->data->colorTileCount + decoder->data->alphaTileCount)) { |
| 2457 | // TODO: assert here? This should be impossible. |
| 2458 | return AVIF_RESULT_UNKNOWN_ERROR; |
| 2459 | } |
| 2460 | |
| 2461 | if ((decoder->data->colorGrid.rows > 0) || (decoder->data->colorGrid.columns > 0)) { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2462 | if (!avifDecoderDataFillImageGrid( |
| 2463 | 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] | 2464 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 2465 | } |
| 2466 | } else { |
| 2467 | // Normal (most common) non-grid path. Just steal the planes from the only "tile". |
| 2468 | |
| 2469 | if (decoder->data->colorTileCount != 1) { |
| 2470 | return AVIF_RESULT_DECODE_COLOR_FAILED; |
| 2471 | } |
| 2472 | |
| 2473 | avifImage * srcColor = decoder->data->tiles.tile[0].image; |
| 2474 | |
| 2475 | if ((decoder->image->width != srcColor->width) || (decoder->image->height != srcColor->height) || |
| 2476 | (decoder->image->depth != srcColor->depth)) { |
| 2477 | avifImageFreePlanes(decoder->image, AVIF_PLANES_ALL); |
| 2478 | |
| 2479 | decoder->image->width = srcColor->width; |
| 2480 | decoder->image->height = srcColor->height; |
| 2481 | decoder->image->depth = srcColor->depth; |
Joe Drago | c554f5f | 2020-06-09 18:59:51 -0700 | [diff] [blame] | 2482 | } |
psi / Ryo Hirafuji | 922d8a1 | 2020-03-10 03:24:57 +0900 | [diff] [blame] | 2483 | |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 2484 | #if 0 |
| 2485 | // 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] | 2486 | if (!decoder->data->cicpSet) { |
| 2487 | decoder->data->cicpSet = AVIF_TRUE; |
| 2488 | decoder->image->colorPrimaries = srcColor->colorPrimaries; |
| 2489 | decoder->image->transferCharacteristics = srcColor->transferCharacteristics; |
| 2490 | decoder->image->matrixCoefficients = srcColor->matrixCoefficients; |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2491 | } |
Joe Drago | ba1eb49 | 2020-06-22 17:05:04 -0700 | [diff] [blame] | 2492 | #endif |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2493 | |
| 2494 | avifImageStealPlanes(decoder->image, srcColor, AVIF_PLANES_YUV); |
| 2495 | } |
| 2496 | |
| 2497 | if ((decoder->data->alphaGrid.rows > 0) || (decoder->data->alphaGrid.columns > 0)) { |
Joe Drago | 800b47f | 2020-03-18 16:22:37 -0700 | [diff] [blame] | 2498 | if (!avifDecoderDataFillImageGrid( |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2499 | decoder->data, &decoder->data->alphaGrid, decoder->image, decoder->data->colorTileCount, decoder->data->alphaTileCount, AVIF_TRUE)) { |
| 2500 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
| 2501 | } |
| 2502 | } else { |
| 2503 | // Normal (most common) non-grid path. Just steal the planes from the only "tile". |
| 2504 | |
| 2505 | if (decoder->data->alphaTileCount == 0) { |
| 2506 | avifImageFreePlanes(decoder->image, AVIF_PLANES_A); // no alpha |
| 2507 | } else { |
| 2508 | if (decoder->data->alphaTileCount != 1) { |
| 2509 | return AVIF_RESULT_DECODE_ALPHA_FAILED; |
| 2510 | } |
| 2511 | |
| 2512 | avifImage * srcAlpha = decoder->data->tiles.tile[decoder->data->colorTileCount].image; |
| 2513 | if ((decoder->image->width != srcAlpha->width) || (decoder->image->height != srcAlpha->height) || |
| 2514 | (decoder->image->depth != srcAlpha->depth)) { |
| 2515 | return AVIF_RESULT_DECODE_ALPHA_FAILED; |
| 2516 | } |
| 2517 | |
| 2518 | avifImageStealPlanes(decoder->image, srcAlpha, AVIF_PLANES_A); |
| 2519 | } |
| 2520 | } |
Joe Drago | 7ad3ad6 | 2019-02-07 11:17:34 -0800 | [diff] [blame] | 2521 | |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2522 | ++decoder->imageIndex; |
| 2523 | if (decoder->data->sourceSampleTable) { |
| 2524 | // Decoding from a track! Provide timing information. |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 2525 | |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 2526 | avifResult timingResult = avifDecoderNthImageTiming(decoder, decoder->imageIndex, &decoder->imageTiming); |
| 2527 | if (timingResult != AVIF_RESULT_OK) { |
| 2528 | return timingResult; |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2529 | } |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 2530 | } |
| 2531 | return AVIF_RESULT_OK; |
| 2532 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2533 | |
Wan-Teh Chang | e184dc1 | 2020-05-11 12:47:21 -0700 | [diff] [blame] | 2534 | avifResult avifDecoderNthImageTiming(const avifDecoder * decoder, uint32_t frameIndex, avifImageTiming * outTiming) |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 2535 | { |
| 2536 | if (!decoder->data) { |
| 2537 | // Nothing has been parsed yet |
| 2538 | return AVIF_RESULT_NO_CONTENT; |
| 2539 | } |
| 2540 | |
| 2541 | if ((int)frameIndex >= decoder->imageCount) { |
| 2542 | // Impossible index |
| 2543 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
| 2544 | } |
| 2545 | |
| 2546 | if (!decoder->data->sourceSampleTable) { |
| 2547 | // There isn't any real timing associated with this decode, so |
| 2548 | // just hand back the defaults chosen in avifDecoderReset(). |
| 2549 | memcpy(outTiming, &decoder->imageTiming, sizeof(avifImageTiming)); |
| 2550 | return AVIF_RESULT_OK; |
| 2551 | } |
| 2552 | |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 2553 | outTiming->timescale = decoder->timescale; |
| 2554 | outTiming->ptsInTimescales = 0; |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 2555 | for (int imageIndex = 0; imageIndex < (int)frameIndex; ++imageIndex) { |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 2556 | outTiming->ptsInTimescales += avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, imageIndex); |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 2557 | } |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 2558 | outTiming->durationInTimescales = avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, frameIndex); |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 2559 | |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 2560 | if (outTiming->timescale > 0) { |
| 2561 | outTiming->pts = (double)outTiming->ptsInTimescales / (double)outTiming->timescale; |
| 2562 | outTiming->duration = (double)outTiming->durationInTimescales / (double)outTiming->timescale; |
Joe Drago | e9c5860 | 2020-04-13 17:23:13 -0700 | [diff] [blame] | 2563 | } else { |
Wan-Teh Chang | 167e406 | 2020-04-14 16:06:12 -0700 | [diff] [blame] | 2564 | outTiming->pts = 0.0; |
| 2565 | outTiming->duration = 0.0; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2566 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2567 | return AVIF_RESULT_OK; |
| 2568 | } |
| 2569 | |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2570 | avifResult avifDecoderNthImage(avifDecoder * decoder, uint32_t frameIndex) |
| 2571 | { |
| 2572 | int requestedIndex = (int)frameIndex; |
| 2573 | if (requestedIndex == decoder->imageIndex) { |
| 2574 | // We're here already, nothing to do |
| 2575 | return AVIF_RESULT_OK; |
| 2576 | } |
| 2577 | |
| 2578 | if (requestedIndex == (decoder->imageIndex + 1)) { |
| 2579 | // it's just the next image, nothing special here |
| 2580 | return avifDecoderNextImage(decoder); |
| 2581 | } |
| 2582 | |
| 2583 | if (requestedIndex >= decoder->imageCount) { |
| 2584 | // Impossible index |
| 2585 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
| 2586 | } |
| 2587 | |
| 2588 | // If we get here, a decoder flush is necessary |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2589 | decoder->imageIndex = ((int)avifDecoderNearestKeyframe(decoder, frameIndex)) - 1; // prepare to read nearest keyframe |
Joe Drago | 8197802 | 2019-09-26 18:23:57 -0700 | [diff] [blame] | 2590 | avifDecoderFlush(decoder); |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2591 | for (;;) { |
| 2592 | avifResult result = avifDecoderNextImage(decoder); |
| 2593 | if (result != AVIF_RESULT_OK) { |
| 2594 | return result; |
| 2595 | } |
| 2596 | |
| 2597 | if (requestedIndex == decoder->imageIndex) { |
| 2598 | break; |
| 2599 | } |
Joe Drago | fc4144e | 2019-09-27 20:35:06 -0700 | [diff] [blame] | 2600 | } |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2601 | return AVIF_RESULT_OK; |
| 2602 | } |
| 2603 | |
Wan-Teh Chang | e184dc1 | 2020-05-11 12:47:21 -0700 | [diff] [blame] | 2604 | avifBool avifDecoderIsKeyframe(const avifDecoder * decoder, uint32_t frameIndex) |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2605 | { |
Joe Drago | 060d534 | 2020-03-03 10:53:49 -0800 | [diff] [blame] | 2606 | if ((decoder->data->tiles.count > 0) && decoder->data->tiles.tile[0].input) { |
| 2607 | if (frameIndex < decoder->data->tiles.tile[0].input->samples.count) { |
| 2608 | return decoder->data->tiles.tile[0].input->samples.sample[frameIndex].sync; |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2609 | } |
| 2610 | } |
| 2611 | return AVIF_FALSE; |
| 2612 | } |
| 2613 | |
Wan-Teh Chang | e184dc1 | 2020-05-11 12:47:21 -0700 | [diff] [blame] | 2614 | uint32_t avifDecoderNearestKeyframe(const avifDecoder * decoder, uint32_t frameIndex) |
Joe Drago | 22c1ad9 | 2019-09-26 12:46:50 -0700 | [diff] [blame] | 2615 | { |
| 2616 | for (; frameIndex != 0; --frameIndex) { |
| 2617 | if (avifDecoderIsKeyframe(decoder, frameIndex)) { |
| 2618 | break; |
| 2619 | } |
| 2620 | } |
| 2621 | return frameIndex; |
| 2622 | } |
| 2623 | |
Wan-Teh Chang | e184dc1 | 2020-05-11 12:47:21 -0700 | [diff] [blame] | 2624 | avifResult avifDecoderRead(avifDecoder * decoder, avifImage * image, const avifROData * input) |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2625 | { |
| 2626 | avifResult result = avifDecoderParse(decoder, input); |
| 2627 | if (result != AVIF_RESULT_OK) { |
| 2628 | return result; |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 2629 | } |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2630 | result = avifDecoderNextImage(decoder); |
| 2631 | if (result != AVIF_RESULT_OK) { |
| 2632 | return result; |
| 2633 | } |
Joe Drago | 250221a | 2020-06-01 11:11:06 -0700 | [diff] [blame] | 2634 | avifImageCopy(image, decoder->image, AVIF_PLANES_ALL); |
Joe Drago | 46ea058 | 2019-07-22 15:55:47 -0700 | [diff] [blame] | 2635 | return AVIF_RESULT_OK; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 2636 | } |