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