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 | 7efd803 | 2019-02-08 14:49:15 -0800 | [diff] [blame] | 8 | // from the MIAF spec: |
| 9 | // --- |
| 10 | // Section 6.7 |
| 11 | // "α is an alpha plane value, scaled into the range of 0 (fully transparent) to 1 (fully opaque), inclusive" |
| 12 | // --- |
| 13 | // Section 7.3.5.2 |
| 14 | // "the sample values of the alpha plane divided by the maximum value (e.g. by 255 for 8-bit sample |
| 15 | // values) provides the multiplier to be used to obtain the intensity for the associated master image" |
| 16 | // --- |
| 17 | // The define AVIF_FIX_STUDIO_ALPHA detects when the alpha OBU is incorrectly using studio range |
| 18 | // and corrects it before returning the alpha pixels to the caller. |
| 19 | #define AVIF_FIX_STUDIO_ALPHA |
| 20 | |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 21 | #define AUXTYPE_SIZE 64 |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 22 | #define MAX_COMPATIBLE_BRANDS 32 |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 23 | |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 24 | // --------------------------------------------------------------------------- |
| 25 | // Box data structures |
| 26 | |
| 27 | // ftyp |
| 28 | typedef struct avifFileType |
| 29 | { |
| 30 | uint8_t majorBrand[4]; |
| 31 | uint32_t minorVersion; |
| 32 | uint8_t compatibleBrands[4 * MAX_COMPATIBLE_BRANDS]; |
| 33 | int compatibleBrandsCount; |
| 34 | } avifFileType; |
| 35 | |
| 36 | // ispe |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 37 | typedef struct avifImageSpatialExtents |
| 38 | { |
| 39 | uint32_t width; |
| 40 | uint32_t height; |
| 41 | } avifImageSpatialExtents; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 42 | |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 43 | // auxC |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 44 | typedef struct avifAuxiliaryType |
| 45 | { |
| 46 | char auxType[AUXTYPE_SIZE]; |
| 47 | } avifAuxiliaryType; |
| 48 | |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 49 | // colr |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 50 | typedef struct avifColourInformationBox |
| 51 | { |
| 52 | avifProfileFormat format; |
| 53 | uint8_t * icc; |
| 54 | size_t iccSize; |
Joe Drago | e7ce20d | 2019-02-11 16:37:38 -0800 | [diff] [blame] | 55 | avifNclxColorProfile nclx; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 56 | } avifColourInformationBox; |
| 57 | |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 58 | // --------------------------------------------------------------------------- |
| 59 | // Top-level structures |
| 60 | |
| 61 | // one "item" worth (all iref, iloc, iprp, etc refer to one of these) |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 62 | typedef struct avifItem |
| 63 | { |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame^] | 64 | uint32_t id; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 65 | uint8_t type[4]; |
| 66 | uint32_t offset; |
| 67 | uint32_t size; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 68 | avifBool ispePresent; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 69 | avifImageSpatialExtents ispe; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 70 | avifBool auxCPresent; |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 71 | avifAuxiliaryType auxC; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 72 | avifBool colrPresent; |
| 73 | avifColourInformationBox colr; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 74 | int thumbnailForID; // if non-zero, this item is a thumbnail for Item #{thumbnailForID} |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 75 | int auxForID; // if non-zero, this item is an auxC plane for Item #{auxForID} |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 76 | } avifItem; |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 77 | AVIF_ARRAY_DECLARE(avifItemArray, avifItem, item); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 78 | |
Joe Drago | b13e572 | 2019-02-08 19:07:25 -0800 | [diff] [blame] | 79 | // Temporary storage for ipco contents until they can be associated and memcpy'd to an avifItem |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 80 | typedef struct avifProperty |
| 81 | { |
| 82 | uint8_t type[4]; |
| 83 | avifImageSpatialExtents ispe; |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 84 | avifAuxiliaryType auxC; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 85 | avifColourInformationBox colr; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 86 | } avifProperty; |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 87 | AVIF_ARRAY_DECLARE(avifPropertyArray, avifProperty, prop); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 88 | |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame^] | 89 | // --------------------------------------------------------------------------- |
| 90 | // avifTrack |
| 91 | |
| 92 | typedef struct avifSampleTableChunk |
| 93 | { |
| 94 | uint64_t offset; |
| 95 | uint64_t size; |
| 96 | } avifSampleTableChunk; |
| 97 | AVIF_ARRAY_DECLARE(avifSampleTableChunkArray, avifSampleTableChunk, chunk); |
| 98 | |
| 99 | typedef struct avifSampleTableSampleToChunk |
| 100 | { |
| 101 | uint32_t firstChunk; |
| 102 | uint32_t samplesPerChunk; |
| 103 | uint32_t sampleDescriptionIndex; |
| 104 | } avifSampleTableSampleToChunk; |
| 105 | AVIF_ARRAY_DECLARE(avifSampleTableSampleToChunkArray, avifSampleTableSampleToChunk, sampleToChunk); |
| 106 | |
| 107 | typedef struct avifSampleTableSampleSize |
| 108 | { |
| 109 | uint32_t size; |
| 110 | } avifSampleTableSampleSize; |
| 111 | AVIF_ARRAY_DECLARE(avifSampleTableSampleSizeArray, avifSampleTableSampleSize, sampleSize); |
| 112 | |
| 113 | typedef struct avifSampleTableTimeToSample |
| 114 | { |
| 115 | uint32_t sampleCount; |
| 116 | uint32_t sampleDelta; |
| 117 | } avifSampleTableTimeToSample; |
| 118 | AVIF_ARRAY_DECLARE(avifSampleTableTimeToSampleArray, avifSampleTableTimeToSample, timeToSample); |
| 119 | |
| 120 | typedef struct avifSampleTable |
| 121 | { |
| 122 | avifSampleTableChunkArray chunks; |
| 123 | avifSampleTableSampleToChunkArray sampleToChunks; |
| 124 | avifSampleTableSampleSizeArray sampleSizes; |
| 125 | avifSampleTableTimeToSampleArray timeToSamples; |
| 126 | } avifSampleTable; |
| 127 | |
| 128 | avifSampleTable * avifSampleTableCreate() |
| 129 | { |
| 130 | avifSampleTable * sampleTable = (avifSampleTable *)avifAlloc(sizeof(avifSampleTable)); |
| 131 | memset(sampleTable, 0, sizeof(avifSampleTable)); |
| 132 | avifArrayCreate(&sampleTable->chunks, sizeof(avifSampleTableChunk), 16); |
| 133 | avifArrayCreate(&sampleTable->sampleToChunks, sizeof(avifSampleTableSampleToChunk), 16); |
| 134 | avifArrayCreate(&sampleTable->sampleSizes, sizeof(avifSampleTableSampleSize), 16); |
| 135 | avifArrayCreate(&sampleTable->timeToSamples, sizeof(avifSampleTableTimeToSample), 16); |
| 136 | return sampleTable; |
| 137 | } |
| 138 | |
| 139 | void avifSampleTableDestroy(avifSampleTable * sampleTable) |
| 140 | { |
| 141 | avifArrayDestroy(&sampleTable->chunks); |
| 142 | avifArrayDestroy(&sampleTable->sampleToChunks); |
| 143 | avifArrayDestroy(&sampleTable->sampleSizes); |
| 144 | avifArrayDestroy(&sampleTable->timeToSamples); |
| 145 | avifFree(sampleTable); |
| 146 | } |
| 147 | |
| 148 | // one video track ("trak" contents) |
| 149 | typedef struct avifTrack |
| 150 | { |
| 151 | uint32_t id; |
| 152 | uint32_t auxForID; // if non-zero, this item is an auxC plane for Track #{auxForID} |
| 153 | uint32_t mediaTimescale; |
| 154 | uint64_t mediaDuration; |
| 155 | avifSampleTable * sampleTable; |
| 156 | } avifTrack; |
| 157 | AVIF_ARRAY_DECLARE(avifTrackArray, avifTrack, track); |
| 158 | |
| 159 | // --------------------------------------------------------------------------- |
| 160 | // avifData |
| 161 | |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 162 | typedef struct avifData |
| 163 | { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 164 | avifFileType ftyp; |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 165 | avifItemArray items; |
| 166 | avifPropertyArray properties; |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame^] | 167 | avifTrackArray tracks; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 168 | int propertyCount; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 169 | } avifData; |
| 170 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 171 | avifData * avifDataCreate() |
| 172 | { |
| 173 | avifData * data = (avifData *)avifAlloc(sizeof(avifData)); |
| 174 | memset(data, 0, sizeof(avifData)); |
| 175 | avifArrayCreate(&data->items, sizeof(avifItem), 8); |
| 176 | avifArrayCreate(&data->properties, sizeof(avifProperty), 16); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame^] | 177 | avifArrayCreate(&data->tracks, sizeof(avifTrack), 2); |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 178 | return data; |
| 179 | } |
| 180 | |
| 181 | void avifDataDestroy(avifData * data) |
| 182 | { |
| 183 | avifArrayDestroy(&data->items); |
| 184 | avifArrayDestroy(&data->properties); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame^] | 185 | for (uint32_t i = 0; i < data->tracks.count; ++i) { |
| 186 | if (data->tracks.track[i].sampleTable) { |
| 187 | avifSampleTableDestroy(data->tracks.track[i].sampleTable); |
| 188 | } |
| 189 | } |
| 190 | avifArrayDestroy(&data->tracks); |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 191 | avifFree(data); |
| 192 | } |
| 193 | |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame^] | 194 | avifItem * avifDataFindItem(avifData * data, uint32_t itemID) |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 195 | { |
| 196 | if (itemID == 0) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 197 | return NULL; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 200 | for (uint32_t i = 0; i < data->items.count; ++i) { |
| 201 | if (data->items.item[i].id == itemID) { |
| 202 | return &data->items.item[i]; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 206 | avifItem * item = (avifItem *)avifArrayPushPtr(&data->items); |
| 207 | item->id = itemID; |
| 208 | return item; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 211 | // --------------------------------------------------------------------------- |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 212 | // URN |
| 213 | |
| 214 | static avifBool isAlphaURN(char * urn) |
| 215 | { |
Joe Drago | 97b071c | 2019-07-17 14:24:56 -0700 | [diff] [blame] | 216 | if (!strcmp(urn, URN_ALPHA0)) |
| 217 | return AVIF_TRUE; |
| 218 | if (!strcmp(urn, URN_ALPHA1)) |
| 219 | return AVIF_TRUE; |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 220 | return AVIF_FALSE; |
| 221 | } |
| 222 | |
| 223 | // --------------------------------------------------------------------------- |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 224 | // BMFF Parsing |
| 225 | |
Joe Drago | 7a9a661 | 2019-07-17 11:21:24 -0700 | [diff] [blame] | 226 | #define BEGIN_STREAM(VARNAME, PTR, SIZE) \ |
| 227 | avifStream VARNAME; \ |
| 228 | avifRawData VARNAME##_rawData = { PTR, SIZE }; \ |
| 229 | avifStreamStart(&VARNAME, &VARNAME##_rawData) |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 230 | |
| 231 | static avifBool avifParseItemLocationBox(avifData * data, uint8_t * raw, size_t rawLen) |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 232 | { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 233 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 234 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 235 | CHECK(avifStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 236 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 237 | uint8_t offsetSizeAndLengthSize; |
| 238 | CHECK(avifStreamRead(&s, &offsetSizeAndLengthSize, 1)); |
| 239 | uint8_t offsetSize = (offsetSizeAndLengthSize >> 4) & 0xf; // unsigned int(4) offset_size; |
| 240 | uint8_t lengthSize = (offsetSizeAndLengthSize >> 0) & 0xf; // unsigned int(4) length_size; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 241 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 242 | uint8_t baseOffsetSizeAndReserved; |
| 243 | CHECK(avifStreamRead(&s, &baseOffsetSizeAndReserved, 1)); |
| 244 | uint8_t baseOffsetSize = (baseOffsetSizeAndReserved >> 4) & 0xf; // unsigned int(4) base_offset_size; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 245 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 246 | uint16_t itemCount; |
| 247 | CHECK(avifStreamReadU16(&s, &itemCount)); // unsigned int(16) item_count; |
Joe Drago | ea252af | 2019-02-12 12:08:38 -0800 | [diff] [blame] | 248 | for (int i = 0; i < itemCount; ++i) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 249 | uint16_t itemID; // unsigned int(16) item_ID; |
| 250 | CHECK(avifStreamReadU16(&s, &itemID)); // |
| 251 | uint16_t dataReferenceIndex; // unsigned int(16) data_reference_index; |
| 252 | CHECK(avifStreamReadU16(&s, &dataReferenceIndex)); // |
| 253 | uint64_t baseOffset; // unsigned int(base_offset_size*8) base_offset; |
| 254 | CHECK(avifStreamReadUX8(&s, &baseOffset, baseOffsetSize)); // |
| 255 | uint16_t extentCount; // unsigned int(16) extent_count; |
| 256 | CHECK(avifStreamReadU16(&s, &extentCount)); // |
| 257 | if (extentCount == 1) { |
| 258 | uint64_t extentOffset; // unsigned int(offset_size*8) extent_offset; |
| 259 | CHECK(avifStreamReadUX8(&s, &extentOffset, offsetSize)); |
| 260 | uint64_t extentLength; // unsigned int(offset_size*8) extent_length; |
| 261 | CHECK(avifStreamReadUX8(&s, &extentLength, lengthSize)); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 262 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 263 | avifItem * item = avifDataFindItem(data, itemID); |
| 264 | item->id = itemID; |
| 265 | item->offset = (uint32_t)(baseOffset + extentOffset); |
| 266 | item->size = (uint32_t)extentLength; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 267 | } else { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 268 | // TODO: support more than one extent |
| 269 | return AVIF_FALSE; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | return AVIF_TRUE; |
| 273 | } |
| 274 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 275 | static avifBool avifParseImageSpatialExtentsProperty(avifData * data, uint8_t * raw, size_t rawLen, int propertyIndex) |
| 276 | { |
| 277 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 278 | CHECK(avifStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 279 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 280 | CHECK(avifStreamReadU32(&s, &data->properties.prop[propertyIndex].ispe.width)); |
| 281 | CHECK(avifStreamReadU32(&s, &data->properties.prop[propertyIndex].ispe.height)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 282 | return AVIF_TRUE; |
| 283 | } |
| 284 | |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 285 | static avifBool avifParseAuxiliaryTypeProperty(avifData * data, uint8_t * raw, size_t rawLen, int propertyIndex) |
| 286 | { |
| 287 | BEGIN_STREAM(s, raw, rawLen); |
| 288 | CHECK(avifStreamReadAndEnforceVersion(&s, 0)); |
| 289 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 290 | CHECK(avifStreamReadString(&s, data->properties.prop[propertyIndex].auxC.auxType, AUXTYPE_SIZE)); |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 291 | return AVIF_TRUE; |
| 292 | } |
| 293 | |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 294 | static avifBool avifParseColourInformationBox(avifData * data, uint8_t * raw, size_t rawLen, int propertyIndex) |
| 295 | { |
| 296 | BEGIN_STREAM(s, raw, rawLen); |
Joe Drago | e7ce20d | 2019-02-11 16:37:38 -0800 | [diff] [blame] | 297 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 298 | data->properties.prop[propertyIndex].colr.format = AVIF_PROFILE_FORMAT_NONE; |
Joe Drago | e7ce20d | 2019-02-11 16:37:38 -0800 | [diff] [blame] | 299 | |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 300 | uint8_t colourType[4]; // unsigned int(32) colour_type; |
| 301 | CHECK(avifStreamRead(&s, colourType, 4)); |
| 302 | if (!memcmp(colourType, "rICC", 4) || !memcmp(colourType, "prof", 4)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 303 | data->properties.prop[propertyIndex].colr.format = AVIF_PROFILE_FORMAT_ICC; |
| 304 | data->properties.prop[propertyIndex].colr.icc = avifStreamCurrent(&s); |
| 305 | data->properties.prop[propertyIndex].colr.iccSize = avifStreamRemainingBytes(&s); |
Joe Drago | e7ce20d | 2019-02-11 16:37:38 -0800 | [diff] [blame] | 306 | } else if (!memcmp(colourType, "nclx", 4)) { |
Joe Drago | 97b071c | 2019-07-17 14:24:56 -0700 | [diff] [blame] | 307 | // unsigned int(16) colour_primaries; |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 308 | CHECK(avifStreamReadU16(&s, &data->properties.prop[propertyIndex].colr.nclx.colourPrimaries)); |
Joe Drago | 97b071c | 2019-07-17 14:24:56 -0700 | [diff] [blame] | 309 | // unsigned int(16) transfer_characteristics; |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 310 | CHECK(avifStreamReadU16(&s, &data->properties.prop[propertyIndex].colr.nclx.transferCharacteristics)); |
Joe Drago | 97b071c | 2019-07-17 14:24:56 -0700 | [diff] [blame] | 311 | // unsigned int(16) matrix_coefficients; |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 312 | CHECK(avifStreamReadU16(&s, &data->properties.prop[propertyIndex].colr.nclx.matrixCoefficients)); |
Joe Drago | 97b071c | 2019-07-17 14:24:56 -0700 | [diff] [blame] | 313 | // unsigned int(1) full_range_flag; |
| 314 | // unsigned int(7) reserved = 0; |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 315 | CHECK(avifStreamRead(&s, &data->properties.prop[propertyIndex].colr.nclx.fullRangeFlag, 1)); |
| 316 | data->properties.prop[propertyIndex].colr.nclx.fullRangeFlag |= 0x80; |
| 317 | data->properties.prop[propertyIndex].colr.format = AVIF_PROFILE_FORMAT_NCLX; |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 318 | } |
| 319 | return AVIF_TRUE; |
| 320 | } |
| 321 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 322 | static avifBool avifParseItemPropertyContainerBox(avifData * data, uint8_t * raw, size_t rawLen) |
| 323 | { |
| 324 | BEGIN_STREAM(s, raw, rawLen); |
| 325 | |
| 326 | data->propertyCount = 0; |
| 327 | |
| 328 | while (avifStreamHasBytesLeft(&s, 1)) { |
| 329 | avifBoxHeader header; |
| 330 | CHECK(avifStreamReadBoxHeader(&s, &header)); |
| 331 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 332 | int propertyIndex = avifArrayPushIndex(&data->properties); |
| 333 | memcpy(data->properties.prop[propertyIndex].type, header.type, 4); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 334 | if (!memcmp(header.type, "ispe", 4)) { |
| 335 | CHECK(avifParseImageSpatialExtentsProperty(data, avifStreamCurrent(&s), header.size, propertyIndex)); |
| 336 | } |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 337 | if (!memcmp(header.type, "auxC", 4)) { |
| 338 | CHECK(avifParseAuxiliaryTypeProperty(data, avifStreamCurrent(&s), header.size, propertyIndex)); |
| 339 | } |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 340 | if (!memcmp(header.type, "colr", 4)) { |
| 341 | CHECK(avifParseColourInformationBox(data, avifStreamCurrent(&s), header.size, propertyIndex)); |
| 342 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 343 | |
| 344 | CHECK(avifStreamSkip(&s, header.size)); |
| 345 | } |
| 346 | return AVIF_TRUE; |
| 347 | } |
| 348 | |
| 349 | static avifBool avifParseItemPropertyAssociation(avifData * data, uint8_t * raw, size_t rawLen) |
| 350 | { |
| 351 | BEGIN_STREAM(s, raw, rawLen); |
| 352 | |
| 353 | uint8_t version; |
| 354 | uint8_t flags[3]; |
| 355 | CHECK(avifStreamReadVersionAndFlags(&s, &version, flags)); |
| 356 | avifBool propertyIndexIsU16 = (flags[2] & 0x1) ? AVIF_TRUE : AVIF_FALSE; // is flags[2] correct? |
| 357 | |
| 358 | uint32_t entryCount; |
| 359 | CHECK(avifStreamReadU32(&s, &entryCount)); |
| 360 | for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) { |
| 361 | unsigned int itemID; |
| 362 | if (version < 1) { |
| 363 | uint16_t tmp; |
| 364 | CHECK(avifStreamReadU16(&s, &tmp)); |
| 365 | itemID = tmp; |
| 366 | } else { |
| 367 | CHECK(avifStreamReadU32(&s, &itemID)); |
| 368 | } |
| 369 | uint8_t associationCount; |
| 370 | CHECK(avifStreamRead(&s, &associationCount, 1)); |
| 371 | for (uint8_t associationIndex = 0; associationIndex < associationCount; ++associationIndex) { |
Joe Drago | 64d26e8 | 2019-07-12 15:50:27 -0700 | [diff] [blame] | 372 | // avifBool essential = AVIF_FALSE; // currently unused |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 373 | uint16_t propertyIndex = 0; |
| 374 | if (propertyIndexIsU16) { |
| 375 | CHECK(avifStreamReadU16(&s, &propertyIndex)); |
Joe Drago | 64d26e8 | 2019-07-12 15:50:27 -0700 | [diff] [blame] | 376 | // essential = (propertyIndex & 0x8000) ? AVIF_TRUE : AVIF_FALSE; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 377 | propertyIndex &= 0x7fff; |
| 378 | } else { |
| 379 | uint8_t tmp; |
| 380 | CHECK(avifStreamRead(&s, &tmp, 1)); |
Joe Drago | 64d26e8 | 2019-07-12 15:50:27 -0700 | [diff] [blame] | 381 | // essential = (tmp & 0x80) ? AVIF_TRUE : AVIF_FALSE; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 382 | propertyIndex = tmp & 0x7f; |
| 383 | } |
| 384 | |
| 385 | if (propertyIndex == 0) { |
| 386 | // Not associated with any item |
| 387 | continue; |
| 388 | } |
| 389 | --propertyIndex; // 1-indexed |
| 390 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 391 | if (propertyIndex >= data->properties.count) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 392 | return AVIF_FALSE; |
| 393 | } |
| 394 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 395 | avifItem * item = avifDataFindItem(data, itemID); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 396 | |
| 397 | // Associate property with item |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 398 | avifProperty * prop = &data->properties.prop[propertyIndex]; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 399 | if (!memcmp(prop->type, "ispe", 4)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 400 | item->ispePresent = AVIF_TRUE; |
| 401 | memcpy(&item->ispe, &prop->ispe, sizeof(avifImageSpatialExtents)); |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 402 | } else if (!memcmp(prop->type, "auxC", 4)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 403 | item->auxCPresent = AVIF_TRUE; |
| 404 | memcpy(&item->auxC, &prop->auxC, sizeof(avifAuxiliaryType)); |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 405 | } else if (!memcmp(prop->type, "colr", 4)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 406 | item->colrPresent = AVIF_TRUE; |
| 407 | memcpy(&item->colr, &prop->colr, sizeof(avifColourInformationBox)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | return AVIF_TRUE; |
| 413 | } |
| 414 | |
| 415 | static avifBool avifParseItemPropertiesBox(avifData * data, uint8_t * raw, size_t rawLen) |
| 416 | { |
| 417 | BEGIN_STREAM(s, raw, rawLen); |
| 418 | |
| 419 | avifBoxHeader ipcoHeader; |
| 420 | CHECK(avifStreamReadBoxHeader(&s, &ipcoHeader)); |
| 421 | if (memcmp(ipcoHeader.type, "ipco", 4) != 0) { |
| 422 | return AVIF_FALSE; |
| 423 | } |
| 424 | |
| 425 | // Read all item properties inside of ItemPropertyContainerBox |
| 426 | CHECK(avifParseItemPropertyContainerBox(data, avifStreamCurrent(&s), ipcoHeader.size)); |
| 427 | CHECK(avifStreamSkip(&s, ipcoHeader.size)); |
| 428 | |
| 429 | // Now read all ItemPropertyAssociation until the end of the box, and make associations |
| 430 | while (avifStreamHasBytesLeft(&s, 1)) { |
| 431 | avifBoxHeader ipmaHeader; |
| 432 | CHECK(avifStreamReadBoxHeader(&s, &ipmaHeader)); |
| 433 | |
| 434 | if (!memcmp(ipmaHeader.type, "ipma", 4)) { |
| 435 | CHECK(avifParseItemPropertyAssociation(data, avifStreamCurrent(&s), ipmaHeader.size)); |
| 436 | } else { |
| 437 | // These must all be type ipma |
| 438 | return AVIF_FALSE; |
| 439 | } |
| 440 | |
| 441 | CHECK(avifStreamSkip(&s, ipmaHeader.size)); |
| 442 | } |
| 443 | return AVIF_TRUE; |
| 444 | } |
| 445 | |
| 446 | static avifBool avifParseItemInfoEntry(avifData * data, uint8_t * raw, size_t rawLen) |
| 447 | { |
| 448 | BEGIN_STREAM(s, raw, rawLen); |
| 449 | |
| 450 | CHECK(avifStreamReadAndEnforceVersion(&s, 2)); // TODO: support version > 2? 2+ is required for item_type |
| 451 | |
| 452 | uint16_t itemID; // unsigned int(16) item_ID; |
| 453 | CHECK(avifStreamReadU16(&s, &itemID)); // |
| 454 | uint16_t itemProtectionIndex; // unsigned int(16) item_protection_index; |
| 455 | CHECK(avifStreamReadU16(&s, &itemProtectionIndex)); // |
| 456 | uint8_t itemType[4]; // unsigned int(32) item_type; |
| 457 | CHECK(avifStreamRead(&s, itemType, 4)); // |
| 458 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 459 | avifItem * item = avifDataFindItem(data, itemID); |
| 460 | memcpy(item->type, itemType, sizeof(itemType)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 461 | return AVIF_TRUE; |
| 462 | } |
| 463 | |
| 464 | static avifBool avifParseItemInfoBox(avifData * data, uint8_t * raw, size_t rawLen) |
| 465 | { |
| 466 | BEGIN_STREAM(s, raw, rawLen); |
| 467 | |
| 468 | uint8_t version; |
| 469 | CHECK(avifStreamReadVersionAndFlags(&s, &version, NULL)); |
| 470 | uint32_t entryCount; |
| 471 | if (version == 0) { |
| 472 | uint16_t tmp; |
| 473 | CHECK(avifStreamReadU16(&s, &tmp)); // unsigned int(16) entry_count; |
| 474 | entryCount = tmp; |
| 475 | } else if (version == 1) { |
| 476 | CHECK(avifStreamReadU32(&s, &entryCount)); // unsigned int(16) entry_count; |
| 477 | } else { |
| 478 | return AVIF_FALSE; |
| 479 | } |
| 480 | |
Joe Drago | 678b938 | 2019-02-09 03:17:47 -0800 | [diff] [blame] | 481 | for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 482 | avifBoxHeader infeHeader; |
| 483 | CHECK(avifStreamReadBoxHeader(&s, &infeHeader)); |
| 484 | |
| 485 | if (!memcmp(infeHeader.type, "infe", 4)) { |
| 486 | CHECK(avifParseItemInfoEntry(data, avifStreamCurrent(&s), infeHeader.size)); |
| 487 | } else { |
| 488 | // These must all be type ipma |
| 489 | return AVIF_FALSE; |
| 490 | } |
| 491 | |
| 492 | CHECK(avifStreamSkip(&s, infeHeader.size)); |
| 493 | } |
| 494 | |
| 495 | return AVIF_TRUE; |
| 496 | } |
| 497 | |
| 498 | static avifBool avifParseItemReferenceBox(avifData * data, uint8_t * raw, size_t rawLen) |
| 499 | { |
| 500 | BEGIN_STREAM(s, raw, rawLen); |
| 501 | |
| 502 | uint8_t version; |
| 503 | CHECK(avifStreamReadVersionAndFlags(&s, &version, NULL)); |
| 504 | |
| 505 | while (avifStreamHasBytesLeft(&s, 1)) { |
| 506 | avifBoxHeader irefHeader; |
| 507 | CHECK(avifStreamReadBoxHeader(&s, &irefHeader)); |
| 508 | |
| 509 | uint32_t fromID = 0; |
| 510 | if (version == 0) { |
| 511 | uint16_t tmp; |
| 512 | CHECK(avifStreamReadU16(&s, &tmp)); // unsigned int(16) from_item_ID; |
| 513 | fromID = tmp; |
| 514 | } else if (version == 1) { |
| 515 | CHECK(avifStreamReadU32(&s, &fromID)); // unsigned int(32) from_item_ID; |
| 516 | } else { |
| 517 | // unsupported iref version, skip it |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | uint16_t referenceCount = 0; |
| 522 | CHECK(avifStreamReadU16(&s, &referenceCount)); // unsigned int(16) reference_count; |
| 523 | |
| 524 | for (uint16_t refIndex = 0; refIndex < referenceCount; ++refIndex) { |
| 525 | uint32_t toID = 0; |
| 526 | if (version == 0) { |
| 527 | uint16_t tmp; |
| 528 | CHECK(avifStreamReadU16(&s, &tmp)); // unsigned int(16) to_item_ID; |
| 529 | toID = tmp; |
| 530 | } else if (version == 1) { |
| 531 | CHECK(avifStreamReadU32(&s, &toID)); // unsigned int(32) to_item_ID; |
| 532 | } else { |
| 533 | // unsupported iref version, skip it |
| 534 | break; |
| 535 | } |
| 536 | |
| 537 | // Read this reference as "{fromID} is a {irefType} for {toID}" |
| 538 | if (fromID && toID) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 539 | avifItem * item = avifDataFindItem(data, fromID); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 540 | if (!memcmp(irefHeader.type, "thmb", 4)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 541 | item->thumbnailForID = toID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 542 | } |
| 543 | if (!memcmp(irefHeader.type, "auxl", 4)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 544 | item->auxForID = toID; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | return AVIF_TRUE; |
| 551 | } |
| 552 | |
| 553 | static avifBool avifParseMetaBox(avifData * data, uint8_t * raw, size_t rawLen) |
| 554 | { |
| 555 | BEGIN_STREAM(s, raw, rawLen); |
| 556 | |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 557 | CHECK(avifStreamReadAndEnforceVersion(&s, 0)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 558 | |
| 559 | while (avifStreamHasBytesLeft(&s, 1)) { |
| 560 | avifBoxHeader header; |
| 561 | CHECK(avifStreamReadBoxHeader(&s, &header)); |
| 562 | |
| 563 | if (!memcmp(header.type, "iloc", 4)) { |
| 564 | CHECK(avifParseItemLocationBox(data, avifStreamCurrent(&s), header.size)); |
| 565 | } else if (!memcmp(header.type, "iprp", 4)) { |
| 566 | CHECK(avifParseItemPropertiesBox(data, avifStreamCurrent(&s), header.size)); |
| 567 | } else if (!memcmp(header.type, "iinf", 4)) { |
| 568 | CHECK(avifParseItemInfoBox(data, avifStreamCurrent(&s), header.size)); |
| 569 | } else if (!memcmp(header.type, "iref", 4)) { |
| 570 | CHECK(avifParseItemReferenceBox(data, avifStreamCurrent(&s), header.size)); |
| 571 | } |
| 572 | |
| 573 | CHECK(avifStreamSkip(&s, header.size)); |
| 574 | } |
| 575 | return AVIF_TRUE; |
| 576 | } |
| 577 | |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame^] | 578 | static avifBool avifParseTrackHeaderBox(avifData * data, avifTrack * track, uint8_t * raw, size_t rawLen) |
| 579 | { |
| 580 | BEGIN_STREAM(s, raw, rawLen); |
| 581 | |
| 582 | uint8_t version; |
| 583 | uint8_t flags[3]; |
| 584 | CHECK(avifStreamReadVersionAndFlags(&s, &version, flags)); |
| 585 | |
| 586 | uint32_t ignored32, trackID; |
| 587 | uint64_t ignored64; |
| 588 | if (version == 1) { |
| 589 | CHECK(avifStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time; |
| 590 | CHECK(avifStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time; |
| 591 | CHECK(avifStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID; |
| 592 | } else if (version == 0) { |
| 593 | CHECK(avifStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time; |
| 594 | CHECK(avifStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time; |
| 595 | CHECK(avifStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID; |
| 596 | } else { |
| 597 | // Unsupported version |
| 598 | return AVIF_FALSE; |
| 599 | } |
| 600 | |
| 601 | // TODO: support scaling based on width/height track header info? |
| 602 | |
| 603 | track->id = trackID; |
| 604 | return AVIF_TRUE; |
| 605 | } |
| 606 | |
| 607 | static avifBool avifParseMediaHeaderBox(avifData * data, avifTrack * track, uint8_t * raw, size_t rawLen) |
| 608 | { |
| 609 | BEGIN_STREAM(s, raw, rawLen); |
| 610 | |
| 611 | uint8_t version; |
| 612 | uint8_t flags[3]; |
| 613 | CHECK(avifStreamReadVersionAndFlags(&s, &version, flags)); |
| 614 | |
| 615 | uint32_t ignored32, mediaTimescale, mediaDuration32; |
| 616 | uint64_t ignored64, mediaDuration64; |
| 617 | if (version == 1) { |
| 618 | CHECK(avifStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time; |
| 619 | CHECK(avifStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time; |
| 620 | CHECK(avifStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale; |
| 621 | CHECK(avifStreamReadU64(&s, &mediaDuration64)); // unsigned int(64) duration; |
| 622 | track->mediaDuration = mediaDuration64; |
| 623 | } else if (version == 0) { |
| 624 | CHECK(avifStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time; |
| 625 | CHECK(avifStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time; |
| 626 | CHECK(avifStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale; |
| 627 | CHECK(avifStreamReadU32(&s, &mediaDuration32)); // unsigned int(32) duration; |
| 628 | track->mediaDuration = (uint64_t)mediaDuration32; |
| 629 | } else { |
| 630 | // Unsupported version |
| 631 | return AVIF_FALSE; |
| 632 | } |
| 633 | |
| 634 | track->mediaTimescale = mediaTimescale; |
| 635 | return AVIF_TRUE; |
| 636 | } |
| 637 | |
| 638 | static avifBool avifParseChunkOffsetBox(avifData * data, avifSampleTable * sampleTable, avifBool largeOffsets, uint8_t * raw, size_t rawLen) |
| 639 | { |
| 640 | BEGIN_STREAM(s, raw, rawLen); |
| 641 | |
| 642 | CHECK(avifStreamReadAndEnforceVersion(&s, 0)); |
| 643 | |
| 644 | uint32_t entryCount; |
| 645 | CHECK(avifStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
| 646 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 647 | uint64_t offset; |
| 648 | if (largeOffsets) { |
| 649 | CHECK(avifStreamReadU64(&s, &offset)); // unsigned int(32) chunk_offset; |
| 650 | } else { |
| 651 | uint32_t offset32; |
| 652 | CHECK(avifStreamReadU32(&s, &offset32)); // unsigned int(32) chunk_offset; |
| 653 | offset = (uint64_t)offset32; |
| 654 | } |
| 655 | |
| 656 | avifSampleTableChunk * chunk = (avifSampleTableChunk *)avifArrayPushPtr(&sampleTable->chunks); |
| 657 | chunk->offset = offset; |
| 658 | } |
| 659 | return AVIF_TRUE; |
| 660 | } |
| 661 | |
| 662 | static avifBool avifParseSampleToChunkBox(avifData * data, avifSampleTable * sampleTable, uint8_t * raw, size_t rawLen) |
| 663 | { |
| 664 | BEGIN_STREAM(s, raw, rawLen); |
| 665 | |
| 666 | CHECK(avifStreamReadAndEnforceVersion(&s, 0)); |
| 667 | |
| 668 | uint32_t entryCount; |
| 669 | CHECK(avifStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
| 670 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 671 | avifSampleTableSampleToChunk * sampleToChunk = (avifSampleTableSampleToChunk *)avifArrayPushPtr(&sampleTable->sampleToChunks); |
| 672 | CHECK(avifStreamReadU32(&s, &sampleToChunk->firstChunk)); // unsigned int(32) first_chunk; |
| 673 | CHECK(avifStreamReadU32(&s, &sampleToChunk->samplesPerChunk)); // unsigned int(32) samples_per_chunk; |
| 674 | CHECK(avifStreamReadU32(&s, &sampleToChunk->sampleDescriptionIndex)); // unsigned int(32) sample_description_index; |
| 675 | } |
| 676 | return AVIF_TRUE; |
| 677 | } |
| 678 | |
| 679 | static avifBool avifParseSampleSizeBox(avifData * data, avifSampleTable * sampleTable, uint8_t * raw, size_t rawLen) |
| 680 | { |
| 681 | BEGIN_STREAM(s, raw, rawLen); |
| 682 | |
| 683 | CHECK(avifStreamReadAndEnforceVersion(&s, 0)); |
| 684 | |
| 685 | uint32_t allSamplesSize, entryCount; |
| 686 | CHECK(avifStreamReadU32(&s, &allSamplesSize)); // unsigned int(32) sample_size; |
| 687 | CHECK(avifStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
| 688 | |
| 689 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 690 | avifSampleTableSampleSize * sampleSize = (avifSampleTableSampleSize *)avifArrayPushPtr(&sampleTable->sampleSizes); |
| 691 | if (allSamplesSize == 0) { |
| 692 | CHECK(avifStreamReadU32(&s, &sampleSize->size)); // unsigned int(32) entry_size; |
| 693 | } else { |
| 694 | // This could be done more efficiently, memory-wise. |
| 695 | sampleSize->size = allSamplesSize; |
| 696 | } |
| 697 | } |
| 698 | return AVIF_TRUE; |
| 699 | } |
| 700 | |
| 701 | static avifBool avifParseTimeToSampleBox(avifData * data, avifSampleTable * sampleTable, uint8_t * raw, size_t rawLen) |
| 702 | { |
| 703 | BEGIN_STREAM(s, raw, rawLen); |
| 704 | |
| 705 | CHECK(avifStreamReadAndEnforceVersion(&s, 0)); |
| 706 | |
| 707 | uint32_t entryCount; |
| 708 | CHECK(avifStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
| 709 | |
| 710 | for (uint32_t i = 0; i < entryCount; ++i) { |
| 711 | avifSampleTableTimeToSample * timeToSample = (avifSampleTableTimeToSample *)avifArrayPushPtr(&sampleTable->timeToSamples); |
| 712 | CHECK(avifStreamReadU32(&s, &timeToSample->sampleCount)); // unsigned int(32) sample_count; |
| 713 | CHECK(avifStreamReadU32(&s, &timeToSample->sampleDelta)); // unsigned int(32) sample_delta; |
| 714 | } |
| 715 | return AVIF_TRUE; |
| 716 | } |
| 717 | |
| 718 | static avifBool avifParseSampleTableBox(avifData * data, avifTrack * track, uint8_t * raw, size_t rawLen) |
| 719 | { |
| 720 | if (track->sampleTable) { |
| 721 | // A TrackBox may only have one SampleTable |
| 722 | return AVIF_FALSE; |
| 723 | } |
| 724 | track->sampleTable = avifSampleTableCreate(); |
| 725 | |
| 726 | BEGIN_STREAM(s, raw, rawLen); |
| 727 | |
| 728 | while (avifStreamHasBytesLeft(&s, 1)) { |
| 729 | avifBoxHeader header; |
| 730 | CHECK(avifStreamReadBoxHeader(&s, &header)); |
| 731 | |
| 732 | if (!memcmp(header.type, "stco", 4)) { |
| 733 | CHECK(avifParseChunkOffsetBox(data, track->sampleTable, AVIF_FALSE, avifStreamCurrent(&s), header.size)); |
| 734 | } else if (!memcmp(header.type, "co64", 4)) { |
| 735 | CHECK(avifParseChunkOffsetBox(data, track->sampleTable, AVIF_TRUE, avifStreamCurrent(&s), header.size)); |
| 736 | } else if (!memcmp(header.type, "stsc", 4)) { |
| 737 | CHECK(avifParseSampleToChunkBox(data, track->sampleTable, avifStreamCurrent(&s), header.size)); |
| 738 | } else if (!memcmp(header.type, "stsz", 4)) { |
| 739 | CHECK(avifParseSampleSizeBox(data, track->sampleTable, avifStreamCurrent(&s), header.size)); |
| 740 | } else if (!memcmp(header.type, "stts", 4)) { |
| 741 | CHECK(avifParseTimeToSampleBox(data, track->sampleTable, avifStreamCurrent(&s), header.size)); |
| 742 | } |
| 743 | |
| 744 | CHECK(avifStreamSkip(&s, header.size)); |
| 745 | } |
| 746 | |
| 747 | // Now calculate chunk sizes from the read-in sample table |
| 748 | uint32_t sampleSizeIndex = 0; |
| 749 | for (uint32_t chunkIndex = 0; chunkIndex < track->sampleTable->chunks.count; ++chunkIndex) { |
| 750 | avifSampleTableChunk * chunk = &track->sampleTable->chunks.chunk[chunkIndex]; |
| 751 | |
| 752 | // First, figure out how many samples are in this chunk |
| 753 | uint32_t sampleCount = 0; |
| 754 | for (int sampleToChunkIndex = track->sampleTable->sampleToChunks.count - 1; sampleToChunkIndex >= 0; --sampleToChunkIndex) { |
| 755 | avifSampleTableSampleToChunk * sampleToChunk = &track->sampleTable->sampleToChunks.sampleToChunk[sampleToChunkIndex]; |
| 756 | if (sampleToChunk->firstChunk <= (chunkIndex + 1)) { |
| 757 | sampleCount = sampleToChunk->samplesPerChunk; |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | if (sampleCount == 0) { |
| 762 | // chunks with 0 samples are invalid |
| 763 | return AVIF_FALSE; |
| 764 | } |
| 765 | |
| 766 | // Then sum up the next sampleCount samples into this chunk |
| 767 | for (uint32_t sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex) { |
| 768 | if (sampleSizeIndex >= track->sampleTable->sampleSizes.count) { |
| 769 | // We've run out of samples to sum |
| 770 | return AVIF_FALSE; |
| 771 | } |
| 772 | |
| 773 | avifSampleTableSampleSize * sampleSize = &track->sampleTable->sampleSizes.sampleSize[sampleSizeIndex]; |
| 774 | chunk->size += sampleSize->size; |
| 775 | ++sampleSizeIndex; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | return AVIF_TRUE; |
| 780 | } |
| 781 | |
| 782 | static avifBool avifParseMediaInformationBox(avifData * data, avifTrack * track, uint8_t * raw, size_t rawLen) |
| 783 | { |
| 784 | BEGIN_STREAM(s, raw, rawLen); |
| 785 | |
| 786 | while (avifStreamHasBytesLeft(&s, 1)) { |
| 787 | avifBoxHeader header; |
| 788 | CHECK(avifStreamReadBoxHeader(&s, &header)); |
| 789 | |
| 790 | if (!memcmp(header.type, "stbl", 4)) { |
| 791 | CHECK(avifParseSampleTableBox(data, track, avifStreamCurrent(&s), header.size)); |
| 792 | } |
| 793 | |
| 794 | CHECK(avifStreamSkip(&s, header.size)); |
| 795 | } |
| 796 | return AVIF_TRUE; |
| 797 | } |
| 798 | |
| 799 | static avifBool avifParseMediaBox(avifData * data, avifTrack * track, uint8_t * raw, size_t rawLen) |
| 800 | { |
| 801 | BEGIN_STREAM(s, raw, rawLen); |
| 802 | |
| 803 | while (avifStreamHasBytesLeft(&s, 1)) { |
| 804 | avifBoxHeader header; |
| 805 | CHECK(avifStreamReadBoxHeader(&s, &header)); |
| 806 | |
| 807 | if (!memcmp(header.type, "mdhd", 4)) { |
| 808 | CHECK(avifParseMediaHeaderBox(data, track, avifStreamCurrent(&s), header.size)); |
| 809 | } else if (!memcmp(header.type, "minf", 4)) { |
| 810 | CHECK(avifParseMediaInformationBox(data, track, avifStreamCurrent(&s), header.size)); |
| 811 | } |
| 812 | |
| 813 | CHECK(avifStreamSkip(&s, header.size)); |
| 814 | } |
| 815 | return AVIF_TRUE; |
| 816 | } |
| 817 | |
| 818 | static avifBool avifParseTrackBox(avifData * data, uint8_t * raw, size_t rawLen) |
| 819 | { |
| 820 | BEGIN_STREAM(s, raw, rawLen); |
| 821 | |
| 822 | avifTrack * track = (avifTrack *)avifArrayPushPtr(&data->tracks); |
| 823 | |
| 824 | while (avifStreamHasBytesLeft(&s, 1)) { |
| 825 | avifBoxHeader header; |
| 826 | CHECK(avifStreamReadBoxHeader(&s, &header)); |
| 827 | |
| 828 | if (!memcmp(header.type, "tkhd", 4)) { |
| 829 | CHECK(avifParseTrackHeaderBox(data, track, avifStreamCurrent(&s), header.size)); |
| 830 | } else if (!memcmp(header.type, "mdia", 4)) { |
| 831 | CHECK(avifParseMediaBox(data, track, avifStreamCurrent(&s), header.size)); |
| 832 | } |
| 833 | |
| 834 | CHECK(avifStreamSkip(&s, header.size)); |
| 835 | } |
| 836 | return AVIF_TRUE; |
| 837 | } |
| 838 | |
| 839 | static avifBool avifParseMoovBox(avifData * data, uint8_t * raw, size_t rawLen) |
| 840 | { |
| 841 | BEGIN_STREAM(s, raw, rawLen); |
| 842 | |
| 843 | while (avifStreamHasBytesLeft(&s, 1)) { |
| 844 | avifBoxHeader header; |
| 845 | CHECK(avifStreamReadBoxHeader(&s, &header)); |
| 846 | |
| 847 | if (!memcmp(header.type, "trak", 4)) { |
| 848 | CHECK(avifParseTrackBox(data, avifStreamCurrent(&s), header.size)); |
| 849 | } |
| 850 | |
| 851 | CHECK(avifStreamSkip(&s, header.size)); |
| 852 | } |
| 853 | return AVIF_TRUE; |
| 854 | } |
| 855 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 856 | static avifBool avifParseFileTypeBox(avifData * data, uint8_t * raw, size_t rawLen) |
| 857 | { |
| 858 | BEGIN_STREAM(s, raw, rawLen); |
| 859 | |
| 860 | CHECK(avifStreamRead(&s, data->ftyp.majorBrand, 4)); |
| 861 | CHECK(avifStreamReadU32(&s, &data->ftyp.minorVersion)); |
| 862 | |
| 863 | size_t compatibleBrandsBytes = avifStreamRemainingBytes(&s); |
| 864 | if ((compatibleBrandsBytes % 4) != 0) { |
| 865 | return AVIF_FALSE; |
| 866 | } |
| 867 | if (compatibleBrandsBytes > (4 * MAX_COMPATIBLE_BRANDS)) { |
| 868 | // TODO: stop clamping and resize this |
| 869 | compatibleBrandsBytes = (4 * MAX_COMPATIBLE_BRANDS); |
| 870 | } |
| 871 | CHECK(avifStreamRead(&s, data->ftyp.compatibleBrands, compatibleBrandsBytes)); |
Joe Drago | 678b938 | 2019-02-09 03:17:47 -0800 | [diff] [blame] | 872 | data->ftyp.compatibleBrandsCount = (int)compatibleBrandsBytes / 4; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 873 | |
| 874 | return AVIF_TRUE; |
| 875 | } |
| 876 | |
| 877 | static avifBool avifParse(avifData * data, uint8_t * raw, size_t rawLen) |
| 878 | { |
| 879 | BEGIN_STREAM(s, raw, rawLen); |
| 880 | |
| 881 | while (avifStreamHasBytesLeft(&s, 1)) { |
| 882 | avifBoxHeader header; |
| 883 | CHECK(avifStreamReadBoxHeader(&s, &header)); |
| 884 | |
| 885 | if (!memcmp(header.type, "ftyp", 4)) { |
| 886 | CHECK(avifParseFileTypeBox(data, avifStreamCurrent(&s), header.size)); |
| 887 | } else if (!memcmp(header.type, "meta", 4)) { |
| 888 | CHECK(avifParseMetaBox(data, avifStreamCurrent(&s), header.size)); |
Joe Drago | ae7e2c3 | 2019-07-18 15:22:25 -0700 | [diff] [blame^] | 889 | } else if (!memcmp(header.type, "moov", 4)) { |
| 890 | CHECK(avifParseMoovBox(data, avifStreamCurrent(&s), header.size)); |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | CHECK(avifStreamSkip(&s, header.size)); |
| 894 | } |
| 895 | return AVIF_TRUE; |
| 896 | } |
| 897 | |
| 898 | // --------------------------------------------------------------------------- |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 899 | |
Joe Drago | 0b05eee | 2019-06-12 13:24:39 -0700 | [diff] [blame] | 900 | avifDecoder * avifDecoderCreate(void) |
| 901 | { |
| 902 | avifDecoder * decoder = (avifDecoder *)avifAlloc(sizeof(avifDecoder)); |
| 903 | memset(decoder, 0, sizeof(avifDecoder)); |
| 904 | return decoder; |
| 905 | } |
| 906 | |
| 907 | void avifDecoderDestroy(avifDecoder * decoder) |
| 908 | { |
| 909 | avifFree(decoder); |
| 910 | } |
| 911 | |
| 912 | avifResult avifDecoderRead(avifDecoder * decoder, avifImage * image, avifRawData * input) |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 913 | { |
Joe Drago | 33f1d36 | 2019-02-13 16:46:22 -0800 | [diff] [blame] | 914 | avifCodec * codec = NULL; |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 915 | avifData * data = NULL; |
| 916 | avifResult result = AVIF_RESULT_UNKNOWN_ERROR; |
Joe Drago | 33f1d36 | 2019-02-13 16:46:22 -0800 | [diff] [blame] | 917 | |
Joe Drago | 177914d | 2019-07-12 17:46:46 -0700 | [diff] [blame] | 918 | #if !defined(AVIF_CODEC_AOM) && !defined(AVIF_CODEC_DAV1D) |
Joe Drago | 3533b97 | 2019-07-12 14:32:20 -0700 | [diff] [blame] | 919 | // Just bail out early, we're not surviving this function without a decoder compiled in |
| 920 | return AVIF_RESULT_NO_CODEC_AVAILABLE; |
| 921 | #endif |
| 922 | |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 923 | // ----------------------------------------------------------------------- |
| 924 | // Parse BMFF boxes |
| 925 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 926 | data = avifDataCreate(); |
| 927 | if (!avifParse(data, input->data, input->size)) { |
| 928 | result = AVIF_RESULT_BMFF_PARSE_FAILED; |
| 929 | goto cleanup; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 930 | } |
| 931 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 932 | avifBool avifCompatible = (memcmp(data->ftyp.majorBrand, "avif", 4) == 0) ? AVIF_TRUE : AVIF_FALSE; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 933 | if (!avifCompatible) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 934 | for (int compatibleBrandIndex = 0; compatibleBrandIndex < data->ftyp.compatibleBrandsCount; ++compatibleBrandIndex) { |
| 935 | uint8_t * compatibleBrand = &data->ftyp.compatibleBrands[4 * compatibleBrandIndex]; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 936 | if (!memcmp(compatibleBrand, "avif", 4)) { |
| 937 | avifCompatible = AVIF_TRUE; |
| 938 | break; |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | if (!avifCompatible) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 943 | result = AVIF_RESULT_INVALID_FTYP; |
| 944 | goto cleanup; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 945 | } |
| 946 | |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 947 | // ----------------------------------------------------------------------- |
| 948 | |
| 949 | avifRawData colorOBU = AVIF_RAW_DATA_EMPTY; |
| 950 | avifRawData alphaOBU = AVIF_RAW_DATA_EMPTY; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 951 | avifItem * colorOBUItem = NULL; |
| 952 | avifItem * alphaOBUItem = NULL; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 953 | |
Joe Drago | 7637023 | 2019-07-16 11:00:52 -0700 | [diff] [blame] | 954 | // Sanity check items |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 955 | for (uint32_t itemIndex = 0; itemIndex < data->items.count; ++itemIndex) { |
| 956 | avifItem * item = &data->items.item[itemIndex]; |
Joe Drago | 7637023 | 2019-07-16 11:00:52 -0700 | [diff] [blame] | 957 | if (item->offset > input->size) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 958 | result = AVIF_RESULT_BMFF_PARSE_FAILED; |
| 959 | goto cleanup; |
Joe Drago | 7637023 | 2019-07-16 11:00:52 -0700 | [diff] [blame] | 960 | } |
| 961 | uint64_t offsetSize = (uint64_t)item->offset + (uint64_t)item->size; |
| 962 | if (offsetSize > (uint64_t)input->size) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 963 | result = AVIF_RESULT_BMFF_PARSE_FAILED; |
| 964 | goto cleanup; |
Joe Drago | 7637023 | 2019-07-16 11:00:52 -0700 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 968 | // Find the colorOBU item |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 969 | for (uint32_t itemIndex = 0; itemIndex < data->items.count; ++itemIndex) { |
| 970 | avifItem * item = &data->items.item[itemIndex]; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 971 | if (!item->id || !item->size) { |
| 972 | break; |
| 973 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 974 | if (memcmp(item->type, "av01", 4)) { |
| 975 | // probably exif or some other data |
| 976 | continue; |
| 977 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 978 | if (item->thumbnailForID != 0) { |
| 979 | // It's a thumbnail, skip it |
| 980 | continue; |
| 981 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 982 | |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 983 | colorOBUItem = item; |
| 984 | colorOBU.data = input->data + item->offset; |
| 985 | colorOBU.size = item->size; |
| 986 | break; |
| 987 | } |
| 988 | |
| 989 | // Find the alphaOBU item, if any |
| 990 | if (colorOBUItem) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 991 | for (uint32_t itemIndex = 0; itemIndex < data->items.count; ++itemIndex) { |
| 992 | avifItem * item = &data->items.item[itemIndex]; |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 993 | if (!item->id || !item->size) { |
| 994 | break; |
| 995 | } |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 996 | if (memcmp(item->type, "av01", 4)) { |
| 997 | // probably exif or some other data |
| 998 | continue; |
| 999 | } |
| 1000 | if (item->thumbnailForID != 0) { |
| 1001 | // It's a thumbnail, skip it |
| 1002 | continue; |
| 1003 | } |
| 1004 | |
Joe Drago | cd1e4c3 | 2019-02-08 11:26:31 -0800 | [diff] [blame] | 1005 | if (isAlphaURN(item->auxC.auxType) && (item->auxForID == colorOBUItem->id)) { |
Joe Drago | 8f7a300 | 2019-02-07 19:35:37 -0800 | [diff] [blame] | 1006 | alphaOBUItem = item; |
| 1007 | alphaOBU.data = input->data + item->offset; |
| 1008 | alphaOBU.size = item->size; |
| 1009 | break; |
| 1010 | } |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | if (colorOBU.size == 0) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 1015 | result = AVIF_RESULT_NO_AV1_ITEMS_FOUND; |
| 1016 | goto cleanup; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1017 | } |
| 1018 | avifBool hasAlpha = (alphaOBU.size > 0) ? AVIF_TRUE : AVIF_FALSE; |
| 1019 | |
Joe Drago | 177914d | 2019-07-12 17:46:46 -0700 | [diff] [blame] | 1020 | #if defined(AVIF_CODEC_DAV1D) |
| 1021 | codec = avifCodecCreateDav1d(); |
| 1022 | #elif defined(AVIF_CODEC_AOM) |
Joe Drago | 3533b97 | 2019-07-12 14:32:20 -0700 | [diff] [blame] | 1023 | codec = avifCodecCreateAOM(); |
| 1024 | #else |
Joe Drago | 7a9a661 | 2019-07-17 11:21:24 -0700 | [diff] [blame] | 1025 | // #error No decoder available! |
Joe Drago | 3533b97 | 2019-07-12 14:32:20 -0700 | [diff] [blame] | 1026 | return AVIF_RESULT_NO_CODEC_AVAILABLE; |
| 1027 | #endif |
| 1028 | if (!codec->decode(codec, AVIF_CODEC_PLANES_COLOR, &colorOBU)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 1029 | result = AVIF_RESULT_DECODE_COLOR_FAILED; |
| 1030 | goto cleanup; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1031 | } |
Joe Drago | 3533b97 | 2019-07-12 14:32:20 -0700 | [diff] [blame] | 1032 | avifCodecImageSize colorPlanesSize = codec->getImageSize(codec, AVIF_CODEC_PLANES_COLOR); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1033 | |
Joe Drago | 33f1d36 | 2019-02-13 16:46:22 -0800 | [diff] [blame] | 1034 | avifCodecImageSize alphaPlanesSize; |
| 1035 | memset(&alphaPlanesSize, 0, sizeof(alphaPlanesSize)); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1036 | if (hasAlpha) { |
Joe Drago | 3533b97 | 2019-07-12 14:32:20 -0700 | [diff] [blame] | 1037 | if (!codec->decode(codec, AVIF_CODEC_PLANES_ALPHA, &alphaOBU)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 1038 | result = AVIF_RESULT_DECODE_ALPHA_FAILED; |
| 1039 | goto cleanup; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1040 | } |
Joe Drago | 3533b97 | 2019-07-12 14:32:20 -0700 | [diff] [blame] | 1041 | alphaPlanesSize = codec->getImageSize(codec, AVIF_CODEC_PLANES_ALPHA); |
Joe Drago | 33f1d36 | 2019-02-13 16:46:22 -0800 | [diff] [blame] | 1042 | |
| 1043 | if ((colorPlanesSize.width != alphaPlanesSize.width) || (colorPlanesSize.height != alphaPlanesSize.height)) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 1044 | result = AVIF_RESULT_COLOR_ALPHA_SIZE_MISMATCH; |
| 1045 | goto cleanup; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
Joe Drago | 97b071c | 2019-07-17 14:24:56 -0700 | [diff] [blame] | 1049 | if ((colorOBUItem && colorOBUItem->ispePresent && |
| 1050 | ((colorOBUItem->ispe.width != colorPlanesSize.width) || (colorOBUItem->ispe.height != colorPlanesSize.height))) || |
| 1051 | (alphaOBUItem && alphaOBUItem->ispePresent && |
| 1052 | ((alphaOBUItem->ispe.width != alphaPlanesSize.width) || (alphaOBUItem->ispe.height != alphaPlanesSize.height)))) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 1053 | result = AVIF_RESULT_ISPE_SIZE_MISMATCH; |
| 1054 | goto cleanup; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
Joe Drago | e7ce20d | 2019-02-11 16:37:38 -0800 | [diff] [blame] | 1057 | if (colorOBUItem->colrPresent) { |
| 1058 | if (colorOBUItem->colr.format == AVIF_PROFILE_FORMAT_ICC) { |
| 1059 | avifImageSetProfileICC(image, colorOBUItem->colr.icc, colorOBUItem->colr.iccSize); |
| 1060 | } else if (colorOBUItem->colr.format == AVIF_PROFILE_FORMAT_NCLX) { |
Joe Drago | f35e41a | 2019-02-11 17:01:41 -0800 | [diff] [blame] | 1061 | avifImageSetProfileNCLX(image, &colorOBUItem->colr.nclx); |
Joe Drago | e7ce20d | 2019-02-11 16:37:38 -0800 | [diff] [blame] | 1062 | } |
Joe Drago | 41eb62b | 2019-02-08 15:38:18 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
Joe Drago | 7ad3ad6 | 2019-02-07 11:17:34 -0800 | [diff] [blame] | 1065 | avifImageFreePlanes(image, AVIF_PLANES_ALL); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1066 | |
Joe Drago | 3533b97 | 2019-07-12 14:32:20 -0700 | [diff] [blame] | 1067 | avifResult imageResult = codec->getDecodedImage(codec, image); |
Joe Drago | 33f1d36 | 2019-02-13 16:46:22 -0800 | [diff] [blame] | 1068 | if (imageResult != AVIF_RESULT_OK) { |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 1069 | result = imageResult; |
| 1070 | goto cleanup; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
Joe Drago | 7efd803 | 2019-02-08 14:49:15 -0800 | [diff] [blame] | 1073 | #if defined(AVIF_FIX_STUDIO_ALPHA) |
Joe Drago | 3533b97 | 2019-07-12 14:32:20 -0700 | [diff] [blame] | 1074 | if (hasAlpha && codec->alphaLimitedRange(codec)) { |
Joe Drago | 33f1d36 | 2019-02-13 16:46:22 -0800 | [diff] [blame] | 1075 | // Naughty! Alpha planes are supposed to be full range. Correct that here. |
| 1076 | if (avifImageUsesU16(image)) { |
| 1077 | for (int j = 0; j < image->height; ++j) { |
| 1078 | for (int i = 0; i < image->height; ++i) { |
| 1079 | uint16_t * alpha = (uint16_t *)&image->alphaPlane[(i * 2) + (j * image->alphaRowBytes)]; |
Joe Drago | cd5b93b | 2019-04-18 11:19:54 -0700 | [diff] [blame] | 1080 | *alpha = (uint16_t)avifLimitedToFullY(image->depth, *alpha); |
Joe Drago | 7efd803 | 2019-02-08 14:49:15 -0800 | [diff] [blame] | 1081 | } |
Joe Drago | 33f1d36 | 2019-02-13 16:46:22 -0800 | [diff] [blame] | 1082 | } |
| 1083 | } else { |
| 1084 | for (int j = 0; j < image->height; ++j) { |
| 1085 | for (int i = 0; i < image->height; ++i) { |
| 1086 | uint8_t * alpha = &image->alphaPlane[i + (j * image->alphaRowBytes)]; |
Joe Drago | cd5b93b | 2019-04-18 11:19:54 -0700 | [diff] [blame] | 1087 | *alpha = (uint8_t)avifLimitedToFullY(image->depth, *alpha); |
Joe Drago | 7efd803 | 2019-02-08 14:49:15 -0800 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | } |
Joe Drago | 7ad3ad6 | 2019-02-07 11:17:34 -0800 | [diff] [blame] | 1091 | } |
Joe Drago | 33f1d36 | 2019-02-13 16:46:22 -0800 | [diff] [blame] | 1092 | #endif |
Joe Drago | 7ad3ad6 | 2019-02-07 11:17:34 -0800 | [diff] [blame] | 1093 | |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 1094 | decoder->ioStats.colorOBUSize = colorOBU.size; |
| 1095 | decoder->ioStats.alphaOBUSize = alphaOBU.size; |
| 1096 | |
| 1097 | result = AVIF_RESULT_OK; |
| 1098 | cleanup: |
Joe Drago | 33f1d36 | 2019-02-13 16:46:22 -0800 | [diff] [blame] | 1099 | if (codec) { |
| 1100 | avifCodecDestroy(codec); |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1101 | } |
Joe Drago | 05559c9 | 2019-07-17 16:33:38 -0700 | [diff] [blame] | 1102 | if (data) { |
| 1103 | avifDataDestroy(data); |
| 1104 | } |
| 1105 | return result; |
Joe Drago | 444f051 | 2019-01-23 17:03:24 -0800 | [diff] [blame] | 1106 | } |