Line | Count | Source |
1 | | // Copyright 2019 Joe Drago. All rights reserved. |
2 | | // SPDX-License-Identifier: BSD-2-Clause |
3 | | |
4 | | #include "avif/avif.h" |
5 | | #include "avif/internal.h" |
6 | | |
7 | | #include <assert.h> |
8 | | #include <ctype.h> |
9 | | #include <inttypes.h> |
10 | | #include <limits.h> |
11 | | #include <math.h> |
12 | | #include <stdio.h> |
13 | | #include <string.h> |
14 | | |
15 | | #define AUXTYPE_SIZE 64 |
16 | | #define CONTENTTYPE_SIZE 64 |
17 | | |
18 | | // class VisualSampleEntry(codingname) extends SampleEntry(codingname) { |
19 | | // unsigned int(16) pre_defined = 0; |
20 | | // const unsigned int(16) reserved = 0; |
21 | | // unsigned int(32)[3] pre_defined = 0; |
22 | | // unsigned int(16) width; |
23 | | // unsigned int(16) height; |
24 | | // template unsigned int(32) horizresolution = 0x00480000; // 72 dpi |
25 | | // template unsigned int(32) vertresolution = 0x00480000; // 72 dpi |
26 | | // const unsigned int(32) reserved = 0; |
27 | | // template unsigned int(16) frame_count = 1; |
28 | | // string[32] compressorname; |
29 | | // template unsigned int(16) depth = 0x0018; |
30 | | // int(16) pre_defined = -1; |
31 | | // // other boxes from derived specifications |
32 | | // CleanApertureBox clap; // optional |
33 | | // PixelAspectRatioBox pasp; // optional |
34 | | // } |
35 | | static const size_t VISUALSAMPLEENTRY_SIZE = 78; |
36 | | |
37 | | // The only supported ipma box values for both version and flags are [0,1], so there technically |
38 | | // can't be more than 4 unique tuples right now. |
39 | 14.6k | #define MAX_IPMA_VERSION_AND_FLAGS_SEEN 4 |
40 | | |
41 | | // --------------------------------------------------------------------------- |
42 | | // AVIF codec type (AV1 or AV2) |
43 | | |
44 | | static avifCodecType avifGetCodecType(const uint8_t * fourcc) |
45 | 87.6k | { |
46 | 87.6k | if (!memcmp(fourcc, "av01", 4)) { |
47 | 82.3k | return AVIF_CODEC_TYPE_AV1; |
48 | 82.3k | } |
49 | | #if defined(AVIF_CODEC_AVM) |
50 | | if (!memcmp(fourcc, "av02", 4)) { |
51 | | return AVIF_CODEC_TYPE_AV2; |
52 | | } |
53 | | #endif |
54 | 5.31k | return AVIF_CODEC_TYPE_UNKNOWN; |
55 | 87.6k | } |
56 | | |
57 | | static const char * avifGetConfigurationPropertyName(avifCodecType codecType) |
58 | 27.8k | { |
59 | 27.8k | static const char kUnknown[] = "****"; |
60 | 27.8k | switch (codecType) { |
61 | 27.8k | case AVIF_CODEC_TYPE_AV1: |
62 | 27.8k | return "av1C"; |
63 | | #if defined(AVIF_CODEC_AVM) |
64 | | case AVIF_CODEC_TYPE_AV2: |
65 | | return "av2C"; |
66 | | #endif |
67 | 0 | default: |
68 | 0 | assert(AVIF_FALSE); |
69 | 0 | return kUnknown; // Easier to deal with than NULL. |
70 | 27.8k | } |
71 | 27.8k | } |
72 | | |
73 | | // --------------------------------------------------------------------------- |
74 | | // Box data structures |
75 | | |
76 | | typedef uint8_t avifBrand[4]; |
77 | | AVIF_ARRAY_DECLARE(avifBrandArray, avifBrand, brand); |
78 | | |
79 | | // ftyp |
80 | | typedef struct avifFileType |
81 | | { |
82 | | uint8_t majorBrand[4]; |
83 | | uint8_t minorVersion[4]; |
84 | | // If not null, points to a memory block of 4 * compatibleBrandsCount bytes. |
85 | | const uint8_t * compatibleBrands; |
86 | | int compatibleBrandsCount; |
87 | | } avifFileType; |
88 | | |
89 | | // ispe |
90 | | typedef struct avifImageSpatialExtents |
91 | | { |
92 | | uint32_t width; |
93 | | uint32_t height; |
94 | | } avifImageSpatialExtents; |
95 | | |
96 | | // auxC |
97 | | typedef struct avifAuxiliaryType |
98 | | { |
99 | | char auxType[AUXTYPE_SIZE]; |
100 | | } avifAuxiliaryType; |
101 | | |
102 | | // infe mime content_type |
103 | | typedef struct avifContentType |
104 | | { |
105 | | char contentType[CONTENTTYPE_SIZE]; |
106 | | } avifContentType; |
107 | | |
108 | | // colr |
109 | | typedef struct avifColourInformationBox |
110 | | { |
111 | | avifBool hasICC; |
112 | | uint64_t iccOffset; |
113 | | size_t iccSize; |
114 | | |
115 | | avifBool hasNCLX; |
116 | | avifColorPrimaries colorPrimaries; |
117 | | avifTransferCharacteristics transferCharacteristics; |
118 | | avifMatrixCoefficients matrixCoefficients; |
119 | | avifRange range; |
120 | | } avifColourInformationBox; |
121 | | |
122 | 2.48k | #define MAX_PIXI_PLANE_DEPTHS 4 |
123 | | typedef struct avifPixelInformationProperty |
124 | | { |
125 | | uint8_t planeDepths[MAX_PIXI_PLANE_DEPTHS]; |
126 | | uint8_t planeCount; |
127 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_EXTENDED_PIXI) |
128 | | avifBool hasExtendedFields; // The fields below were signaled if this is true. |
129 | | uint8_t subsamplingFlag[MAX_PIXI_PLANE_DEPTHS]; // The fields below were signaled if this is true for a given channel. |
130 | | uint8_t subsamplingType[MAX_PIXI_PLANE_DEPTHS]; |
131 | | uint8_t subsamplingLocation[MAX_PIXI_PLANE_DEPTHS]; |
132 | | #endif // AVIF_ENABLE_EXPERIMENTAL_EXTENDED_PIXI |
133 | | } avifPixelInformationProperty; |
134 | | |
135 | | typedef struct avifOperatingPointSelectorProperty |
136 | | { |
137 | | uint8_t opIndex; |
138 | | } avifOperatingPointSelectorProperty; |
139 | | |
140 | | typedef struct avifLayerSelectorProperty |
141 | | { |
142 | | uint16_t layerID; |
143 | | } avifLayerSelectorProperty; |
144 | | |
145 | | typedef struct avifAV1LayeredImageIndexingProperty |
146 | | { |
147 | | uint32_t layerSize[3]; |
148 | | } avifAV1LayeredImageIndexingProperty; |
149 | | |
150 | | typedef struct avifOpaqueProperty |
151 | | { |
152 | | uint8_t usertype[16]; // Same as in avifImageItemProperty. |
153 | | avifRWData boxPayload; // Same as in avifImageItemProperty. |
154 | | } avifOpaqueProperty; |
155 | | |
156 | | // Array of item or track ids. |
157 | | AVIF_ARRAY_DECLARE(avifCodecEntityIDs, uint32_t, ids); |
158 | | |
159 | | // Content of a box inside a 'grpl' box, representing a group of entities. |
160 | | typedef struct avifEntityToGroup |
161 | | { |
162 | | uint8_t groupingType[4]; |
163 | | uint32_t groupID; |
164 | | avifCodecEntityIDs entityIDs; |
165 | | } avifEntityToGroup; |
166 | | AVIF_ARRAY_DECLARE(avifEntityToGroups, avifEntityToGroup, groups); |
167 | | |
168 | | // --------------------------------------------------------------------------- |
169 | | // Top-level structures |
170 | | |
171 | | struct avifMeta; |
172 | | |
173 | | // Temporary storage for ipco/stsd contents until they can be associated and memcpy'd to an avifDecoderItem |
174 | | typedef struct avifProperty |
175 | | { |
176 | | uint8_t type[4]; |
177 | | avifBool isOpaque; |
178 | | union |
179 | | { |
180 | | avifImageSpatialExtents ispe; |
181 | | avifAuxiliaryType auxC; // Contents of 'auxC' for items, or 'auxi' for tracks |
182 | | avifColourInformationBox colr; |
183 | | avifCodecConfigurationBox av1C; // TODO(yguyon): Rename or add av2C |
184 | | avifPixelAspectRatioBox pasp; |
185 | | avifCleanApertureBox clap; |
186 | | avifImageRotation irot; |
187 | | avifImageMirror imir; |
188 | | avifPixelInformationProperty pixi; |
189 | | avifOperatingPointSelectorProperty a1op; |
190 | | avifLayerSelectorProperty lsel; |
191 | | avifAV1LayeredImageIndexingProperty a1lx; |
192 | | avifContentLightLevelInformationBox clli; |
193 | | avifOpaqueProperty opaque; |
194 | | } u; |
195 | | } avifProperty; |
196 | | AVIF_ARRAY_DECLARE(avifPropertyArray, avifProperty, prop); |
197 | | |
198 | | // Finds the first property of a given type. |
199 | | static const avifProperty * avifPropertyArrayFind(const avifPropertyArray * properties, const char * type) |
200 | 179k | { |
201 | 717k | for (uint32_t propertyIndex = 0; propertyIndex < properties->count; ++propertyIndex) { |
202 | 590k | const avifProperty * prop = &properties->prop[propertyIndex]; |
203 | 590k | if (!memcmp(prop->type, type, 4)) { |
204 | 51.4k | return prop; |
205 | 51.4k | } |
206 | 590k | } |
207 | 127k | return NULL; |
208 | 179k | } |
209 | | |
210 | | AVIF_ARRAY_DECLARE(avifExtentArray, avifExtent, extent); |
211 | | |
212 | | // one "item" worth for decoding (all iref, iloc, iprp, etc refer to one of these) |
213 | | typedef struct avifDecoderItem |
214 | | { |
215 | | uint32_t id; |
216 | | struct avifMeta * meta; // Unowned; A back-pointer for convenience |
217 | | uint8_t type[4]; |
218 | | size_t size; |
219 | | avifBool idatStored; // If true, offset is relative to the associated meta box's idat box (iloc construction_method==1) |
220 | | uint32_t width; // Set from this item's ispe property, if present |
221 | | uint32_t height; // Set from this item's ispe property, if present |
222 | | avifContentType contentType; |
223 | | avifPropertyArray properties; |
224 | | avifExtentArray extents; // All extent offsets/sizes |
225 | | avifRWData mergedExtents; // A single contiguous block of this item's extents |
226 | | avifBool ownsMergedExtents; // If true, mergedExtents must be freed when this item is destroyed. |
227 | | // If false, mergedExtents is used as an avifROData and points to a |
228 | | // buffer it doesn't own. |
229 | | avifBool partialMergedExtents; // If true, mergedExtents doesn't have all of the item data yet |
230 | | uint32_t thumbnailForID; // if non-zero, this item is a thumbnail for Item #{thumbnailForID} |
231 | | uint32_t auxForID; // if non-zero, this item is an auxC plane for Item #{auxForID} |
232 | | uint32_t descForID; // if non-zero, this item is a content description for Item #{descForID} |
233 | | uint32_t dimgForID; // if non-zero, this item is an input of derived Item #{dimgForID} |
234 | | uint32_t dimgIdx; // If dimgForId is non-zero, this is the zero-based index of this item in the list of Item #{dimgForID}'s dimg. |
235 | | avifBool hasDimgFrom; // whether there is a 'dimg' box with this item's id as 'fromID' |
236 | | uint32_t premByID; // if non-zero, this item is premultiplied by Item #{premByID} |
237 | | avifBool hasUnsupportedEssentialProperty; // If true, this item cites a property flagged as 'essential' that libavif doesn't support (yet). Ignore the item, if so. |
238 | | avifBool ipmaSeen; // if true, this item already received a property association |
239 | | avifBool progressive; // if true, this item has progressive layers (a1lx), but does not select a specific layer (the layer_id value in lsel is set to 0xFFFF) |
240 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
241 | | avifPixelFormat miniBoxPixelFormat; // Set from the MinimizedImageBox, if present (AVIF_PIXEL_FORMAT_NONE otherwise) |
242 | | avifChromaSamplePosition miniBoxChromaSamplePosition; // Set from the MinimizedImageBox, if present (AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN otherwise) |
243 | | #endif |
244 | | } avifDecoderItem; |
245 | | AVIF_ARRAY_DECLARE(avifDecoderItemArray, avifDecoderItem *, item); |
246 | | |
247 | | // grid storage |
248 | | typedef struct avifImageGrid |
249 | | { |
250 | | uint32_t rows; // Legal range: [1-256] |
251 | | uint32_t columns; // Legal range: [1-256] |
252 | | uint32_t outputWidth; |
253 | | uint32_t outputHeight; |
254 | | } avifImageGrid; |
255 | | |
256 | | // --------------------------------------------------------------------------- |
257 | | // avifTrack |
258 | | |
259 | | typedef struct avifSampleTableChunk |
260 | | { |
261 | | uint64_t offset; |
262 | | } avifSampleTableChunk; |
263 | | AVIF_ARRAY_DECLARE(avifSampleTableChunkArray, avifSampleTableChunk, chunk); |
264 | | |
265 | | typedef struct avifSampleTableSampleToChunk |
266 | | { |
267 | | uint32_t firstChunk; |
268 | | uint32_t samplesPerChunk; |
269 | | uint32_t sampleDescriptionIndex; |
270 | | } avifSampleTableSampleToChunk; |
271 | | AVIF_ARRAY_DECLARE(avifSampleTableSampleToChunkArray, avifSampleTableSampleToChunk, sampleToChunk); |
272 | | |
273 | | typedef struct avifSampleTableSampleSize |
274 | | { |
275 | | uint32_t size; |
276 | | } avifSampleTableSampleSize; |
277 | | AVIF_ARRAY_DECLARE(avifSampleTableSampleSizeArray, avifSampleTableSampleSize, sampleSize); |
278 | | |
279 | | typedef struct avifSampleTableTimeToSample |
280 | | { |
281 | | uint32_t sampleCount; |
282 | | uint32_t sampleDelta; |
283 | | } avifSampleTableTimeToSample; |
284 | | AVIF_ARRAY_DECLARE(avifSampleTableTimeToSampleArray, avifSampleTableTimeToSample, timeToSample); |
285 | | |
286 | | typedef struct avifSyncSample |
287 | | { |
288 | | uint32_t sampleNumber; |
289 | | } avifSyncSample; |
290 | | AVIF_ARRAY_DECLARE(avifSyncSampleArray, avifSyncSample, syncSample); |
291 | | |
292 | | typedef struct avifSampleDescription |
293 | | { |
294 | | uint8_t format[4]; |
295 | | avifPropertyArray properties; |
296 | | } avifSampleDescription; |
297 | | AVIF_ARRAY_DECLARE(avifSampleDescriptionArray, avifSampleDescription, description); |
298 | | |
299 | | typedef struct avifSampleTable |
300 | | { |
301 | | avifSampleTableChunkArray chunks; |
302 | | avifSampleDescriptionArray sampleDescriptions; |
303 | | avifSampleTableSampleToChunkArray sampleToChunks; |
304 | | avifSampleTableSampleSizeArray sampleSizes; |
305 | | avifSampleTableTimeToSampleArray timeToSamples; |
306 | | avifSyncSampleArray syncSamples; |
307 | | uint32_t allSamplesSize; // If this is non-zero, sampleSizes will be empty and all samples will be this size |
308 | | } avifSampleTable; |
309 | | |
310 | | static void avifSampleTableDestroy(avifSampleTable * sampleTable); |
311 | | |
312 | | static avifSampleTable * avifSampleTableCreate(void) |
313 | 606 | { |
314 | 606 | avifSampleTable * sampleTable = (avifSampleTable *)avifAlloc(sizeof(avifSampleTable)); |
315 | 606 | if (sampleTable == NULL) { |
316 | 0 | return NULL; |
317 | 0 | } |
318 | 606 | memset(sampleTable, 0, sizeof(avifSampleTable)); |
319 | 606 | if (!avifArrayCreate(&sampleTable->chunks, sizeof(avifSampleTableChunk), 16) || |
320 | 606 | !avifArrayCreate(&sampleTable->sampleDescriptions, sizeof(avifSampleDescription), 2) || |
321 | 606 | !avifArrayCreate(&sampleTable->sampleToChunks, sizeof(avifSampleTableSampleToChunk), 16) || |
322 | 606 | !avifArrayCreate(&sampleTable->sampleSizes, sizeof(avifSampleTableSampleSize), 16) || |
323 | 606 | !avifArrayCreate(&sampleTable->timeToSamples, sizeof(avifSampleTableTimeToSample), 16) || |
324 | 606 | !avifArrayCreate(&sampleTable->syncSamples, sizeof(avifSyncSample), 16)) { |
325 | 0 | avifSampleTableDestroy(sampleTable); |
326 | 0 | return NULL; |
327 | 0 | } |
328 | 606 | return sampleTable; |
329 | 606 | } |
330 | | |
331 | | static void avifPropertyArrayDestroy(avifPropertyArray * array) |
332 | 42.1k | { |
333 | 180k | for (size_t i = 0; i < array->count; ++i) { |
334 | 138k | if (array->prop[i].isOpaque) { |
335 | 40.0k | avifRWDataFree(&array->prop[i].u.opaque.boxPayload); |
336 | 40.0k | } |
337 | 138k | } |
338 | 42.1k | avifArrayDestroy(array); |
339 | 42.1k | } |
340 | | |
341 | | static void avifSampleTableDestroy(avifSampleTable * sampleTable) |
342 | 606 | { |
343 | 606 | avifArrayDestroy(&sampleTable->chunks); |
344 | 1.04k | for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) { |
345 | 443 | avifSampleDescription * description = &sampleTable->sampleDescriptions.description[i]; |
346 | 443 | avifPropertyArrayDestroy(&description->properties); |
347 | 443 | } |
348 | 606 | avifArrayDestroy(&sampleTable->sampleDescriptions); |
349 | 606 | avifArrayDestroy(&sampleTable->sampleToChunks); |
350 | 606 | avifArrayDestroy(&sampleTable->sampleSizes); |
351 | 606 | avifArrayDestroy(&sampleTable->timeToSamples); |
352 | 606 | avifArrayDestroy(&sampleTable->syncSamples); |
353 | 606 | avifFree(sampleTable); |
354 | 606 | } |
355 | | |
356 | | static uint32_t avifSampleTableGetImageDelta(const avifSampleTable * sampleTable, uint32_t imageIndex) |
357 | 340 | { |
358 | 340 | uint32_t maxSampleIndex = 0; |
359 | 340 | for (uint32_t i = 0; i < sampleTable->timeToSamples.count; ++i) { |
360 | 315 | const avifSampleTableTimeToSample * timeToSample = &sampleTable->timeToSamples.timeToSample[i]; |
361 | 315 | maxSampleIndex += timeToSample->sampleCount; |
362 | 315 | if ((imageIndex < maxSampleIndex) || (i == (sampleTable->timeToSamples.count - 1))) { |
363 | 315 | return timeToSample->sampleDelta; |
364 | 315 | } |
365 | 315 | } |
366 | | |
367 | | // TODO: fail here? |
368 | 25 | return 1; |
369 | 340 | } |
370 | | |
371 | | static avifCodecType avifSampleTableGetCodecType(const avifSampleTable * sampleTable) |
372 | 637 | { |
373 | 650 | for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) { |
374 | 638 | const avifCodecType codecType = avifGetCodecType(sampleTable->sampleDescriptions.description[i].format); |
375 | 638 | if (codecType != AVIF_CODEC_TYPE_UNKNOWN) { |
376 | 625 | return codecType; |
377 | 625 | } |
378 | 638 | } |
379 | 12 | return AVIF_CODEC_TYPE_UNKNOWN; |
380 | 637 | } |
381 | | |
382 | | static uint32_t avifCodecConfigurationBoxGetDepth(const avifCodecConfigurationBox * av1C) |
383 | 15.5k | { |
384 | 15.5k | if (av1C->twelveBit) { |
385 | 2.72k | return 12; |
386 | 12.8k | } else if (av1C->highBitdepth) { |
387 | 4.33k | return 10; |
388 | 4.33k | } |
389 | 8.54k | return 8; |
390 | 15.5k | } |
391 | | |
392 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_EXTENDED_PIXI) |
393 | | uint8_t avifCodecConfigurationBoxGetSubsamplingType(const avifCodecConfigurationBox * av1C, uint8_t channelIndex) |
394 | | { |
395 | | if (channelIndex == 0) { |
396 | | return AVIF_PIXI_444; |
397 | | } |
398 | | if (av1C->chromaSubsamplingX == 0) { |
399 | | if (av1C->chromaSubsamplingY == 0) { |
400 | | return AVIF_PIXI_444; |
401 | | } |
402 | | return AVIF_PIXI_440; |
403 | | } |
404 | | if (av1C->chromaSubsamplingY == 0) { |
405 | | return AVIF_PIXI_422; |
406 | | } |
407 | | return AVIF_PIXI_420; |
408 | | } |
409 | | |
410 | | // Mapping from PixelInformationBox subsampling_type and subsampling_location as defined in ISO/IEC 23008-12:2024/CDAM 2:2025 section 6.5.6.3 |
411 | | // to chroma_sample_position as defined in AV1 specification Section 6.4.2. |
412 | | static uint8_t avifSubsamplingLocationToChromaSamplePosition(uint8_t subsamplingType, uint8_t subsamplingLocation) |
413 | | { |
414 | | if (subsamplingType == AVIF_PIXI_444) { |
415 | | return AVIF_CHROMA_SAMPLE_POSITION_COLOCATED; |
416 | | } |
417 | | if (subsamplingType == AVIF_PIXI_422) { |
418 | | if (subsamplingLocation == 0 || subsamplingLocation == 2 || subsamplingLocation == 4) { |
419 | | return AVIF_CHROMA_SAMPLE_POSITION_COLOCATED; |
420 | | } |
421 | | } |
422 | | if (subsamplingType == AVIF_PIXI_420) { |
423 | | if (subsamplingLocation == 0) { |
424 | | return AVIF_CHROMA_SAMPLE_POSITION_VERTICAL; |
425 | | } |
426 | | if (subsamplingLocation == 2) { |
427 | | return AVIF_CHROMA_SAMPLE_POSITION_COLOCATED; |
428 | | } |
429 | | } |
430 | | if (subsamplingType == AVIF_PIXI_411) { |
431 | | if (subsamplingLocation == 0 || subsamplingLocation == 2 || subsamplingLocation == 4) { |
432 | | return AVIF_CHROMA_SAMPLE_POSITION_COLOCATED; |
433 | | } |
434 | | } |
435 | | if (subsamplingType == AVIF_PIXI_440) { |
436 | | if (subsamplingLocation == 0 || subsamplingLocation == 1) { |
437 | | return AVIF_CHROMA_SAMPLE_POSITION_VERTICAL; |
438 | | } |
439 | | if (subsamplingLocation == 2 || subsamplingLocation == 3) { |
440 | | return AVIF_CHROMA_SAMPLE_POSITION_COLOCATED; |
441 | | } |
442 | | } |
443 | | return AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN; |
444 | | } |
445 | | #endif // AVIF_ENABLE_EXPERIMENTAL_EXTENDED_PIXI |
446 | | |
447 | | static const avifPropertyArray * avifSampleTableGetProperties(const avifSampleTable * sampleTable, avifCodecType codecType) |
448 | 625 | { |
449 | 625 | for (uint32_t i = 0; i < sampleTable->sampleDescriptions.count; ++i) { |
450 | 625 | const avifSampleDescription * description = &sampleTable->sampleDescriptions.description[i]; |
451 | 625 | if (avifGetCodecType(description->format) == codecType) { |
452 | 625 | return &description->properties; |
453 | 625 | } |
454 | 625 | } |
455 | 0 | return NULL; |
456 | 625 | } |
457 | | |
458 | | // one video track ("trak" contents) |
459 | | typedef struct avifTrack |
460 | | { |
461 | | uint32_t id; |
462 | | uint8_t handlerType[4]; |
463 | | uint32_t auxForID; // if non-zero, this track is an auxC plane for Track #{auxForID} |
464 | | uint32_t premByID; // if non-zero, this track is premultiplied by Track #{premByID} |
465 | | uint32_t mediaTimescale; |
466 | | uint64_t mediaDuration; |
467 | | uint64_t trackDuration; |
468 | | uint64_t segmentDuration; |
469 | | avifBool isRepeating; |
470 | | int repetitionCount; |
471 | | uint32_t width; |
472 | | uint32_t height; |
473 | | avifSampleTable * sampleTable; |
474 | | struct avifMeta * meta; |
475 | | } avifTrack; |
476 | | AVIF_ARRAY_DECLARE(avifTrackArray, avifTrack, track); |
477 | | |
478 | | // --------------------------------------------------------------------------- |
479 | | // avifCodecDecodeInput |
480 | | |
481 | | avifCodecDecodeInput * avifCodecDecodeInputCreate(void) |
482 | 15.5k | { |
483 | 15.5k | avifCodecDecodeInput * decodeInput = (avifCodecDecodeInput *)avifAlloc(sizeof(avifCodecDecodeInput)); |
484 | 15.5k | if (decodeInput == NULL) { |
485 | 0 | return NULL; |
486 | 0 | } |
487 | 15.5k | memset(decodeInput, 0, sizeof(avifCodecDecodeInput)); |
488 | 15.5k | if (!avifArrayCreate(&decodeInput->samples, sizeof(avifDecodeSample), 1)) { |
489 | 0 | avifFree(decodeInput); |
490 | 0 | return NULL; |
491 | 0 | } |
492 | 15.5k | return decodeInput; |
493 | 15.5k | } |
494 | | |
495 | | void avifCodecDecodeInputDestroy(avifCodecDecodeInput * decodeInput) |
496 | 15.5k | { |
497 | 36.0k | for (uint32_t sampleIndex = 0; sampleIndex < decodeInput->samples.count; ++sampleIndex) { |
498 | 20.4k | avifDecodeSample * sample = &decodeInput->samples.sample[sampleIndex]; |
499 | 20.4k | if (sample->ownsData) { |
500 | 0 | avifRWDataFree((avifRWData *)&sample->data); |
501 | 0 | } |
502 | 20.4k | } |
503 | 15.5k | avifArrayDestroy(&decodeInput->samples); |
504 | 15.5k | avifFree(decodeInput); |
505 | 15.5k | } |
506 | | |
507 | | // Returns how many samples are in the chunk. |
508 | | static uint32_t avifGetSampleCountOfChunk(const avifSampleTableSampleToChunkArray * sampleToChunks, uint32_t chunkIndex) |
509 | 694 | { |
510 | 694 | uint32_t sampleCount = 0; |
511 | 889 | for (int sampleToChunkIndex = sampleToChunks->count - 1; sampleToChunkIndex >= 0; --sampleToChunkIndex) { |
512 | 887 | const avifSampleTableSampleToChunk * sampleToChunk = &sampleToChunks->sampleToChunk[sampleToChunkIndex]; |
513 | 887 | if (sampleToChunk->firstChunk <= (chunkIndex + 1)) { |
514 | 692 | sampleCount = sampleToChunk->samplesPerChunk; |
515 | 692 | break; |
516 | 692 | } |
517 | 887 | } |
518 | 694 | return sampleCount; |
519 | 694 | } |
520 | | |
521 | | static avifResult avifCodecDecodeInputFillFromSampleTable(avifCodecDecodeInput * decodeInput, |
522 | | avifSampleTable * sampleTable, |
523 | | const uint32_t imageCountLimit, |
524 | | const uint64_t sizeHint, |
525 | | avifDiagnostics * diag) |
526 | 312 | { |
527 | 312 | if (imageCountLimit) { |
528 | | // Verify that the we're not about to exceed the frame count limit. |
529 | | |
530 | 312 | uint32_t imageCountLeft = imageCountLimit; |
531 | 665 | for (uint32_t chunkIndex = 0; chunkIndex < sampleTable->chunks.count; ++chunkIndex) { |
532 | | // First, figure out how many samples are in this chunk |
533 | 366 | uint32_t sampleCount = avifGetSampleCountOfChunk(&sampleTable->sampleToChunks, chunkIndex); |
534 | 366 | if (sampleCount == 0) { |
535 | | // chunks with 0 samples are invalid |
536 | 3 | avifDiagnosticsPrintf(diag, "Sample table contains a chunk with 0 samples"); |
537 | 3 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
538 | 3 | } |
539 | | |
540 | 363 | if (sampleCount > imageCountLeft) { |
541 | | // This file exceeds the imageCountLimit, bail out |
542 | 10 | avifDiagnosticsPrintf(diag, "Exceeded avifDecoder's imageCountLimit"); |
543 | 10 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
544 | 10 | } |
545 | 353 | imageCountLeft -= sampleCount; |
546 | 353 | } |
547 | 312 | } |
548 | | |
549 | 299 | uint32_t sampleSizeIndex = 0; |
550 | 587 | for (uint32_t chunkIndex = 0; chunkIndex < sampleTable->chunks.count; ++chunkIndex) { |
551 | 328 | avifSampleTableChunk * chunk = &sampleTable->chunks.chunk[chunkIndex]; |
552 | | |
553 | | // First, figure out how many samples are in this chunk |
554 | 328 | uint32_t sampleCount = avifGetSampleCountOfChunk(&sampleTable->sampleToChunks, chunkIndex); |
555 | 328 | if (sampleCount == 0) { |
556 | | // chunks with 0 samples are invalid |
557 | 0 | avifDiagnosticsPrintf(diag, "Sample table contains a chunk with 0 samples"); |
558 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
559 | 0 | } |
560 | | |
561 | 328 | uint64_t sampleOffset = chunk->offset; |
562 | 5.64k | for (uint32_t sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex) { |
563 | 5.35k | uint32_t sampleSize = sampleTable->allSamplesSize; |
564 | 5.35k | if (sampleSize == 0) { |
565 | 3.47k | if (sampleSizeIndex >= sampleTable->sampleSizes.count) { |
566 | | // We've run out of samples to sum |
567 | 2 | avifDiagnosticsPrintf(diag, "Truncated sample table"); |
568 | 2 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
569 | 2 | } |
570 | 3.47k | avifSampleTableSampleSize * sampleSizePtr = &sampleTable->sampleSizes.sampleSize[sampleSizeIndex]; |
571 | 3.47k | sampleSize = sampleSizePtr->size; |
572 | 3.47k | } |
573 | | |
574 | 5.35k | avifDecodeSample * sample = (avifDecodeSample *)avifArrayPush(&decodeInput->samples); |
575 | 5.35k | AVIF_CHECKERR(sample != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
576 | 5.35k | sample->offset = sampleOffset; |
577 | 5.35k | sample->size = sampleSize; |
578 | 5.35k | sample->spatialID = AVIF_SPATIAL_ID_UNSET; // Not filtering by spatial_id |
579 | 5.35k | sample->sync = AVIF_FALSE; // to potentially be set to true following the outer loop |
580 | | |
581 | 5.35k | if (sampleSize > UINT64_MAX - sampleOffset) { |
582 | 0 | avifDiagnosticsPrintf(diag, |
583 | 0 | "Sample table contains an offset/size pair which overflows: [%" PRIu64 " / %u]", |
584 | 0 | sampleOffset, |
585 | 0 | sampleSize); |
586 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
587 | 0 | } |
588 | 5.35k | if (sizeHint && ((sampleOffset + sampleSize) > sizeHint)) { |
589 | 38 | avifDiagnosticsPrintf(diag, "Exceeded avifIO's sizeHint, possibly truncated data"); |
590 | 38 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
591 | 38 | } |
592 | | |
593 | 5.31k | sampleOffset += sampleSize; |
594 | 5.31k | ++sampleSizeIndex; |
595 | 5.31k | } |
596 | 328 | } |
597 | | |
598 | | // Mark appropriate samples as sync |
599 | 756 | for (uint32_t syncSampleIndex = 0; syncSampleIndex < sampleTable->syncSamples.count; ++syncSampleIndex) { |
600 | 497 | uint32_t frameIndex = sampleTable->syncSamples.syncSample[syncSampleIndex].sampleNumber - 1; // sampleNumber is 1-based |
601 | 497 | if (frameIndex < decodeInput->samples.count) { |
602 | 480 | decodeInput->samples.sample[frameIndex].sync = AVIF_TRUE; |
603 | 480 | } |
604 | 497 | } |
605 | | |
606 | | // Assume frame 0 is sync, just in case the stss box is absent in the BMFF. (Unnecessary?) |
607 | 259 | if (decodeInput->samples.count > 0) { |
608 | 259 | decodeInput->samples.sample[0].sync = AVIF_TRUE; |
609 | 259 | } |
610 | 259 | return AVIF_RESULT_OK; |
611 | 299 | } |
612 | | |
613 | | static avifResult avifCodecDecodeInputFillFromDecoderItem(avifCodecDecodeInput * decodeInput, |
614 | | avifDecoderItem * item, |
615 | | avifBool allowProgressive, |
616 | | const uint32_t imageCountLimit, |
617 | | const uint64_t sizeHint, |
618 | | avifDiagnostics * diag) |
619 | 15.2k | { |
620 | 15.2k | if (sizeHint && (item->size > sizeHint)) { |
621 | 162 | avifDiagnosticsPrintf(diag, "Exceeded avifIO's sizeHint, possibly truncated data"); |
622 | 162 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
623 | 162 | } |
624 | | |
625 | 15.1k | uint8_t layerCount = 0; |
626 | 15.1k | size_t layerSizes[4] = { 0 }; |
627 | 15.1k | const avifProperty * a1lxProp = avifPropertyArrayFind(&item->properties, "a1lx"); |
628 | 15.1k | if (a1lxProp) { |
629 | | // Calculate layer count and all layer sizes from the a1lx box, and then validate |
630 | | |
631 | 57 | size_t remainingSize = item->size; |
632 | 141 | for (int i = 0; i < 3; ++i) { |
633 | 117 | ++layerCount; |
634 | | |
635 | 117 | const size_t layerSize = (size_t)a1lxProp->u.a1lx.layerSize[i]; |
636 | 117 | if (layerSize) { |
637 | 87 | if (layerSize >= remainingSize) { // >= instead of > because there must be room for the last layer |
638 | 3 | avifDiagnosticsPrintf(diag, "a1lx layer index [%d] does not fit in item size", i); |
639 | 3 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
640 | 3 | } |
641 | 84 | layerSizes[i] = layerSize; |
642 | 84 | remainingSize -= layerSize; |
643 | 84 | } else { |
644 | 30 | layerSizes[i] = remainingSize; |
645 | 30 | remainingSize = 0; |
646 | 30 | break; |
647 | 30 | } |
648 | 117 | } |
649 | 54 | if (remainingSize > 0) { |
650 | 24 | AVIF_ASSERT_OR_RETURN(layerCount == 3); |
651 | 24 | ++layerCount; |
652 | 24 | layerSizes[3] = remainingSize; |
653 | 24 | } |
654 | 54 | } |
655 | | |
656 | 15.1k | const avifProperty * lselProp = avifPropertyArrayFind(&item->properties, "lsel"); |
657 | | // Progressive images offer layers via the a1lxProp, but don't specify a layer selection with lsel. |
658 | | // |
659 | | // For backward compatibility with earlier drafts of AVIF spec v1.1.0, treat an absent lsel as |
660 | | // equivalent to layer_id == 0xFFFF during the transitional period. Remove !lselProp when the test |
661 | | // images have been updated to the v1.1.0 spec. |
662 | 15.1k | item->progressive = (a1lxProp && (!lselProp || (lselProp->u.lsel.layerID == 0xFFFF))); |
663 | 15.1k | if (lselProp && (lselProp->u.lsel.layerID != 0xFFFF)) { |
664 | | // Layer selection. This requires that the underlying AV1 codec decodes all layers, |
665 | | // and then only returns the requested layer as a single frame. To the user of libavif, |
666 | | // this appears to be a single frame. |
667 | | |
668 | 97 | decodeInput->allLayers = AVIF_TRUE; |
669 | | |
670 | 97 | size_t sampleSize = 0; |
671 | 97 | if (layerCount > 0) { |
672 | | // Optimization: If we're selecting a layer that doesn't require the entire image's payload (hinted via the a1lx box) |
673 | | |
674 | 22 | if (lselProp->u.lsel.layerID >= layerCount) { |
675 | 1 | avifDiagnosticsPrintf(diag, |
676 | 1 | "lsel property requests layer index [%u] which isn't present in a1lx property ([%u] layers)", |
677 | 1 | lselProp->u.lsel.layerID, |
678 | 1 | layerCount); |
679 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
680 | 1 | } |
681 | | |
682 | 83 | for (uint8_t i = 0; i <= lselProp->u.lsel.layerID; ++i) { |
683 | 62 | sampleSize += layerSizes[i]; |
684 | 62 | } |
685 | 75 | } else { |
686 | | // This layer's payload subsection is unknown, just use the whole payload |
687 | 75 | sampleSize = item->size; |
688 | 75 | } |
689 | | |
690 | 96 | avifDecodeSample * sample = (avifDecodeSample *)avifArrayPush(&decodeInput->samples); |
691 | 96 | AVIF_CHECKERR(sample != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
692 | 96 | sample->itemID = item->id; |
693 | 96 | sample->offset = 0; |
694 | 96 | sample->size = sampleSize; |
695 | 96 | AVIF_ASSERT_OR_RETURN(lselProp->u.lsel.layerID < AVIF_MAX_AV1_LAYER_COUNT); |
696 | 96 | sample->spatialID = (uint8_t)lselProp->u.lsel.layerID; |
697 | 96 | sample->sync = AVIF_TRUE; |
698 | 15.0k | } else if (allowProgressive && item->progressive) { |
699 | | // Progressive image. Decode all layers and expose them all to the user. |
700 | |
|
701 | 0 | if (imageCountLimit && (layerCount > imageCountLimit)) { |
702 | 0 | avifDiagnosticsPrintf(diag, "Exceeded avifDecoder's imageCountLimit (progressive)"); |
703 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
704 | 0 | } |
705 | | |
706 | 0 | decodeInput->allLayers = AVIF_TRUE; |
707 | |
|
708 | 0 | size_t offset = 0; |
709 | 0 | for (int i = 0; i < layerCount; ++i) { |
710 | 0 | avifDecodeSample * sample = (avifDecodeSample *)avifArrayPush(&decodeInput->samples); |
711 | 0 | AVIF_CHECKERR(sample != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
712 | 0 | sample->itemID = item->id; |
713 | 0 | sample->offset = offset; |
714 | 0 | sample->size = layerSizes[i]; |
715 | 0 | sample->spatialID = AVIF_SPATIAL_ID_UNSET; |
716 | 0 | sample->sync = (i == 0); // Assume all layers depend on the first layer |
717 | |
|
718 | 0 | offset += layerSizes[i]; |
719 | 0 | } |
720 | 15.0k | } else { |
721 | | // Typical case: Use the entire item's payload for a single frame output |
722 | | |
723 | 15.0k | avifDecodeSample * sample = (avifDecodeSample *)avifArrayPush(&decodeInput->samples); |
724 | 15.0k | AVIF_CHECKERR(sample != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
725 | 15.0k | sample->itemID = item->id; |
726 | 15.0k | sample->offset = 0; |
727 | 15.0k | sample->size = item->size; |
728 | 15.0k | sample->spatialID = AVIF_SPATIAL_ID_UNSET; |
729 | 15.0k | sample->sync = AVIF_TRUE; |
730 | 15.0k | } |
731 | 15.1k | return AVIF_RESULT_OK; |
732 | 15.1k | } |
733 | | |
734 | | // --------------------------------------------------------------------------- |
735 | | // Helper macros / functions |
736 | | |
737 | | #define BEGIN_STREAM(VARNAME, PTR, SIZE, DIAG, CONTEXT) \ |
738 | 313k | avifROStream VARNAME; \ |
739 | 313k | avifROData VARNAME##_roData; \ |
740 | 313k | VARNAME##_roData.data = PTR; \ |
741 | 313k | VARNAME##_roData.size = SIZE; \ |
742 | 313k | avifROStreamStart(&VARNAME, &VARNAME##_roData, DIAG, CONTEXT) |
743 | | |
744 | | typedef enum avifUniqueBoxFlag |
745 | | { |
746 | | AVIF_UNIQUE_ILOC = 0, |
747 | | AVIF_UNIQUE_PITM, |
748 | | AVIF_UNIQUE_IDAT, |
749 | | AVIF_UNIQUE_IPRP, |
750 | | AVIF_UNIQUE_IINF, |
751 | | AVIF_UNIQUE_IREF, |
752 | | AVIF_UNIQUE_GRPL, |
753 | | } avifUniqueBoxFlag; |
754 | | // Use this to keep track of whether or not a child box that must be unique (0 or 1 present) has |
755 | | // been seen yet, when parsing a parent box. If the "seen" bit is already set for a given box when |
756 | | // it is encountered during parse, an error is thrown. Which bit corresponds to which box is |
757 | | // dictated entirely by the calling function. |
758 | | static avifBool uniqueBoxSeen(uint32_t * uniqueBoxFlags, |
759 | | avifUniqueBoxFlag whichFlag, |
760 | | const char * parentBoxType, |
761 | | const char * boxType, |
762 | | avifDiagnostics * diagnostics) |
763 | 61.0k | { |
764 | 61.0k | const uint32_t flag = 1 << whichFlag; |
765 | 61.0k | if (*uniqueBoxFlags & flag) { |
766 | | // This box has already been seen. Error! |
767 | 7 | avifDiagnosticsPrintf(diagnostics, "Box[%s] contains a duplicate unique box of type '%s'", parentBoxType, boxType); |
768 | 7 | return AVIF_FALSE; |
769 | 7 | } |
770 | | |
771 | | // Mark this box as seen. |
772 | 60.9k | *uniqueBoxFlags |= flag; |
773 | 60.9k | return AVIF_TRUE; |
774 | 61.0k | } |
775 | | |
776 | | // --------------------------------------------------------------------------- |
777 | | // avifDecoderData |
778 | | |
779 | | typedef struct avifTile |
780 | | { |
781 | | avifCodecDecodeInput * input; |
782 | | avifCodecType codecType; |
783 | | // This may point to a codec that it owns or point to a shared codec that it does not own. In the shared case, this will |
784 | | // point to one of the avifCodec instances in avifDecoderData. |
785 | | struct avifCodec * codec; |
786 | | avifImage * image; |
787 | | uint32_t width; // Either avifTrack.width or avifDecoderItem.width |
788 | | uint32_t height; // Either avifTrack.height or avifDecoderItem.height |
789 | | uint8_t operatingPoint; |
790 | | } avifTile; |
791 | | AVIF_ARRAY_DECLARE(avifTileArray, avifTile, tile); |
792 | | |
793 | | // This holds one "meta" box (from the BMFF and HEIF standards) worth of relevant-to-AVIF information. |
794 | | // * If a meta box is parsed from the root level of the BMFF, it can contain the information about |
795 | | // "items" which might be color planes, alpha planes, or EXIF or XMP metadata. |
796 | | // * If a meta box is parsed from inside of a track ("trak") box, any metadata (EXIF/XMP) items inside |
797 | | // of that box are implicitly associated with that track. |
798 | | typedef struct avifMeta |
799 | | { |
800 | | // Items (from HEIF) are the generic storage for any data that does not require timed processing |
801 | | // (single image color planes, alpha planes, EXIF, XMP, etc). Each item has a unique integer ID >1, |
802 | | // and is defined by a series of child boxes in a meta box: |
803 | | // * iloc - location: byte offset to item data, item size in bytes |
804 | | // * iinf - information: type of item (color planes, alpha plane, EXIF, XMP) |
805 | | // * ipco - properties: dimensions, aspect ratio, image transformations, references to other items |
806 | | // * ipma - associations: Attaches an item in the properties list to a given item |
807 | | // |
808 | | // Items are lazily created in this array when any of the above boxes refer to one by a new (unseen) ID, |
809 | | // and are then further modified/updated as new information for an item's ID is parsed. |
810 | | avifDecoderItemArray items; |
811 | | |
812 | | // Any ipco boxes explained above are populated into this array as a staging area, which are |
813 | | // then duplicated into the appropriate items upon encountering an item property association |
814 | | // (ipma) box. |
815 | | avifPropertyArray properties; |
816 | | |
817 | | // Filled with the contents of this meta box's "idat" box, which is raw data that an item can |
818 | | // directly refer to in its item location box (iloc) instead of just giving an offset into the |
819 | | // overall file. If all items' iloc boxes simply point at an offset/length in the file itself, |
820 | | // this buffer will likely be empty. |
821 | | avifRWData idat; |
822 | | |
823 | | // Ever-incrementing ID for uniquely identifying which 'meta' box contains an idat (when |
824 | | // multiple meta boxes exist as BMFF siblings). Each time avifParseMetaBox() is called on an |
825 | | // avifMeta struct, this value is incremented. Any time an additional meta box is detected at |
826 | | // the same "level" (root level, trak level, etc), this ID helps distinguish which meta box's |
827 | | // "idat" is which, as items implicitly reference idat boxes that exist in the same meta |
828 | | // box. |
829 | | uint32_t idatID; |
830 | | |
831 | | // Contents of a pitm box, which signal which of the items in this file is the main image. For |
832 | | // AVIF, this should point at an image item containing color planes, and all other items |
833 | | // are ignored unless they refer to this item in some way (alpha plane, EXIF/XMP metadata). |
834 | | uint32_t primaryItemID; |
835 | | |
836 | | // Contents of grpl box, which signal groups of entities (items or tracks). |
837 | | avifEntityToGroups entityToGroups; |
838 | | |
839 | | // Parsed from Sample Transform metadata if present, otherwise empty. |
840 | | avifSampleTransformExpression sampleTransformExpression; |
841 | | // Bit depth extracted from the pixi property of the Sample Transform derived image item, if any. |
842 | | uint32_t sampleTransformDepth; |
843 | | |
844 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
845 | | // If true, the fields above were extracted from a MinimizedImageBox. |
846 | | avifBool fromMiniBox; |
847 | | #endif |
848 | | } avifMeta; |
849 | | |
850 | | static void avifMetaDestroy(avifMeta * meta); |
851 | | |
852 | | static avifMeta * avifMetaCreate(void) |
853 | 17.6k | { |
854 | 17.6k | avifMeta * meta = (avifMeta *)avifAlloc(sizeof(avifMeta)); |
855 | 17.6k | if (meta == NULL) { |
856 | 0 | return NULL; |
857 | 0 | } |
858 | 17.6k | memset(meta, 0, sizeof(avifMeta)); |
859 | 17.6k | if (!avifArrayCreate(&meta->items, sizeof(avifDecoderItem *), 8) || !avifArrayCreate(&meta->properties, sizeof(avifProperty), 16) || |
860 | 17.6k | !avifArrayCreate(&meta->entityToGroups, sizeof(avifEntityToGroup), 1)) { |
861 | 0 | avifMetaDestroy(meta); |
862 | 0 | return NULL; |
863 | 0 | } |
864 | 17.6k | return meta; |
865 | 17.6k | } |
866 | | |
867 | | static void avifMetaDestroy(avifMeta * meta) |
868 | 17.6k | { |
869 | 41.7k | for (uint32_t i = 0; i < meta->items.count; ++i) { |
870 | 24.0k | avifDecoderItem * item = meta->items.item[i]; |
871 | 24.0k | avifPropertyArrayDestroy(&item->properties); |
872 | 24.0k | avifArrayDestroy(&item->extents); |
873 | 24.0k | if (item->ownsMergedExtents) { |
874 | 26 | avifRWDataFree(&item->mergedExtents); |
875 | 26 | } |
876 | 24.0k | avifFree(item); |
877 | 24.0k | } |
878 | 17.6k | avifArrayDestroy(&meta->items); |
879 | 17.6k | avifPropertyArrayDestroy(&meta->properties); |
880 | 17.6k | avifRWDataFree(&meta->idat); |
881 | 17.6k | avifArrayDestroy(&meta->sampleTransformExpression); |
882 | 17.6k | for (uint32_t i = 0; i < meta->entityToGroups.count; ++i) { |
883 | 46 | avifArrayDestroy(&meta->entityToGroups.groups[i].entityIDs); |
884 | 46 | } |
885 | 17.6k | avifArrayDestroy(&meta->entityToGroups); |
886 | 17.6k | avifFree(meta); |
887 | 17.6k | } |
888 | | |
889 | | static avifResult avifCheckItemID(const char * boxFourcc, uint32_t itemID, avifDiagnostics * diag) |
890 | 75.7k | { |
891 | | // Section 8.11.1.1 of ISO/IEC 14496-12 about MetaBox definition: |
892 | | // The item_ID value of 0 should not be used |
893 | | // Section 8.11.6 of ISO/IEC 14496-12 about ItemInfoEntry syntax and semantics: |
894 | | // item_ID contains either 0 for the primary resource (e.g. the XML contained in an XMLBox) |
895 | | // or the ID of the item for which the following information is defined. |
896 | | // Assuming 'infe' is the only way to properly define an item in AVIF, a compliant item cannot have an ID of zero. |
897 | | // One way to bypass that rule would be to have 'infe' with item_ID being 0, referring to "the primary resource", |
898 | | // and 'pitm' defining "the primary resource" as the item with an item_ID of 0. libavif considers that as invalid. |
899 | 75.7k | if (itemID == 0) { |
900 | 38 | avifDiagnosticsPrintf(diag, "Box[%.4s] has an invalid item ID [%u]", boxFourcc, itemID); |
901 | 38 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
902 | 38 | } |
903 | 75.7k | return AVIF_RESULT_OK; |
904 | 75.7k | } |
905 | | |
906 | | static avifResult avifMetaFindOrCreateItem(avifMeta * meta, uint32_t itemID, avifDecoderItem ** item) |
907 | 103k | { |
908 | 103k | *item = NULL; |
909 | 103k | AVIF_ASSERT_OR_RETURN(itemID != 0); |
910 | | |
911 | 635k | for (uint32_t i = 0; i < meta->items.count; ++i) { |
912 | 611k | if (meta->items.item[i]->id == itemID) { |
913 | 79.0k | *item = meta->items.item[i]; |
914 | 79.0k | return AVIF_RESULT_OK; |
915 | 79.0k | } |
916 | 611k | } |
917 | | |
918 | 24.0k | avifDecoderItem ** itemPtr = (avifDecoderItem **)avifArrayPush(&meta->items); |
919 | 24.0k | AVIF_CHECKERR(itemPtr != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
920 | 24.0k | *item = (avifDecoderItem *)avifAlloc(sizeof(avifDecoderItem)); |
921 | 24.0k | if (*item == NULL) { |
922 | 0 | avifArrayPop(&meta->items); |
923 | 0 | return AVIF_RESULT_OUT_OF_MEMORY; |
924 | 0 | } |
925 | 24.0k | memset(*item, 0, sizeof(avifDecoderItem)); |
926 | | |
927 | 24.0k | *itemPtr = *item; |
928 | 24.0k | if (!avifArrayCreate(&(*item)->properties, sizeof(avifProperty), 16)) { |
929 | 0 | avifFree(*item); |
930 | 0 | *item = NULL; |
931 | 0 | avifArrayPop(&meta->items); |
932 | 0 | return AVIF_RESULT_OUT_OF_MEMORY; |
933 | 0 | } |
934 | 24.0k | if (!avifArrayCreate(&(*item)->extents, sizeof(avifExtent), 1)) { |
935 | 0 | avifPropertyArrayDestroy(&(*item)->properties); |
936 | 0 | avifFree(*item); |
937 | 0 | *item = NULL; |
938 | 0 | avifArrayPop(&meta->items); |
939 | 0 | return AVIF_RESULT_OUT_OF_MEMORY; |
940 | 0 | } |
941 | 24.0k | (*item)->id = itemID; |
942 | 24.0k | (*item)->meta = meta; |
943 | 24.0k | return AVIF_RESULT_OK; |
944 | 24.0k | } |
945 | | |
946 | | // A group of AVIF tiles in an image item, such as a single tile or a grid of multiple tiles. |
947 | | typedef struct avifTileInfo |
948 | | { |
949 | | unsigned int tileCount; |
950 | | unsigned int decodedTileCount; |
951 | | unsigned int firstTileIndex; // Within avifDecoderData.tiles. |
952 | | avifImageGrid grid; |
953 | | } avifTileInfo; |
954 | | |
955 | | typedef struct avifDecoderData |
956 | | { |
957 | | avifMeta * meta; // The root-level meta box |
958 | | avifTrackArray tracks; |
959 | | avifTileArray tiles; |
960 | | avifTileInfo tileInfos[AVIF_ITEM_CATEGORY_COUNT]; |
961 | | avifDecoderSource source; |
962 | | // When decoding AVIF images with grid, use a single decoder instance for all the tiles instead of creating a decoder instance |
963 | | // for each tile. If that is the case, |codec| will be used by all the tiles. |
964 | | // |
965 | | // There are some edge cases where we will still need multiple decoder instances: |
966 | | // * For animated AVIF with alpha, we will need two instances (one for the color planes and one for the alpha plane since they are both |
967 | | // encoded as separate video sequences). In this case, |codec| will be used for the color planes and |codecAlpha| will be |
968 | | // used for the alpha plane. |
969 | | // * For grid images with multiple layers. In this case, each tile will need its own decoder instance since there would be |
970 | | // multiple layers in each tile. In this case, |codec| and |codecAlpha| are not used and each tile will have its own |
971 | | // decoder instance. |
972 | | // * For grid images where the operating points of all the tiles are not the same. In this case, each tile needs its own |
973 | | // decoder instance (same as above). |
974 | | avifCodec * codec; |
975 | | avifCodec * codecAlpha; |
976 | | uint8_t majorBrand[4]; // From the file's ftyp, used by AVIF_DECODER_SOURCE_AUTO |
977 | | avifBrandArray compatibleBrands; // From the file's ftyp |
978 | | avifDiagnostics * diag; // Shallow copy; owned by avifDecoder |
979 | | const avifSampleTable * sourceSampleTable; // NULL unless (source == AVIF_DECODER_SOURCE_TRACKS), owned by an avifTrack |
980 | | avifBool cicpSet; // True if avifDecoder's image has had its CICP set correctly yet. |
981 | | // This allows nclx colr boxes to override AV1 CICP, as specified in the MIAF |
982 | | // standard (ISO/IEC 23000-22:2019), section 7.3.6.4: |
983 | | // The colour information property takes precedence over any colour information |
984 | | // in the image bitstream, i.e. if the property is present, colour information in |
985 | | // the bitstream shall be ignored. |
986 | | |
987 | | // Remember the dimg association order to the Sample Transform derived image item. |
988 | | // Colour items only. The alpha items are implicit. |
989 | | uint8_t sampleTransformNumInputImageItems; // At most AVIF_SAMPLE_TRANSFORM_MAX_NUM_INPUT_IMAGE_ITEMS. |
990 | | avifItemCategory sampleTransformInputImageItems[AVIF_SAMPLE_TRANSFORM_MAX_NUM_INPUT_IMAGE_ITEMS]; |
991 | | } avifDecoderData; |
992 | | |
993 | | static void avifDecoderDataDestroy(avifDecoderData * data); |
994 | | |
995 | | static avifDecoderData * avifDecoderDataCreate(void) |
996 | 16.5k | { |
997 | 16.5k | avifDecoderData * data = (avifDecoderData *)avifAlloc(sizeof(avifDecoderData)); |
998 | 16.5k | if (data == NULL) { |
999 | 0 | return NULL; |
1000 | 0 | } |
1001 | 16.5k | memset(data, 0, sizeof(avifDecoderData)); |
1002 | 16.5k | data->meta = avifMetaCreate(); |
1003 | 16.5k | if (data->meta == NULL || !avifArrayCreate(&data->tracks, sizeof(avifTrack), 2) || |
1004 | 16.5k | !avifArrayCreate(&data->tiles, sizeof(avifTile), 8)) { |
1005 | 0 | avifDecoderDataDestroy(data); |
1006 | 0 | return NULL; |
1007 | 0 | } |
1008 | 16.5k | return data; |
1009 | 16.5k | } |
1010 | | |
1011 | | static void avifDecoderDataResetCodec(avifDecoderData * data) |
1012 | 13.7k | { |
1013 | 28.6k | for (unsigned int i = 0; i < data->tiles.count; ++i) { |
1014 | 14.8k | avifTile * tile = &data->tiles.tile[i]; |
1015 | 14.8k | if (tile->image) { |
1016 | 14.8k | avifImageFreePlanes(tile->image, AVIF_PLANES_ALL); // forget any pointers into codec image buffers |
1017 | 14.8k | } |
1018 | 14.8k | if (tile->codec) { |
1019 | | // Check if tile->codec was created separately and destroy it in that case. |
1020 | 0 | if (tile->codec != data->codec && tile->codec != data->codecAlpha) { |
1021 | 0 | avifCodecDestroy(tile->codec); |
1022 | 0 | } |
1023 | 0 | tile->codec = NULL; |
1024 | 0 | } |
1025 | 14.8k | } |
1026 | 123k | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
1027 | 110k | data->tileInfos[c].decodedTileCount = 0; |
1028 | 110k | } |
1029 | 13.7k | if (data->codec) { |
1030 | 0 | avifCodecDestroy(data->codec); |
1031 | 0 | data->codec = NULL; |
1032 | 0 | } |
1033 | 13.7k | if (data->codecAlpha) { |
1034 | 0 | avifCodecDestroy(data->codecAlpha); |
1035 | 0 | data->codecAlpha = NULL; |
1036 | 0 | } |
1037 | 13.7k | } |
1038 | | |
1039 | | static avifTile * avifDecoderDataCreateTile(avifDecoderData * data, avifCodecType codecType, uint32_t width, uint32_t height, uint8_t operatingPoint) |
1040 | 15.5k | { |
1041 | 15.5k | avifTile * tile = (avifTile *)avifArrayPush(&data->tiles); |
1042 | 15.5k | if (tile == NULL) { |
1043 | 0 | return NULL; |
1044 | 0 | } |
1045 | 15.5k | tile->codecType = codecType; |
1046 | 15.5k | tile->image = avifImageCreateEmpty(); |
1047 | 15.5k | if (!tile->image) { |
1048 | 0 | goto error; |
1049 | 0 | } |
1050 | 15.5k | tile->input = avifCodecDecodeInputCreate(); |
1051 | 15.5k | if (!tile->input) { |
1052 | 0 | goto error; |
1053 | 0 | } |
1054 | 15.5k | tile->width = width; |
1055 | 15.5k | tile->height = height; |
1056 | 15.5k | tile->operatingPoint = operatingPoint; |
1057 | 15.5k | return tile; |
1058 | | |
1059 | 0 | error: |
1060 | 0 | if (tile->input) { |
1061 | 0 | avifCodecDecodeInputDestroy(tile->input); |
1062 | 0 | } |
1063 | 0 | if (tile->image) { |
1064 | 0 | avifImageDestroy(tile->image); |
1065 | 0 | } |
1066 | 0 | avifArrayPop(&data->tiles); |
1067 | 0 | return NULL; |
1068 | 15.5k | } |
1069 | | |
1070 | | static avifTrack * avifDecoderDataCreateTrack(avifDecoderData * data) |
1071 | 1.13k | { |
1072 | 1.13k | avifTrack * track = (avifTrack *)avifArrayPush(&data->tracks); |
1073 | 1.13k | if (track == NULL) { |
1074 | 0 | return NULL; |
1075 | 0 | } |
1076 | 1.13k | track->meta = avifMetaCreate(); |
1077 | 1.13k | if (track->meta == NULL) { |
1078 | 0 | avifArrayPop(&data->tracks); |
1079 | 0 | return NULL; |
1080 | 0 | } |
1081 | 1.13k | return track; |
1082 | 1.13k | } |
1083 | | |
1084 | | static void avifDecoderDataClearTiles(avifDecoderData * data) |
1085 | 31.1k | { |
1086 | 46.7k | for (unsigned int i = 0; i < data->tiles.count; ++i) { |
1087 | 15.5k | avifTile * tile = &data->tiles.tile[i]; |
1088 | 15.5k | if (tile->input) { |
1089 | 15.5k | avifCodecDecodeInputDestroy(tile->input); |
1090 | 15.5k | tile->input = NULL; |
1091 | 15.5k | } |
1092 | 15.5k | if (tile->codec) { |
1093 | | // Check if tile->codec was created separately and destroy it in that case. |
1094 | 14.8k | if (tile->codec != data->codec && tile->codec != data->codecAlpha) { |
1095 | 124 | avifCodecDestroy(tile->codec); |
1096 | 124 | } |
1097 | 14.8k | tile->codec = NULL; |
1098 | 14.8k | } |
1099 | 15.5k | if (tile->image) { |
1100 | 15.5k | avifImageDestroy(tile->image); |
1101 | 15.5k | tile->image = NULL; |
1102 | 15.5k | } |
1103 | 15.5k | } |
1104 | 31.1k | data->tiles.count = 0; |
1105 | 280k | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
1106 | 249k | data->tileInfos[c].tileCount = 0; |
1107 | 249k | data->tileInfos[c].decodedTileCount = 0; |
1108 | 249k | } |
1109 | 31.1k | if (data->codec) { |
1110 | 13.7k | avifCodecDestroy(data->codec); |
1111 | 13.7k | data->codec = NULL; |
1112 | 13.7k | } |
1113 | 31.1k | if (data->codecAlpha) { |
1114 | 0 | avifCodecDestroy(data->codecAlpha); |
1115 | 0 | data->codecAlpha = NULL; |
1116 | 0 | } |
1117 | 31.1k | } |
1118 | | |
1119 | | static void avifDecoderDataDestroy(avifDecoderData * data) |
1120 | 16.5k | { |
1121 | 16.5k | if (data->meta) { |
1122 | 16.5k | avifMetaDestroy(data->meta); |
1123 | 16.5k | } |
1124 | 17.6k | for (uint32_t i = 0; i < data->tracks.count; ++i) { |
1125 | 1.13k | avifTrack * track = &data->tracks.track[i]; |
1126 | 1.13k | if (track->sampleTable) { |
1127 | 606 | avifSampleTableDestroy(track->sampleTable); |
1128 | 606 | } |
1129 | 1.13k | if (track->meta) { |
1130 | 1.13k | avifMetaDestroy(track->meta); |
1131 | 1.13k | } |
1132 | 1.13k | } |
1133 | 16.5k | avifArrayDestroy(&data->tracks); |
1134 | 16.5k | avifDecoderDataClearTiles(data); |
1135 | 16.5k | avifArrayDestroy(&data->tiles); |
1136 | 16.5k | avifArrayDestroy(&data->compatibleBrands); |
1137 | 16.5k | avifFree(data); |
1138 | 16.5k | } |
1139 | | |
1140 | | // This returns the max extent that has to be read in order to decode this item. If |
1141 | | // the item is stored in an idat, the data has already been read during Parse() and |
1142 | | // this function will return AVIF_RESULT_OK with a 0-byte extent. |
1143 | | static avifResult avifDecoderItemMaxExtent(const avifDecoderItem * item, const avifDecodeSample * sample, avifExtent * outExtent) |
1144 | 0 | { |
1145 | 0 | if (item->extents.count == 0) { |
1146 | 0 | return AVIF_RESULT_TRUNCATED_DATA; |
1147 | 0 | } |
1148 | | |
1149 | 0 | if (item->idatStored) { |
1150 | | // construction_method: idat(1) |
1151 | |
|
1152 | 0 | if (item->meta->idat.size > 0) { |
1153 | | // Already read from a meta box during Parse() |
1154 | 0 | memset(outExtent, 0, sizeof(avifExtent)); |
1155 | 0 | return AVIF_RESULT_OK; |
1156 | 0 | } |
1157 | | |
1158 | | // no associated idat box was found in the meta box, bail out |
1159 | 0 | return AVIF_RESULT_NO_CONTENT; |
1160 | 0 | } |
1161 | | |
1162 | | // construction_method: file(0) |
1163 | | |
1164 | 0 | if (sample->size == 0) { |
1165 | 0 | return AVIF_RESULT_TRUNCATED_DATA; |
1166 | 0 | } |
1167 | 0 | uint64_t remainingOffset = sample->offset; |
1168 | 0 | size_t remainingBytes = sample->size; // This may be smaller than item->size if the item is progressive |
1169 | | |
1170 | | // Assert that the for loop below will execute at least one iteration. |
1171 | 0 | AVIF_ASSERT_OR_RETURN(item->extents.count != 0); |
1172 | 0 | uint64_t minOffset = UINT64_MAX; |
1173 | 0 | uint64_t maxOffset = 0; |
1174 | 0 | for (uint32_t extentIter = 0; extentIter < item->extents.count; ++extentIter) { |
1175 | 0 | avifExtent * extent = &item->extents.extent[extentIter]; |
1176 | | |
1177 | | // Make local copies of extent->offset and extent->size as they might need to be adjusted |
1178 | | // due to the sample's offset. |
1179 | 0 | uint64_t startOffset = extent->offset; |
1180 | 0 | size_t extentSize = extent->size; |
1181 | 0 | if (remainingOffset) { |
1182 | 0 | if (remainingOffset >= extentSize) { |
1183 | 0 | remainingOffset -= extentSize; |
1184 | 0 | continue; |
1185 | 0 | } else { |
1186 | 0 | if (remainingOffset > UINT64_MAX - startOffset) { |
1187 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1188 | 0 | } |
1189 | 0 | startOffset += remainingOffset; |
1190 | 0 | extentSize -= (size_t)remainingOffset; |
1191 | 0 | remainingOffset = 0; |
1192 | 0 | } |
1193 | 0 | } |
1194 | | |
1195 | 0 | const size_t usedExtentSize = (extentSize < remainingBytes) ? extentSize : remainingBytes; |
1196 | |
|
1197 | 0 | if (usedExtentSize > UINT64_MAX - startOffset) { |
1198 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1199 | 0 | } |
1200 | 0 | const uint64_t endOffset = startOffset + usedExtentSize; |
1201 | |
|
1202 | 0 | if (minOffset > startOffset) { |
1203 | 0 | minOffset = startOffset; |
1204 | 0 | } |
1205 | 0 | if (maxOffset < endOffset) { |
1206 | 0 | maxOffset = endOffset; |
1207 | 0 | } |
1208 | |
|
1209 | 0 | remainingBytes -= usedExtentSize; |
1210 | 0 | if (remainingBytes == 0) { |
1211 | | // We've got enough bytes for this sample. |
1212 | 0 | break; |
1213 | 0 | } |
1214 | 0 | } |
1215 | | |
1216 | 0 | if (remainingBytes != 0) { |
1217 | 0 | return AVIF_RESULT_TRUNCATED_DATA; |
1218 | 0 | } |
1219 | | |
1220 | 0 | outExtent->offset = minOffset; |
1221 | 0 | const uint64_t extentLength = maxOffset - minOffset; |
1222 | | #if UINT64_MAX > SIZE_MAX |
1223 | | if (extentLength > SIZE_MAX) { |
1224 | | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1225 | | } |
1226 | | #endif |
1227 | 0 | outExtent->size = (size_t)extentLength; |
1228 | 0 | return AVIF_RESULT_OK; |
1229 | 0 | } |
1230 | | |
1231 | | static uint8_t avifDecoderItemOperatingPoint(const avifDecoderItem * item) |
1232 | 15.2k | { |
1233 | 15.2k | const avifProperty * a1opProp = avifPropertyArrayFind(&item->properties, "a1op"); |
1234 | 15.2k | if (a1opProp) { |
1235 | 76 | return a1opProp->u.a1op.opIndex; |
1236 | 76 | } |
1237 | 15.2k | return 0; // default |
1238 | 15.2k | } |
1239 | | |
1240 | | static avifResult avifDecoderItemValidateProperties(const avifDecoderItem * item, |
1241 | | const char * configPropName, |
1242 | | avifDiagnostics * diag, |
1243 | | const avifStrictFlags strictFlags) |
1244 | 13.9k | { |
1245 | 13.9k | const avifProperty * const configProp = avifPropertyArrayFind(&item->properties, configPropName); |
1246 | 13.9k | if (!configProp) { |
1247 | | // An item configuration property box is mandatory in all valid AVIF configurations. Bail out. |
1248 | 7 | avifDiagnosticsPrintf(diag, "Item ID %u of type '%.4s' is missing mandatory %s property", item->id, (const char *)item->type, configPropName); |
1249 | 7 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1250 | 7 | } |
1251 | | |
1252 | 13.9k | if (!memcmp(item->type, "grid", 4)) { |
1253 | 2.72k | for (uint32_t i = 0; i < item->meta->items.count; ++i) { |
1254 | 2.64k | avifDecoderItem * tile = item->meta->items.item[i]; |
1255 | 2.64k | if (tile->dimgForID != item->id) { |
1256 | 1.41k | continue; |
1257 | 1.41k | } |
1258 | | // Tile item types were checked in avifDecoderGenerateImageTiles(), no need to do it here. |
1259 | | |
1260 | | // MIAF (ISO 23000-22:2019), Section 7.3.11.4.1: |
1261 | | // All input images of a grid image item shall use the same [...] chroma sampling format, |
1262 | | // and the same decoder configuration (see 7.3.6.2). |
1263 | | |
1264 | | // The chroma sampling format is part of the decoder configuration. |
1265 | 1.22k | const avifProperty * tileConfigProp = avifPropertyArrayFind(&tile->properties, configPropName); |
1266 | 1.22k | if (!tileConfigProp) { |
1267 | 12 | avifDiagnosticsPrintf(diag, |
1268 | 12 | "Tile item ID %u of type '%.4s' is missing mandatory %s property", |
1269 | 12 | tile->id, |
1270 | 12 | (const char *)tile->type, |
1271 | 12 | configPropName); |
1272 | 12 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1273 | 12 | } |
1274 | | // configProp was copied from a tile item to the grid item. Comparing tileConfigProp with it |
1275 | | // is equivalent to comparing tileConfigProp with the configPropName from the first tile. |
1276 | 1.21k | if ((tileConfigProp->u.av1C.seqProfile != configProp->u.av1C.seqProfile) || |
1277 | 1.21k | (tileConfigProp->u.av1C.seqLevelIdx0 != configProp->u.av1C.seqLevelIdx0) || |
1278 | 1.21k | (tileConfigProp->u.av1C.seqTier0 != configProp->u.av1C.seqTier0) || |
1279 | 1.20k | (tileConfigProp->u.av1C.highBitdepth != configProp->u.av1C.highBitdepth) || |
1280 | 1.20k | (tileConfigProp->u.av1C.twelveBit != configProp->u.av1C.twelveBit) || |
1281 | 1.20k | (tileConfigProp->u.av1C.monochrome != configProp->u.av1C.monochrome) || |
1282 | 1.20k | (tileConfigProp->u.av1C.chromaSubsamplingX != configProp->u.av1C.chromaSubsamplingX) || |
1283 | 1.20k | (tileConfigProp->u.av1C.chromaSubsamplingY != configProp->u.av1C.chromaSubsamplingY) || |
1284 | 1.20k | (tileConfigProp->u.av1C.chromaSamplePosition != configProp->u.av1C.chromaSamplePosition)) { |
1285 | 10 | avifDiagnosticsPrintf(diag, |
1286 | 10 | "The fields of the %s property of tile item ID %u of type '%.4s' differs from other tiles", |
1287 | 10 | configPropName, |
1288 | 10 | tile->id, |
1289 | 10 | (const char *)tile->type); |
1290 | 10 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1291 | 10 | } |
1292 | 1.21k | } |
1293 | 101 | } |
1294 | | |
1295 | 13.9k | const avifProperty * pixiProp = avifPropertyArrayFind(&item->properties, "pixi"); |
1296 | 13.9k | if (!pixiProp && (strictFlags & AVIF_STRICT_PIXI_REQUIRED)) { |
1297 | | // A pixi box is mandatory in all valid AVIF configurations. Bail out. |
1298 | 0 | avifDiagnosticsPrintf(diag, |
1299 | 0 | "[Strict] Item ID %u of type '%.4s' is missing mandatory pixi property", |
1300 | 0 | item->id, |
1301 | 0 | (const char *)item->type); |
1302 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1303 | 0 | } |
1304 | | |
1305 | 13.9k | if (pixiProp) { |
1306 | 1.82k | const uint32_t configDepth = avifCodecConfigurationBoxGetDepth(&configProp->u.av1C); |
1307 | 6.58k | for (uint8_t i = 0; i < pixiProp->u.pixi.planeCount; ++i) { |
1308 | 4.76k | if (pixiProp->u.pixi.planeDepths[i] != configDepth) { |
1309 | | // pixi depth must match configuration property depth |
1310 | 2 | avifDiagnosticsPrintf(diag, |
1311 | 2 | "Item ID %u depth specified by pixi property [%u] does not match %s property depth [%u]", |
1312 | 2 | item->id, |
1313 | 2 | pixiProp->u.pixi.planeDepths[i], |
1314 | 2 | configPropName, |
1315 | 2 | configDepth); |
1316 | 2 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1317 | 2 | } |
1318 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_EXTENDED_PIXI) |
1319 | | if (pixiProp->u.pixi.subsamplingFlag[i]) { |
1320 | | if (pixiProp->u.pixi.subsamplingType[i] != avifCodecConfigurationBoxGetSubsamplingType(&configProp->u.av1C, i)) { |
1321 | | avifDiagnosticsPrintf(diag, |
1322 | | "Item ID %u subsampling type specified by pixi property [%u] for channel %u does not match %s property [%u,%u]", |
1323 | | item->id, |
1324 | | pixiProp->u.pixi.subsamplingType[i], |
1325 | | i, |
1326 | | configPropName, |
1327 | | configProp->u.av1C.chromaSubsamplingX, |
1328 | | configProp->u.av1C.chromaSubsamplingY); |
1329 | | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1330 | | } |
1331 | | if (configProp->u.av1C.chromaSamplePosition != AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN) { |
1332 | | const avifChromaSamplePosition expectedChromaSamplePosition = |
1333 | | i == AVIF_CHAN_Y ? AVIF_CHROMA_SAMPLE_POSITION_COLOCATED : configProp->u.av1C.chromaSamplePosition; |
1334 | | if (avifSubsamplingLocationToChromaSamplePosition(pixiProp->u.pixi.subsamplingType[i], |
1335 | | pixiProp->u.pixi.subsamplingLocation[i]) != |
1336 | | expectedChromaSamplePosition) { |
1337 | | avifDiagnosticsPrintf(diag, |
1338 | | "Item ID %u subsampling type and location specified by pixi property [%u,%u] for channel %u does not match %s property chroma sample position [%u]", |
1339 | | item->id, |
1340 | | pixiProp->u.pixi.subsamplingType[i], |
1341 | | pixiProp->u.pixi.subsamplingLocation[i], |
1342 | | i, |
1343 | | configPropName, |
1344 | | configProp->u.av1C.chromaSamplePosition); |
1345 | | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1346 | | } |
1347 | | } |
1348 | | } |
1349 | | #endif // AVIF_ENABLE_EXPERIMENTAL_EXTENDED_PIXI |
1350 | 4.76k | } |
1351 | 1.82k | } |
1352 | | |
1353 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
1354 | | if (item->miniBoxPixelFormat != AVIF_PIXEL_FORMAT_NONE) { |
1355 | | // This is a MinimizedImageBox ('mini'). |
1356 | | |
1357 | | avifPixelFormat av1CPixelFormat; |
1358 | | if (configProp->u.av1C.monochrome) { |
1359 | | av1CPixelFormat = AVIF_PIXEL_FORMAT_YUV400; |
1360 | | } else if (configProp->u.av1C.chromaSubsamplingY == 1) { |
1361 | | av1CPixelFormat = AVIF_PIXEL_FORMAT_YUV420; |
1362 | | } else if (configProp->u.av1C.chromaSubsamplingX == 1) { |
1363 | | av1CPixelFormat = AVIF_PIXEL_FORMAT_YUV422; |
1364 | | } else { |
1365 | | av1CPixelFormat = AVIF_PIXEL_FORMAT_YUV444; |
1366 | | } |
1367 | | if (item->miniBoxPixelFormat != av1CPixelFormat) { |
1368 | | avifDiagnosticsPrintf(diag, |
1369 | | "Item ID %u format [%s] specified by MinimizedImageBox does not match %s property format [%s]", |
1370 | | item->id, |
1371 | | avifPixelFormatToString(item->miniBoxPixelFormat), |
1372 | | configPropName, |
1373 | | avifPixelFormatToString(av1CPixelFormat)); |
1374 | | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1375 | | } |
1376 | | |
1377 | | if (configProp->u.av1C.chromaSamplePosition == /*CSP_UNKNOWN=*/0) { |
1378 | | // Section 6.4.2. Color config semantics of AV1 specification says: |
1379 | | // CSP_UNKNOWN - the source video transfer function must be signaled outside the AV1 bitstream |
1380 | | // See https://aomediacodec.github.io/av1-spec/#color-config-semantics |
1381 | | |
1382 | | // So item->miniBoxChromaSamplePosition can differ and will override the AV1 value. |
1383 | | } else if ((uint8_t)item->miniBoxChromaSamplePosition != configProp->u.av1C.chromaSamplePosition) { |
1384 | | avifDiagnosticsPrintf(diag, |
1385 | | "Item ID %u chroma sample position [%u] specified by MinimizedImageBox does not match %s property chroma sample position [%u]", |
1386 | | item->id, |
1387 | | (uint32_t)item->miniBoxChromaSamplePosition, |
1388 | | configPropName, |
1389 | | configProp->u.av1C.chromaSamplePosition); |
1390 | | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1391 | | } |
1392 | | } |
1393 | | #endif // AVIF_ENABLE_EXPERIMENTAL_MINI |
1394 | | |
1395 | 13.9k | if (strictFlags & AVIF_STRICT_CLAP_VALID) { |
1396 | 0 | const avifProperty * clapProp = avifPropertyArrayFind(&item->properties, "clap"); |
1397 | 0 | if (clapProp) { |
1398 | 0 | const avifProperty * ispeProp = avifPropertyArrayFind(&item->properties, "ispe"); |
1399 | 0 | if (!ispeProp) { |
1400 | 0 | avifDiagnosticsPrintf(diag, |
1401 | 0 | "[Strict] Item ID %u is missing an ispe property, so its clap property cannot be validated", |
1402 | 0 | item->id); |
1403 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1404 | 0 | } |
1405 | | |
1406 | 0 | avifCropRect cropRect; |
1407 | 0 | const uint32_t imageW = ispeProp->u.ispe.width; |
1408 | 0 | const uint32_t imageH = ispeProp->u.ispe.height; |
1409 | 0 | const avifBool validClap = avifCropRectFromCleanApertureBox(&cropRect, &clapProp->u.clap, imageW, imageH, diag); |
1410 | 0 | if (!validClap) { |
1411 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1412 | 0 | } |
1413 | 0 | } |
1414 | 0 | } |
1415 | 13.9k | return AVIF_RESULT_OK; |
1416 | 13.9k | } |
1417 | | |
1418 | | static avifResult avifDecoderItemRead(avifDecoderItem * item, |
1419 | | avifIO * io, |
1420 | | avifROData * outData, |
1421 | | size_t offset, |
1422 | | size_t partialByteCount, |
1423 | | avifDiagnostics * diag) |
1424 | 33.4k | { |
1425 | 33.4k | if (item->mergedExtents.data && !item->partialMergedExtents) { |
1426 | | // Multiple extents have already been concatenated for this item, just return it |
1427 | 11 | if (offset >= item->mergedExtents.size) { |
1428 | 0 | avifDiagnosticsPrintf(diag, "Item ID %u read has overflowing offset", item->id); |
1429 | 0 | return AVIF_RESULT_TRUNCATED_DATA; |
1430 | 0 | } |
1431 | 11 | outData->data = item->mergedExtents.data + offset; |
1432 | 11 | outData->size = item->mergedExtents.size - offset; |
1433 | 11 | return AVIF_RESULT_OK; |
1434 | 11 | } |
1435 | | |
1436 | 33.4k | if (item->extents.count == 0) { |
1437 | 0 | avifDiagnosticsPrintf(diag, "Item ID %u has zero extents", item->id); |
1438 | 0 | return AVIF_RESULT_TRUNCATED_DATA; |
1439 | 0 | } |
1440 | | |
1441 | | // Find this item's source of all extents' data, based on the construction method |
1442 | 33.4k | const avifRWData * idatBuffer = NULL; |
1443 | 33.4k | if (item->idatStored) { |
1444 | | // construction_method: idat(1) |
1445 | | |
1446 | 96 | if (item->meta->idat.size > 0) { |
1447 | 95 | idatBuffer = &item->meta->idat; |
1448 | 95 | } else { |
1449 | | // no associated idat box was found in the meta box, bail out |
1450 | 1 | avifDiagnosticsPrintf(diag, "Item ID %u is stored in an idat, but no associated idat box was found", item->id); |
1451 | 1 | return AVIF_RESULT_NO_CONTENT; |
1452 | 1 | } |
1453 | 96 | } |
1454 | | |
1455 | | // Merge extents into a single contiguous buffer |
1456 | 33.4k | if ((io->sizeHint > 0) && (item->size > io->sizeHint)) { |
1457 | | // Sanity check: somehow the sum of extents exceeds the entire file or idat size! |
1458 | 24 | avifDiagnosticsPrintf(diag, "Item ID %u reported size failed size hint sanity check. Truncated data?", item->id); |
1459 | 24 | return AVIF_RESULT_TRUNCATED_DATA; |
1460 | 24 | } |
1461 | | |
1462 | 33.4k | if (offset >= item->size) { |
1463 | 0 | avifDiagnosticsPrintf(diag, "Item ID %u read has overflowing offset", item->id); |
1464 | 0 | return AVIF_RESULT_TRUNCATED_DATA; |
1465 | 0 | } |
1466 | 33.4k | const size_t maxOutputSize = item->size - offset; |
1467 | 33.4k | const size_t readOutputSize = (partialByteCount && (partialByteCount < maxOutputSize)) ? partialByteCount : maxOutputSize; |
1468 | 33.4k | const size_t totalBytesToRead = offset + readOutputSize; |
1469 | | |
1470 | | // If there is a single extent for this item and the source of the read buffer is going to be |
1471 | | // persistent for the lifetime of the avifDecoder (whether it comes from its own internal |
1472 | | // idatBuffer or from a known-persistent IO), we can avoid buffer duplication and just use the |
1473 | | // preexisting buffer. |
1474 | 33.4k | avifBool singlePersistentBuffer = ((item->extents.count == 1) && (idatBuffer || io->persistent)); |
1475 | 33.4k | if (!singlePersistentBuffer) { |
1476 | | // Always allocate the item's full size here, as progressive image decodes will do partial |
1477 | | // reads into this buffer and begin feeding the buffer to the underlying AV1 decoder, but |
1478 | | // will then write more into this buffer without flushing the AV1 decoder (which is still |
1479 | | // holding the address of the previous allocation of this buffer). This strategy avoids |
1480 | | // use-after-free issues in the AV1 decoder and unnecessary reallocs as a typical |
1481 | | // progressive decode use case will eventually decode the final layer anyway. |
1482 | 129 | AVIF_CHECKRES(avifRWDataRealloc(&item->mergedExtents, item->size)); |
1483 | 129 | item->ownsMergedExtents = AVIF_TRUE; |
1484 | 129 | } |
1485 | | |
1486 | | // Set this until we manage to fill the entire mergedExtents buffer |
1487 | 33.4k | item->partialMergedExtents = AVIF_TRUE; |
1488 | | |
1489 | 33.4k | size_t writeOffset = 0; // Write offset for item->mergedExtents.data |
1490 | 33.4k | size_t remainingBytes = totalBytesToRead; |
1491 | 33.4k | for (uint32_t extentIter = 0; extentIter < item->extents.count; ++extentIter) { |
1492 | 33.4k | avifExtent * extent = &item->extents.extent[extentIter]; |
1493 | | |
1494 | 33.4k | size_t bytesToRead = extent->size; |
1495 | 33.4k | if (bytesToRead > remainingBytes) { |
1496 | 17.8k | bytesToRead = remainingBytes; |
1497 | 17.8k | } |
1498 | | |
1499 | 33.4k | avifROData offsetBuffer; |
1500 | 33.4k | if (idatBuffer) { |
1501 | 95 | if (extent->offset > idatBuffer->size) { |
1502 | 4 | avifDiagnosticsPrintf(diag, "Item ID %u has impossible extent offset in idat buffer", item->id); |
1503 | 4 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1504 | 4 | } |
1505 | | // Since extent->offset (a uint64_t) is not bigger than idatBuffer->size (a size_t), |
1506 | | // it is safe to cast extent->offset to size_t. |
1507 | 91 | const size_t extentOffset = (size_t)extent->offset; |
1508 | 91 | if (extent->size > idatBuffer->size - extentOffset) { |
1509 | 1 | avifDiagnosticsPrintf(diag, "Item ID %u has impossible extent size in idat buffer", item->id); |
1510 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1511 | 1 | } |
1512 | 90 | offsetBuffer.data = idatBuffer->data + extentOffset; |
1513 | 90 | offsetBuffer.size = idatBuffer->size - extentOffset; |
1514 | 33.3k | } else { |
1515 | | // construction_method: file(0) |
1516 | | |
1517 | 33.3k | if ((io->sizeHint > 0) && (extent->offset > io->sizeHint)) { |
1518 | 43 | avifDiagnosticsPrintf(diag, "Item ID %u extent offset failed size hint sanity check. Truncated data?", item->id); |
1519 | 43 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1520 | 43 | } |
1521 | 33.3k | avifResult readResult = io->read(io, 0, extent->offset, bytesToRead, &offsetBuffer); |
1522 | 33.3k | if (readResult != AVIF_RESULT_OK) { |
1523 | 0 | return readResult; |
1524 | 0 | } |
1525 | 33.3k | if (bytesToRead != offsetBuffer.size) { |
1526 | 298 | avifDiagnosticsPrintf(diag, |
1527 | 298 | "Item ID %u tried to read %zu bytes, but only received %zu bytes", |
1528 | 298 | item->id, |
1529 | 298 | bytesToRead, |
1530 | 298 | offsetBuffer.size); |
1531 | 298 | return AVIF_RESULT_TRUNCATED_DATA; |
1532 | 298 | } |
1533 | 33.3k | } |
1534 | | |
1535 | 33.1k | if (singlePersistentBuffer) { |
1536 | 32.9k | item->mergedExtents.data = (uint8_t *)offsetBuffer.data; // const_cast |
1537 | 32.9k | AVIF_ASSERT_OR_RETURN(bytesToRead <= offsetBuffer.size); |
1538 | 32.9k | item->mergedExtents.size = bytesToRead; |
1539 | 32.9k | } else { |
1540 | 199 | AVIF_ASSERT_OR_RETURN(item->ownsMergedExtents); |
1541 | 199 | AVIF_ASSERT_OR_RETURN(writeOffset < item->mergedExtents.size); |
1542 | 199 | AVIF_ASSERT_OR_RETURN(bytesToRead <= item->mergedExtents.size - writeOffset); |
1543 | 199 | memcpy(item->mergedExtents.data + writeOffset, offsetBuffer.data, bytesToRead); |
1544 | 199 | writeOffset += bytesToRead; |
1545 | 199 | } |
1546 | | |
1547 | 33.1k | remainingBytes -= bytesToRead; |
1548 | 33.1k | if (remainingBytes == 0) { |
1549 | | // This happens when partialByteCount is set |
1550 | 33.0k | break; |
1551 | 33.0k | } |
1552 | 33.1k | } |
1553 | 33.0k | if (remainingBytes != 0) { |
1554 | | // This should be impossible? |
1555 | 0 | avifDiagnosticsPrintf(diag, "Item ID %u has %zu unexpected trailing bytes", item->id, remainingBytes); |
1556 | 0 | return AVIF_RESULT_TRUNCATED_DATA; |
1557 | 0 | } |
1558 | | |
1559 | 33.0k | outData->data = item->mergedExtents.data + offset; |
1560 | 33.0k | outData->size = readOutputSize; |
1561 | 33.0k | item->partialMergedExtents = (item->size != totalBytesToRead); |
1562 | 33.0k | return AVIF_RESULT_OK; |
1563 | 33.0k | } |
1564 | | |
1565 | | // Returns the avifCodecType of the first tile of the gridItem. |
1566 | | static avifCodecType avifDecoderItemGetGridCodecType(const avifDecoderItem * gridItem) |
1567 | 212 | { |
1568 | 648 | for (uint32_t i = 0; i < gridItem->meta->items.count; ++i) { |
1569 | 643 | avifDecoderItem * item = gridItem->meta->items.item[i]; |
1570 | 643 | const avifCodecType tileCodecType = avifGetCodecType(item->type); |
1571 | 643 | if ((item->dimgForID == gridItem->id) && (tileCodecType != AVIF_CODEC_TYPE_UNKNOWN)) { |
1572 | 207 | return tileCodecType; |
1573 | 207 | } |
1574 | 643 | } |
1575 | 5 | return AVIF_CODEC_TYPE_UNKNOWN; |
1576 | 212 | } |
1577 | | |
1578 | | // Fills the dimgIdxToItemIdx array with a mapping from each 0-based tile index in the 'dimg' reference |
1579 | | // to its corresponding 0-based index in the avifMeta::items array. |
1580 | | static avifResult avifFillDimgIdxToItemIdxArray(uint32_t * dimgIdxToItemIdx, uint32_t numExpectedTiles, const avifDecoderItem * gridItem) |
1581 | 280 | { |
1582 | 280 | const uint32_t itemIndexNotSet = UINT32_MAX; |
1583 | 3.26k | for (uint32_t dimgIdx = 0; dimgIdx < numExpectedTiles; ++dimgIdx) { |
1584 | 2.98k | dimgIdxToItemIdx[dimgIdx] = itemIndexNotSet; |
1585 | 2.98k | } |
1586 | 280 | uint32_t numTiles = 0; |
1587 | 7.01k | for (uint32_t i = 0; i < gridItem->meta->items.count; ++i) { |
1588 | 6.73k | if (gridItem->meta->items.item[i]->dimgForID == gridItem->id) { |
1589 | 2.98k | const uint32_t tileItemDimgIdx = gridItem->meta->items.item[i]->dimgIdx; |
1590 | 2.98k | AVIF_CHECKERR(tileItemDimgIdx < numExpectedTiles, AVIF_RESULT_INVALID_IMAGE_GRID); |
1591 | 2.98k | AVIF_CHECKERR(dimgIdxToItemIdx[tileItemDimgIdx] == itemIndexNotSet, AVIF_RESULT_INVALID_IMAGE_GRID); |
1592 | 2.98k | dimgIdxToItemIdx[tileItemDimgIdx] = i; |
1593 | 2.98k | ++numTiles; |
1594 | 2.98k | } |
1595 | 6.73k | } |
1596 | | // The number of tiles has been verified in avifDecoderItemReadAndParse(). |
1597 | 280 | AVIF_ASSERT_OR_RETURN(numTiles == numExpectedTiles); |
1598 | 280 | return AVIF_RESULT_OK; |
1599 | 280 | } |
1600 | | |
1601 | | // Copies the codec type property (av1C or av2C) from the first grid tile to the grid item. |
1602 | | // Also checks that all tiles have the same codec type and that it's valid. |
1603 | | static avifResult avifDecoderAdoptGridTileCodecType(avifDecoder * decoder, |
1604 | | avifDecoderItem * gridItem, |
1605 | | const uint32_t * dimgIdxToItemIdx, |
1606 | | uint32_t numTiles) |
1607 | 161 | { |
1608 | 161 | avifDecoderItem * firstTileItem = NULL; |
1609 | 1.59k | for (uint32_t dimgIdx = 0; dimgIdx < numTiles; ++dimgIdx) { |
1610 | 1.47k | const uint32_t itemIdx = dimgIdxToItemIdx[dimgIdx]; |
1611 | 1.47k | AVIF_ASSERT_OR_RETURN(itemIdx < gridItem->meta->items.count); |
1612 | 1.47k | avifDecoderItem * item = gridItem->meta->items.item[itemIdx]; |
1613 | | |
1614 | | // According to HEIF (ISO 14496-12), Section 6.6.2.3.1, the SingleItemTypeReferenceBox of type 'dimg' |
1615 | | // identifies the input images of the derived image item of type 'grid'. Since the reference_count |
1616 | | // shall be equal to rows*columns, unknown tile item types cannot be skipped but must be considered |
1617 | | // as errors. |
1618 | 1.47k | const avifCodecType tileCodecType = avifGetCodecType(item->type); |
1619 | 1.47k | if (tileCodecType == AVIF_CODEC_TYPE_UNKNOWN) { |
1620 | 24 | char type[4]; |
1621 | 120 | for (int j = 0; j < 4; j++) { |
1622 | 96 | if (isprint((unsigned char)item->type[j])) { |
1623 | 62 | type[j] = item->type[j]; |
1624 | 62 | } else { |
1625 | 34 | type[j] = '.'; |
1626 | 34 | } |
1627 | 96 | } |
1628 | 24 | avifDiagnosticsPrintf(&decoder->diag, |
1629 | 24 | "Tile item ID %u has an unknown item type '%.4s' (%02x%02x%02x%02x)", |
1630 | 24 | item->id, |
1631 | 24 | type, |
1632 | 24 | item->type[0], |
1633 | 24 | item->type[1], |
1634 | 24 | item->type[2], |
1635 | 24 | item->type[3]); |
1636 | 24 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
1637 | 24 | } |
1638 | | |
1639 | 1.45k | if (item->hasUnsupportedEssentialProperty) { |
1640 | | // An essential property isn't supported by libavif; can't |
1641 | | // decode a grid image if any tile in the grid isn't supported. |
1642 | 15 | avifDiagnosticsPrintf(&decoder->diag, "Grid image contains tile with an unsupported property marked as essential"); |
1643 | 15 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
1644 | 15 | } |
1645 | | |
1646 | 1.43k | if (firstTileItem == NULL) { |
1647 | 131 | firstTileItem = item; |
1648 | | // Adopt the configuration property of the first image item tile, so that it can be queried from |
1649 | | // the top-level color/alpha item during avifDecoderReset(). |
1650 | 131 | const avifCodecType codecType = avifGetCodecType(item->type); |
1651 | 131 | const char * configPropName = avifGetConfigurationPropertyName(codecType); |
1652 | 131 | const avifProperty * srcProp = avifPropertyArrayFind(&item->properties, configPropName); |
1653 | 131 | if (!srcProp) { |
1654 | 3 | avifDiagnosticsPrintf(&decoder->diag, "Grid image's first tile is missing an %s property", configPropName); |
1655 | 3 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
1656 | 3 | } |
1657 | 128 | avifProperty * dstProp = (avifProperty *)avifArrayPush(&gridItem->properties); |
1658 | 128 | AVIF_CHECKERR(dstProp != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
1659 | 128 | *dstProp = *srcProp; |
1660 | | |
1661 | 1.30k | } else if (memcmp(item->type, firstTileItem->type, 4)) { |
1662 | | // MIAF (ISO 23000-22:2019), Section 7.3.11.4.1: |
1663 | | // All input images of a grid image item shall use the same coding format [...] |
1664 | | // The coding format is defined by the item type. |
1665 | 0 | avifDiagnosticsPrintf(&decoder->diag, |
1666 | 0 | "Tile item ID %u of type '%.4s' differs from other tile type '%.4s'", |
1667 | 0 | item->id, |
1668 | 0 | (const char *)item->type, |
1669 | 0 | (const char *)firstTileItem->type); |
1670 | 0 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
1671 | 0 | } |
1672 | 1.43k | } |
1673 | 119 | return AVIF_RESULT_OK; |
1674 | 161 | } |
1675 | | |
1676 | | // If the item is a grid, copies the codec type property (av1C or av2C) from the first grid tile to the grid item. |
1677 | | // Also checks that all tiles have the same codec type and that it's valid. |
1678 | | static avifResult avifDecoderAdoptGridTileCodecTypeIfNeeded(avifDecoder * decoder, avifDecoderItem * item, const avifTileInfo * info) |
1679 | 14.1k | { |
1680 | 14.1k | if ((info->grid.rows > 0) && (info->grid.columns > 0)) { |
1681 | | // The number of tiles was verified in avifDecoderItemReadAndParse(). |
1682 | 161 | const uint32_t numTiles = info->grid.rows * info->grid.columns; |
1683 | 161 | uint32_t * dimgIdxToItemIdx = (uint32_t *)avifAlloc(numTiles * sizeof(uint32_t)); |
1684 | 161 | AVIF_CHECKERR(dimgIdxToItemIdx != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
1685 | 161 | avifResult result = avifFillDimgIdxToItemIdxArray(dimgIdxToItemIdx, numTiles, item); |
1686 | 161 | if (result == AVIF_RESULT_OK) { |
1687 | 161 | result = avifDecoderAdoptGridTileCodecType(decoder, item, dimgIdxToItemIdx, numTiles); |
1688 | 161 | } |
1689 | 161 | avifFree(dimgIdxToItemIdx); |
1690 | 161 | AVIF_CHECKRES(result); |
1691 | 161 | } |
1692 | 14.1k | return AVIF_RESULT_OK; |
1693 | 14.1k | } |
1694 | | |
1695 | | // Creates the tiles and associate them to the items in the order of the 'dimg' association. |
1696 | | static avifResult avifDecoderGenerateImageGridTiles(avifDecoder * decoder, |
1697 | | avifDecoderItem * gridItem, |
1698 | | avifItemCategory itemCategory, |
1699 | | const uint32_t * dimgIdxToItemIdx, |
1700 | | uint32_t numTiles) |
1701 | 119 | { |
1702 | 119 | avifBool progressive = AVIF_TRUE; |
1703 | 1.39k | for (uint32_t dimgIdx = 0; dimgIdx < numTiles; ++dimgIdx) { |
1704 | 1.29k | const uint32_t itemIdx = dimgIdxToItemIdx[dimgIdx]; |
1705 | 1.29k | AVIF_ASSERT_OR_RETURN(itemIdx < gridItem->meta->items.count); |
1706 | 1.29k | avifDecoderItem * item = gridItem->meta->items.item[itemIdx]; |
1707 | | |
1708 | 1.29k | const avifCodecType tileCodecType = avifGetCodecType(item->type); |
1709 | 1.29k | AVIF_CHECKERR(tileCodecType != AVIF_CODEC_TYPE_UNKNOWN, AVIF_RESULT_INVALID_IMAGE_GRID); |
1710 | 1.29k | const avifTile * tile = |
1711 | 1.29k | avifDecoderDataCreateTile(decoder->data, tileCodecType, item->width, item->height, avifDecoderItemOperatingPoint(item)); |
1712 | 1.29k | AVIF_CHECKERR(tile != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
1713 | 1.29k | AVIF_CHECKRES(avifCodecDecodeInputFillFromDecoderItem(tile->input, |
1714 | 1.29k | item, |
1715 | 1.29k | decoder->allowProgressive, |
1716 | 1.29k | decoder->imageCountLimit, |
1717 | 1.29k | decoder->io->sizeHint, |
1718 | 1.29k | &decoder->diag)); |
1719 | 1.28k | tile->input->itemCategory = itemCategory; |
1720 | | |
1721 | 1.28k | if (!item->progressive) { |
1722 | 1.25k | progressive = AVIF_FALSE; |
1723 | 1.25k | } |
1724 | 1.28k | } |
1725 | 101 | if (itemCategory == AVIF_ITEM_COLOR && progressive) { |
1726 | | // If all the items that make up the grid are progressive, then propagate that status to the top-level grid item. |
1727 | 5 | gridItem->progressive = AVIF_TRUE; |
1728 | 5 | } |
1729 | 101 | return AVIF_RESULT_OK; |
1730 | 119 | } |
1731 | | |
1732 | | // Allocates the dstImage. Also verifies some spec compliance rules for grids, if relevant. |
1733 | | static avifResult avifDecoderDataAllocateImagePlanes(const avifDecoderData * data, const avifTileInfo * info, avifImage * dstImage, avifBool * cicpSet) |
1734 | 51 | { |
1735 | 51 | const avifTile * tile = &data->tiles.tile[info->firstTileIndex]; |
1736 | 51 | uint32_t dstWidth; |
1737 | 51 | uint32_t dstHeight; |
1738 | | |
1739 | 51 | if (info->grid.rows > 0 && info->grid.columns > 0) { |
1740 | 51 | const avifImageGrid * grid = &info->grid; |
1741 | | // Validate grid image size and tile size. |
1742 | | // |
1743 | | // HEIF (ISO/IEC 23008-12:2017), Section 6.6.2.3.1: |
1744 | | // The tiled input images shall completely "cover" the reconstructed image grid canvas, ... |
1745 | 51 | if ((((uint64_t)tile->image->width * grid->columns) < grid->outputWidth) || |
1746 | 50 | (((uint64_t)tile->image->height * grid->rows) < grid->outputHeight)) { |
1747 | 2 | avifDiagnosticsPrintf(data->diag, |
1748 | 2 | "Grid image tiles do not completely cover the image (HEIF (ISO/IEC 23008-12:2017), Section 6.6.2.3.1)"); |
1749 | 2 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
1750 | 2 | } |
1751 | | // Tiles in the rightmost column and bottommost row must overlap the reconstructed image grid canvas. See MIAF (ISO/IEC 23000-22:2019), Section 7.3.11.4.2, Figure 2. |
1752 | 49 | if ((((uint64_t)tile->image->width * (grid->columns - 1)) >= grid->outputWidth) || |
1753 | 48 | (((uint64_t)tile->image->height * (grid->rows - 1)) >= grid->outputHeight)) { |
1754 | 2 | avifDiagnosticsPrintf(data->diag, |
1755 | 2 | "Grid image tiles in the rightmost column and bottommost row do not overlap the reconstructed image grid canvas. See MIAF (ISO/IEC 23000-22:2019), Section 7.3.11.4.2, Figure 2"); |
1756 | 2 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
1757 | 2 | } |
1758 | 47 | if (!avifAreGridDimensionsValid(tile->image->yuvFormat, |
1759 | 47 | grid->outputWidth, |
1760 | 47 | grid->outputHeight, |
1761 | 47 | tile->image->width, |
1762 | 47 | tile->image->height, |
1763 | 47 | data->diag)) { |
1764 | 3 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
1765 | 3 | } |
1766 | 44 | dstWidth = grid->outputWidth; |
1767 | 44 | dstHeight = grid->outputHeight; |
1768 | 44 | } else { |
1769 | | // Only one tile. Width and height are inherited from the 'ispe' property of the corresponding avifDecoderItem. |
1770 | 0 | dstWidth = tile->width; |
1771 | 0 | dstHeight = tile->height; |
1772 | 0 | } |
1773 | | |
1774 | 44 | const avifBool alpha = avifIsAlpha(tile->input->itemCategory); |
1775 | 44 | if (alpha) { |
1776 | | // An alpha tile does not contain any YUV pixels. |
1777 | 6 | AVIF_ASSERT_OR_RETURN(tile->image->yuvFormat == AVIF_PIXEL_FORMAT_NONE); |
1778 | 6 | } |
1779 | | |
1780 | 44 | const uint32_t dstDepth = tile->image->depth; |
1781 | | |
1782 | | // Lazily populate dstImage with the new frame's properties. |
1783 | 44 | const avifBool dimsOrDepthIsDifferent = (dstImage->width != dstWidth) || (dstImage->height != dstHeight) || |
1784 | 25 | (dstImage->depth != dstDepth); |
1785 | 44 | const avifBool yuvFormatIsDifferent = !alpha && (dstImage->yuvFormat != tile->image->yuvFormat); |
1786 | 44 | if (dimsOrDepthIsDifferent || yuvFormatIsDifferent) { |
1787 | 21 | if (alpha) { |
1788 | | // Alpha doesn't match size, just bail out |
1789 | 0 | avifDiagnosticsPrintf(data->diag, "Alpha plane dimensions do not match color plane dimensions"); |
1790 | 0 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
1791 | 0 | } |
1792 | | |
1793 | 21 | if (dimsOrDepthIsDifferent) { |
1794 | 20 | avifImageFreePlanes(dstImage, AVIF_PLANES_ALL); |
1795 | 20 | dstImage->width = dstWidth; |
1796 | 20 | dstImage->height = dstHeight; |
1797 | 20 | dstImage->depth = dstDepth; |
1798 | 20 | } |
1799 | 21 | if (yuvFormatIsDifferent) { |
1800 | 17 | avifImageFreePlanes(dstImage, AVIF_PLANES_YUV); |
1801 | 17 | dstImage->yuvFormat = tile->image->yuvFormat; |
1802 | 17 | } |
1803 | | // Keep dstImage->yuvRange which is already set to its correct value |
1804 | | // (extracted from the 'colr' box if parsed or from a Sequence Header OBU otherwise). |
1805 | | |
1806 | 21 | if (!*cicpSet) { |
1807 | 0 | *cicpSet = AVIF_TRUE; |
1808 | 0 | dstImage->colorPrimaries = tile->image->colorPrimaries; |
1809 | 0 | dstImage->transferCharacteristics = tile->image->transferCharacteristics; |
1810 | 0 | dstImage->matrixCoefficients = tile->image->matrixCoefficients; |
1811 | 0 | } |
1812 | 21 | } |
1813 | | |
1814 | 44 | if (avifImageAllocatePlanes(dstImage, alpha ? AVIF_PLANES_A : AVIF_PLANES_YUV) != AVIF_RESULT_OK) { |
1815 | 0 | avifDiagnosticsPrintf(data->diag, "Image allocation failure"); |
1816 | 0 | return AVIF_RESULT_OUT_OF_MEMORY; |
1817 | 0 | } |
1818 | 44 | return AVIF_RESULT_OK; |
1819 | 44 | } |
1820 | | |
1821 | | // Copies over the pixels from the tile into dstImage. |
1822 | | // Verifies that the relevant properties of the tile match those of the first tile in case of a grid. |
1823 | | static avifResult avifDecoderDataCopyTileToImage(avifDecoderData * data, |
1824 | | const avifTileInfo * info, |
1825 | | avifImage * dstImage, |
1826 | | const avifTile * tile, |
1827 | | unsigned int tileIndex) |
1828 | 364 | { |
1829 | 364 | const avifTile * firstTile = &data->tiles.tile[info->firstTileIndex]; |
1830 | 364 | if (tile != firstTile) { |
1831 | | // Check for tile consistency. All tiles in a grid image should match the first tile in the properties checked below. |
1832 | 320 | if ((tile->image->width != firstTile->image->width) || (tile->image->height != firstTile->image->height) || |
1833 | 319 | (tile->image->depth != firstTile->image->depth) || (tile->image->yuvFormat != firstTile->image->yuvFormat) || |
1834 | 317 | (tile->image->yuvRange != firstTile->image->yuvRange) || (tile->image->colorPrimaries != firstTile->image->colorPrimaries) || |
1835 | 314 | (tile->image->transferCharacteristics != firstTile->image->transferCharacteristics) || |
1836 | 313 | (tile->image->matrixCoefficients != firstTile->image->matrixCoefficients)) { |
1837 | 8 | avifDiagnosticsPrintf(data->diag, "Grid image contains mismatched tiles"); |
1838 | 8 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
1839 | 8 | } |
1840 | 320 | } |
1841 | | |
1842 | | // Only keep the relevant planes in the destination image. Otherwise, |
1843 | | // unjustified failures may come from trying to copy alpha tiles with odd |
1844 | | // coordinates into the dstImage when the chroma planes are subsampled. |
1845 | 356 | avifImage dstView; |
1846 | 356 | avifImageSetDefaults(&dstView); |
1847 | 356 | const avifCropRect srcViewRect = { 0, 0, dstImage->width, dstImage->height }; |
1848 | 356 | AVIF_ASSERT_OR_RETURN(avifImageSetViewRect(&dstView, dstImage, &srcViewRect) == AVIF_RESULT_OK); |
1849 | 356 | if (avifIsAlpha(tile->input->itemCategory)) { |
1850 | 48 | avifImageFreePlanes(&dstView, AVIF_PLANES_YUV); |
1851 | 48 | dstView.yuvFormat = AVIF_PIXEL_FORMAT_NONE; |
1852 | 308 | } else { |
1853 | 308 | avifImageFreePlanes(&dstView, AVIF_PLANES_A); |
1854 | 308 | } |
1855 | | |
1856 | 356 | avifImage srcTileView; |
1857 | 356 | avifImageSetDefaults(&srcTileView); |
1858 | 356 | avifImage dstTileView; |
1859 | 356 | avifImageSetDefaults(&dstTileView); |
1860 | 356 | avifCropRect dstTileViewRect = { 0, 0, firstTile->image->width, firstTile->image->height }; |
1861 | 356 | if (info->grid.rows > 0 && info->grid.columns > 0) { |
1862 | 356 | unsigned int rowIndex = tileIndex / info->grid.columns; |
1863 | 356 | unsigned int colIndex = tileIndex % info->grid.columns; |
1864 | 356 | dstTileViewRect.x = firstTile->image->width * colIndex; |
1865 | 356 | dstTileViewRect.y = firstTile->image->height * rowIndex; |
1866 | 356 | if (dstTileViewRect.x + dstTileViewRect.width > info->grid.outputWidth) { |
1867 | 83 | dstTileViewRect.width = info->grid.outputWidth - dstTileViewRect.x; |
1868 | 83 | } |
1869 | 356 | if (dstTileViewRect.y + dstTileViewRect.height > info->grid.outputHeight) { |
1870 | 46 | dstTileViewRect.height = info->grid.outputHeight - dstTileViewRect.y; |
1871 | 46 | } |
1872 | 356 | } |
1873 | 356 | const avifCropRect srcTileViewRect = { 0, 0, dstTileViewRect.width, dstTileViewRect.height }; |
1874 | 356 | AVIF_ASSERT_OR_RETURN(avifImageSetViewRect(&dstTileView, &dstView, &dstTileViewRect) == AVIF_RESULT_OK); |
1875 | 356 | AVIF_ASSERT_OR_RETURN(avifImageSetViewRect(&srcTileView, tile->image, &srcTileViewRect) == AVIF_RESULT_OK); |
1876 | 356 | avifImageCopySamples(&dstTileView, &srcTileView, avifIsAlpha(tile->input->itemCategory) ? AVIF_PLANES_A : AVIF_PLANES_YUV); |
1877 | 356 | return AVIF_RESULT_OK; |
1878 | 356 | } |
1879 | | |
1880 | | // If colorId == 0 (a sentinel value as item IDs must be nonzero), accept any found EXIF/XMP metadata. Passing in 0 |
1881 | | // is used when finding metadata in a meta box embedded in a trak box, as any items inside of a meta box that is |
1882 | | // inside of a trak box are implicitly associated to the track. |
1883 | | static avifResult avifDecoderFindMetadata(avifDecoder * decoder, avifMeta * meta, avifImage * image, uint32_t colorId) |
1884 | 14.4k | { |
1885 | 14.4k | if (decoder->ignoreExif && decoder->ignoreXMP) { |
1886 | | // Nothing to do! |
1887 | 0 | return AVIF_RESULT_OK; |
1888 | 0 | } |
1889 | | |
1890 | 34.6k | for (uint32_t itemIndex = 0; itemIndex < meta->items.count; ++itemIndex) { |
1891 | 20.2k | avifDecoderItem * item = meta->items.item[itemIndex]; |
1892 | 20.2k | if (!item->size) { |
1893 | 594 | continue; |
1894 | 594 | } |
1895 | 19.6k | if (item->hasUnsupportedEssentialProperty) { |
1896 | | // An essential property isn't supported by libavif; ignore the item. |
1897 | 243 | continue; |
1898 | 243 | } |
1899 | | |
1900 | 19.4k | if ((colorId > 0) && (item->descForID != colorId)) { |
1901 | | // Not a content description (metadata) for the colorOBU, skip it |
1902 | 18.0k | continue; |
1903 | 18.0k | } |
1904 | | |
1905 | 1.41k | if (!decoder->ignoreExif && !memcmp(item->type, "Exif", 4)) { |
1906 | 680 | avifROData exifContents; |
1907 | 680 | avifResult readResult = avifDecoderItemRead(item, decoder->io, &exifContents, 0, 0, &decoder->diag); |
1908 | 680 | if (readResult != AVIF_RESULT_OK) { |
1909 | 8 | return readResult; |
1910 | 8 | } |
1911 | | |
1912 | | // Advance past Annex A.2.1's header |
1913 | 672 | BEGIN_STREAM(exifBoxStream, exifContents.data, exifContents.size, &decoder->diag, "Exif header"); |
1914 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
1915 | | // The MinimizedImageBox does not signal the exifTiffHeaderOffset. |
1916 | | if (!meta->fromMiniBox) |
1917 | | #endif |
1918 | 672 | { |
1919 | 672 | uint32_t exifTiffHeaderOffset; |
1920 | 672 | AVIF_CHECKERR(avifROStreamReadU32(&exifBoxStream, &exifTiffHeaderOffset), |
1921 | 672 | AVIF_RESULT_INVALID_EXIF_PAYLOAD); // unsigned int(32) exif_tiff_header_offset; |
1922 | 670 | size_t expectedExifTiffHeaderOffset; |
1923 | 670 | AVIF_CHECKRES(avifGetExifTiffHeaderOffset(avifROStreamCurrent(&exifBoxStream), |
1924 | 670 | avifROStreamRemainingBytes(&exifBoxStream), |
1925 | 670 | &expectedExifTiffHeaderOffset)); |
1926 | 652 | AVIF_CHECKERR(exifTiffHeaderOffset == expectedExifTiffHeaderOffset, AVIF_RESULT_INVALID_EXIF_PAYLOAD); |
1927 | 652 | } |
1928 | | |
1929 | 631 | AVIF_CHECKRES(avifRWDataSet(&image->exif, avifROStreamCurrent(&exifBoxStream), avifROStreamRemainingBytes(&exifBoxStream))); |
1930 | 731 | } else if (!decoder->ignoreXMP && !memcmp(item->type, "mime", 4) && |
1931 | 543 | !strcmp(item->contentType.contentType, AVIF_CONTENT_TYPE_XMP)) { |
1932 | 110 | avifROData xmpContents; |
1933 | 110 | avifResult readResult = avifDecoderItemRead(item, decoder->io, &xmpContents, 0, 0, &decoder->diag); |
1934 | 110 | if (readResult != AVIF_RESULT_OK) { |
1935 | 4 | return readResult; |
1936 | 4 | } |
1937 | | |
1938 | 106 | AVIF_CHECKRES(avifImageSetMetadataXMP(image, xmpContents.data, xmpContents.size)); |
1939 | 106 | } |
1940 | 1.41k | } |
1941 | 14.3k | return AVIF_RESULT_OK; |
1942 | 14.4k | } |
1943 | | |
1944 | | // --------------------------------------------------------------------------- |
1945 | | // URN |
1946 | | |
1947 | | static avifBool isAlphaURN(const char * urn) |
1948 | 1.36k | { |
1949 | 1.36k | return !strcmp(urn, AVIF_URN_ALPHA0) || !strcmp(urn, AVIF_URN_ALPHA1); |
1950 | 1.36k | } |
1951 | | |
1952 | | // --------------------------------------------------------------------------- |
1953 | | // BMFF Parsing |
1954 | | |
1955 | | static avifBool avifParseHandlerBox(const uint8_t * raw, size_t rawLen, uint8_t handlerType[4], avifDiagnostics * diag) |
1956 | 15.9k | { |
1957 | 15.9k | BEGIN_STREAM(s, raw, rawLen, diag, "Box[hdlr]"); |
1958 | | |
1959 | 15.9k | AVIF_CHECK(avifROStreamReadAndEnforceVersion(&s, /*enforcedVersion=*/0, /*flags=*/NULL)); |
1960 | | |
1961 | 15.9k | uint32_t predefined; |
1962 | 15.9k | AVIF_CHECK(avifROStreamReadU32(&s, &predefined)); // unsigned int(32) pre_defined = 0; |
1963 | 15.9k | if (predefined != 0) { |
1964 | 31 | avifDiagnosticsPrintf(diag, "Box[hdlr] contains a pre_defined value that is nonzero"); |
1965 | 31 | return AVIF_FALSE; |
1966 | 31 | } |
1967 | | |
1968 | 15.9k | AVIF_CHECK(avifROStreamRead(&s, handlerType, 4)); // unsigned int(32) handler_type; |
1969 | | |
1970 | 63.6k | for (int i = 0; i < 3; ++i) { |
1971 | 47.7k | uint32_t reserved; |
1972 | 47.7k | AVIF_CHECK(avifROStreamReadU32(&s, &reserved)); // const unsigned int(32)[3] reserved = 0; |
1973 | 47.7k | } |
1974 | | |
1975 | | // Verify that a valid string is here, but don't bother to store it |
1976 | 15.9k | AVIF_CHECK(avifROStreamReadString(&s, NULL, 0)); // string name; |
1977 | 15.9k | return AVIF_TRUE; |
1978 | 15.9k | } |
1979 | | |
1980 | | static avifResult avifParseItemLocationBox(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
1981 | 15.0k | { |
1982 | 15.0k | BEGIN_STREAM(s, raw, rawLen, diag, "Box[iloc]"); |
1983 | | |
1984 | | // Section 8.11.3.2 of ISO/IEC 14496-12. |
1985 | 15.0k | uint8_t version; |
1986 | 15.0k | AVIF_CHECKERR(avifROStreamReadVersionAndFlags(&s, &version, NULL), AVIF_RESULT_BMFF_PARSE_FAILED); |
1987 | 15.0k | if (version > 2) { |
1988 | 1 | avifDiagnosticsPrintf(diag, "Box[iloc] has an unsupported version [%u]", version); |
1989 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
1990 | 1 | } |
1991 | | |
1992 | 15.0k | uint8_t offsetSize, lengthSize, baseOffsetSize, indexSize = 0; |
1993 | 15.0k | uint32_t reserved; |
1994 | 15.0k | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &offsetSize, /*bitCount=*/4), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(4) offset_size; |
1995 | 15.0k | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &lengthSize, /*bitCount=*/4), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(4) length_size; |
1996 | 15.0k | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &baseOffsetSize, /*bitCount=*/4), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(4) base_offset_size; |
1997 | 15.0k | if (version == 1 || version == 2) { |
1998 | 198 | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &indexSize, /*bitCount=*/4), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(4) index_size; |
1999 | 14.8k | } else { |
2000 | 14.8k | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &reserved, /*bitCount=*/4), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(4) reserved; |
2001 | 14.8k | } |
2002 | | |
2003 | | // Section 8.11.3.3 of ISO/IEC 14496-12. |
2004 | 15.0k | if ((offsetSize != 0 && offsetSize != 4 && offsetSize != 8) || (lengthSize != 0 && lengthSize != 4 && lengthSize != 8) || |
2005 | 14.9k | (baseOffsetSize != 0 && baseOffsetSize != 4 && baseOffsetSize != 8) || (indexSize != 0 && indexSize != 4 && indexSize != 8)) { |
2006 | 8 | avifDiagnosticsPrintf(diag, "Box[iloc] has an invalid size"); |
2007 | 8 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2008 | 8 | } |
2009 | | |
2010 | 14.9k | uint16_t tmp16; |
2011 | 14.9k | uint32_t itemCount; |
2012 | 14.9k | if (version < 2) { |
2013 | 14.9k | AVIF_CHECKERR(avifROStreamReadU16(&s, &tmp16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) item_count; |
2014 | 14.9k | itemCount = tmp16; |
2015 | 14.9k | } else { |
2016 | 53 | AVIF_CHECKERR(avifROStreamReadU32(&s, &itemCount), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) item_count; |
2017 | 53 | } |
2018 | 36.8k | for (uint32_t i = 0; i < itemCount; ++i) { |
2019 | 22.1k | uint32_t itemID; |
2020 | 22.1k | if (version < 2) { |
2021 | 22.0k | AVIF_CHECKERR(avifROStreamReadU16(&s, &tmp16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) item_ID; |
2022 | 21.9k | itemID = tmp16; |
2023 | 21.9k | } else { |
2024 | 93 | AVIF_CHECKERR(avifROStreamReadU32(&s, &itemID), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) item_ID; |
2025 | 93 | } |
2026 | 22.0k | AVIF_CHECKRES(avifCheckItemID("iloc", itemID, diag)); |
2027 | | |
2028 | 22.0k | avifDecoderItem * item; |
2029 | 22.0k | AVIF_CHECKRES(avifMetaFindOrCreateItem(meta, itemID, &item)); |
2030 | 22.0k | if (item->extents.count > 0) { |
2031 | | // This item has already been given extents via this iloc box. This is invalid. |
2032 | 8 | avifDiagnosticsPrintf(diag, "Item ID [%u] contains duplicate sets of extents", itemID); |
2033 | 8 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2034 | 8 | } |
2035 | | |
2036 | 22.0k | if (version == 1 || version == 2) { |
2037 | 4.04k | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &reserved, /*bitCount=*/12), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(12) reserved = 0; |
2038 | 4.01k | if (reserved) { |
2039 | 12 | avifDiagnosticsPrintf(diag, "Box[iloc] has a non null reserved field [%u]", reserved); |
2040 | 12 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2041 | 12 | } |
2042 | 4.00k | uint8_t constructionMethod; |
2043 | 4.00k | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &constructionMethod, /*bitCount=*/4), |
2044 | 4.00k | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(4) construction_method; |
2045 | 4.00k | if (constructionMethod != 0 /* file offset */ && constructionMethod != 1 /* idat offset */) { |
2046 | | // construction method 2 (item offset) unsupported |
2047 | 2 | avifDiagnosticsPrintf(diag, "Box[iloc] has an unsupported construction method [%u]", constructionMethod); |
2048 | 2 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2049 | 2 | } |
2050 | 3.99k | if (constructionMethod == 1) { |
2051 | 171 | item->idatStored = AVIF_TRUE; |
2052 | 171 | } |
2053 | 3.99k | } |
2054 | | |
2055 | 21.9k | uint16_t dataReferenceIndex; |
2056 | 21.9k | AVIF_CHECKERR(avifROStreamReadU16(&s, &dataReferenceIndex), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) data_reference_index; |
2057 | 21.9k | uint64_t baseOffset; |
2058 | 21.9k | AVIF_CHECKERR(avifROStreamReadUX8(&s, &baseOffset, baseOffsetSize), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(base_offset_size*8) base_offset; |
2059 | 21.9k | uint16_t extentCount; |
2060 | 21.9k | AVIF_CHECKERR(avifROStreamReadU16(&s, &extentCount), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) extent_count; |
2061 | 9.59M | for (int extentIter = 0; extentIter < extentCount; ++extentIter) { |
2062 | 9.57M | if ((version == 1 || version == 2) && indexSize > 0) { |
2063 | | // Section 8.11.3.1 of ISO/IEC 14496-12: |
2064 | | // The item_reference_index is only used for the method item_offset; it indicates the 1-based index |
2065 | | // of the item reference with referenceType 'iloc' linked from this item. If index_size is 0, then |
2066 | | // the value 1 is implied; the value 0 is reserved. |
2067 | 123 | uint64_t itemReferenceIndex; // Ignored unless construction_method=2 which is unsupported, but still read it. |
2068 | 123 | AVIF_CHECKERR(avifROStreamReadUX8(&s, &itemReferenceIndex, indexSize), |
2069 | 123 | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(index_size*8) item_reference_index; |
2070 | 123 | } |
2071 | | |
2072 | 9.57M | uint64_t extentOffset; |
2073 | 9.57M | AVIF_CHECKERR(avifROStreamReadUX8(&s, &extentOffset, offsetSize), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(offset_size*8) extent_offset; |
2074 | 9.57M | uint64_t extentLength; |
2075 | 9.57M | AVIF_CHECKERR(avifROStreamReadUX8(&s, &extentLength, lengthSize), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(length_size*8) extent_length; |
2076 | | |
2077 | 9.57M | avifExtent * extent = (avifExtent *)avifArrayPush(&item->extents); |
2078 | 9.57M | AVIF_CHECKERR(extent != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
2079 | 9.57M | if (extentOffset > UINT64_MAX - baseOffset) { |
2080 | 1 | avifDiagnosticsPrintf(diag, |
2081 | 1 | "Item ID [%u] contains an extent offset which overflows: [base: %" PRIu64 " offset:%" PRIu64 "]", |
2082 | 1 | itemID, |
2083 | 1 | baseOffset, |
2084 | 1 | extentOffset); |
2085 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2086 | 1 | } |
2087 | 9.57M | uint64_t offset = baseOffset + extentOffset; |
2088 | 9.57M | extent->offset = offset; |
2089 | | #if UINT64_MAX > SIZE_MAX |
2090 | | if (extentLength > SIZE_MAX) { |
2091 | | avifDiagnosticsPrintf(diag, "Item ID [%u] contains an extent length which overflows: [%" PRIu64 "]", itemID, extentLength); |
2092 | | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2093 | | } |
2094 | | #endif |
2095 | 9.57M | extent->size = (size_t)extentLength; |
2096 | 9.57M | if (extent->size > SIZE_MAX - item->size) { |
2097 | 4 | avifDiagnosticsPrintf(diag, |
2098 | 4 | "Item ID [%u] contains an extent length which overflows the item size: [%zu, %zu]", |
2099 | 4 | itemID, |
2100 | 4 | extent->size, |
2101 | 4 | item->size); |
2102 | 4 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2103 | 4 | } |
2104 | 9.57M | item->size += extent->size; |
2105 | 9.57M | } |
2106 | 21.9k | } |
2107 | 14.7k | return AVIF_RESULT_OK; |
2108 | 14.9k | } |
2109 | | |
2110 | | static avifResult avifParseImageGridBox(avifImageGrid * grid, |
2111 | | const uint8_t * raw, |
2112 | | size_t rawLen, |
2113 | | uint32_t imageSizeLimit, |
2114 | | uint32_t imageDimensionLimit, |
2115 | | avifDiagnostics * diag) |
2116 | 201 | { |
2117 | 201 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[grid]"); |
2118 | | |
2119 | 201 | uint8_t version, flags; |
2120 | 201 | AVIF_CHECKERR(avifROStreamRead(&s, &version, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(8) version = 0; |
2121 | 201 | if (version != 0) { |
2122 | 1 | avifDiagnosticsPrintf(diag, "Box[grid] has unsupported version [%u]", version); |
2123 | 1 | return AVIF_RESULT_NOT_IMPLEMENTED; |
2124 | 1 | } |
2125 | 200 | uint8_t rowsMinusOne, columnsMinusOne; |
2126 | 200 | AVIF_CHECKERR(avifROStreamRead(&s, &flags, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(8) flags; |
2127 | 199 | AVIF_CHECKERR(avifROStreamRead(&s, &rowsMinusOne, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(8) rows_minus_one; |
2128 | 198 | AVIF_CHECKERR(avifROStreamRead(&s, &columnsMinusOne, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(8) columns_minus_one; |
2129 | 197 | grid->rows = (uint32_t)rowsMinusOne + 1; |
2130 | 197 | grid->columns = (uint32_t)columnsMinusOne + 1; |
2131 | | |
2132 | 197 | uint32_t fieldLength = ((flags & 1) + 1) * 16; |
2133 | 197 | if (fieldLength == 16) { |
2134 | 184 | uint16_t outputWidth16, outputHeight16; |
2135 | 184 | AVIF_CHECKERR(avifROStreamReadU16(&s, &outputWidth16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(FieldLength) output_width; |
2136 | 183 | AVIF_CHECKERR(avifROStreamReadU16(&s, &outputHeight16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(FieldLength) output_height; |
2137 | 182 | grid->outputWidth = outputWidth16; |
2138 | 182 | grid->outputHeight = outputHeight16; |
2139 | 182 | } else { |
2140 | 13 | if (fieldLength != 32) { |
2141 | | // This should be impossible |
2142 | 0 | avifDiagnosticsPrintf(diag, "Grid box contains illegal field length: [%u]", fieldLength); |
2143 | 0 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
2144 | 0 | } |
2145 | 13 | AVIF_CHECKERR(avifROStreamReadU32(&s, &grid->outputWidth), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(FieldLength) output_width; |
2146 | 12 | AVIF_CHECKERR(avifROStreamReadU32(&s, &grid->outputHeight), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(FieldLength) output_height; |
2147 | 12 | } |
2148 | 193 | if ((grid->outputWidth == 0) || (grid->outputHeight == 0)) { |
2149 | 2 | avifDiagnosticsPrintf(diag, "Grid box contains illegal dimensions: [%u x %u]", grid->outputWidth, grid->outputHeight); |
2150 | 2 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
2151 | 2 | } |
2152 | 191 | if (avifDimensionsTooLarge(grid->outputWidth, grid->outputHeight, imageSizeLimit, imageDimensionLimit)) { |
2153 | 12 | avifDiagnosticsPrintf(diag, "Grid box dimensions are too large: [%u x %u]", grid->outputWidth, grid->outputHeight); |
2154 | 12 | return AVIF_RESULT_NOT_IMPLEMENTED; |
2155 | 12 | } |
2156 | 179 | if (avifROStreamRemainingBytes(&s) != 0) { |
2157 | 2 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2158 | 2 | } |
2159 | 177 | return AVIF_RESULT_OK; |
2160 | 179 | } |
2161 | | |
2162 | | static avifBool avifParseGainMapMetadata(avifGainMap * gainMap, avifROStream * s) |
2163 | 0 | { |
2164 | 0 | uint32_t isMultichannel; |
2165 | 0 | AVIF_CHECK(avifROStreamReadBitsU32(s, &isMultichannel, 1)); // unsigned int(1) is_multichannel; |
2166 | 0 | const uint8_t channelCount = isMultichannel ? 3 : 1; |
2167 | |
|
2168 | 0 | uint32_t useBaseColorSpace; |
2169 | 0 | AVIF_CHECK(avifROStreamReadBitsU32(s, &useBaseColorSpace, 1)); // unsigned int(1) use_base_colour_space; |
2170 | 0 | gainMap->useBaseColorSpace = useBaseColorSpace ? AVIF_TRUE : AVIF_FALSE; |
2171 | |
|
2172 | 0 | uint32_t reserved; |
2173 | 0 | AVIF_CHECK(avifROStreamReadBitsU32(s, &reserved, 6)); // unsigned int(6) reserved; |
2174 | | |
2175 | 0 | AVIF_CHECK(avifROStreamReadU32(s, &gainMap->baseHdrHeadroom.n)); // unsigned int(32) base_hdr_headroom_numerator; |
2176 | 0 | AVIF_CHECK(avifROStreamReadU32(s, &gainMap->baseHdrHeadroom.d)); // unsigned int(32) base_hdr_headroom_denominator; |
2177 | 0 | AVIF_CHECK(avifROStreamReadU32(s, &gainMap->alternateHdrHeadroom.n)); // unsigned int(32) alternate_hdr_headroom_numerator; |
2178 | 0 | AVIF_CHECK(avifROStreamReadU32(s, &gainMap->alternateHdrHeadroom.d)); // unsigned int(32) alternate_hdr_headroom_denominator; |
2179 | | |
2180 | 0 | for (int c = 0; c < channelCount; ++c) { |
2181 | 0 | AVIF_CHECK(avifROStreamReadU32(s, (uint32_t *)&gainMap->gainMapMin[c].n)); // int(32) gain_map_min_numerator; |
2182 | 0 | AVIF_CHECK(avifROStreamReadU32(s, &gainMap->gainMapMin[c].d)); // unsigned int(32) gain_map_min_denominator; |
2183 | 0 | AVIF_CHECK(avifROStreamReadU32(s, (uint32_t *)&gainMap->gainMapMax[c].n)); // int(32) gain_map_max_numerator; |
2184 | 0 | AVIF_CHECK(avifROStreamReadU32(s, &gainMap->gainMapMax[c].d)); // unsigned int(32) gain_map_max_denominator; |
2185 | 0 | AVIF_CHECK(avifROStreamReadU32(s, &gainMap->gainMapGamma[c].n)); // unsigned int(32) gamma_numerator; |
2186 | 0 | AVIF_CHECK(avifROStreamReadU32(s, &gainMap->gainMapGamma[c].d)); // unsigned int(32) gamma_denominator; |
2187 | 0 | AVIF_CHECK(avifROStreamReadU32(s, (uint32_t *)&gainMap->baseOffset[c].n)); // int(32) base_offset_numerator; |
2188 | 0 | AVIF_CHECK(avifROStreamReadU32(s, &gainMap->baseOffset[c].d)); // unsigned int(32) base_offset_denominator; |
2189 | 0 | AVIF_CHECK(avifROStreamReadU32(s, (uint32_t *)&gainMap->alternateOffset[c].n)); // int(32) alternate_offset_numerator; |
2190 | 0 | AVIF_CHECK(avifROStreamReadU32(s, &gainMap->alternateOffset[c].d)); // unsigned int(32) alternate_offset_denominator; |
2191 | 0 | } |
2192 | | |
2193 | | // Fill the remaining values by copying those from the first channel. |
2194 | 0 | for (int c = channelCount; c < 3; ++c) { |
2195 | 0 | gainMap->gainMapMin[c] = gainMap->gainMapMin[0]; |
2196 | 0 | gainMap->gainMapMax[c] = gainMap->gainMapMax[0]; |
2197 | 0 | gainMap->gainMapGamma[c] = gainMap->gainMapGamma[0]; |
2198 | 0 | gainMap->baseOffset[c] = gainMap->baseOffset[0]; |
2199 | 0 | gainMap->alternateOffset[c] = gainMap->alternateOffset[0]; |
2200 | 0 | } |
2201 | 0 | return AVIF_TRUE; |
2202 | 0 | } |
2203 | | |
2204 | | // If the gain map's version or minimum_version tag is not supported, returns AVIF_RESULT_NOT_IMPLEMENTED. |
2205 | | static avifResult avifParseToneMappedImageBox(avifGainMap * gainMap, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
2206 | 0 | { |
2207 | 0 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[tmap]"); |
2208 | |
|
2209 | 0 | uint8_t version; |
2210 | 0 | AVIF_CHECKERR(avifROStreamRead(&s, &version, 1), AVIF_RESULT_INVALID_TONE_MAPPED_IMAGE); // unsigned int(8) version = 0; |
2211 | 0 | if (version != 0) { |
2212 | 0 | avifDiagnosticsPrintf(diag, "Box[tmap] has unsupported version [%u]", version); |
2213 | 0 | return AVIF_RESULT_NOT_IMPLEMENTED; |
2214 | 0 | } |
2215 | | |
2216 | 0 | uint16_t minimumVersion; |
2217 | 0 | AVIF_CHECKERR(avifROStreamReadU16(&s, &minimumVersion), AVIF_RESULT_INVALID_TONE_MAPPED_IMAGE); // unsigned int(16) minimum_version; |
2218 | 0 | const uint16_t supportedMetadataVersion = 0; |
2219 | 0 | if (minimumVersion > supportedMetadataVersion) { |
2220 | 0 | avifDiagnosticsPrintf(diag, "Box[tmap] has unsupported minimum version [%u]", minimumVersion); |
2221 | 0 | return AVIF_RESULT_NOT_IMPLEMENTED; |
2222 | 0 | } |
2223 | 0 | uint16_t writerVersion; |
2224 | 0 | AVIF_CHECKERR(avifROStreamReadU16(&s, &writerVersion), AVIF_RESULT_INVALID_TONE_MAPPED_IMAGE); // unsigned int(16) writer_version; |
2225 | 0 | AVIF_CHECKERR(writerVersion >= minimumVersion, AVIF_RESULT_INVALID_TONE_MAPPED_IMAGE); |
2226 | | |
2227 | 0 | AVIF_CHECKERR(avifParseGainMapMetadata(gainMap, &s), AVIF_RESULT_INVALID_TONE_MAPPED_IMAGE); |
2228 | | |
2229 | 0 | if (writerVersion <= supportedMetadataVersion) { |
2230 | 0 | AVIF_CHECKERR(avifROStreamRemainingBytes(&s) == 0, AVIF_RESULT_INVALID_TONE_MAPPED_IMAGE); |
2231 | 0 | } |
2232 | | |
2233 | 0 | if (avifGainMapValidateMetadata(gainMap, diag) != AVIF_RESULT_OK) { |
2234 | 0 | return AVIF_RESULT_INVALID_TONE_MAPPED_IMAGE; |
2235 | 0 | } |
2236 | | |
2237 | 0 | return AVIF_RESULT_OK; |
2238 | 0 | } |
2239 | | |
2240 | | // bit_depth is assumed to be 2 (32-bit). |
2241 | | static avifResult avifParseSampleTransformTokens(avifROStream * s, avifSampleTransformExpression * expression) |
2242 | 0 | { |
2243 | 0 | uint8_t tokenCount; |
2244 | 0 | AVIF_CHECKERR(avifROStreamRead(s, &tokenCount, /*size=*/1), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(8) token_count; |
2245 | 0 | AVIF_CHECKERR(tokenCount != 0, AVIF_RESULT_BMFF_PARSE_FAILED); |
2246 | 0 | AVIF_CHECKERR(avifArrayCreate(expression, sizeof(expression->tokens[0]), tokenCount), AVIF_RESULT_OUT_OF_MEMORY); |
2247 | | |
2248 | 0 | for (uint32_t t = 0; t < tokenCount; ++t) { |
2249 | 0 | avifSampleTransformToken * token = (avifSampleTransformToken *)avifArrayPush(expression); |
2250 | 0 | AVIF_CHECKERR(token != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
2251 | | |
2252 | 0 | uint8_t tokenValue; |
2253 | 0 | AVIF_CHECKERR(avifROStreamRead(s, &tokenValue, /*size=*/1), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(8) token; |
2254 | 0 | if (tokenValue == AVIF_SAMPLE_TRANSFORM_CONSTANT) { |
2255 | 0 | token->type = AVIF_SAMPLE_TRANSFORM_CONSTANT; |
2256 | | // Two's complement representation is assumed here. |
2257 | 0 | uint32_t constant; |
2258 | 0 | AVIF_CHECKERR(avifROStreamReadU32(s, &constant), AVIF_RESULT_BMFF_PARSE_FAILED); // signed int(1<<(bit_depth+3)) constant; |
2259 | 0 | token->constant = (int32_t)constant; |
2260 | 0 | } else if (tokenValue <= AVIF_SAMPLE_TRANSFORM_LAST_INPUT_IMAGE_ITEM_INDEX) { |
2261 | 0 | AVIF_ASSERT_OR_RETURN(tokenValue >= AVIF_SAMPLE_TRANSFORM_FIRST_INPUT_IMAGE_ITEM_INDEX); |
2262 | 0 | token->type = AVIF_SAMPLE_TRANSFORM_INPUT_IMAGE_ITEM_INDEX; |
2263 | 0 | token->inputImageItemIndex = tokenValue; |
2264 | 0 | } else if (tokenValue >= AVIF_SAMPLE_TRANSFORM_FIRST_UNARY_OPERATOR && tokenValue <= AVIF_SAMPLE_TRANSFORM_LAST_UNARY_OPERATOR) { |
2265 | 0 | token->type = (avifSampleTransformTokenType)tokenValue; // unary operator |
2266 | 0 | } else if (tokenValue >= AVIF_SAMPLE_TRANSFORM_FIRST_BINARY_OPERATOR && tokenValue <= AVIF_SAMPLE_TRANSFORM_LAST_BINARY_OPERATOR) { |
2267 | 0 | token->type = (avifSampleTransformTokenType)tokenValue; // binary operator |
2268 | 0 | } else { |
2269 | 0 | token->type = AVIF_SAMPLE_TRANSFORM_RESERVED; |
2270 | 0 | } |
2271 | 0 | } |
2272 | 0 | AVIF_CHECKERR(avifROStreamRemainingBytes(s) == 0, AVIF_RESULT_BMFF_PARSE_FAILED); |
2273 | 0 | return AVIF_RESULT_OK; |
2274 | 0 | } |
2275 | | |
2276 | | // Parses the raw bitstream of the 'sato' Sample Transform derived image item and extracts the expression. |
2277 | | static avifResult avifParseSampleTransformImageBox(const uint8_t * raw, |
2278 | | size_t rawLen, |
2279 | | uint32_t numInputImageItems, |
2280 | | avifSampleTransformExpression * expression, |
2281 | | avifDiagnostics * diag) |
2282 | 0 | { |
2283 | 0 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[sato]"); |
2284 | |
|
2285 | 0 | uint8_t version, reserved, bitDepth; |
2286 | 0 | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &version, /*bitCount=*/2), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(2) version = 0; |
2287 | 0 | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &reserved, /*bitCount=*/4), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(4) reserved; |
2288 | 0 | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &bitDepth, /*bitCount=*/2), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(2) bit_depth; |
2289 | 0 | AVIF_CHECKERR(version == 0, AVIF_RESULT_NOT_IMPLEMENTED); |
2290 | 0 | AVIF_CHECKERR(bitDepth == AVIF_SAMPLE_TRANSFORM_BIT_DEPTH_32, AVIF_RESULT_NOT_IMPLEMENTED); |
2291 | | |
2292 | 0 | const avifResult result = avifParseSampleTransformTokens(&s, expression); |
2293 | 0 | if (result != AVIF_RESULT_OK) { |
2294 | 0 | avifArrayDestroy(expression); |
2295 | 0 | return result; |
2296 | 0 | } |
2297 | 0 | if (!avifSampleTransformExpressionIsValid(expression, numInputImageItems)) { |
2298 | 0 | avifArrayDestroy(expression); |
2299 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2300 | 0 | } |
2301 | 0 | return AVIF_RESULT_OK; |
2302 | 0 | } |
2303 | | |
2304 | | static const avifProperty * avifDecoderItemCodecConfigOrFirstCellCodecConfig(const avifDecoderItem * item) |
2305 | 0 | { |
2306 | 0 | if (!memcmp(item->type, "grid", 4)) { |
2307 | | // In case of a grid, return the codec configuration property of the first cell. |
2308 | | // avifDecoderAdoptGridTileCodecType() copies that property from the first cell to the grid item anyway. |
2309 | 0 | for (uint32_t i = 0; i < item->meta->items.count; ++i) { |
2310 | 0 | avifDecoderItem * inputImageItem = item->meta->items.item[i]; |
2311 | 0 | if (inputImageItem->dimgForID == item->id) { |
2312 | 0 | return avifPropertyArrayFind(&inputImageItem->properties, |
2313 | 0 | avifGetConfigurationPropertyName(avifGetCodecType(inputImageItem->type))); |
2314 | 0 | } |
2315 | 0 | } |
2316 | | // The number of tiles was verified in avifDecoderItemReadAndParse(). |
2317 | 0 | assert(AVIF_FALSE); |
2318 | 0 | } |
2319 | 0 | return avifPropertyArrayFind(&item->properties, avifGetConfigurationPropertyName(avifGetCodecType(item->type))); |
2320 | 0 | } |
2321 | | |
2322 | | static avifResult avifDecoderSampleTransformItemValidateProperties(const avifDecoderItem * satoItem, avifDiagnostics * diag) |
2323 | 0 | { |
2324 | 0 | AVIF_ASSERT_OR_RETURN(memcmp(satoItem->type, "sato", 4) == 0); |
2325 | 0 | const avifProperty * pixiProp = avifPropertyArrayFind(&satoItem->properties, "pixi"); |
2326 | 0 | if (!pixiProp) { |
2327 | 0 | avifDiagnosticsPrintf(diag, "Item ID %u of type 'sato' is missing mandatory pixi property", satoItem->id); |
2328 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2329 | 0 | } |
2330 | 0 | for (uint8_t i = 1; i < pixiProp->u.pixi.planeCount; ++i) { |
2331 | | // This is enforced in avifParsePixelInformationProperty(). |
2332 | 0 | AVIF_ASSERT_OR_RETURN(pixiProp->u.pixi.planeDepths[i] == pixiProp->u.pixi.planeDepths[0]); |
2333 | 0 | } |
2334 | 0 | AVIF_ASSERT_OR_RETURN(pixiProp->u.pixi.planeCount >= 1); |
2335 | 0 | const uint8_t depth = pixiProp->u.pixi.planeDepths[0]; |
2336 | 0 | if (depth != 8 && depth != 10 && depth != 12 && depth != 16) { |
2337 | 0 | avifDiagnosticsPrintf(diag, |
2338 | 0 | "Item ID %u of type 'sato' with depth %u (specified by pixi property) is not supported", |
2339 | 0 | satoItem->id, |
2340 | 0 | depth); |
2341 | 0 | return AVIF_RESULT_NOT_IMPLEMENTED; |
2342 | 0 | } |
2343 | | |
2344 | 0 | const avifProperty * ispeProp = avifPropertyArrayFind(&satoItem->properties, "ispe"); |
2345 | 0 | if (!ispeProp) { |
2346 | 0 | avifDiagnosticsPrintf(diag, "Item ID %u of type 'sato' is missing mandatory ispe property", satoItem->id); |
2347 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2348 | 0 | } |
2349 | | |
2350 | | // Check that all input image items of the 'sato' derived image item share the same properties. |
2351 | 0 | for (uint32_t i = 0; i < satoItem->meta->items.count; ++i) { |
2352 | 0 | avifDecoderItem * inputImageItem = satoItem->meta->items.item[i]; |
2353 | 0 | if (inputImageItem->dimgForID != satoItem->id) { |
2354 | 0 | continue; |
2355 | 0 | } |
2356 | | |
2357 | | // Require all input image items of the 'sato' derived image item to be associated with a ImageSpatialExtentsProperty. |
2358 | 0 | const avifProperty * inputImageItemIspeProp = avifPropertyArrayFind(&inputImageItem->properties, "ispe"); |
2359 | 0 | if (inputImageItemIspeProp == NULL) { |
2360 | 0 | avifDiagnosticsPrintf(diag, "Item ID %u is missing mandatory ispe property", inputImageItem->id); |
2361 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2362 | 0 | } |
2363 | | |
2364 | | // The codec configuration property must be present, at least on the first cell for a 'grid' item. |
2365 | 0 | const avifProperty * inputImageItemCodecConfig = avifDecoderItemCodecConfigOrFirstCellCodecConfig(inputImageItem); |
2366 | 0 | if (inputImageItemCodecConfig == NULL) { |
2367 | 0 | avifDiagnosticsPrintf(diag, |
2368 | 0 | "Item ID %u of type '%.4s' is missing mandatory codec configuration property", |
2369 | 0 | inputImageItem->id, |
2370 | 0 | (const char *)inputImageItem->type); |
2371 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2372 | 0 | } |
2373 | | |
2374 | 0 | for (uint32_t j = i + 1; j < satoItem->meta->items.count; ++j) { |
2375 | 0 | avifDecoderItem * otherInputImageItem = satoItem->meta->items.item[j]; |
2376 | 0 | if (otherInputImageItem->dimgForID != satoItem->id) { |
2377 | 0 | continue; |
2378 | 0 | } |
2379 | | |
2380 | | // Require all input image items of the 'sato' derived image item to be associated with a ImageSpatialExtentsProperty. |
2381 | 0 | const avifProperty * otherInputImageItemIspeProp = avifPropertyArrayFind(&otherInputImageItem->properties, "ispe"); |
2382 | 0 | if (otherInputImageItemIspeProp == NULL) { |
2383 | 0 | avifDiagnosticsPrintf(diag, |
2384 | 0 | "Item ID %u of type '%.4s' is missing mandatory ispe property", |
2385 | 0 | otherInputImageItem->id, |
2386 | 0 | (const char *)otherInputImageItem->type); |
2387 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2388 | 0 | } |
2389 | | |
2390 | 0 | if (inputImageItemIspeProp->u.ispe.width != otherInputImageItemIspeProp->u.ispe.width || |
2391 | 0 | inputImageItemIspeProp->u.ispe.height != otherInputImageItemIspeProp->u.ispe.height) { |
2392 | 0 | avifDiagnosticsPrintf(diag, |
2393 | 0 | "The fields of the ispe property of item ID %u of type '%.4s' differs from item ID %u", |
2394 | 0 | inputImageItem->id, |
2395 | 0 | (const char *)inputImageItem->type, |
2396 | 0 | otherInputImageItem->id); |
2397 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2398 | 0 | } |
2399 | | |
2400 | | // The codec configuration property must be present, at least on the first cell for a 'grid' item. |
2401 | 0 | const avifProperty * otherInputImageItemCodecConfig = avifDecoderItemCodecConfigOrFirstCellCodecConfig(otherInputImageItem); |
2402 | 0 | if (otherInputImageItemCodecConfig == NULL) { |
2403 | 0 | avifDiagnosticsPrintf(diag, |
2404 | 0 | "Item ID %u of type '%.4s' is missing mandatory codec configuration property", |
2405 | 0 | otherInputImageItem->id, |
2406 | 0 | (const char *)otherInputImageItem->type); |
2407 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2408 | 0 | } |
2409 | | |
2410 | 0 | if (inputImageItemCodecConfig->u.av1C.monochrome != otherInputImageItemCodecConfig->u.av1C.monochrome || |
2411 | 0 | inputImageItemCodecConfig->u.av1C.chromaSubsamplingX != otherInputImageItemCodecConfig->u.av1C.chromaSubsamplingX || |
2412 | 0 | inputImageItemCodecConfig->u.av1C.chromaSubsamplingY != otherInputImageItemCodecConfig->u.av1C.chromaSubsamplingY || |
2413 | 0 | inputImageItemCodecConfig->u.av1C.chromaSamplePosition != otherInputImageItemCodecConfig->u.av1C.chromaSamplePosition) { |
2414 | 0 | avifDiagnosticsPrintf(diag, |
2415 | 0 | "The plane count or subsampling in the codec configuration property of item ID %u of type '%.4s' differs from item ID %u", |
2416 | 0 | inputImageItem->id, |
2417 | 0 | (const char *)inputImageItem->type, |
2418 | 0 | otherInputImageItem->id); |
2419 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2420 | 0 | } |
2421 | | |
2422 | | // If the input image item of the 'sato' derived image item is itself a grid, |
2423 | | // its own input image items will be checked in avifDecoderItemValidateProperties(). |
2424 | 0 | } |
2425 | 0 | break; |
2426 | 0 | } |
2427 | | |
2428 | 0 | AVIF_CHECKERR(avifPropertyArrayFind(&satoItem->properties, "clap") == NULL, AVIF_RESULT_NOT_IMPLEMENTED); |
2429 | 0 | return AVIF_RESULT_OK; |
2430 | 0 | } |
2431 | | |
2432 | | // Extracts the codecType from the item type or from its children. |
2433 | | // Also parses and outputs grid information if the item is a grid. |
2434 | | // isItemInInput must be false if the item is a made-up structure |
2435 | | // (and thus not part of the parseable input bitstream). |
2436 | | static avifResult avifDecoderItemReadAndParse(const avifDecoder * decoder, |
2437 | | avifDecoderItem * item, |
2438 | | avifBool isItemInInput, |
2439 | | avifImageGrid * grid, |
2440 | | avifCodecType * codecType) |
2441 | 14.2k | { |
2442 | 14.2k | if (!memcmp(item->type, "grid", 4)) { |
2443 | 271 | if (isItemInInput) { |
2444 | 232 | avifROData readData; |
2445 | 232 | AVIF_CHECKRES(avifDecoderItemRead(item, decoder->io, &readData, 0, 0, decoder->data->diag)); |
2446 | 201 | AVIF_CHECKRES(avifParseImageGridBox(grid, |
2447 | 201 | readData.data, |
2448 | 201 | readData.size, |
2449 | 201 | decoder->imageSizeLimit, |
2450 | 201 | decoder->imageDimensionLimit, |
2451 | 201 | decoder->data->diag)); |
2452 | | // Validate that there are exactly the same number of dimg items to form the grid. |
2453 | 177 | uint32_t dimgItemCount = 0; |
2454 | 4.18k | for (uint32_t i = 0; i < item->meta->items.count; ++i) { |
2455 | 4.00k | if (item->meta->items.item[i]->dimgForID == item->id) { |
2456 | 1.76k | ++dimgItemCount; |
2457 | 1.76k | } |
2458 | 4.00k | } |
2459 | 177 | AVIF_CHECKERR(dimgItemCount == grid->rows * grid->columns, AVIF_RESULT_INVALID_IMAGE_GRID); |
2460 | 177 | } else { |
2461 | | // item was generated for convenience and is not part of the bitstream. |
2462 | | // grid information should already be set. |
2463 | 39 | AVIF_ASSERT_OR_RETURN(grid->rows > 0 && grid->columns > 0); |
2464 | 39 | } |
2465 | 212 | *codecType = avifDecoderItemGetGridCodecType(item); |
2466 | 212 | AVIF_CHECKERR(*codecType != AVIF_CODEC_TYPE_UNKNOWN, AVIF_RESULT_INVALID_IMAGE_GRID); |
2467 | 14.0k | } else { |
2468 | 14.0k | *codecType = avifGetCodecType(item->type); |
2469 | 14.0k | AVIF_ASSERT_OR_RETURN(*codecType != AVIF_CODEC_TYPE_UNKNOWN); |
2470 | 14.0k | } |
2471 | 14.2k | return AVIF_RESULT_OK; |
2472 | 14.2k | } |
2473 | | |
2474 | | static avifBool avifParseImageSpatialExtentsProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
2475 | 15.4k | { |
2476 | 15.4k | BEGIN_STREAM(s, raw, rawLen, diag, "Box[ispe]"); |
2477 | 15.4k | AVIF_CHECK(avifROStreamReadAndEnforceVersion(&s, /*enforcedVersion=*/0, /*flags=*/NULL)); |
2478 | | |
2479 | 15.4k | avifImageSpatialExtents * ispe = &prop->u.ispe; |
2480 | 15.4k | AVIF_CHECK(avifROStreamReadU32(&s, &ispe->width)); |
2481 | 15.4k | AVIF_CHECK(avifROStreamReadU32(&s, &ispe->height)); |
2482 | 15.4k | return AVIF_TRUE; |
2483 | 15.4k | } |
2484 | | |
2485 | | static avifBool avifParseAuxiliaryTypeProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
2486 | 311 | { |
2487 | 311 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[auxC]"); |
2488 | 311 | AVIF_CHECK(avifROStreamReadAndEnforceVersion(&s, /*enforcedVersion=*/0, /*flags=*/NULL)); |
2489 | | |
2490 | 309 | AVIF_CHECK(avifROStreamReadString(&s, prop->u.auxC.auxType, AUXTYPE_SIZE)); |
2491 | 308 | return AVIF_TRUE; |
2492 | 309 | } |
2493 | | |
2494 | | static avifBool avifParseColourInformationBox(avifProperty * prop, uint64_t rawOffset, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
2495 | 5.24k | { |
2496 | 5.24k | BEGIN_STREAM(s, raw, rawLen, diag, "Box[colr]"); |
2497 | | |
2498 | 5.24k | avifColourInformationBox * colr = &prop->u.colr; |
2499 | 5.24k | colr->hasICC = AVIF_FALSE; |
2500 | 5.24k | colr->hasNCLX = AVIF_FALSE; |
2501 | | |
2502 | 5.24k | uint8_t colorType[4]; // unsigned int(32) colour_type; |
2503 | 5.24k | AVIF_CHECK(avifROStreamRead(&s, colorType, 4)); |
2504 | 5.24k | if (!memcmp(colorType, "rICC", 4) || !memcmp(colorType, "prof", 4)) { |
2505 | | // Remember the offset of the ICC payload relative to the beginning of the stream. A direct pointer cannot be stored |
2506 | | // because decoder->io->persistent could have been AVIF_FALSE when obtaining raw through decoder->io->read(). |
2507 | | // The bytes could be copied now instead of remembering the offset, but it is as invasive as passing rawOffset everywhere. |
2508 | 1.50k | colr->iccOffset = rawOffset + avifROStreamOffset(&s); |
2509 | 1.50k | colr->iccSize = avifROStreamRemainingBytes(&s); |
2510 | 1.50k | if (colr->iccSize == 0) { |
2511 | 1 | avifDiagnosticsPrintf(diag, "Box[colr] contains empty ICC_profile"); |
2512 | 1 | return AVIF_FALSE; |
2513 | 1 | } |
2514 | 1.50k | colr->hasICC = AVIF_TRUE; |
2515 | 3.73k | } else if (!memcmp(colorType, "nclx", 4)) { |
2516 | 1.74k | AVIF_CHECK(avifROStreamReadU16(&s, &colr->colorPrimaries)); // unsigned int(16) colour_primaries; |
2517 | 1.74k | AVIF_CHECK(avifROStreamReadU16(&s, &colr->transferCharacteristics)); // unsigned int(16) transfer_characteristics; |
2518 | 1.73k | AVIF_CHECK(avifROStreamReadU16(&s, &colr->matrixCoefficients)); // unsigned int(16) matrix_coefficients; |
2519 | 1.73k | uint8_t full_range_flag; |
2520 | 1.73k | AVIF_CHECK(avifROStreamReadBitsU8(&s, &full_range_flag, /*bitCount=*/1)); // unsigned int(1) full_range_flag; |
2521 | 1.73k | colr->range = full_range_flag ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED; |
2522 | 1.73k | uint8_t reserved; |
2523 | 1.73k | AVIF_CHECK(avifROStreamReadBitsU8(&s, &reserved, /*bitCount=*/7)); // unsigned int(7) reserved = 0; |
2524 | 1.73k | if (reserved) { |
2525 | 1 | avifDiagnosticsPrintf(diag, "Box[colr] contains nonzero reserved bits [%u]", reserved); |
2526 | 1 | return AVIF_FALSE; |
2527 | 1 | } |
2528 | 1.73k | colr->hasNCLX = AVIF_TRUE; |
2529 | 1.73k | } |
2530 | 5.24k | return AVIF_TRUE; |
2531 | 5.24k | } |
2532 | | |
2533 | | static avifResult avifParseContentLightLevelInformation(avifROStream * s, avifContentLightLevelInformationBox * clli) |
2534 | 240 | { |
2535 | 240 | AVIF_CHECKERR(avifROStreamReadBitsU16(s, &clli->maxCLL, 16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) max_content_light_level |
2536 | 238 | AVIF_CHECKERR(avifROStreamReadBitsU16(s, &clli->maxPALL, 16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) max_pic_average_light_level |
2537 | 236 | return AVIF_RESULT_OK; |
2538 | 238 | } |
2539 | | static avifResult avifParseContentLightLevelInformationBox(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
2540 | 240 | { |
2541 | 240 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[clli]"); |
2542 | 240 | AVIF_CHECKRES(avifParseContentLightLevelInformation(&s, &prop->u.clli)); |
2543 | 236 | return AVIF_RESULT_OK; |
2544 | 240 | } |
2545 | | |
2546 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
2547 | | static avifResult avifSkipMasteringDisplayColourVolume(avifROStream * s) |
2548 | | { |
2549 | | for (int c = 0; c < 3; c++) { |
2550 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) display_primaries_x; |
2551 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) display_primaries_y; |
2552 | | } |
2553 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) white_point_x; |
2554 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) white_point_y; |
2555 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 32), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) max_display_mastering_luminance; |
2556 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 32), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) min_display_mastering_luminance; |
2557 | | return AVIF_RESULT_OK; |
2558 | | } |
2559 | | |
2560 | | static avifResult avifSkipContentColourVolume(avifROStream * s) |
2561 | | { |
2562 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(1) reserved = 0; // ccv_cancel_flag |
2563 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(1) reserved = 0; // ccv_persistence_flag |
2564 | | uint8_t ccvPrimariesPresent; |
2565 | | AVIF_CHECKERR(avifROStreamReadBitsU8(s, &ccvPrimariesPresent, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(1) ccv_primaries_present_flag; |
2566 | | uint8_t ccvMinLuminanceValuePresent, ccvMaxLuminanceValuePresent, ccvAvgLuminanceValuePresent; |
2567 | | AVIF_CHECKERR(avifROStreamReadBitsU8(s, &ccvMinLuminanceValuePresent, 1), |
2568 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(1) ccv_min_luminance_value_present_flag; |
2569 | | AVIF_CHECKERR(avifROStreamReadBitsU8(s, &ccvMaxLuminanceValuePresent, 1), |
2570 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(1) ccv_max_luminance_value_present_flag; |
2571 | | AVIF_CHECKERR(avifROStreamReadBitsU8(s, &ccvAvgLuminanceValuePresent, 1), |
2572 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(1) ccv_avg_luminance_value_present_flag; |
2573 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 2), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(2) reserved = 0; |
2574 | | |
2575 | | if (ccvPrimariesPresent) { |
2576 | | for (int c = 0; c < 3; c++) { |
2577 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 32), AVIF_RESULT_BMFF_PARSE_FAILED); // signed int(32) ccv_primaries_x[[c]]; |
2578 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 32), AVIF_RESULT_BMFF_PARSE_FAILED); // signed int(32) ccv_primaries_y[[c]]; |
2579 | | } |
2580 | | } |
2581 | | if (ccvMinLuminanceValuePresent) { |
2582 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 32), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) ccv_min_luminance_value; |
2583 | | } |
2584 | | if (ccvMaxLuminanceValuePresent) { |
2585 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 32), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) ccv_max_luminance_value; |
2586 | | } |
2587 | | if (ccvAvgLuminanceValuePresent) { |
2588 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 32), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) ccv_avg_luminance_value; |
2589 | | } |
2590 | | return AVIF_RESULT_OK; |
2591 | | } |
2592 | | |
2593 | | static avifResult avifSkipAmbientViewingEnvironment(avifROStream * s) |
2594 | | { |
2595 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 32), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) ambient_illuminance; |
2596 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) ambient_light_x; |
2597 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) ambient_light_y; |
2598 | | return AVIF_RESULT_OK; |
2599 | | } |
2600 | | |
2601 | | static avifResult avifSkipReferenceViewingEnvironment(avifROStream * s) |
2602 | | { |
2603 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 32), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) surround_luminance; |
2604 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) surround_light_x; |
2605 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) surround_light_y; |
2606 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 32), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) periphery_luminance; |
2607 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) periphery_light_x; |
2608 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 16), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) periphery_light_y; |
2609 | | return AVIF_RESULT_OK; |
2610 | | } |
2611 | | |
2612 | | static avifResult avifSkipNominalDiffuseWhite(avifROStream * s) |
2613 | | { |
2614 | | AVIF_CHECKERR(avifROStreamSkipBits(s, 32), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) diffuse_white_luminance; |
2615 | | return AVIF_RESULT_OK; |
2616 | | } |
2617 | | |
2618 | | static avifResult avifParseMiniHDRProperties(avifROStream * s, uint32_t * hasClli, avifContentLightLevelInformationBox * clli) |
2619 | | { |
2620 | | AVIF_CHECKERR(avifROStreamReadBitsU32(s, hasClli, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) clli_flag; |
2621 | | uint32_t hasMdcv, hasCclv, hasAmve, hasReve, hasNdwt; |
2622 | | AVIF_CHECKERR(avifROStreamReadBitsU32(s, &hasMdcv, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) mdcv_flag; |
2623 | | AVIF_CHECKERR(avifROStreamReadBitsU32(s, &hasCclv, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) cclv_flag; |
2624 | | AVIF_CHECKERR(avifROStreamReadBitsU32(s, &hasAmve, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) amve_flag; |
2625 | | AVIF_CHECKERR(avifROStreamReadBitsU32(s, &hasReve, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) reve_flag; |
2626 | | AVIF_CHECKERR(avifROStreamReadBitsU32(s, &hasNdwt, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) ndwt_flag; |
2627 | | if (*hasClli) { |
2628 | | AVIF_CHECKRES(avifParseContentLightLevelInformation(s, clli)); // ContentLightLevel clli; |
2629 | | } |
2630 | | if (hasMdcv) { |
2631 | | AVIF_CHECKRES(avifSkipMasteringDisplayColourVolume(s)); // MasteringDisplayColourVolume mdcv; |
2632 | | } |
2633 | | if (hasCclv) { |
2634 | | AVIF_CHECKRES(avifSkipContentColourVolume(s)); // ContentColourVolume cclv; |
2635 | | } |
2636 | | if (hasAmve) { |
2637 | | AVIF_CHECKRES(avifSkipAmbientViewingEnvironment(s)); // AmbientViewingEnvironment amve; |
2638 | | } |
2639 | | if (hasReve) { |
2640 | | AVIF_CHECKRES(avifSkipReferenceViewingEnvironment(s)); // ReferenceViewingEnvironment reve; |
2641 | | } |
2642 | | if (hasNdwt) { |
2643 | | AVIF_CHECKRES(avifSkipNominalDiffuseWhite(s)); // NominalDiffuseWhite ndwt; |
2644 | | } |
2645 | | return AVIF_RESULT_OK; |
2646 | | } |
2647 | | #endif // AVIF_ENABLE_EXPERIMENTAL_MINI |
2648 | | |
2649 | | // Implementation of section 2.3.3 of AV1 Codec ISO Media File Format Binding specification v1.2.0. |
2650 | | // See https://aomediacodec.github.io/av1-isobmff/v1.2.0.html#av1codecconfigurationbox-syntax. |
2651 | | static avifBool avifParseCodecConfiguration(avifROStream * s, avifCodecConfigurationBox * config, const char * configPropName, avifDiagnostics * diag) |
2652 | 15.3k | { |
2653 | 15.3k | const size_t av1COffset = avifROStreamOffset(s); |
2654 | | |
2655 | 15.3k | uint32_t marker, version; |
2656 | 15.3k | AVIF_CHECK(avifROStreamReadBitsU32(s, &marker, /*bitCount=*/1)); // unsigned int (1) marker = 1; |
2657 | 15.3k | if (!marker) { |
2658 | 1 | avifDiagnosticsPrintf(diag, "%.4s contains illegal marker: [%u]", configPropName, marker); |
2659 | 1 | return AVIF_FALSE; |
2660 | 1 | } |
2661 | 15.3k | AVIF_CHECK(avifROStreamReadBitsU32(s, &version, /*bitCount=*/7)); // unsigned int (7) version = 1; |
2662 | 15.3k | if (version != 1) { |
2663 | 1 | avifDiagnosticsPrintf(diag, "%.4s contains illegal version: [%u]", configPropName, version); |
2664 | 1 | return AVIF_FALSE; |
2665 | 1 | } |
2666 | | |
2667 | 15.3k | AVIF_CHECK(avifROStreamReadBitsU8(s, &config->seqProfile, /*bitCount=*/3)); // unsigned int (3) seq_profile; |
2668 | 15.3k | AVIF_CHECK(avifROStreamReadBitsU8(s, &config->seqLevelIdx0, /*bitCount=*/5)); // unsigned int (5) seq_level_idx_0; |
2669 | 15.3k | AVIF_CHECK(avifROStreamReadBitsU8(s, &config->seqTier0, /*bitCount=*/1)); // unsigned int (1) seq_tier_0; |
2670 | 15.3k | AVIF_CHECK(avifROStreamReadBitsU8(s, &config->highBitdepth, /*bitCount=*/1)); // unsigned int (1) high_bitdepth; |
2671 | 15.3k | AVIF_CHECK(avifROStreamReadBitsU8(s, &config->twelveBit, /*bitCount=*/1)); // unsigned int (1) twelve_bit; |
2672 | 15.3k | AVIF_CHECK(avifROStreamReadBitsU8(s, &config->monochrome, /*bitCount=*/1)); // unsigned int (1) monochrome; |
2673 | 15.3k | AVIF_CHECK(avifROStreamReadBitsU8(s, &config->chromaSubsamplingX, /*bitCount=*/1)); // unsigned int (1) chroma_subsampling_x; |
2674 | 15.3k | AVIF_CHECK(avifROStreamReadBitsU8(s, &config->chromaSubsamplingY, /*bitCount=*/1)); // unsigned int (1) chroma_subsampling_y; |
2675 | 15.3k | AVIF_CHECK(avifROStreamReadBitsU8(s, &config->chromaSamplePosition, /*bitCount=*/2)); // unsigned int (2) chroma_sample_position; |
2676 | | |
2677 | | // unsigned int (3) reserved = 0; |
2678 | | // unsigned int (1) initial_presentation_delay_present; |
2679 | | // if (initial_presentation_delay_present) { |
2680 | | // unsigned int (4) initial_presentation_delay_minus_one; |
2681 | | // } else { |
2682 | | // unsigned int (4) reserved = 0; |
2683 | | // } |
2684 | 15.3k | AVIF_CHECK(avifROStreamSkip(s, /*byteCount=*/1)); |
2685 | | |
2686 | | // According to section 2.2.1 of AV1 Image File Format specification v1.1.0: |
2687 | | // - Sequence Header OBUs should not be present in the AV1CodecConfigurationBox. |
2688 | | // - If a Sequence Header OBU is present in the AV1CodecConfigurationBox, |
2689 | | // it shall match the Sequence Header OBU in the AV1 Image Item Data. |
2690 | | // - Metadata OBUs, if present, shall match the values given in other item properties, |
2691 | | // such as the PixelInformationProperty or ColourInformationBox. |
2692 | | // See https://aomediacodec.github.io/av1-avif/v1.1.0.html#av1-configuration-item-property. |
2693 | | // For simplicity, the constraints above are not enforced. |
2694 | | // The following is skipped by avifParseItemPropertyContainerBox(). |
2695 | | // unsigned int (8) configOBUs[]; |
2696 | | |
2697 | 15.3k | AVIF_CHECK(avifROStreamOffset(s) - av1COffset == 4); // Make sure avifParseCodecConfiguration() reads exactly 4 bytes. |
2698 | 15.3k | return AVIF_TRUE; |
2699 | 15.3k | } |
2700 | | |
2701 | | static avifBool avifParseCodecConfigurationBoxProperty(avifProperty * prop, |
2702 | | const uint8_t * raw, |
2703 | | size_t rawLen, |
2704 | | const char * configPropName, |
2705 | | avifDiagnostics * diag) |
2706 | 15.3k | { |
2707 | 15.3k | char diagContext[10]; |
2708 | 15.3k | snprintf(diagContext, sizeof(diagContext), "Box[%.4s]", configPropName); // "Box[av1C]" or "Box[av2C]" |
2709 | 15.3k | BEGIN_STREAM(s, raw, rawLen, diag, diagContext); |
2710 | 15.3k | return avifParseCodecConfiguration(&s, &prop->u.av1C, configPropName, diag); |
2711 | 15.3k | } |
2712 | | |
2713 | | static avifBool avifParsePixelAspectRatioBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
2714 | 669 | { |
2715 | 669 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[pasp]"); |
2716 | | |
2717 | 669 | avifPixelAspectRatioBox * pasp = &prop->u.pasp; |
2718 | 669 | AVIF_CHECK(avifROStreamReadU32(&s, &pasp->hSpacing)); // unsigned int(32) hSpacing; |
2719 | 668 | AVIF_CHECK(avifROStreamReadU32(&s, &pasp->vSpacing)); // unsigned int(32) vSpacing; |
2720 | 667 | return AVIF_TRUE; |
2721 | 668 | } |
2722 | | |
2723 | | static avifBool avifParseCleanApertureBoxProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
2724 | 25 | { |
2725 | 25 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[clap]"); |
2726 | | |
2727 | 25 | avifCleanApertureBox * clap = &prop->u.clap; |
2728 | 25 | AVIF_CHECK(avifROStreamReadU32(&s, &clap->widthN)); // unsigned int(32) cleanApertureWidthN; |
2729 | 24 | AVIF_CHECK(avifROStreamReadU32(&s, &clap->widthD)); // unsigned int(32) cleanApertureWidthD; |
2730 | 23 | AVIF_CHECK(avifROStreamReadU32(&s, &clap->heightN)); // unsigned int(32) cleanApertureHeightN; |
2731 | 22 | AVIF_CHECK(avifROStreamReadU32(&s, &clap->heightD)); // unsigned int(32) cleanApertureHeightD; |
2732 | 21 | AVIF_CHECK(avifROStreamReadU32(&s, &clap->horizOffN)); // unsigned int(32) horizOffN; |
2733 | 20 | AVIF_CHECK(avifROStreamReadU32(&s, &clap->horizOffD)); // unsigned int(32) horizOffD; |
2734 | 19 | AVIF_CHECK(avifROStreamReadU32(&s, &clap->vertOffN)); // unsigned int(32) vertOffN; |
2735 | 18 | AVIF_CHECK(avifROStreamReadU32(&s, &clap->vertOffD)); // unsigned int(32) vertOffD; |
2736 | 17 | return AVIF_TRUE; |
2737 | 18 | } |
2738 | | |
2739 | | static avifBool avifParseImageRotationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
2740 | 270 | { |
2741 | 270 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[irot]"); |
2742 | | |
2743 | 270 | avifImageRotation * irot = &prop->u.irot; |
2744 | 270 | uint8_t reserved; |
2745 | 270 | AVIF_CHECK(avifROStreamReadBitsU8(&s, &reserved, /*bitCount=*/6)); // unsigned int (6) reserved = 0; |
2746 | 269 | if (reserved) { |
2747 | 1 | avifDiagnosticsPrintf(diag, "Box[irot] contains nonzero reserved bits [%u]", reserved); |
2748 | 1 | return AVIF_FALSE; |
2749 | 1 | } |
2750 | 268 | AVIF_CHECK(avifROStreamReadBitsU8(&s, &irot->angle, /*bitCount=*/2)); // unsigned int (2) angle; |
2751 | 268 | return AVIF_TRUE; |
2752 | 268 | } |
2753 | | |
2754 | | static avifBool avifParseImageMirrorProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
2755 | 69 | { |
2756 | 69 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[imir]"); |
2757 | | |
2758 | 69 | avifImageMirror * imir = &prop->u.imir; |
2759 | 69 | uint8_t reserved; |
2760 | 69 | AVIF_CHECK(avifROStreamReadBitsU8(&s, &reserved, /*bitCount=*/7)); // unsigned int(7) reserved = 0; |
2761 | 68 | if (reserved) { |
2762 | 1 | avifDiagnosticsPrintf(diag, "Box[imir] contains nonzero reserved bits [%u]", reserved); |
2763 | 1 | return AVIF_FALSE; |
2764 | 1 | } |
2765 | 67 | AVIF_CHECK(avifROStreamReadBitsU8(&s, &imir->axis, /*bitCount=*/1)); // unsigned int(1) axis; |
2766 | 67 | return AVIF_TRUE; |
2767 | 67 | } |
2768 | | |
2769 | | static avifResult avifParsePixelInformationProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
2770 | 2.48k | { |
2771 | 2.48k | BEGIN_STREAM(s, raw, rawLen, diag, "Box[pixi]"); |
2772 | 2.48k | uint32_t flags = 0; // px_flags |
2773 | 2.48k | AVIF_CHECKERR(avifROStreamReadAndEnforceVersion(&s, /*enforcedVersion=*/0, &flags), AVIF_RESULT_BMFF_PARSE_FAILED); |
2774 | | |
2775 | 2.48k | avifPixelInformationProperty * pixi = &prop->u.pixi; |
2776 | 2.48k | AVIF_CHECKERR(avifROStreamRead(&s, &pixi->planeCount, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int (8) num_channels; |
2777 | 2.48k | if (pixi->planeCount < 1 || pixi->planeCount > MAX_PIXI_PLANE_DEPTHS) { |
2778 | 2 | avifDiagnosticsPrintf(diag, "Box[pixi] contains unsupported plane count [%u]", pixi->planeCount); |
2779 | 2 | return AVIF_RESULT_NOT_IMPLEMENTED; |
2780 | 2 | } |
2781 | 8.86k | for (uint8_t i = 0; i < pixi->planeCount; ++i) { |
2782 | 6.38k | AVIF_CHECKERR(avifROStreamRead(&s, &pixi->planeDepths[i], 1), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int (8) bits_per_channel; |
2783 | 6.38k | if (pixi->planeDepths[i] == 0) { |
2784 | 1 | avifDiagnosticsPrintf(diag, "Box[pixi] plane depth shall not be 0 for channel %u", i); |
2785 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2786 | 1 | } |
2787 | 6.38k | if (pixi->planeDepths[i] > 16) { |
2788 | 2 | avifDiagnosticsPrintf(diag, "Box[pixi] plane depth %d is not supported", (int)pixi->planeDepths[i]); |
2789 | 2 | return AVIF_RESULT_NOT_IMPLEMENTED; |
2790 | 2 | } |
2791 | 6.38k | if (pixi->planeDepths[i] != pixi->planeDepths[0]) { |
2792 | 2 | avifDiagnosticsPrintf(diag, |
2793 | 2 | "Box[pixi] contains unsupported mismatched plane depths [%u != %u]", |
2794 | 2 | pixi->planeDepths[i], |
2795 | 2 | pixi->planeDepths[0]); |
2796 | 2 | return AVIF_RESULT_NOT_IMPLEMENTED; |
2797 | 2 | } |
2798 | 6.38k | } |
2799 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_EXTENDED_PIXI) |
2800 | | if (flags & 1) { |
2801 | | for (uint8_t i = 0; i < pixi->planeCount; ++i) { |
2802 | | uint8_t channelIdc, reserved, componentFormat, channelLabelFlag; |
2803 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &channelIdc, /*bitCount=*/3), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(3) channel_idc; |
2804 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &reserved, /*bitCount=*/1), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(1) reserved = 0; |
2805 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &componentFormat, /*bitCount=*/2), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(2) component_format; |
2806 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &pixi->subsamplingFlag[i], /*bitCount=*/1), |
2807 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(1) subsampling_flag; |
2808 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &channelLabelFlag, /*bitCount=*/1), |
2809 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(1) channel_label_flag; |
2810 | | if (pixi->subsamplingFlag[i]) { |
2811 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &pixi->subsamplingType[i], /*bitCount=*/4), |
2812 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(4) subsampling_type; |
2813 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &pixi->subsamplingLocation[i], /*bitCount=*/4), |
2814 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(4) subsampling_location; |
2815 | | } |
2816 | | |
2817 | | // ISO/IEC 23008-12:2024/CDAM 2:2025 section 6.5.6.3: |
2818 | | // This field indicates the contents of the channel. A value of 0 indicates colour/grayscale. A value of |
2819 | | // 1 indicates alpha. A value of 2 indicates depth. Values 3-7 are reserved for future use. At most one |
2820 | | // channel shall have a channel_idc of 1. |
2821 | | if (channelIdc != 0) { |
2822 | | avifDiagnosticsPrintf(diag, "Box[pixi] contains unsupported channel_idc %u for channel %u", channelIdc, i); |
2823 | | return AVIF_RESULT_NOT_IMPLEMENTED; |
2824 | | } |
2825 | | if (reserved != 0) { |
2826 | | avifDiagnosticsPrintf(diag, "Box[pixi] contains non-zero reserved field %u for channel %u", reserved, i); |
2827 | | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2828 | | } |
2829 | | // ISO/IEC 23008-12:2024/CDAM 2:2025 section 6.5.6.3: |
2830 | | // component_format: This field indicates the data type of the channel as defined by the component_format |
2831 | | // values in ISO/IEC 23001-17 where component_bit_depth is considered to be equal to bits_per_channel. |
2832 | | // ISO/IEC 23001-17 section 5.2.1.2: |
2833 | | // component_format: When equal to 0, component value is an unsigned integer coded on component_bit_depth bits. |
2834 | | if (componentFormat != 0) { |
2835 | | avifDiagnosticsPrintf(diag, "Box[pixi] contains unsupported component_format %u for channel %u", componentFormat, i); |
2836 | | return AVIF_RESULT_NOT_IMPLEMENTED; |
2837 | | } |
2838 | | if (pixi->subsamplingFlag[i]) { |
2839 | | if (pixi->subsamplingType[i] >= AVIF_PIXI_SUBSAMPLING_RESERVED) { |
2840 | | avifDiagnosticsPrintf(diag, |
2841 | | "Box[pixi] contains reserved subsampling_type %u for channel %u", |
2842 | | pixi->subsamplingType[i], |
2843 | | i); |
2844 | | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2845 | | } |
2846 | | if (pixi->subsamplingLocation[i] > 4) { |
2847 | | avifDiagnosticsPrintf(diag, |
2848 | | "Box[pixi] contains reserved subsampling_location %u for channel %u", |
2849 | | pixi->subsamplingLocation[i], |
2850 | | i); |
2851 | | return AVIF_RESULT_BMFF_PARSE_FAILED; |
2852 | | } |
2853 | | } |
2854 | | if (channelLabelFlag) { |
2855 | | AVIF_CHECKERR(avifROStreamReadString(&s, NULL, 0), AVIF_RESULT_BMFF_PARSE_FAILED); // utf8string channel_label; (skipped) |
2856 | | } |
2857 | | } |
2858 | | } |
2859 | | #endif // AVIF_ENABLE_EXPERIMENTAL_EXTENDED_PIXI |
2860 | 2.47k | return AVIF_RESULT_OK; |
2861 | 2.47k | } |
2862 | | |
2863 | | static avifBool avifParseOperatingPointSelectorProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
2864 | 86 | { |
2865 | 86 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[a1op]"); |
2866 | | |
2867 | 86 | avifOperatingPointSelectorProperty * a1op = &prop->u.a1op; |
2868 | 86 | AVIF_CHECK(avifROStreamRead(&s, &a1op->opIndex, 1)); |
2869 | 85 | if (a1op->opIndex > 31) { // 31 is AV1's max operating point value |
2870 | 1 | avifDiagnosticsPrintf(diag, "Box[a1op] contains an unsupported operating point [%u]", a1op->opIndex); |
2871 | 1 | return AVIF_FALSE; |
2872 | 1 | } |
2873 | 84 | return AVIF_TRUE; |
2874 | 85 | } |
2875 | | |
2876 | | static avifBool avifParseLayerSelectorProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
2877 | 200 | { |
2878 | 200 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[lsel]"); |
2879 | | |
2880 | 200 | avifLayerSelectorProperty * lsel = &prop->u.lsel; |
2881 | 200 | AVIF_CHECK(avifROStreamReadU16(&s, &lsel->layerID)); |
2882 | 199 | if ((lsel->layerID != 0xFFFF) && (lsel->layerID >= AVIF_MAX_AV1_LAYER_COUNT)) { |
2883 | 12 | avifDiagnosticsPrintf(diag, "Box[lsel] contains an unsupported layer [%u]", lsel->layerID); |
2884 | 12 | return AVIF_FALSE; |
2885 | 12 | } |
2886 | 187 | return AVIF_TRUE; |
2887 | 199 | } |
2888 | | |
2889 | | static avifBool avifParseAV1LayeredImageIndexingProperty(avifProperty * prop, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
2890 | 124 | { |
2891 | 124 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[a1lx]"); |
2892 | | |
2893 | 124 | avifAV1LayeredImageIndexingProperty * a1lx = &prop->u.a1lx; |
2894 | | |
2895 | 124 | uint8_t largeSize = 0; |
2896 | 124 | AVIF_CHECK(avifROStreamRead(&s, &largeSize, 1)); |
2897 | 123 | if (largeSize & 0xFE) { |
2898 | 1 | avifDiagnosticsPrintf(diag, "Box[a1lx] has bits set in the reserved section [%u]", largeSize); |
2899 | 1 | return AVIF_FALSE; |
2900 | 1 | } |
2901 | | |
2902 | 480 | for (int i = 0; i < 3; ++i) { |
2903 | 363 | if (largeSize) { |
2904 | 188 | AVIF_CHECK(avifROStreamReadU32(&s, &a1lx->layerSize[i])); |
2905 | 188 | } else { |
2906 | 175 | uint16_t layerSize16; |
2907 | 175 | AVIF_CHECK(avifROStreamReadU16(&s, &layerSize16)); |
2908 | 172 | a1lx->layerSize[i] = (uint32_t)layerSize16; |
2909 | 172 | } |
2910 | 363 | } |
2911 | | |
2912 | | // Layer sizes will be validated later (when the item's size is known) |
2913 | 117 | return AVIF_TRUE; |
2914 | 122 | } |
2915 | | |
2916 | | static avifResult avifParseItemPropertyContainerBox(avifPropertyArray * properties, |
2917 | | uint64_t rawOffset, |
2918 | | const uint8_t * raw, |
2919 | | size_t rawLen, |
2920 | | avifBool isTrack, |
2921 | | avifDiagnostics * diag) |
2922 | 15.1k | { |
2923 | 15.1k | BEGIN_STREAM(s, raw, rawLen, diag, "Box[ipco]"); |
2924 | | |
2925 | 78.0k | while (avifROStreamHasBytesLeft(&s, 1)) { |
2926 | 62.9k | avifBoxHeader header; |
2927 | 62.9k | AVIF_CHECKERR(avifROStreamReadBoxHeader(&s, &header), AVIF_RESULT_BMFF_PARSE_FAILED); |
2928 | | |
2929 | 62.9k | avifProperty * prop = (avifProperty *)avifArrayPush(properties); |
2930 | 62.9k | AVIF_CHECKERR(prop != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
2931 | 62.9k | memcpy(prop->type, header.type, 4); |
2932 | 62.9k | prop->isOpaque = AVIF_FALSE; |
2933 | 62.9k | if (!memcmp(header.type, "ispe", 4)) { |
2934 | 15.4k | AVIF_CHECKERR(avifParseImageSpatialExtentsProperty(prop, avifROStreamCurrent(&s), header.size, diag), |
2935 | 15.4k | AVIF_RESULT_BMFF_PARSE_FAILED); |
2936 | 47.4k | } else if ((!memcmp(header.type, "auxC", 4) && !isTrack) || (!memcmp(header.type, "auxi", 4) && isTrack)) { |
2937 | 311 | AVIF_CHECKERR(avifParseAuxiliaryTypeProperty(prop, avifROStreamCurrent(&s), header.size, diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
2938 | 47.1k | } else if (!memcmp(header.type, "colr", 4)) { |
2939 | 5.24k | AVIF_CHECKERR(avifParseColourInformationBox(prop, rawOffset + avifROStreamOffset(&s), avifROStreamCurrent(&s), header.size, diag), |
2940 | 5.24k | AVIF_RESULT_BMFF_PARSE_FAILED); |
2941 | 41.8k | } else if (!memcmp(header.type, "av1C", 4)) { |
2942 | 15.3k | AVIF_CHECKERR(avifParseCodecConfigurationBoxProperty(prop, avifROStreamCurrent(&s), header.size, "av1C", diag), |
2943 | 15.3k | AVIF_RESULT_BMFF_PARSE_FAILED); |
2944 | | #if defined(AVIF_CODEC_AVM) |
2945 | | } else if (!memcmp(header.type, "av2C", 4)) { |
2946 | | AVIF_CHECKERR(avifParseCodecConfigurationBoxProperty(prop, avifROStreamCurrent(&s), header.size, "av2C", diag), |
2947 | | AVIF_RESULT_BMFF_PARSE_FAILED); |
2948 | | #endif |
2949 | 26.5k | } else if (!memcmp(header.type, "pasp", 4)) { |
2950 | 669 | AVIF_CHECKERR(avifParsePixelAspectRatioBoxProperty(prop, avifROStreamCurrent(&s), header.size, diag), |
2951 | 669 | AVIF_RESULT_BMFF_PARSE_FAILED); |
2952 | 25.8k | } else if (!memcmp(header.type, "clap", 4)) { |
2953 | 25 | AVIF_CHECKERR(avifParseCleanApertureBoxProperty(prop, avifROStreamCurrent(&s), header.size, diag), |
2954 | 25 | AVIF_RESULT_BMFF_PARSE_FAILED); |
2955 | 25.8k | } else if (!memcmp(header.type, "irot", 4)) { |
2956 | 270 | AVIF_CHECKERR(avifParseImageRotationProperty(prop, avifROStreamCurrent(&s), header.size, diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
2957 | 25.5k | } else if (!memcmp(header.type, "imir", 4)) { |
2958 | 69 | AVIF_CHECKERR(avifParseImageMirrorProperty(prop, avifROStreamCurrent(&s), header.size, diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
2959 | 25.5k | } else if (!memcmp(header.type, "pixi", 4)) { |
2960 | 2.48k | AVIF_CHECKRES(avifParsePixelInformationProperty(prop, avifROStreamCurrent(&s), header.size, diag)); |
2961 | 23.0k | } else if (!memcmp(header.type, "a1op", 4)) { |
2962 | 86 | AVIF_CHECKERR(avifParseOperatingPointSelectorProperty(prop, avifROStreamCurrent(&s), header.size, diag), |
2963 | 86 | AVIF_RESULT_BMFF_PARSE_FAILED); |
2964 | 22.9k | } else if (!memcmp(header.type, "lsel", 4)) { |
2965 | 200 | AVIF_CHECKERR(avifParseLayerSelectorProperty(prop, avifROStreamCurrent(&s), header.size, diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
2966 | 22.7k | } else if (!memcmp(header.type, "a1lx", 4)) { |
2967 | 124 | AVIF_CHECKERR(avifParseAV1LayeredImageIndexingProperty(prop, avifROStreamCurrent(&s), header.size, diag), |
2968 | 124 | AVIF_RESULT_BMFF_PARSE_FAILED); |
2969 | 22.6k | } else if (!memcmp(header.type, "clli", 4)) { |
2970 | 240 | AVIF_CHECKRES(avifParseContentLightLevelInformationBox(prop, avifROStreamCurrent(&s), header.size, diag)); |
2971 | 22.3k | } else { |
2972 | 22.3k | prop->isOpaque = AVIF_TRUE; |
2973 | 22.3k | memset(&prop->u.opaque, 0, sizeof(prop->u.opaque)); |
2974 | 22.3k | memcpy(prop->u.opaque.usertype, header.usertype, sizeof(prop->u.opaque.usertype)); |
2975 | 22.3k | AVIF_CHECKRES(avifRWDataSet(&prop->u.opaque.boxPayload, avifROStreamCurrent(&s), header.size)); |
2976 | 22.3k | } |
2977 | | |
2978 | 62.8k | AVIF_CHECKERR(avifROStreamSkip(&s, header.size), AVIF_RESULT_BMFF_PARSE_FAILED); |
2979 | 62.8k | } |
2980 | 15.0k | return AVIF_RESULT_OK; |
2981 | 15.1k | } |
2982 | | |
2983 | | static avifResult avifParseItemPropertyAssociation(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag, uint32_t * outVersionAndFlags) |
2984 | 14.7k | { |
2985 | | // NOTE: If this function ever adds support for versions other than [0,1] or flags other than |
2986 | | // [0,1], please increase the value of MAX_IPMA_VERSION_AND_FLAGS_SEEN accordingly. |
2987 | | |
2988 | 14.7k | BEGIN_STREAM(s, raw, rawLen, diag, "Box[ipma]"); |
2989 | | |
2990 | 14.7k | uint8_t version; |
2991 | 14.7k | uint32_t flags; |
2992 | 14.7k | AVIF_CHECKERR(avifROStreamReadVersionAndFlags(&s, &version, &flags), AVIF_RESULT_BMFF_PARSE_FAILED); |
2993 | 14.7k | avifBool propertyIndexIsU15 = ((flags & 0x1) != 0); |
2994 | 14.7k | *outVersionAndFlags = ((uint32_t)version << 24) | flags; |
2995 | | |
2996 | 14.7k | uint32_t entryCount; |
2997 | 14.7k | AVIF_CHECKERR(avifROStreamReadU32(&s, &entryCount), AVIF_RESULT_BMFF_PARSE_FAILED); |
2998 | 14.7k | unsigned int prevItemID = 0; |
2999 | 34.3k | for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) { |
3000 | | // ISO/IEC 14496-12, Seventh edition, 2022-01, Section 8.11.14.1: |
3001 | | // Each ItemPropertyAssociationBox shall be ordered by increasing item_ID, and there shall |
3002 | | // be at most one occurrence of a given item_ID, in the set of ItemPropertyAssociationBox |
3003 | | // boxes. |
3004 | 19.6k | unsigned int itemID; |
3005 | 19.6k | if (version < 1) { |
3006 | 19.5k | uint16_t tmp; |
3007 | 19.5k | AVIF_CHECKERR(avifROStreamReadU16(&s, &tmp), AVIF_RESULT_BMFF_PARSE_FAILED); |
3008 | 19.5k | itemID = tmp; |
3009 | 19.5k | } else { |
3010 | 154 | AVIF_CHECKERR(avifROStreamReadU32(&s, &itemID), AVIF_RESULT_BMFF_PARSE_FAILED); |
3011 | 154 | } |
3012 | 19.6k | AVIF_CHECKRES(avifCheckItemID("ipma", itemID, diag)); |
3013 | 19.6k | if (itemID <= prevItemID) { |
3014 | 10 | avifDiagnosticsPrintf(diag, "Box[ipma] item IDs are not ordered by increasing ID"); |
3015 | 10 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3016 | 10 | } |
3017 | 19.6k | prevItemID = itemID; |
3018 | | |
3019 | 19.6k | avifDecoderItem * item; |
3020 | 19.6k | AVIF_CHECKRES(avifMetaFindOrCreateItem(meta, itemID, &item)); |
3021 | 19.6k | if (item->ipmaSeen) { |
3022 | 1 | avifDiagnosticsPrintf(diag, "Duplicate Box[ipma] for item ID [%u]", itemID); |
3023 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3024 | 1 | } |
3025 | 19.6k | item->ipmaSeen = AVIF_TRUE; |
3026 | | |
3027 | 19.6k | uint8_t associationCount; |
3028 | 19.6k | AVIF_CHECKERR(avifROStreamRead(&s, &associationCount, 1), AVIF_RESULT_BMFF_PARSE_FAILED); |
3029 | 99.6k | for (uint8_t associationIndex = 0; associationIndex < associationCount; ++associationIndex) { |
3030 | 80.0k | uint8_t essential; |
3031 | 80.0k | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &essential, /*bitCount=*/1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) essential; |
3032 | 80.0k | uint32_t propertyIndex; |
3033 | 80.0k | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &propertyIndex, /*bitCount=*/propertyIndexIsU15 ? 15 : 7), |
3034 | 80.0k | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(7/15) property_index; |
3035 | | |
3036 | | // ISO/IEC 14496-12 Section 8.11.14.3: |
3037 | | // 0 indicating that no property is associated (the essential indicator shall also be 0) |
3038 | 80.0k | if (propertyIndex == 0) { |
3039 | 4.92k | if (essential) { |
3040 | 2 | avifDiagnosticsPrintf(diag, "Box[ipma] for item ID [%u] contains an illegal essential property index 0", itemID); |
3041 | 2 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3042 | 2 | } |
3043 | 4.91k | continue; |
3044 | 4.92k | } |
3045 | 75.1k | --propertyIndex; // 1-indexed |
3046 | | |
3047 | 75.1k | if (propertyIndex >= meta->properties.count) { |
3048 | 49 | avifDiagnosticsPrintf(diag, |
3049 | 49 | "Box[ipma] for item ID [%u] contains an illegal property index [%u] (out of [%u] properties)", |
3050 | 49 | itemID, |
3051 | 49 | propertyIndex, |
3052 | 49 | meta->properties.count); |
3053 | 49 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3054 | 49 | } |
3055 | | |
3056 | | // Copy property to item |
3057 | 75.0k | const avifProperty * srcProp = &meta->properties.prop[propertyIndex]; |
3058 | | |
3059 | | // Some properties are supported and parsed by libavif. |
3060 | | // Other properties are forwarded to the user as opaque blobs. |
3061 | 75.0k | const avifBool supportedType = !srcProp->isOpaque; |
3062 | 75.0k | if (supportedType) { |
3063 | 57.4k | if (essential) { |
3064 | | // Verify that it is legal for this property to be flagged as essential. Any |
3065 | | // types in this list are *required* in the spec to not be flagged as essential |
3066 | | // when associated with an item. |
3067 | 23.4k | static const char * const nonessentialTypes[] = { |
3068 | | |
3069 | | // AVIF: Section 2.3.2.3.2: "If associated, it shall not be marked as essential." |
3070 | 23.4k | "a1lx" |
3071 | | |
3072 | 23.4k | }; |
3073 | 23.4k | size_t nonessentialTypesCount = sizeof(nonessentialTypes) / sizeof(nonessentialTypes[0]); |
3074 | 46.9k | for (size_t i = 0; i < nonessentialTypesCount; ++i) { |
3075 | 23.4k | if (!memcmp(srcProp->type, nonessentialTypes[i], 4)) { |
3076 | 1 | avifDiagnosticsPrintf(diag, |
3077 | 1 | "Item ID [%u] has a %s property association which must not be marked essential, but is", |
3078 | 1 | itemID, |
3079 | 1 | nonessentialTypes[i]); |
3080 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3081 | 1 | } |
3082 | 23.4k | } |
3083 | 33.9k | } else { |
3084 | | // Verify that it is legal for this property to not be flagged as essential. Any |
3085 | | // types in this list are *required* in the spec to be flagged as essential when |
3086 | | // associated with an item. |
3087 | 33.9k | static const char * const essentialTypes[] = { |
3088 | | |
3089 | | // AVIF: Section 2.3.2.1.1: "If associated, it shall be marked as essential." |
3090 | 33.9k | "a1op", |
3091 | | |
3092 | | // HEIF: Section 6.5.11.1: "essential shall be equal to 1 for an 'lsel' item property." |
3093 | 33.9k | "lsel", |
3094 | | |
3095 | | // MIAF 2019/Amd. 2:2021: Section 7.3.9: |
3096 | | // All transformative properties associated with coded and derived images shall be |
3097 | | // marked as essential |
3098 | | // It makes no sense to allow for non-essential crop/orientation associated with an item |
3099 | | // that is not a coded or derived image, so for simplicity 'item' is not checked here. |
3100 | 33.9k | "clap", |
3101 | 33.9k | "irot", |
3102 | 33.9k | "imir" |
3103 | | |
3104 | 33.9k | }; |
3105 | 33.9k | size_t essentialTypesCount = sizeof(essentialTypes) / sizeof(essentialTypes[0]); |
3106 | 203k | for (size_t i = 0; i < essentialTypesCount; ++i) { |
3107 | 169k | if (!memcmp(srcProp->type, essentialTypes[i], 4)) { |
3108 | 5 | avifDiagnosticsPrintf(diag, |
3109 | 5 | "Item ID [%u] has a %s property association which must be marked essential, but is not", |
3110 | 5 | itemID, |
3111 | 5 | essentialTypes[i]); |
3112 | 5 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3113 | 5 | } |
3114 | 169k | } |
3115 | 33.9k | } |
3116 | | |
3117 | | // Supported and valid; associate it with this item. |
3118 | 57.4k | avifProperty * dstProp = (avifProperty *)avifArrayPush(&item->properties); |
3119 | 57.4k | AVIF_CHECKERR(dstProp != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
3120 | 57.4k | *dstProp = *srcProp; |
3121 | 57.4k | } else { |
3122 | 17.6k | if (essential) { |
3123 | | // ISO/IEC 23008-12 Section 10.2.1: |
3124 | | // Under any brand, the primary item (or an alternative if alternative support is required) |
3125 | | // shall be processable by a reader implementing only the required features of that brand. |
3126 | | // Specifically, given that each brand has a set of properties that a reader is required to |
3127 | | // support: the item shall not have properties that are marked as essential and are outside |
3128 | | // this set. |
3129 | | // It is assumed that this rule also applies to items the primary item depends on (such as |
3130 | | // the cells of a grid). |
3131 | | |
3132 | | // Discovered an essential item property that libavif doesn't support! |
3133 | | // Make a note to ignore this item later. |
3134 | 836 | item->hasUnsupportedEssentialProperty = AVIF_TRUE; |
3135 | 836 | } |
3136 | | |
3137 | | // Will be forwarded to the user through avifImage::properties. |
3138 | 17.6k | avifProperty * dstProp = (avifProperty *)avifArrayPush(&item->properties); |
3139 | 17.6k | AVIF_CHECKERR(dstProp != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
3140 | 17.6k | dstProp->isOpaque = AVIF_TRUE; |
3141 | 17.6k | memcpy(dstProp->type, srcProp->type, sizeof(dstProp->type)); |
3142 | 17.6k | memcpy(dstProp->u.opaque.usertype, srcProp->u.opaque.usertype, sizeof(dstProp->u.opaque.usertype)); |
3143 | 17.6k | AVIF_CHECKRES( |
3144 | 17.6k | avifRWDataSet(&dstProp->u.opaque.boxPayload, srcProp->u.opaque.boxPayload.data, srcProp->u.opaque.boxPayload.size)); |
3145 | 17.6k | } |
3146 | 75.0k | } |
3147 | 19.6k | } |
3148 | 14.6k | return AVIF_RESULT_OK; |
3149 | 14.7k | } |
3150 | | |
3151 | | static avifBool avifParsePrimaryItemBox(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3152 | 14.8k | { |
3153 | 14.8k | if (meta->primaryItemID > 0) { |
3154 | | // Illegal to have multiple pitm boxes, bail out |
3155 | 1 | avifDiagnosticsPrintf(diag, "Multiple boxes of unique Box[pitm] found"); |
3156 | 1 | return AVIF_FALSE; |
3157 | 1 | } |
3158 | | |
3159 | 14.8k | BEGIN_STREAM(s, raw, rawLen, diag, "Box[pitm]"); |
3160 | | |
3161 | 14.8k | uint8_t version; |
3162 | 14.8k | AVIF_CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
3163 | | |
3164 | 14.8k | if (version == 0) { |
3165 | 14.8k | uint16_t tmp16; |
3166 | 14.8k | AVIF_CHECK(avifROStreamReadU16(&s, &tmp16)); // unsigned int(16) item_ID; |
3167 | 14.8k | meta->primaryItemID = tmp16; |
3168 | 14.8k | } else { |
3169 | 10 | AVIF_CHECK(avifROStreamReadU32(&s, &meta->primaryItemID)); // unsigned int(32) item_ID; |
3170 | 10 | } |
3171 | 14.8k | return AVIF_TRUE; |
3172 | 14.8k | } |
3173 | | |
3174 | | static avifBool avifParseItemDataBox(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3175 | 112 | { |
3176 | | // Check to see if we've already seen an idat box for this meta box. If so, bail out |
3177 | 112 | if (meta->idat.size > 0) { |
3178 | 0 | avifDiagnosticsPrintf(diag, "Meta box contains multiple idat boxes"); |
3179 | 0 | return AVIF_FALSE; |
3180 | 0 | } |
3181 | 112 | if (rawLen == 0) { |
3182 | 1 | avifDiagnosticsPrintf(diag, "idat box has a length of 0"); |
3183 | 1 | return AVIF_FALSE; |
3184 | 1 | } |
3185 | | |
3186 | 111 | if (avifRWDataSet(&meta->idat, raw, rawLen) != AVIF_RESULT_OK) { |
3187 | 0 | return AVIF_FALSE; |
3188 | 0 | } |
3189 | 111 | return AVIF_TRUE; |
3190 | 111 | } |
3191 | | |
3192 | | static avifResult avifParseItemPropertiesBox(avifMeta * meta, uint64_t rawOffset, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3193 | 14.8k | { |
3194 | 14.8k | BEGIN_STREAM(s, raw, rawLen, diag, "Box[iprp]"); |
3195 | | |
3196 | 14.8k | avifBoxHeader ipcoHeader; |
3197 | 14.8k | AVIF_CHECKERR(avifROStreamReadBoxHeader(&s, &ipcoHeader), AVIF_RESULT_BMFF_PARSE_FAILED); |
3198 | 14.8k | if (memcmp(ipcoHeader.type, "ipco", 4)) { |
3199 | 1 | avifDiagnosticsPrintf(diag, "Failed to find Box[ipco] as the first box in Box[iprp]"); |
3200 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3201 | 1 | } |
3202 | | |
3203 | | // Read all item properties inside of ItemPropertyContainerBox |
3204 | 14.8k | AVIF_CHECKRES(avifParseItemPropertyContainerBox(&meta->properties, |
3205 | 14.8k | rawOffset + avifROStreamOffset(&s), |
3206 | 14.8k | avifROStreamCurrent(&s), |
3207 | 14.8k | ipcoHeader.size, |
3208 | 14.8k | /*isTrack=*/AVIF_FALSE, |
3209 | 14.8k | diag)); |
3210 | 14.7k | AVIF_CHECKERR(avifROStreamSkip(&s, ipcoHeader.size), AVIF_RESULT_BMFF_PARSE_FAILED); |
3211 | | |
3212 | 14.7k | uint32_t versionAndFlagsSeen[MAX_IPMA_VERSION_AND_FLAGS_SEEN]; |
3213 | 14.7k | uint32_t versionAndFlagsSeenCount = 0; |
3214 | | |
3215 | | // Now read all ItemPropertyAssociation until the end of the box, and make associations |
3216 | 29.3k | while (avifROStreamHasBytesLeft(&s, 1)) { |
3217 | 14.7k | avifBoxHeader ipmaHeader; |
3218 | 14.7k | AVIF_CHECKERR(avifROStreamReadBoxHeader(&s, &ipmaHeader), AVIF_RESULT_BMFF_PARSE_FAILED); |
3219 | | |
3220 | 14.7k | if (!memcmp(ipmaHeader.type, "ipma", 4)) { |
3221 | 14.7k | uint32_t versionAndFlags; |
3222 | 14.7k | AVIF_CHECKRES(avifParseItemPropertyAssociation(meta, avifROStreamCurrent(&s), ipmaHeader.size, diag, &versionAndFlags)); |
3223 | 14.6k | for (uint32_t i = 0; i < versionAndFlagsSeenCount; ++i) { |
3224 | 40 | if (versionAndFlagsSeen[i] == versionAndFlags) { |
3225 | | // BMFF (ISO/IEC 14496-12:2022) 8.11.14.1 - There shall be at most one |
3226 | | // ItemPropertyAssociationBox with a given pair of values of version and |
3227 | | // flags. |
3228 | 2 | avifDiagnosticsPrintf(diag, "Multiple Box[ipma] with a given pair of values of version and flags. See BMFF (ISO/IEC 14496-12:2022) 8.11.14.1"); |
3229 | 2 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3230 | 2 | } |
3231 | 40 | } |
3232 | 14.6k | if (versionAndFlagsSeenCount == MAX_IPMA_VERSION_AND_FLAGS_SEEN) { |
3233 | 1 | avifDiagnosticsPrintf(diag, "Exceeded possible count of unique ipma version and flags tuples"); |
3234 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3235 | 1 | } |
3236 | 14.6k | versionAndFlagsSeen[versionAndFlagsSeenCount] = versionAndFlags; |
3237 | 14.6k | ++versionAndFlagsSeenCount; |
3238 | 14.6k | } else { |
3239 | | // These must all be type ipma |
3240 | 4 | avifDiagnosticsPrintf(diag, "Box[iprp] contains a box that isn't type 'ipma'"); |
3241 | 4 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3242 | 4 | } |
3243 | | |
3244 | 14.6k | AVIF_CHECKERR(avifROStreamSkip(&s, ipmaHeader.size), AVIF_RESULT_BMFF_PARSE_FAILED); |
3245 | 14.6k | } |
3246 | 14.5k | return AVIF_RESULT_OK; |
3247 | 14.7k | } |
3248 | | |
3249 | | static avifResult avifParseItemInfoEntry(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3250 | 21.3k | { |
3251 | | // Section 8.11.6.2 of ISO/IEC 14496-12. |
3252 | 21.3k | BEGIN_STREAM(s, raw, rawLen, diag, "Box[infe]"); |
3253 | | |
3254 | 21.3k | uint8_t version; |
3255 | 21.3k | uint32_t flags; |
3256 | 21.3k | AVIF_CHECKERR(avifROStreamReadVersionAndFlags(&s, &version, &flags), AVIF_RESULT_BMFF_PARSE_FAILED); |
3257 | | // Version 2+ is required for item_type |
3258 | 21.3k | if (version != 2 && version != 3) { |
3259 | 3 | avifDiagnosticsPrintf(s.diag, "%s: Expecting box version 2 or 3, got version %u", s.diagContext, version); |
3260 | 3 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3261 | 3 | } |
3262 | | // Ignore flags&1. A value of 1 corresponds to a hidden image item (not intended to be displayed). |
3263 | | // There could be files wrongly setting that flag to 1 for items output as "to be displayed" |
3264 | | // by libavif so far, so keep that lenient behavior for simplicity and backward compatibility. |
3265 | | |
3266 | 21.3k | uint32_t itemID; |
3267 | 21.3k | if (version == 2) { |
3268 | 21.3k | uint16_t tmp; |
3269 | 21.3k | AVIF_CHECKERR(avifROStreamReadU16(&s, &tmp), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) item_ID; |
3270 | 21.3k | itemID = tmp; |
3271 | 21.3k | } else { |
3272 | 13 | AVIF_ASSERT_OR_RETURN(version == 3); |
3273 | 13 | AVIF_CHECKERR(avifROStreamReadU32(&s, &itemID), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) item_ID; |
3274 | 13 | } |
3275 | 21.3k | AVIF_CHECKRES(avifCheckItemID("infe", itemID, diag)); |
3276 | 21.3k | uint16_t itemProtectionIndex; |
3277 | 21.3k | AVIF_CHECKERR(avifROStreamReadU16(&s, &itemProtectionIndex), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) item_protection_index; |
3278 | 21.3k | uint8_t itemType[4]; |
3279 | 21.3k | AVIF_CHECKERR(avifROStreamRead(&s, itemType, 4), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) item_type; |
3280 | 21.3k | AVIF_CHECKERR(avifROStreamReadString(&s, NULL, 0), AVIF_RESULT_BMFF_PARSE_FAILED); // utf8string item_name; (skipped) |
3281 | 21.3k | avifContentType contentType; |
3282 | 21.3k | if (!memcmp(itemType, "mime", 4)) { |
3283 | 713 | AVIF_CHECKERR(avifROStreamReadString(&s, contentType.contentType, CONTENTTYPE_SIZE), AVIF_RESULT_BMFF_PARSE_FAILED); // utf8string content_type; |
3284 | | // utf8string content_encoding; //optional |
3285 | 20.6k | } else { |
3286 | | // if (item_type == 'uri ') { |
3287 | | // utf8string item_uri_type; |
3288 | | // } |
3289 | 20.6k | memset(&contentType, 0, sizeof(contentType)); |
3290 | 20.6k | } |
3291 | | |
3292 | 21.3k | avifDecoderItem * item; |
3293 | 21.3k | AVIF_CHECKRES(avifMetaFindOrCreateItem(meta, itemID, &item)); |
3294 | | |
3295 | 21.3k | memcpy(item->type, itemType, sizeof(itemType)); |
3296 | 21.3k | item->contentType = contentType; |
3297 | 21.3k | return AVIF_RESULT_OK; |
3298 | 21.3k | } |
3299 | | |
3300 | | static avifResult avifParseItemInfoBox(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3301 | 14.7k | { |
3302 | 14.7k | BEGIN_STREAM(s, raw, rawLen, diag, "Box[iinf]"); |
3303 | | |
3304 | 14.7k | uint8_t version; |
3305 | 14.7k | AVIF_CHECKERR(avifROStreamReadVersionAndFlags(&s, &version, NULL), AVIF_RESULT_BMFF_PARSE_FAILED); |
3306 | 14.7k | uint32_t entryCount; |
3307 | 14.7k | if (version == 0) { |
3308 | 14.3k | uint16_t tmp; |
3309 | 14.3k | AVIF_CHECKERR(avifROStreamReadU16(&s, &tmp), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) entry_count; |
3310 | 14.3k | entryCount = tmp; |
3311 | 14.3k | } else if (version == 1) { |
3312 | 323 | AVIF_CHECKERR(avifROStreamReadU32(&s, &entryCount), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) entry_count; |
3313 | 323 | } else { |
3314 | 1 | avifDiagnosticsPrintf(diag, "Box[iinf] has an unsupported version %u", version); |
3315 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3316 | 1 | } |
3317 | | |
3318 | 36.1k | for (uint32_t entryIndex = 0; entryIndex < entryCount; ++entryIndex) { |
3319 | 21.4k | avifBoxHeader infeHeader; |
3320 | 21.4k | AVIF_CHECKERR(avifROStreamReadBoxHeader(&s, &infeHeader), AVIF_RESULT_BMFF_PARSE_FAILED); |
3321 | | |
3322 | 21.3k | if (!memcmp(infeHeader.type, "infe", 4)) { |
3323 | 21.3k | AVIF_CHECKRES(avifParseItemInfoEntry(meta, avifROStreamCurrent(&s), infeHeader.size, diag)); |
3324 | 21.3k | } else { |
3325 | | // These must all be type infe |
3326 | 2 | avifDiagnosticsPrintf(diag, "Box[iinf] contains a box that isn't type 'infe'"); |
3327 | 2 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3328 | 2 | } |
3329 | | |
3330 | 21.3k | AVIF_CHECKERR(avifROStreamSkip(&s, infeHeader.size), AVIF_RESULT_BMFF_PARSE_FAILED); |
3331 | 21.3k | } |
3332 | | |
3333 | 14.6k | return AVIF_RESULT_OK; |
3334 | 14.7k | } |
3335 | | |
3336 | | static avifResult avifParseItemReferenceBox(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3337 | 1.44k | { |
3338 | 1.44k | BEGIN_STREAM(s, raw, rawLen, diag, "Box[iref]"); |
3339 | | |
3340 | 1.44k | uint8_t version; |
3341 | 1.44k | AVIF_CHECKERR(avifROStreamReadVersionAndFlags(&s, &version, NULL), AVIF_RESULT_BMFF_PARSE_FAILED); |
3342 | 1.44k | if (version > 1) { |
3343 | | // iref versions > 1 are not supported. Skip it. |
3344 | 134 | return AVIF_RESULT_OK; |
3345 | 134 | } |
3346 | | |
3347 | 5.46k | while (avifROStreamHasBytesLeft(&s, 1)) { |
3348 | 4.27k | avifBoxHeader irefHeader; |
3349 | 4.27k | AVIF_CHECKERR(avifROStreamReadBoxHeader(&s, &irefHeader), AVIF_RESULT_BMFF_PARSE_FAILED); |
3350 | | |
3351 | 4.26k | uint32_t fromID = 0; |
3352 | 4.26k | if (version == 0) { |
3353 | 4.17k | uint16_t tmp; |
3354 | 4.17k | AVIF_CHECKERR(avifROStreamReadU16(&s, &tmp), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) from_item_ID; |
3355 | 4.17k | fromID = tmp; |
3356 | 4.17k | } else { |
3357 | | // version == 1 |
3358 | 82 | AVIF_CHECKERR(avifROStreamReadU32(&s, &fromID), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) from_item_ID; |
3359 | 82 | } |
3360 | | // ISO 14496-12 section 8.11.12.1: "index values start at 1" |
3361 | 4.25k | AVIF_CHECKRES(avifCheckItemID("iref", fromID, diag)); |
3362 | | |
3363 | 4.25k | avifDecoderItem * item; |
3364 | 4.25k | AVIF_CHECKRES(avifMetaFindOrCreateItem(meta, fromID, &item)); |
3365 | 4.25k | if (!memcmp(irefHeader.type, "dimg", 4)) { |
3366 | 343 | if (item->hasDimgFrom) { |
3367 | | // ISO/IEC 23008-12 (HEIF) 6.6.1: The number of SingleItemTypeReferenceBoxes with the box type 'dimg' |
3368 | | // and with the same value of from_item_ID shall not be greater than 1. |
3369 | 1 | avifDiagnosticsPrintf(diag, "Box[iinf] contains duplicate boxes of type 'dimg' with the same from_item_ID value %u", fromID); |
3370 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3371 | 1 | } |
3372 | 342 | item->hasDimgFrom = AVIF_TRUE; |
3373 | 342 | } |
3374 | | |
3375 | 4.25k | uint16_t referenceCount = 0; |
3376 | 4.25k | AVIF_CHECKERR(avifROStreamReadU16(&s, &referenceCount), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) reference_count; |
3377 | | |
3378 | 12.5k | for (uint16_t refIndex = 0; refIndex < referenceCount; ++refIndex) { |
3379 | 8.42k | uint32_t toID = 0; |
3380 | 8.42k | if (version == 0) { |
3381 | 8.17k | uint16_t tmp; |
3382 | 8.17k | AVIF_CHECKERR(avifROStreamReadU16(&s, &tmp), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(16) to_item_ID; |
3383 | 8.15k | toID = tmp; |
3384 | 8.15k | } else { |
3385 | | // version == 1 |
3386 | 257 | AVIF_CHECKERR(avifROStreamReadU32(&s, &toID), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) to_item_ID; |
3387 | 257 | } |
3388 | 8.36k | AVIF_CHECKRES(avifCheckItemID("iref", toID, diag)); |
3389 | | |
3390 | | // Read this reference as "{fromID} is a {irefType} for {toID}" |
3391 | 8.33k | if (!memcmp(irefHeader.type, "thmb", 4)) { |
3392 | 467 | item->thumbnailForID = toID; |
3393 | 7.87k | } else if (!memcmp(irefHeader.type, "auxl", 4)) { |
3394 | 2.19k | item->auxForID = toID; |
3395 | 5.67k | } else if (!memcmp(irefHeader.type, "cdsc", 4)) { |
3396 | 1.77k | item->descForID = toID; |
3397 | 3.90k | } else if (!memcmp(irefHeader.type, "dimg", 4)) { |
3398 | | // derived images refer in the opposite direction |
3399 | 3.26k | avifDecoderItem * dimg; |
3400 | 3.26k | AVIF_CHECKRES(avifMetaFindOrCreateItem(meta, toID, &dimg)); |
3401 | | |
3402 | | // Section 8.11.12.1 of ISO/IEC 14496-12: |
3403 | | // The items linked to are then represented by an array of to_item_IDs; |
3404 | | // within a given array, a given value shall occur at most once. |
3405 | 3.26k | AVIF_CHECKERR(dimg->dimgForID != fromID, AVIF_RESULT_INVALID_IMAGE_GRID); |
3406 | | // A given value may occur within multiple arrays but this is not supported by libavif. |
3407 | 3.26k | AVIF_CHECKERR(dimg->dimgForID == 0, AVIF_RESULT_NOT_IMPLEMENTED); |
3408 | 3.26k | dimg->dimgForID = fromID; |
3409 | 3.26k | dimg->dimgIdx = refIndex; |
3410 | 3.26k | } else if (!memcmp(irefHeader.type, "prem", 4)) { |
3411 | 216 | item->premByID = toID; |
3412 | 216 | } |
3413 | 8.33k | } |
3414 | 4.25k | } |
3415 | | |
3416 | 1.18k | return AVIF_RESULT_OK; |
3417 | 1.30k | } |
3418 | | |
3419 | | static avifResult avifParseGroupsListBox(avifMeta * meta, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3420 | 33 | { |
3421 | 33 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[grpl]"); |
3422 | | |
3423 | 51 | while (avifROStreamHasBytesLeft(&s, 1)) { |
3424 | 50 | avifBoxHeader groupHeader; |
3425 | 50 | AVIF_CHECKERR(avifROStreamReadBoxHeader(&s, &groupHeader), AVIF_RESULT_BMFF_PARSE_FAILED); |
3426 | | // We don't check the flag or version as they depend on the grouping type (and for simplicity). |
3427 | | // ISO/IEC 14496-12:2024 Section 8.15.3.2 |
3428 | | // version shall be 0 unless defined otherwise for the grouping_type. Any values of flags such that |
3429 | | // (flags & 0x000FFF) is not equal to 0 are reserved. The values of flags shall be such that (flags |
3430 | | // & 0xFFF000) is equal to 0 unless defined otherwise for the grouping_type. |
3431 | 47 | AVIF_CHECKERR(avifROStreamReadVersionAndFlags(&s, NULL, NULL), AVIF_RESULT_BMFF_PARSE_FAILED); |
3432 | | |
3433 | 46 | avifEntityToGroup * group = avifArrayPush(&meta->entityToGroups); |
3434 | 46 | AVIF_CHECKERR(group != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
3435 | 46 | AVIF_CHECKERR(avifArrayCreate(&group->entityIDs, sizeof(uint32_t), 2), AVIF_RESULT_OUT_OF_MEMORY); |
3436 | | |
3437 | 46 | memcpy(group->groupingType, groupHeader.type, 4); |
3438 | 46 | AVIF_CHECKERR(avifROStreamReadU32(&s, &group->groupID), AVIF_RESULT_BMFF_PARSE_FAILED); |
3439 | 45 | uint32_t numEntitiesInGroup; |
3440 | 45 | AVIF_CHECKERR(avifROStreamReadU32(&s, &numEntitiesInGroup), AVIF_RESULT_BMFF_PARSE_FAILED); |
3441 | 228 | for (uint32_t i = 0; i < numEntitiesInGroup; ++i) { |
3442 | 210 | uint32_t * entityId = avifArrayPush(&group->entityIDs); |
3443 | 210 | AVIF_CHECKERR(entityId != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
3444 | 210 | AVIF_CHECKERR(avifROStreamReadU32(&s, entityId), AVIF_RESULT_BMFF_PARSE_FAILED); |
3445 | 210 | } |
3446 | 44 | } |
3447 | | |
3448 | 1 | return AVIF_RESULT_OK; |
3449 | 33 | } |
3450 | | |
3451 | | static avifResult avifParseMetaBox(avifMeta * meta, uint64_t rawOffset, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3452 | 15.6k | { |
3453 | 15.6k | BEGIN_STREAM(s, raw, rawLen, diag, "Box[meta]"); |
3454 | | |
3455 | 15.6k | uint32_t flags; |
3456 | 15.6k | AVIF_CHECKERR(avifROStreamReadAndEnforceVersion(&s, 0, &flags), AVIF_RESULT_BMFF_PARSE_FAILED); |
3457 | | |
3458 | 15.6k | ++meta->idatID; // for tracking idat |
3459 | | |
3460 | 15.6k | avifBool firstBox = AVIF_TRUE; |
3461 | 15.6k | uint32_t uniqueBoxFlags = 0; |
3462 | 91.9k | while (avifROStreamHasBytesLeft(&s, 1)) { |
3463 | 77.0k | avifBoxHeader header; |
3464 | 77.0k | AVIF_CHECKERR(avifROStreamReadBoxHeader(&s, &header), AVIF_RESULT_BMFF_PARSE_FAILED); |
3465 | | |
3466 | 77.0k | if (firstBox) { |
3467 | 15.5k | if (!memcmp(header.type, "hdlr", 4)) { |
3468 | 15.5k | uint8_t handlerType[4]; |
3469 | 15.5k | AVIF_CHECKERR(avifParseHandlerBox(avifROStreamCurrent(&s), header.size, handlerType, diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
3470 | | // HEIF (ISO/IEC 23008-12:2022), Section 6.2: |
3471 | | // The handler type for the MetaBox shall be 'pict'. |
3472 | 15.5k | if (memcmp(handlerType, "pict", 4) != 0) { |
3473 | 6 | avifDiagnosticsPrintf(diag, "Box[hdlr] handler_type is not 'pict'"); |
3474 | 6 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3475 | 6 | } |
3476 | 15.5k | firstBox = AVIF_FALSE; |
3477 | 15.5k | } else { |
3478 | | // hdlr must be the first box! |
3479 | 8 | avifDiagnosticsPrintf(diag, "Box[meta] does not have a Box[hdlr] as its first child box"); |
3480 | 8 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3481 | 8 | } |
3482 | 61.4k | } else if (!memcmp(header.type, "hdlr", 4)) { |
3483 | 1 | avifDiagnosticsPrintf(diag, "Box[meta] contains a duplicate unique box of type 'hdlr'"); |
3484 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3485 | 61.4k | } else if (!memcmp(header.type, "iloc", 4)) { |
3486 | 15.0k | AVIF_CHECKERR(uniqueBoxSeen(&uniqueBoxFlags, AVIF_UNIQUE_ILOC, "meta", "iloc", diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
3487 | 15.0k | AVIF_CHECKRES(avifParseItemLocationBox(meta, avifROStreamCurrent(&s), header.size, diag)); |
3488 | 46.4k | } else if (!memcmp(header.type, "pitm", 4)) { |
3489 | 14.8k | AVIF_CHECKERR(uniqueBoxSeen(&uniqueBoxFlags, AVIF_UNIQUE_PITM, "meta", "pitm", diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
3490 | 14.8k | AVIF_CHECKERR(avifParsePrimaryItemBox(meta, avifROStreamCurrent(&s), header.size, diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
3491 | 31.6k | } else if (!memcmp(header.type, "idat", 4)) { |
3492 | 113 | AVIF_CHECKERR(uniqueBoxSeen(&uniqueBoxFlags, AVIF_UNIQUE_IDAT, "meta", "idat", diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
3493 | 112 | AVIF_CHECKERR(avifParseItemDataBox(meta, avifROStreamCurrent(&s), header.size, diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
3494 | 31.5k | } else if (!memcmp(header.type, "iprp", 4)) { |
3495 | 14.8k | AVIF_CHECKERR(uniqueBoxSeen(&uniqueBoxFlags, AVIF_UNIQUE_IPRP, "meta", "iprp", diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
3496 | 14.8k | AVIF_CHECKRES(avifParseItemPropertiesBox(meta, rawOffset + avifROStreamOffset(&s), avifROStreamCurrent(&s), header.size, diag)); |
3497 | 16.6k | } else if (!memcmp(header.type, "iinf", 4)) { |
3498 | 14.7k | AVIF_CHECKERR(uniqueBoxSeen(&uniqueBoxFlags, AVIF_UNIQUE_IINF, "meta", "iinf", diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
3499 | 14.7k | AVIF_CHECKRES(avifParseItemInfoBox(meta, avifROStreamCurrent(&s), header.size, diag)); |
3500 | 14.7k | } else if (!memcmp(header.type, "iref", 4)) { |
3501 | 1.44k | AVIF_CHECKERR(uniqueBoxSeen(&uniqueBoxFlags, AVIF_UNIQUE_IREF, "meta", "iref", diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
3502 | 1.44k | AVIF_CHECKRES(avifParseItemReferenceBox(meta, avifROStreamCurrent(&s), header.size, diag)); |
3503 | 1.44k | } else if (!memcmp(header.type, "grpl", 4)) { |
3504 | 34 | AVIF_CHECKERR(uniqueBoxSeen(&uniqueBoxFlags, AVIF_UNIQUE_GRPL, "meta", "grpl", diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
3505 | 33 | AVIF_CHECKRES(avifParseGroupsListBox(meta, avifROStreamCurrent(&s), header.size, diag)); |
3506 | 33 | } |
3507 | | |
3508 | 76.2k | AVIF_CHECKERR(avifROStreamSkip(&s, header.size), AVIF_RESULT_BMFF_PARSE_FAILED); |
3509 | 76.2k | } |
3510 | 14.8k | if (firstBox) { |
3511 | | // The meta box must not be empty (it must contain at least a hdlr box) |
3512 | 1 | avifDiagnosticsPrintf(diag, "Box[meta] has no child boxes"); |
3513 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3514 | 1 | } |
3515 | 14.8k | return AVIF_RESULT_OK; |
3516 | 14.8k | } |
3517 | | |
3518 | | static avifBool avifParseTrackHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3519 | 749 | { |
3520 | 749 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[tkhd]"); |
3521 | | |
3522 | 749 | uint8_t version; |
3523 | 749 | AVIF_CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
3524 | | |
3525 | 748 | uint32_t ignored32, trackID; |
3526 | 748 | uint64_t ignored64; |
3527 | 748 | if (version == 1) { |
3528 | 255 | AVIF_CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time; |
3529 | 254 | AVIF_CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time; |
3530 | 253 | AVIF_CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID; |
3531 | 252 | AVIF_CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0; |
3532 | 251 | AVIF_CHECK(avifROStreamReadU64(&s, &track->trackDuration)); // unsigned int(64) duration; |
3533 | 493 | } else if (version == 0) { |
3534 | 488 | uint32_t trackDuration; |
3535 | 488 | AVIF_CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time; |
3536 | 487 | AVIF_CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time; |
3537 | 486 | AVIF_CHECK(avifROStreamReadU32(&s, &trackID)); // unsigned int(32) track_ID; |
3538 | 485 | AVIF_CHECK(avifROStreamReadU32(&s, &ignored32)); // const unsigned int(32) reserved = 0; |
3539 | 484 | AVIF_CHECK(avifROStreamReadU32(&s, &trackDuration)); // unsigned int(32) duration; |
3540 | 483 | track->trackDuration = (trackDuration == AVIF_INDEFINITE_DURATION32) ? AVIF_INDEFINITE_DURATION64 : trackDuration; |
3541 | 483 | } else { |
3542 | | // Unsupported version |
3543 | 5 | avifDiagnosticsPrintf(diag, "Box[tkhd] has an unsupported version [%u]", version); |
3544 | 5 | return AVIF_FALSE; |
3545 | 5 | } |
3546 | 733 | track->id = trackID; |
3547 | | |
3548 | | // Skipping the following 52 bytes here: |
3549 | | // ------------------------------------ |
3550 | | // const unsigned int(32)[2] reserved = 0; |
3551 | | // template int(16) layer = 0; |
3552 | | // template int(16) alternate_group = 0; |
3553 | | // template int(16) volume = {if track_is_audio 0x0100 else 0}; |
3554 | | // const unsigned int(16) reserved = 0; |
3555 | | // template int(32)[9] matrix= { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 }; // unity matrix |
3556 | 733 | AVIF_CHECK(avifROStreamSkip(&s, 52)); |
3557 | | |
3558 | 710 | uint32_t width, height; |
3559 | 710 | AVIF_CHECK(avifROStreamReadU32(&s, &width)); // unsigned int(32) width; |
3560 | 709 | AVIF_CHECK(avifROStreamReadU32(&s, &height)); // unsigned int(32) height; |
3561 | 708 | track->width = width >> 16; |
3562 | 708 | track->height = height >> 16; |
3563 | | |
3564 | | // TODO: support scaling based on width/height track header info? |
3565 | | |
3566 | 708 | return AVIF_TRUE; |
3567 | 709 | } |
3568 | | |
3569 | | static avifBool avifParseMediaHeaderBox(avifTrack * track, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3570 | 376 | { |
3571 | 376 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[mdhd]"); |
3572 | | |
3573 | 376 | uint8_t version; |
3574 | 376 | AVIF_CHECK(avifROStreamReadVersionAndFlags(&s, &version, NULL)); |
3575 | | |
3576 | 375 | uint32_t ignored32, mediaTimescale, mediaDuration32; |
3577 | 375 | uint64_t ignored64, mediaDuration64; |
3578 | 375 | if (version == 1) { |
3579 | 221 | AVIF_CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) creation_time; |
3580 | 220 | AVIF_CHECK(avifROStreamReadU64(&s, &ignored64)); // unsigned int(64) modification_time; |
3581 | 219 | AVIF_CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale; |
3582 | 218 | AVIF_CHECK(avifROStreamReadU64(&s, &mediaDuration64)); // unsigned int(64) duration; |
3583 | 217 | track->mediaDuration = mediaDuration64; |
3584 | 217 | } else if (version == 0) { |
3585 | 152 | AVIF_CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) creation_time; |
3586 | 151 | AVIF_CHECK(avifROStreamReadU32(&s, &ignored32)); // unsigned int(32) modification_time; |
3587 | 150 | AVIF_CHECK(avifROStreamReadU32(&s, &mediaTimescale)); // unsigned int(32) timescale; |
3588 | 149 | AVIF_CHECK(avifROStreamReadU32(&s, &mediaDuration32)); // unsigned int(32) duration; |
3589 | 148 | track->mediaDuration = (uint64_t)mediaDuration32; |
3590 | 148 | } else { |
3591 | | // Unsupported version |
3592 | 2 | avifDiagnosticsPrintf(diag, "Box[mdhd] has an unsupported version [%u]", version); |
3593 | 2 | return AVIF_FALSE; |
3594 | 2 | } |
3595 | | |
3596 | 365 | track->mediaTimescale = mediaTimescale; |
3597 | 365 | return AVIF_TRUE; |
3598 | 375 | } |
3599 | | |
3600 | | static avifResult avifParseChunkOffsetBox(avifSampleTable * sampleTable, avifBool largeOffsets, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3601 | 429 | { |
3602 | 429 | BEGIN_STREAM(s, raw, rawLen, diag, largeOffsets ? "Box[co64]" : "Box[stco]"); |
3603 | | |
3604 | 429 | AVIF_CHECKERR(avifROStreamReadAndEnforceVersion(&s, /*enforcedVersion=*/0, /*flags=*/NULL), AVIF_RESULT_BMFF_PARSE_FAILED); |
3605 | | |
3606 | 427 | uint32_t entryCount; |
3607 | 427 | AVIF_CHECKERR(avifROStreamReadU32(&s, &entryCount), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) entry_count; |
3608 | 1.24k | for (uint32_t i = 0; i < entryCount; ++i) { |
3609 | 852 | uint64_t offset; |
3610 | 852 | if (largeOffsets) { |
3611 | 89 | AVIF_CHECKERR(avifROStreamReadU64(&s, &offset), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(64) chunk_offset; |
3612 | 763 | } else { |
3613 | 763 | uint32_t offset32; |
3614 | 763 | AVIF_CHECKERR(avifROStreamReadU32(&s, &offset32), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) chunk_offset; |
3615 | 740 | offset = (uint64_t)offset32; |
3616 | 740 | } |
3617 | | |
3618 | 821 | avifSampleTableChunk * chunk = (avifSampleTableChunk *)avifArrayPush(&sampleTable->chunks); |
3619 | 821 | AVIF_CHECKERR(chunk != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
3620 | 821 | chunk->offset = offset; |
3621 | 821 | } |
3622 | 395 | return AVIF_RESULT_OK; |
3623 | 426 | } |
3624 | | |
3625 | | static avifResult avifParseSampleToChunkBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3626 | 378 | { |
3627 | 378 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[stsc]"); |
3628 | | |
3629 | 378 | AVIF_CHECKERR(avifROStreamReadAndEnforceVersion(&s, /*enforcedVersion=*/0, /*flags=*/NULL), AVIF_RESULT_BMFF_PARSE_FAILED); |
3630 | | |
3631 | 376 | uint32_t entryCount; |
3632 | 376 | AVIF_CHECKERR(avifROStreamReadU32(&s, &entryCount), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) entry_count; |
3633 | 375 | uint32_t prevFirstChunk = 0; |
3634 | 811 | for (uint32_t i = 0; i < entryCount; ++i) { |
3635 | 466 | avifSampleTableSampleToChunk * sampleToChunk = (avifSampleTableSampleToChunk *)avifArrayPush(&sampleTable->sampleToChunks); |
3636 | 466 | AVIF_CHECKERR(sampleToChunk != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
3637 | 466 | AVIF_CHECKERR(avifROStreamReadU32(&s, &sampleToChunk->firstChunk), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) first_chunk; |
3638 | 464 | AVIF_CHECKERR(avifROStreamReadU32(&s, &sampleToChunk->samplesPerChunk), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) samples_per_chunk; |
3639 | 453 | AVIF_CHECKERR(avifROStreamReadU32(&s, &sampleToChunk->sampleDescriptionIndex), |
3640 | 453 | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) sample_description_index; |
3641 | | // The first_chunk fields should start with 1 and be strictly increasing. |
3642 | 444 | if (i == 0) { |
3643 | 350 | if (sampleToChunk->firstChunk != 1) { |
3644 | 5 | avifDiagnosticsPrintf(diag, "Box[stsc] does not begin with chunk 1 [%u]", sampleToChunk->firstChunk); |
3645 | 5 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3646 | 5 | } |
3647 | 350 | } else { |
3648 | 94 | if (sampleToChunk->firstChunk <= prevFirstChunk) { |
3649 | 3 | avifDiagnosticsPrintf(diag, "Box[stsc] chunks are not strictly increasing"); |
3650 | 3 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3651 | 3 | } |
3652 | 94 | } |
3653 | 436 | prevFirstChunk = sampleToChunk->firstChunk; |
3654 | 436 | } |
3655 | 345 | return AVIF_RESULT_OK; |
3656 | 375 | } |
3657 | | |
3658 | | static avifResult avifParseSampleSizeBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3659 | 405 | { |
3660 | 405 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[stsz]"); |
3661 | | |
3662 | 405 | AVIF_CHECKERR(avifROStreamReadAndEnforceVersion(&s, /*enforcedVersion=*/0, /*flags=*/NULL), AVIF_RESULT_BMFF_PARSE_FAILED); |
3663 | | |
3664 | 404 | uint32_t allSamplesSize, sampleCount; |
3665 | 404 | AVIF_CHECKERR(avifROStreamReadU32(&s, &allSamplesSize), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) sample_size; |
3666 | 403 | AVIF_CHECKERR(avifROStreamReadU32(&s, &sampleCount), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) sample_count; |
3667 | | |
3668 | 402 | if (allSamplesSize > 0) { |
3669 | 96 | sampleTable->allSamplesSize = allSamplesSize; |
3670 | 306 | } else { |
3671 | 4.42k | for (uint32_t i = 0; i < sampleCount; ++i) { |
3672 | 4.14k | avifSampleTableSampleSize * sampleSize = (avifSampleTableSampleSize *)avifArrayPush(&sampleTable->sampleSizes); |
3673 | 4.14k | AVIF_CHECKERR(sampleSize != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
3674 | 4.14k | AVIF_CHECKERR(avifROStreamReadU32(&s, &sampleSize->size), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) entry_size; |
3675 | 4.14k | } |
3676 | 306 | } |
3677 | 375 | return AVIF_RESULT_OK; |
3678 | 402 | } |
3679 | | |
3680 | | static avifResult avifParseSyncSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3681 | 369 | { |
3682 | 369 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[stss]"); |
3683 | | |
3684 | 369 | AVIF_CHECKERR(avifROStreamReadAndEnforceVersion(&s, /*enforcedVersion=*/0, /*flags=*/NULL), AVIF_RESULT_BMFF_PARSE_FAILED); |
3685 | | |
3686 | 368 | uint32_t entryCount; |
3687 | 368 | AVIF_CHECKERR(avifROStreamReadU32(&s, &entryCount), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) entry_count; |
3688 | | |
3689 | 1.10k | for (uint32_t i = 0; i < entryCount; ++i) { |
3690 | 766 | uint32_t sampleNumber = 0; |
3691 | 766 | AVIF_CHECKERR(avifROStreamReadU32(&s, &sampleNumber), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) sample_number; |
3692 | 740 | avifSyncSample * syncSample = (avifSyncSample *)avifArrayPush(&sampleTable->syncSamples); |
3693 | 740 | AVIF_CHECKERR(syncSample != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
3694 | 740 | syncSample->sampleNumber = sampleNumber; |
3695 | 740 | } |
3696 | 341 | return AVIF_RESULT_OK; |
3697 | 367 | } |
3698 | | |
3699 | | static avifResult avifParseTimeToSampleBox(avifSampleTable * sampleTable, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3700 | 290 | { |
3701 | 290 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[stts]"); |
3702 | | |
3703 | 290 | AVIF_CHECKERR(avifROStreamReadAndEnforceVersion(&s, /*enforcedVersion=*/0, /*flags=*/NULL), AVIF_RESULT_BMFF_PARSE_FAILED); |
3704 | | |
3705 | 289 | uint32_t entryCount; |
3706 | 289 | AVIF_CHECKERR(avifROStreamReadU32(&s, &entryCount), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) entry_count; |
3707 | | |
3708 | 663 | for (uint32_t i = 0; i < entryCount; ++i) { |
3709 | 395 | avifSampleTableTimeToSample * timeToSample = (avifSampleTableTimeToSample *)avifArrayPush(&sampleTable->timeToSamples); |
3710 | 395 | AVIF_CHECKERR(timeToSample != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
3711 | 395 | AVIF_CHECKERR(avifROStreamReadU32(&s, &timeToSample->sampleCount), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) sample_count; |
3712 | 377 | AVIF_CHECKERR(avifROStreamReadU32(&s, &timeToSample->sampleDelta), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) sample_delta; |
3713 | 377 | } |
3714 | 268 | return AVIF_RESULT_OK; |
3715 | 288 | } |
3716 | | |
3717 | | static avifResult avifParseSampleDescriptionBox(avifSampleTable * sampleTable, |
3718 | | uint64_t rawOffset, |
3719 | | const uint8_t * raw, |
3720 | | size_t rawLen, |
3721 | | avifDiagnostics * diag) |
3722 | 413 | { |
3723 | 413 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[stsd]"); |
3724 | | |
3725 | 413 | uint8_t version; |
3726 | 413 | AVIF_CHECKERR(avifROStreamReadVersionAndFlags(&s, &version, NULL), AVIF_RESULT_BMFF_PARSE_FAILED); |
3727 | | |
3728 | | // Section 8.5.2.3 of ISO/IEC 14496-12: |
3729 | | // version is set to zero. A version number of 1 shall be treated as a version of 0. |
3730 | 412 | if (version != 0 && version != 1) { |
3731 | 2 | avifDiagnosticsPrintf(diag, "Box[stsd]: Expecting box version 0 or 1, got version %u", version); |
3732 | 2 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3733 | 2 | } |
3734 | | |
3735 | 410 | uint32_t entryCount; |
3736 | 410 | AVIF_CHECKERR(avifROStreamReadU32(&s, &entryCount), AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(32) entry_count; |
3737 | | |
3738 | 847 | for (uint32_t i = 0; i < entryCount; ++i) { |
3739 | 474 | avifBoxHeader sampleEntryHeader; |
3740 | 474 | AVIF_CHECKERR(avifROStreamReadBoxHeader(&s, &sampleEntryHeader), AVIF_RESULT_BMFF_PARSE_FAILED); |
3741 | | |
3742 | 443 | avifSampleDescription * description = (avifSampleDescription *)avifArrayPush(&sampleTable->sampleDescriptions); |
3743 | 443 | AVIF_CHECKERR(description != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
3744 | 443 | if (!avifArrayCreate(&description->properties, sizeof(avifProperty), 16)) { |
3745 | 0 | avifArrayPop(&sampleTable->sampleDescriptions); |
3746 | 0 | return AVIF_RESULT_OUT_OF_MEMORY; |
3747 | 0 | } |
3748 | 443 | memcpy(description->format, sampleEntryHeader.type, sizeof(description->format)); |
3749 | 443 | const size_t sampleEntryBytes = sampleEntryHeader.size; |
3750 | 443 | if (avifGetCodecType(description->format) != AVIF_CODEC_TYPE_UNKNOWN) { |
3751 | 339 | if (sampleEntryBytes < VISUALSAMPLEENTRY_SIZE) { |
3752 | 1 | avifDiagnosticsPrintf(diag, "Not enough bytes to parse VisualSampleEntry"); |
3753 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3754 | 1 | } |
3755 | 338 | AVIF_CHECKRES(avifParseItemPropertyContainerBox(&description->properties, |
3756 | 338 | rawOffset + avifROStreamOffset(&s) + VISUALSAMPLEENTRY_SIZE, |
3757 | 338 | avifROStreamCurrent(&s) + VISUALSAMPLEENTRY_SIZE, |
3758 | 338 | sampleEntryBytes - VISUALSAMPLEENTRY_SIZE, |
3759 | 338 | /*isTrack=*/AVIF_TRUE, |
3760 | 338 | diag)); |
3761 | 338 | } |
3762 | | |
3763 | 438 | AVIF_CHECKERR(avifROStreamSkip(&s, sampleEntryBytes), AVIF_RESULT_BMFF_PARSE_FAILED); |
3764 | 438 | } |
3765 | 373 | return AVIF_RESULT_OK; |
3766 | 409 | } |
3767 | | |
3768 | | static avifResult avifParseSampleTableBox(avifTrack * track, uint64_t rawOffset, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3769 | 607 | { |
3770 | 607 | if (track->sampleTable) { |
3771 | | // A TrackBox may only have one SampleTable |
3772 | 1 | avifDiagnosticsPrintf(diag, "Duplicate Box[stbl] for a single track detected"); |
3773 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3774 | 1 | } |
3775 | 606 | track->sampleTable = avifSampleTableCreate(); |
3776 | 606 | AVIF_CHECKERR(track->sampleTable != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
3777 | | |
3778 | 606 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[stbl]"); |
3779 | | |
3780 | 2.99k | while (avifROStreamHasBytesLeft(&s, 1)) { |
3781 | 2.60k | avifBoxHeader header; |
3782 | 2.60k | AVIF_CHECKERR(avifROStreamReadBoxHeader(&s, &header), AVIF_RESULT_BMFF_PARSE_FAILED); |
3783 | | |
3784 | 2.57k | if (!memcmp(header.type, "stco", 4)) { |
3785 | 404 | AVIF_CHECKRES(avifParseChunkOffsetBox(track->sampleTable, AVIF_FALSE, avifROStreamCurrent(&s), header.size, diag)); |
3786 | 2.17k | } else if (!memcmp(header.type, "co64", 4)) { |
3787 | 25 | AVIF_CHECKRES(avifParseChunkOffsetBox(track->sampleTable, AVIF_TRUE, avifROStreamCurrent(&s), header.size, diag)); |
3788 | 2.14k | } else if (!memcmp(header.type, "stsc", 4)) { |
3789 | 378 | AVIF_CHECKRES(avifParseSampleToChunkBox(track->sampleTable, avifROStreamCurrent(&s), header.size, diag)); |
3790 | 1.77k | } else if (!memcmp(header.type, "stsz", 4)) { |
3791 | 405 | AVIF_CHECKRES(avifParseSampleSizeBox(track->sampleTable, avifROStreamCurrent(&s), header.size, diag)); |
3792 | 1.36k | } else if (!memcmp(header.type, "stss", 4)) { |
3793 | 369 | AVIF_CHECKRES(avifParseSyncSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size, diag)); |
3794 | 996 | } else if (!memcmp(header.type, "stts", 4)) { |
3795 | 290 | AVIF_CHECKRES(avifParseTimeToSampleBox(track->sampleTable, avifROStreamCurrent(&s), header.size, diag)); |
3796 | 706 | } else if (!memcmp(header.type, "stsd", 4)) { |
3797 | 413 | AVIF_CHECKRES(avifParseSampleDescriptionBox(track->sampleTable, |
3798 | 413 | rawOffset + avifROStreamOffset(&s), |
3799 | 413 | avifROStreamCurrent(&s), |
3800 | 413 | header.size, |
3801 | 413 | diag)); |
3802 | 413 | } |
3803 | | |
3804 | 2.39k | AVIF_CHECKERR(avifROStreamSkip(&s, header.size), AVIF_RESULT_BMFF_PARSE_FAILED); |
3805 | 2.39k | } |
3806 | 388 | return AVIF_RESULT_OK; |
3807 | 606 | } |
3808 | | |
3809 | | static avifResult avifParseMediaInformationBox(avifTrack * track, uint64_t rawOffset, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3810 | 644 | { |
3811 | 644 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[minf]"); |
3812 | | |
3813 | 2.05k | while (avifROStreamHasBytesLeft(&s, 1)) { |
3814 | 1.63k | avifBoxHeader header; |
3815 | 1.63k | AVIF_CHECKERR(avifROStreamReadBoxHeader(&s, &header), AVIF_RESULT_BMFF_PARSE_FAILED); |
3816 | | |
3817 | 1.62k | if (!memcmp(header.type, "stbl", 4)) { |
3818 | 607 | AVIF_CHECKRES(avifParseSampleTableBox(track, rawOffset + avifROStreamOffset(&s), avifROStreamCurrent(&s), header.size, diag)); |
3819 | 607 | } |
3820 | | |
3821 | 1.40k | AVIF_CHECKERR(avifROStreamSkip(&s, header.size), AVIF_RESULT_BMFF_PARSE_FAILED); |
3822 | 1.40k | } |
3823 | 420 | return AVIF_RESULT_OK; |
3824 | 644 | } |
3825 | | |
3826 | | static avifResult avifParseMediaBox(avifTrack * track, uint64_t rawOffset, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3827 | 680 | { |
3828 | 680 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[mdia]"); |
3829 | | |
3830 | 2.15k | while (avifROStreamHasBytesLeft(&s, 1)) { |
3831 | 1.73k | avifBoxHeader header; |
3832 | 1.73k | AVIF_CHECKERR(avifROStreamReadBoxHeader(&s, &header), AVIF_RESULT_BMFF_PARSE_FAILED); |
3833 | | |
3834 | 1.71k | if (!memcmp(header.type, "mdhd", 4)) { |
3835 | 376 | AVIF_CHECKERR(avifParseMediaHeaderBox(track, avifROStreamCurrent(&s), header.size, diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
3836 | 1.33k | } else if (!memcmp(header.type, "minf", 4)) { |
3837 | 644 | AVIF_CHECKRES( |
3838 | 644 | avifParseMediaInformationBox(track, rawOffset + avifROStreamOffset(&s), avifROStreamCurrent(&s), header.size, diag)); |
3839 | 694 | } else if (!memcmp(header.type, "hdlr", 4)) { |
3840 | 355 | AVIF_CHECKERR(avifParseHandlerBox(avifROStreamCurrent(&s), header.size, track->handlerType, diag), |
3841 | 355 | AVIF_RESULT_BMFF_PARSE_FAILED); |
3842 | 355 | } |
3843 | | |
3844 | 1.47k | AVIF_CHECKERR(avifROStreamSkip(&s, header.size), AVIF_RESULT_BMFF_PARSE_FAILED); |
3845 | 1.47k | } |
3846 | 422 | return AVIF_RESULT_OK; |
3847 | 680 | } |
3848 | | |
3849 | | static avifBool avifTrackReferenceBox(avifTrack * track, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3850 | 187 | { |
3851 | 187 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[tref]"); |
3852 | | |
3853 | 377 | while (avifROStreamHasBytesLeft(&s, 1)) { |
3854 | 199 | avifBoxHeader header; |
3855 | 199 | AVIF_CHECK(avifROStreamReadBoxHeader(&s, &header)); |
3856 | | |
3857 | 194 | if (!memcmp(header.type, "auxl", 4)) { |
3858 | 6 | uint32_t toID; |
3859 | 6 | AVIF_CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) track_IDs[]; |
3860 | 5 | AVIF_CHECK(avifROStreamSkip(&s, header.size - sizeof(uint32_t))); // just take the first one |
3861 | 4 | track->auxForID = toID; |
3862 | 188 | } else if (!memcmp(header.type, "prem", 4)) { |
3863 | 5 | uint32_t byID; |
3864 | 5 | AVIF_CHECK(avifROStreamReadU32(&s, &byID)); // unsigned int(32) track_IDs[]; |
3865 | 4 | AVIF_CHECK(avifROStreamSkip(&s, header.size - sizeof(uint32_t))); // just take the first one |
3866 | 3 | track->premByID = byID; |
3867 | 183 | } else { |
3868 | 183 | AVIF_CHECK(avifROStreamSkip(&s, header.size)); |
3869 | 183 | } |
3870 | 194 | } |
3871 | 178 | return AVIF_TRUE; |
3872 | 187 | } |
3873 | | |
3874 | | static avifBool avifParseEditListBox(avifTrack * track, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3875 | 465 | { |
3876 | 465 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[elst]"); |
3877 | | |
3878 | 465 | uint8_t version; |
3879 | 465 | uint32_t flags; |
3880 | 465 | AVIF_CHECK(avifROStreamReadVersionAndFlags(&s, &version, &flags)); |
3881 | | |
3882 | 464 | if ((flags & 1) == 0) { |
3883 | 114 | track->isRepeating = AVIF_FALSE; |
3884 | 114 | return AVIF_TRUE; |
3885 | 114 | } |
3886 | | |
3887 | 350 | track->isRepeating = AVIF_TRUE; |
3888 | 350 | uint32_t entryCount; |
3889 | 350 | AVIF_CHECK(avifROStreamReadU32(&s, &entryCount)); // unsigned int(32) entry_count; |
3890 | 349 | if (entryCount != 1) { |
3891 | 28 | avifDiagnosticsPrintf(diag, "Box[elst] contains an entry_count != 1 [%u]", entryCount); |
3892 | 28 | return AVIF_FALSE; |
3893 | 28 | } |
3894 | | |
3895 | 321 | if (version == 1) { |
3896 | 307 | AVIF_CHECK(avifROStreamReadU64(&s, &track->segmentDuration)); // unsigned int(64) segment_duration; |
3897 | 307 | } else if (version == 0) { |
3898 | 10 | uint32_t segmentDuration; |
3899 | 10 | AVIF_CHECK(avifROStreamReadU32(&s, &segmentDuration)); // unsigned int(32) segment_duration; |
3900 | 9 | track->segmentDuration = segmentDuration; |
3901 | 9 | } else { |
3902 | | // Unsupported version |
3903 | 4 | avifDiagnosticsPrintf(diag, "Box[elst] has an unsupported version [%u]", version); |
3904 | 4 | return AVIF_FALSE; |
3905 | 4 | } |
3906 | 315 | if (track->segmentDuration == 0) { |
3907 | 1 | avifDiagnosticsPrintf(diag, "Box[elst] Invalid value for segment_duration (0)."); |
3908 | 1 | return AVIF_FALSE; |
3909 | 1 | } |
3910 | 314 | return AVIF_TRUE; |
3911 | 315 | } |
3912 | | |
3913 | | static avifBool avifParseEditBox(avifTrack * track, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
3914 | 470 | { |
3915 | 470 | BEGIN_STREAM(s, raw, rawLen, diag, "Box[edts]"); |
3916 | | |
3917 | 470 | avifBool elstBoxSeen = AVIF_FALSE; |
3918 | 919 | while (avifROStreamHasBytesLeft(&s, 1)) { |
3919 | 489 | avifBoxHeader header; |
3920 | 489 | AVIF_CHECK(avifROStreamReadBoxHeader(&s, &header)); |
3921 | | |
3922 | 486 | if (!memcmp(header.type, "elst", 4)) { |
3923 | 465 | if (elstBoxSeen) { |
3924 | 0 | avifDiagnosticsPrintf(diag, "More than one [elst] Box was found."); |
3925 | 0 | return AVIF_FALSE; |
3926 | 0 | } |
3927 | 465 | AVIF_CHECK(avifParseEditListBox(track, avifROStreamCurrent(&s), header.size, diag)); |
3928 | 428 | elstBoxSeen = AVIF_TRUE; |
3929 | 428 | } |
3930 | 449 | AVIF_CHECK(avifROStreamSkip(&s, header.size)); |
3931 | 449 | } |
3932 | 430 | if (!elstBoxSeen) { |
3933 | 4 | avifDiagnosticsPrintf(diag, "Box[edts] contains no [elst] Box."); |
3934 | 4 | return AVIF_FALSE; |
3935 | 4 | } |
3936 | 426 | return AVIF_TRUE; |
3937 | 430 | } |
3938 | | |
3939 | | static avifResult avifParseTrackBox(avifDecoderData * data, uint64_t rawOffset, const uint8_t * raw, size_t rawLen) |
3940 | 1.13k | { |
3941 | 1.13k | BEGIN_STREAM(s, raw, rawLen, data->diag, "Box[trak]"); |
3942 | | |
3943 | 1.13k | avifTrack * track = avifDecoderDataCreateTrack(data); |
3944 | 1.13k | AVIF_CHECKERR(track != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
3945 | | |
3946 | 1.13k | avifBool edtsBoxSeen = AVIF_FALSE; |
3947 | 1.13k | avifBool tkhdSeen = AVIF_FALSE; |
3948 | 3.65k | while (avifROStreamHasBytesLeft(&s, 1)) { |
3949 | 2.90k | avifBoxHeader header; |
3950 | 2.90k | AVIF_CHECKERR(avifROStreamReadBoxHeader(&s, &header), AVIF_RESULT_BMFF_PARSE_FAILED); |
3951 | | |
3952 | 2.88k | if (!memcmp(header.type, "tkhd", 4)) { |
3953 | 750 | if (tkhdSeen) { |
3954 | 1 | avifDiagnosticsPrintf(data->diag, "Box[trak] contains a duplicate unique box of type 'tkhd'"); |
3955 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3956 | 1 | } |
3957 | 749 | AVIF_CHECKERR(avifParseTrackHeaderBox(track, avifROStreamCurrent(&s), header.size, data->diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
3958 | 708 | tkhdSeen = AVIF_TRUE; |
3959 | 2.13k | } else if (!memcmp(header.type, "meta", 4)) { |
3960 | 12 | AVIF_CHECKRES( |
3961 | 12 | avifParseMetaBox(track->meta, rawOffset + avifROStreamOffset(&s), avifROStreamCurrent(&s), header.size, data->diag)); |
3962 | 2.12k | } else if (!memcmp(header.type, "mdia", 4)) { |
3963 | 680 | AVIF_CHECKRES(avifParseMediaBox(track, rawOffset + avifROStreamOffset(&s), avifROStreamCurrent(&s), header.size, data->diag)); |
3964 | 1.44k | } else if (!memcmp(header.type, "tref", 4)) { |
3965 | 187 | AVIF_CHECKERR(avifTrackReferenceBox(track, avifROStreamCurrent(&s), header.size, data->diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
3966 | 1.25k | } else if (!memcmp(header.type, "edts", 4)) { |
3967 | 471 | if (edtsBoxSeen) { |
3968 | 1 | avifDiagnosticsPrintf(data->diag, "Box[trak] contains a duplicate unique box of type 'edts'"); |
3969 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3970 | 1 | } |
3971 | 470 | AVIF_CHECKERR(avifParseEditBox(track, avifROStreamCurrent(&s), header.size, data->diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
3972 | 426 | edtsBoxSeen = AVIF_TRUE; |
3973 | 426 | } |
3974 | | |
3975 | 2.52k | AVIF_CHECKERR(avifROStreamSkip(&s, header.size), AVIF_RESULT_BMFF_PARSE_FAILED); |
3976 | 2.52k | } |
3977 | 748 | if (!tkhdSeen) { |
3978 | 105 | avifDiagnosticsPrintf(data->diag, "Box[trak] does not contain a mandatory [tkhd] box"); |
3979 | 105 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
3980 | 105 | } |
3981 | 643 | if (!edtsBoxSeen) { |
3982 | 314 | track->repetitionCount = AVIF_REPETITION_COUNT_UNKNOWN; |
3983 | 329 | } else if (track->isRepeating) { |
3984 | 253 | if (track->trackDuration == AVIF_INDEFINITE_DURATION64) { |
3985 | | // If isRepeating is true and the track duration is unknown/indefinite, then set the repetition count to infinite |
3986 | | // (Section 9.6.1 of ISO/IEC 23008-12 Part 12). |
3987 | 1 | track->repetitionCount = AVIF_REPETITION_COUNT_INFINITE; |
3988 | 252 | } else { |
3989 | | // Section 9.6.1. of ISO/IEC 23008-12 Part 12: 1, the entire edit list is repeated a sufficient number of times to |
3990 | | // equal the track duration. |
3991 | | // |
3992 | | // Since libavif uses repetitionCount (which is 0-based), we subtract the value by 1 to derive the number of |
3993 | | // repetitions. |
3994 | 252 | AVIF_ASSERT_OR_RETURN(track->segmentDuration != 0); |
3995 | | // We specifically check for trackDuration == 0 here and not when it is actually read in order to accept files which |
3996 | | // inadvertently has a trackDuration of 0 without any edit lists. |
3997 | 252 | if (track->trackDuration == 0) { |
3998 | 1 | avifDiagnosticsPrintf(data->diag, "Invalid track duration 0."); |
3999 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
4000 | 1 | } |
4001 | 251 | const uint64_t repetitionCount = |
4002 | 251 | (track->trackDuration / track->segmentDuration) + (track->trackDuration % track->segmentDuration != 0) - 1; |
4003 | 251 | if (repetitionCount > INT_MAX) { |
4004 | | // repetitionCount does not fit in an integer and hence it is |
4005 | | // likely to be a very large value. So, we just set it to |
4006 | | // infinite. |
4007 | 5 | track->repetitionCount = AVIF_REPETITION_COUNT_INFINITE; |
4008 | 246 | } else { |
4009 | 246 | track->repetitionCount = (int)repetitionCount; |
4010 | 246 | } |
4011 | 251 | } |
4012 | 253 | } else { |
4013 | 76 | track->repetitionCount = 0; |
4014 | 76 | } |
4015 | | |
4016 | 642 | return AVIF_RESULT_OK; |
4017 | 643 | } |
4018 | | |
4019 | | static avifResult avifParseMovieBox(avifDecoderData * data, |
4020 | | uint64_t rawOffset, |
4021 | | const uint8_t * raw, |
4022 | | size_t rawLen, |
4023 | | uint32_t imageSizeLimit, |
4024 | | uint32_t imageDimensionLimit) |
4025 | 917 | { |
4026 | 917 | BEGIN_STREAM(s, raw, rawLen, data->diag, "Box[moov]"); |
4027 | | |
4028 | 917 | avifBool hasTrak = AVIF_FALSE; |
4029 | 3.09k | while (avifROStreamHasBytesLeft(&s, 1)) { |
4030 | 2.75k | avifBoxHeader header; |
4031 | 2.75k | AVIF_CHECKERR(avifROStreamReadBoxHeader(&s, &header), AVIF_RESULT_BMFF_PARSE_FAILED); |
4032 | | |
4033 | 2.67k | if (!memcmp(header.type, "trak", 4)) { |
4034 | 1.13k | AVIF_CHECKRES(avifParseTrackBox(data, rawOffset + avifROStreamOffset(&s), avifROStreamCurrent(&s), header.size)); |
4035 | 642 | hasTrak = AVIF_TRUE; |
4036 | | |
4037 | 642 | const avifTrack * track = &data->tracks.track[data->tracks.count - 1]; |
4038 | 642 | if (!memcmp(track->handlerType, "pict", 4) || !memcmp(track->handlerType, "vide", 4) || |
4039 | 350 | !memcmp(track->handlerType, "auxv", 4)) { |
4040 | 298 | if ((track->width == 0) || (track->height == 0)) { |
4041 | 3 | avifDiagnosticsPrintf(data->diag, "Track ID [%u] has an invalid size [%ux%u]", track->id, track->width, track->height); |
4042 | 3 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
4043 | 3 | } |
4044 | 295 | if (avifDimensionsTooLarge(track->width, track->height, imageSizeLimit, imageDimensionLimit)) { |
4045 | 5 | avifDiagnosticsPrintf(data->diag, |
4046 | 5 | "Track ID [%u] dimensions are too large [%ux%u]", |
4047 | 5 | track->id, |
4048 | 5 | track->width, |
4049 | 5 | track->height); |
4050 | 5 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
4051 | 5 | } |
4052 | 295 | } |
4053 | 642 | } |
4054 | | |
4055 | 2.18k | AVIF_CHECKERR(avifROStreamSkip(&s, header.size), AVIF_RESULT_BMFF_PARSE_FAILED); |
4056 | 2.18k | } |
4057 | 340 | if (!hasTrak) { |
4058 | 4 | avifDiagnosticsPrintf(data->diag, "moov box does not contain any tracks"); |
4059 | 4 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
4060 | 4 | } |
4061 | 336 | return AVIF_RESULT_OK; |
4062 | 340 | } |
4063 | | |
4064 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
4065 | | static avifProperty * avifMetaCreateProperty(avifMeta * meta, const char * propertyType) |
4066 | | { |
4067 | | avifProperty * metaProperty = avifArrayPush(&meta->properties); |
4068 | | AVIF_CHECKERR(metaProperty, NULL); |
4069 | | memcpy(metaProperty->type, propertyType, 4); |
4070 | | return metaProperty; |
4071 | | } |
4072 | | |
4073 | | static avifProperty * avifDecoderItemAddProperty(avifDecoderItem * item, const avifProperty * metaProperty) |
4074 | | { |
4075 | | avifProperty * itemProperty = avifArrayPush(&item->properties); |
4076 | | AVIF_CHECKERR(itemProperty, NULL); |
4077 | | *itemProperty = *metaProperty; |
4078 | | return itemProperty; |
4079 | | } |
4080 | | |
4081 | | static avifResult avifParseMinimizedImageBox(avifDecoderData * data, |
4082 | | uint64_t rawOffset, |
4083 | | const uint8_t * raw, |
4084 | | size_t rawLen, |
4085 | | avifBool isAvifAccordingToMinorVersion, |
4086 | | avifDiagnostics * diag) |
4087 | | { |
4088 | | avifMeta * meta = data->meta; |
4089 | | BEGIN_STREAM(s, raw, rawLen, diag, "Box[mini]"); |
4090 | | |
4091 | | meta->fromMiniBox = AVIF_TRUE; |
4092 | | |
4093 | | uint32_t version; |
4094 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &version, 2), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(2) version = 0; |
4095 | | AVIF_CHECKERR(version == 0, AVIF_RESULT_BMFF_PARSE_FAILED); |
4096 | | |
4097 | | // flags |
4098 | | uint32_t hasExplicitCodecTypes, floatFlag, fullRange, hasAlpha, hasExplicitCicp, hasHdr, hasIcc, hasExif, hasXmp; |
4099 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &hasExplicitCodecTypes, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) explicit_codec_types_flag; |
4100 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &floatFlag, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) float_flag; |
4101 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &fullRange, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) full_range_flag; |
4102 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &hasAlpha, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) alpha_flag; |
4103 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &hasExplicitCicp, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) explicit_cicp_flag; |
4104 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &hasHdr, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) hdr_flag; |
4105 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &hasIcc, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) icc_flag; |
4106 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &hasExif, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) exif_flag; |
4107 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &hasXmp, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) xmp_flag; |
4108 | | |
4109 | | uint32_t chromaSubsampling, orientation; |
4110 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &chromaSubsampling, 2), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(2) chroma_subsampling; |
4111 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &orientation, 3), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(3) orientation_minus1; |
4112 | | ++orientation; |
4113 | | |
4114 | | // Spatial extents |
4115 | | uint32_t largeDimensionsFlag, width, height; |
4116 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &largeDimensionsFlag, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) large_dimensions_flag; |
4117 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &width, largeDimensionsFlag ? 15 : 7), |
4118 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_dimensions_flag ? 15 : 7) width_minus1; |
4119 | | ++width; |
4120 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &height, largeDimensionsFlag ? 15 : 7), |
4121 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_dimensions_flag ? 15 : 7) height_minus1; |
4122 | | ++height; |
4123 | | |
4124 | | // Pixel information |
4125 | | uint32_t chromaIsHorizontallyCentered = 0, chromaIsVerticallyCentered = 0; |
4126 | | if (chromaSubsampling == 1 || chromaSubsampling == 2) { |
4127 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &chromaIsHorizontallyCentered, 1), |
4128 | | AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) chroma_is_horizontally_centered; |
4129 | | } |
4130 | | if (chromaSubsampling == 1) { |
4131 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &chromaIsVerticallyCentered, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) chroma_is_vertically_centered; |
4132 | | } |
4133 | | |
4134 | | uint32_t bitDepth; |
4135 | | if (floatFlag) { |
4136 | | // bit(2) bit_depth_log2_minus4; |
4137 | | return AVIF_RESULT_BMFF_PARSE_FAILED; // Either invalid AVIF or unsupported non-AVIF. |
4138 | | } else { |
4139 | | uint32_t highBitDepthFlag; |
4140 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &highBitDepthFlag, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) high_bit_depth_flag; |
4141 | | if (highBitDepthFlag) { |
4142 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &bitDepth, 3), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(3) bit_depth_minus9; |
4143 | | bitDepth += 9; |
4144 | | } else { |
4145 | | bitDepth = 8; |
4146 | | } |
4147 | | } |
4148 | | |
4149 | | uint32_t alphaIsPremultiplied = 0; |
4150 | | if (hasAlpha) { |
4151 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &alphaIsPremultiplied, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) alpha_is_premultiplied; |
4152 | | } |
4153 | | |
4154 | | // Colour properties |
4155 | | uint8_t colorPrimaries; |
4156 | | uint8_t transferCharacteristics; |
4157 | | uint8_t matrixCoefficients; |
4158 | | if (hasExplicitCicp) { |
4159 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &colorPrimaries, 8), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(8) colour_primaries; |
4160 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &transferCharacteristics, 8), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(8) transfer_characteristics; |
4161 | | if (chromaSubsampling != 0) { |
4162 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &matrixCoefficients, 8), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(8) matrix_coefficients; |
4163 | | } else { |
4164 | | matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED; // 2 |
4165 | | } |
4166 | | } else { |
4167 | | colorPrimaries = hasIcc ? AVIF_COLOR_PRIMARIES_UNSPECIFIED // 2 |
4168 | | : AVIF_COLOR_PRIMARIES_BT709; // 1 |
4169 | | transferCharacteristics = hasIcc ? AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED // 2 |
4170 | | : AVIF_TRANSFER_CHARACTERISTICS_SRGB; // 13 |
4171 | | matrixCoefficients = chromaSubsampling == 0 ? AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED // 2 |
4172 | | : AVIF_MATRIX_COEFFICIENTS_BT601; // 6 |
4173 | | } |
4174 | | |
4175 | | uint8_t infeType[4]; |
4176 | | uint8_t codecConfigType[4]; |
4177 | | if (hasExplicitCodecTypes) { |
4178 | | // bit(32) infe_type; |
4179 | | for (int i = 0; i < 4; ++i) { |
4180 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &infeType[i], 8), AVIF_RESULT_BMFF_PARSE_FAILED); |
4181 | | } |
4182 | | // bit(32) codec_config_type; |
4183 | | for (int i = 0; i < 4; ++i) { |
4184 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &codecConfigType[i], 8), AVIF_RESULT_BMFF_PARSE_FAILED); |
4185 | | } |
4186 | | #if defined(AVIF_CODEC_AVM) |
4187 | | AVIF_CHECKERR((!memcmp(infeType, "av01", 4) && !memcmp(codecConfigType, "av1C", 4)) || |
4188 | | (!memcmp(infeType, "av02", 4) && !memcmp(codecConfigType, "av2C", 4)), |
4189 | | AVIF_RESULT_BMFF_PARSE_FAILED); |
4190 | | #else |
4191 | | AVIF_CHECKERR(!memcmp(infeType, "av01", 4) && !memcmp(codecConfigType, "av1C", 4), AVIF_RESULT_BMFF_PARSE_FAILED); |
4192 | | #endif |
4193 | | } else { |
4194 | | AVIF_CHECKERR(isAvifAccordingToMinorVersion, AVIF_RESULT_BMFF_PARSE_FAILED); |
4195 | | memcpy(infeType, "av01", 4); |
4196 | | memcpy(codecConfigType, "av1C", 4); |
4197 | | } |
4198 | | |
4199 | | // High Dynamic Range properties |
4200 | | uint32_t hasGainmap = AVIF_FALSE; |
4201 | | uint32_t tmapHasIcc = AVIF_FALSE; |
4202 | | uint32_t gainmapWidth = 0, gainmapHeight = 0; |
4203 | | uint8_t gainmapMatrixCoefficients = 0; |
4204 | | uint32_t gainmapFullRange = 0; |
4205 | | uint32_t gainmapChromaSubsampling = 0; |
4206 | | uint32_t gainmapBitDepth = 0; |
4207 | | uint32_t tmapHasExplicitCicp = AVIF_FALSE; |
4208 | | uint8_t tmapColorPrimaries = AVIF_COLOR_PRIMARIES_UNKNOWN; |
4209 | | uint8_t tmapTransferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_UNKNOWN; |
4210 | | uint8_t tmapMatrixCoefficients = AVIF_MATRIX_COEFFICIENTS_IDENTITY; |
4211 | | uint32_t tmapFullRange = AVIF_FALSE; |
4212 | | uint32_t hasClli = AVIF_FALSE, tmapHasClli = AVIF_FALSE; |
4213 | | avifContentLightLevelInformationBox clli = { 0 }, tmapClli = { 0 }; |
4214 | | if (hasHdr) { |
4215 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &hasGainmap, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) gainmap_flag; |
4216 | | if (hasGainmap) { |
4217 | | // avifDecoderReset() requires the 'tmap' brand to be registered for the tone mapping derived image item to be parsed. |
4218 | | if (data->compatibleBrands.capacity == 0) { |
4219 | | AVIF_CHECKERR(avifArrayCreate(&data->compatibleBrands, sizeof(avifBrand), 1), AVIF_RESULT_OUT_OF_MEMORY); |
4220 | | } |
4221 | | avifBrand * brand = avifArrayPush(&data->compatibleBrands); |
4222 | | AVIF_CHECKERR(brand != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
4223 | | memcpy(brand, "tmap", sizeof(avifBrand)); |
4224 | | |
4225 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &gainmapWidth, largeDimensionsFlag ? 15 : 7), |
4226 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_dimensions_flag ? 15 : 7) gainmap_width_minus1; |
4227 | | ++gainmapWidth; |
4228 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &gainmapHeight, largeDimensionsFlag ? 15 : 7), |
4229 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_dimensions_flag ? 15 : 7) gainmap_height_minus1; |
4230 | | ++gainmapHeight; |
4231 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &gainmapMatrixCoefficients, 8), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(8) gainmap_matrix_coefficients; |
4232 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &gainmapFullRange, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) gainmap_full_range_flag; |
4233 | | |
4234 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &gainmapChromaSubsampling, 2), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(2) gainmap_chroma_subsampling; |
4235 | | uint32_t gainmapChromaIsHorizontallyCentered = 0, gainmapChromaIsVerticallyCentered = 0; |
4236 | | if (gainmapChromaSubsampling == 1 || gainmapChromaSubsampling == 2) { |
4237 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &gainmapChromaIsHorizontallyCentered, 1), |
4238 | | AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) gainmap_chroma_is_horizontally_centered; |
4239 | | } |
4240 | | if (gainmapChromaSubsampling == 1) { |
4241 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &gainmapChromaIsVerticallyCentered, 1), |
4242 | | AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) gainmap_chroma_is_vertically_centered; |
4243 | | } |
4244 | | |
4245 | | uint32_t gainmapFloatFlag; |
4246 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &gainmapFloatFlag, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) gainmap_float_flag; |
4247 | | if (gainmapFloatFlag) { |
4248 | | // bit(2) gainmap_bit_depth_log2_minus4; |
4249 | | return AVIF_RESULT_BMFF_PARSE_FAILED; // Either invalid AVIF or unsupported non-AVIF. |
4250 | | } else { |
4251 | | uint32_t gainmapHighBitDepthFlag; |
4252 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &gainmapHighBitDepthFlag, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) gainmap_high_bit_depth_flag; |
4253 | | if (gainmapHighBitDepthFlag) { |
4254 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &gainmapBitDepth, 3), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(3) gainmap_bit_depth_minus9; |
4255 | | gainmapBitDepth += 9; |
4256 | | } else { |
4257 | | gainmapBitDepth = 8; |
4258 | | } |
4259 | | } |
4260 | | |
4261 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &tmapHasIcc, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) tmap_icc_flag; |
4262 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &tmapHasExplicitCicp, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) tmap_explicit_cicp_flag; |
4263 | | if (tmapHasExplicitCicp) { |
4264 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &tmapColorPrimaries, 8), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(8) tmap_colour_primaries; |
4265 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &tmapTransferCharacteristics, 8), |
4266 | | AVIF_RESULT_BMFF_PARSE_FAILED); // bit(8) tmap_transfer_characteristics; |
4267 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &tmapMatrixCoefficients, 8), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(8) tmap_matrix_coefficients; |
4268 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &tmapFullRange, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) tmap_full_range_flag; |
4269 | | } else { |
4270 | | tmapColorPrimaries = AVIF_COLOR_PRIMARIES_BT709; // 1 |
4271 | | tmapTransferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_SRGB; // 13 |
4272 | | tmapMatrixCoefficients = AVIF_MATRIX_COEFFICIENTS_BT601; // 6 |
4273 | | tmapFullRange = 1; |
4274 | | } |
4275 | | } |
4276 | | AVIF_CHECKRES(avifParseMiniHDRProperties(&s, &hasClli, &clli)); |
4277 | | if (hasGainmap) { |
4278 | | AVIF_CHECKRES(avifParseMiniHDRProperties(&s, &tmapHasClli, &tmapClli)); |
4279 | | } |
4280 | | } |
4281 | | |
4282 | | // Chunk sizes |
4283 | | uint32_t largeMetadataFlag = 0, largeCodecConfigFlag = 0, largeItemDataFlag = 0; |
4284 | | if (hasIcc || hasExif || hasXmp || (hasHdr && hasGainmap)) { |
4285 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &largeMetadataFlag, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) large_metadata_flag; |
4286 | | } |
4287 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &largeCodecConfigFlag, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) large_codec_config_flag; |
4288 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &largeItemDataFlag, 1), AVIF_RESULT_BMFF_PARSE_FAILED); // bit(1) large_item_data_flag; |
4289 | | |
4290 | | uint32_t iccDataSize = 0; |
4291 | | if (hasIcc) { |
4292 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &iccDataSize, largeMetadataFlag ? 20 : 10), |
4293 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_metadata_flag ? 20 : 10) icc_data_size_minus1; |
4294 | | ++iccDataSize; |
4295 | | } |
4296 | | uint32_t tmapIccDataSize = 0; |
4297 | | if (hasHdr && hasGainmap && tmapHasIcc) { |
4298 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &tmapIccDataSize, largeMetadataFlag ? 20 : 10), |
4299 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_metadata_flag ? 20 : 10) tmap_icc_data_size_minus1; |
4300 | | ++tmapIccDataSize; |
4301 | | } |
4302 | | |
4303 | | uint32_t gainmapMetadataSize = 0, gainmapItemDataSize = 0, gainmapItemCodecConfigSize = 0; |
4304 | | if (hasHdr && hasGainmap) { |
4305 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &gainmapMetadataSize, largeMetadataFlag ? 20 : 10), |
4306 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_metadata_flag ? 20 : 10) gainmap_metadata_size; |
4307 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &gainmapItemDataSize, largeItemDataFlag ? 28 : 15), |
4308 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_item_data_flag ? 28 : 15) gainmap_item_data_size; |
4309 | | if (gainmapItemDataSize != 0) { |
4310 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &gainmapItemCodecConfigSize, largeCodecConfigFlag ? 12 : 3), |
4311 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_codec_config_flag ? 12 : 3) gainmap_item_codec_config_size; |
4312 | | } |
4313 | | } |
4314 | | |
4315 | | uint32_t mainItemCodecConfigSize, mainItemDataSize; |
4316 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &mainItemCodecConfigSize, largeCodecConfigFlag ? 12 : 3), |
4317 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_codec_config_flag ? 12 : 3) main_item_codec_config_size; |
4318 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &mainItemDataSize, largeItemDataFlag ? 28 : 15), |
4319 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_item_data_flag ? 28 : 15) main_item_data_size_minus1; |
4320 | | ++mainItemDataSize; |
4321 | | |
4322 | | uint32_t alphaItemCodecConfigSize = 0, alphaItemDataSize = 0; |
4323 | | if (hasAlpha) { |
4324 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &alphaItemDataSize, largeItemDataFlag ? 28 : 15), |
4325 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_item_data_flag ? 28 : 15) alpha_item_data_size; |
4326 | | } |
4327 | | if (hasAlpha && alphaItemDataSize != 0) { |
4328 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &alphaItemCodecConfigSize, largeCodecConfigFlag ? 12 : 3), |
4329 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_codec_config_flag ? 12 : 3) alpha_item_codec_config_size; |
4330 | | } |
4331 | | |
4332 | | if (hasExif || hasXmp) { |
4333 | | uint8_t exifXmpCompressedFlag; |
4334 | | AVIF_CHECKERR(avifROStreamReadBitsU8(&s, &exifXmpCompressedFlag, 1), |
4335 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(1) exif_xmp_compressed_flag; |
4336 | | AVIF_CHECKERR(!exifXmpCompressedFlag, AVIF_RESULT_NOT_IMPLEMENTED); |
4337 | | } |
4338 | | uint32_t exifDataSize = 0; |
4339 | | if (hasExif) { |
4340 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &exifDataSize, largeMetadataFlag ? 20 : 10), |
4341 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_metadata_flag ? 20 : 10) exif_data_size_minus_one; |
4342 | | ++exifDataSize; |
4343 | | } |
4344 | | uint32_t xmpDataSize = 0; |
4345 | | if (hasXmp) { |
4346 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &xmpDataSize, largeMetadataFlag ? 20 : 10), |
4347 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(large_metadata_flag ? 20 : 10) xmp_data_size_minus_one; |
4348 | | ++xmpDataSize; |
4349 | | } |
4350 | | |
4351 | | // trailing_bits(); // bit padding till byte alignment |
4352 | | if (s.numUsedBitsInPartialByte) { |
4353 | | uint32_t padding; |
4354 | | AVIF_CHECKERR(avifROStreamReadBitsU32(&s, &padding, 8 - s.numUsedBitsInPartialByte), AVIF_RESULT_BMFF_PARSE_FAILED); |
4355 | | AVIF_CHECKERR(padding == 0, AVIF_RESULT_BMFF_PARSE_FAILED); // Only accept zeros as padding. |
4356 | | } |
4357 | | |
4358 | | // Codec configuration ('av1C' always uses 4 bytes) |
4359 | | avifCodecConfigurationBox mainItemCodecConfig; |
4360 | | AVIF_CHECKERR(mainItemCodecConfigSize == 4, AVIF_RESULT_BMFF_PARSE_FAILED); |
4361 | | AVIF_CHECKERR(avifParseCodecConfiguration(&s, &mainItemCodecConfig, (const char *)codecConfigType, diag), |
4362 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(8) main_item_codec_config[main_item_codec_config_size]; |
4363 | | avifCodecConfigurationBox alphaItemCodecConfig = { 0 }; |
4364 | | if (hasAlpha && alphaItemDataSize != 0) { |
4365 | | if (alphaItemCodecConfigSize == 0) { |
4366 | | alphaItemCodecConfigSize = mainItemCodecConfigSize; |
4367 | | alphaItemCodecConfig = mainItemCodecConfig; |
4368 | | } else { |
4369 | | AVIF_CHECKERR(alphaItemCodecConfigSize == 4, AVIF_RESULT_BMFF_PARSE_FAILED); |
4370 | | AVIF_CHECKERR(avifParseCodecConfiguration(&s, &alphaItemCodecConfig, (const char *)codecConfigType, diag), |
4371 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(8) alpha_item_codec_config[alpha_item_codec_config_size]; |
4372 | | } |
4373 | | } |
4374 | | avifCodecConfigurationBox gainmapItemCodecConfig = { 0 }; |
4375 | | if (hasHdr && hasGainmap && gainmapItemDataSize != 0) { |
4376 | | if (gainmapItemCodecConfigSize == 0) { |
4377 | | gainmapItemCodecConfigSize = mainItemCodecConfigSize; |
4378 | | gainmapItemCodecConfig = mainItemCodecConfig; |
4379 | | } else { |
4380 | | AVIF_CHECKERR(gainmapItemCodecConfigSize == 4, AVIF_RESULT_BMFF_PARSE_FAILED); |
4381 | | AVIF_CHECKERR(avifParseCodecConfiguration(&s, &gainmapItemCodecConfig, (const char *)codecConfigType, diag), |
4382 | | AVIF_RESULT_BMFF_PARSE_FAILED); // unsigned int(8) gainmap_item_codec_config[gainmap_item_codec_config_size]; |
4383 | | } |
4384 | | } |
4385 | | |
4386 | | // Make sure all metadata and coded chunks fit into the 'meta' box whose size is rawLen. |
4387 | | // There should be no missing nor unused byte. |
4388 | | |
4389 | | AVIF_CHECKERR(avifROStreamRemainingBytes(&s) == (uint64_t)iccDataSize + tmapIccDataSize + gainmapMetadataSize + alphaItemDataSize + |
4390 | | gainmapItemDataSize + mainItemDataSize + exifDataSize + xmpDataSize, |
4391 | | AVIF_RESULT_BMFF_PARSE_FAILED); |
4392 | | |
4393 | | // Create the items and properties generated by the MinimizedImageBox. |
4394 | | // The MinimizedImageBox always creates 8 properties for specification easiness. |
4395 | | // Use FreeSpaceBoxes as no-op placeholder properties when necessary. |
4396 | | // There is no need to use placeholder items because item IDs do not have to |
4397 | | // be contiguous, whereas property indices shall be 1, 2, 3, 4, 5 etc. |
4398 | | |
4399 | | meta->primaryItemID = 1; |
4400 | | avifDecoderItem * colorItem; |
4401 | | AVIF_CHECKRES(avifMetaFindOrCreateItem(meta, meta->primaryItemID, &colorItem)); |
4402 | | memcpy(colorItem->type, infeType, 4); |
4403 | | colorItem->width = width; |
4404 | | colorItem->height = height; |
4405 | | colorItem->miniBoxPixelFormat = chromaSubsampling == 0 ? AVIF_PIXEL_FORMAT_YUV400 |
4406 | | : chromaSubsampling == 1 ? AVIF_PIXEL_FORMAT_YUV420 |
4407 | | : chromaSubsampling == 2 ? AVIF_PIXEL_FORMAT_YUV422 |
4408 | | : AVIF_PIXEL_FORMAT_YUV444; |
4409 | | if (colorItem->miniBoxPixelFormat == AVIF_PIXEL_FORMAT_YUV422) { |
4410 | | // In AV1, the chroma_sample_position syntax element is not present for the YUV 4:2:2 format. |
4411 | | // Assume that AV1 uses the same 4:2:2 chroma sample location as HEVC and VVC (colocated). |
4412 | | AVIF_CHECKERR(!chromaIsHorizontallyCentered, AVIF_RESULT_BMFF_PARSE_FAILED); |
4413 | | // chromaIsVerticallyCentered: Ignored unless chroma_subsampling is 1. |
4414 | | colorItem->miniBoxChromaSamplePosition = AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN; |
4415 | | } else if (colorItem->miniBoxPixelFormat == AVIF_PIXEL_FORMAT_YUV420) { |
4416 | | if (chromaIsHorizontallyCentered) { |
4417 | | // There is no way to describe this with AV1's chroma_sample_position enum besides CSP_UNKNOWN. |
4418 | | // There is a proposal to assign the reserved value 3 (CSP_RESERVED) to the center chroma sample position. |
4419 | | colorItem->miniBoxChromaSamplePosition = AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN; |
4420 | | } else { |
4421 | | colorItem->miniBoxChromaSamplePosition = chromaIsVerticallyCentered ? AVIF_CHROMA_SAMPLE_POSITION_VERTICAL |
4422 | | : AVIF_CHROMA_SAMPLE_POSITION_COLOCATED; |
4423 | | } |
4424 | | } else { |
4425 | | // chromaIsHorizontallyCentered: Ignored unless chroma_subsampling is 1 or 2. |
4426 | | // chromaIsVerticallyCentered: Ignored unless chroma_subsampling is 1. |
4427 | | colorItem->miniBoxChromaSamplePosition = AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN; |
4428 | | } |
4429 | | |
4430 | | avifDecoderItem * alphaItem = NULL; |
4431 | | if (hasAlpha) { |
4432 | | AVIF_CHECKRES(avifMetaFindOrCreateItem(meta, /*itemID=*/2, &alphaItem)); |
4433 | | memcpy(alphaItem->type, infeType, 4); |
4434 | | alphaItem->width = width; |
4435 | | alphaItem->height = height; |
4436 | | alphaItem->miniBoxPixelFormat = AVIF_PIXEL_FORMAT_YUV400; |
4437 | | alphaItem->miniBoxChromaSamplePosition = AVIF_CHROMA_SAMPLE_POSITION_UNKNOWN; |
4438 | | } |
4439 | | |
4440 | | avifDecoderItem * tmapItem = NULL; |
4441 | | if (hasGainmap) { |
4442 | | AVIF_CHECKRES(avifMetaFindOrCreateItem(meta, /*itemID=*/3, &tmapItem)); |
4443 | | memcpy(tmapItem->type, "tmap", 4); |
4444 | | colorItem->dimgForID = tmapItem->id; |
4445 | | colorItem->dimgIdx = 0; |
4446 | | |
4447 | | // avifDecoderReset() requires the 'tmap' item to be an alternative to the primary item. |
4448 | | avifEntityToGroup * group = avifArrayPush(&data->meta->entityToGroups); |
4449 | | AVIF_CHECKERR(group != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
4450 | | memcpy(group->groupingType, "altr", 4); |
4451 | | AVIF_CHECKERR(avifArrayCreate(&group->entityIDs, sizeof(uint32_t), 2), AVIF_RESULT_OUT_OF_MEMORY); |
4452 | | uint32_t * groupEntityId = avifArrayPush(&group->entityIDs); |
4453 | | AVIF_CHECKERR(groupEntityId != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
4454 | | *groupEntityId = tmapItem->id; |
4455 | | groupEntityId = avifArrayPush(&group->entityIDs); |
4456 | | AVIF_CHECKERR(groupEntityId != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
4457 | | *groupEntityId = colorItem->id; |
4458 | | } |
4459 | | avifDecoderItem * gainmapItem = NULL; |
4460 | | if (gainmapItemDataSize != 0) { |
4461 | | AVIF_CHECKRES(avifMetaFindOrCreateItem(meta, /*itemID=*/4, &gainmapItem)); |
4462 | | memcpy(gainmapItem->type, infeType, 4); |
4463 | | gainmapItem->width = gainmapWidth; |
4464 | | gainmapItem->height = gainmapHeight; |
4465 | | gainmapItem->dimgForID = tmapItem->id; |
4466 | | gainmapItem->dimgIdx = 1; |
4467 | | } |
4468 | | |
4469 | | // Property with fixed index 1. |
4470 | | avifProperty * colorCodecConfigProp = avifMetaCreateProperty(meta, (const char *)codecConfigType); |
4471 | | AVIF_CHECKERR(colorCodecConfigProp, AVIF_RESULT_OUT_OF_MEMORY); |
4472 | | colorCodecConfigProp->u.av1C = mainItemCodecConfig; |
4473 | | AVIF_CHECKERR(avifDecoderItemAddProperty(colorItem, colorCodecConfigProp), AVIF_RESULT_OUT_OF_MEMORY); |
4474 | | |
4475 | | // Property with fixed index 2. |
4476 | | avifProperty * ispeProp = avifMetaCreateProperty(meta, "ispe"); |
4477 | | AVIF_CHECKERR(ispeProp, AVIF_RESULT_OUT_OF_MEMORY); |
4478 | | ispeProp->u.ispe.width = width; |
4479 | | ispeProp->u.ispe.height = height; |
4480 | | AVIF_CHECKERR(avifDecoderItemAddProperty(colorItem, ispeProp), AVIF_RESULT_OUT_OF_MEMORY); |
4481 | | |
4482 | | // Property with fixed index 3. |
4483 | | avifProperty * pixiProp = avifMetaCreateProperty(meta, "pixi"); |
4484 | | AVIF_CHECKERR(pixiProp, AVIF_RESULT_OUT_OF_MEMORY); |
4485 | | pixiProp->u.pixi.planeCount = chromaSubsampling == 0 ? 1 : 3; |
4486 | | for (uint8_t plane = 0; plane < pixiProp->u.pixi.planeCount; ++plane) { |
4487 | | pixiProp->u.pixi.planeDepths[plane] = (uint8_t)bitDepth; |
4488 | | } |
4489 | | AVIF_CHECKERR(avifDecoderItemAddProperty(colorItem, pixiProp), AVIF_RESULT_OUT_OF_MEMORY); |
4490 | | |
4491 | | // Property with fixed index 4. |
4492 | | avifProperty * colrPropNCLX = avifMetaCreateProperty(meta, "colr"); |
4493 | | AVIF_CHECKERR(colrPropNCLX, AVIF_RESULT_OUT_OF_MEMORY); |
4494 | | colrPropNCLX->u.colr.hasNCLX = AVIF_TRUE; // colour_type "nclx" |
4495 | | colrPropNCLX->u.colr.colorPrimaries = (avifColorPrimaries)colorPrimaries; |
4496 | | colrPropNCLX->u.colr.transferCharacteristics = (avifTransferCharacteristics)transferCharacteristics; |
4497 | | colrPropNCLX->u.colr.matrixCoefficients = (avifMatrixCoefficients)matrixCoefficients; |
4498 | | colrPropNCLX->u.colr.range = fullRange ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED; |
4499 | | AVIF_CHECKERR(avifDecoderItemAddProperty(colorItem, colrPropNCLX), AVIF_RESULT_OUT_OF_MEMORY); |
4500 | | |
4501 | | // Property with fixed index 5. |
4502 | | if (iccDataSize != 0) { |
4503 | | avifProperty * colrPropICC = avifMetaCreateProperty(meta, "colr"); |
4504 | | AVIF_CHECKERR(colrPropICC, AVIF_RESULT_OUT_OF_MEMORY); |
4505 | | colrPropICC->u.colr.hasICC = AVIF_TRUE; // colour_type "rICC" or "prof" |
4506 | | colrPropICC->u.colr.iccOffset = rawOffset + avifROStreamOffset(&s); |
4507 | | colrPropICC->u.colr.iccSize = (size_t)iccDataSize; |
4508 | | AVIF_CHECKERR(avifROStreamSkip(&s, colrPropICC->u.colr.iccSize), AVIF_RESULT_BMFF_PARSE_FAILED); |
4509 | | AVIF_CHECKERR(avifDecoderItemAddProperty(colorItem, colrPropICC), AVIF_RESULT_OUT_OF_MEMORY); |
4510 | | } else { |
4511 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); // Placeholder. |
4512 | | } |
4513 | | |
4514 | | if (alphaItemCodecConfigSize != 0) { |
4515 | | // Property with fixed index 6. |
4516 | | avifProperty * alphaCodecConfigProp = avifMetaCreateProperty(meta, (const char *)codecConfigType); |
4517 | | AVIF_CHECKERR(alphaCodecConfigProp, AVIF_RESULT_OUT_OF_MEMORY); |
4518 | | alphaCodecConfigProp->u.av1C = alphaItemCodecConfig; |
4519 | | AVIF_CHECKERR(avifDecoderItemAddProperty(alphaItem, alphaCodecConfigProp), AVIF_RESULT_OUT_OF_MEMORY); |
4520 | | } else { |
4521 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); // Placeholder. |
4522 | | } |
4523 | | |
4524 | | if (hasAlpha) { |
4525 | | // Property with fixed index 7. |
4526 | | alphaItem->auxForID = colorItem->id; |
4527 | | colorItem->premByID = alphaIsPremultiplied; |
4528 | | avifProperty * alphaAuxProp = avifMetaCreateProperty(meta, "auxC"); |
4529 | | AVIF_CHECKERR(alphaAuxProp, AVIF_RESULT_OUT_OF_MEMORY); |
4530 | | static_assert(sizeof(alphaAuxProp->u.auxC.auxType) >= sizeof(AVIF_URN_ALPHA0), ""); |
4531 | | memcpy(alphaAuxProp->u.auxC.auxType, AVIF_URN_ALPHA0, sizeof(AVIF_URN_ALPHA0)); |
4532 | | AVIF_CHECKERR(avifDecoderItemAddProperty(alphaItem, alphaAuxProp), AVIF_RESULT_OUT_OF_MEMORY); |
4533 | | |
4534 | | // Property with fixed index 2 (reused). |
4535 | | AVIF_CHECKERR(avifDecoderItemAddProperty(alphaItem, ispeProp), AVIF_RESULT_OUT_OF_MEMORY); |
4536 | | |
4537 | | // Property with fixed index 8. |
4538 | | avifProperty * alphaPixiProp = avifMetaCreateProperty(meta, "pixi"); |
4539 | | AVIF_CHECKERR(alphaPixiProp, AVIF_RESULT_OUT_OF_MEMORY); |
4540 | | memcpy(alphaPixiProp->type, "pixi", 4); |
4541 | | alphaPixiProp->u.pixi.planeCount = 1; |
4542 | | alphaPixiProp->u.pixi.planeDepths[0] = (uint8_t)bitDepth; |
4543 | | AVIF_CHECKERR(avifDecoderItemAddProperty(alphaItem, alphaPixiProp), AVIF_RESULT_OUT_OF_MEMORY); |
4544 | | } else { |
4545 | | // Placeholders 7 and 8. |
4546 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); |
4547 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); |
4548 | | } |
4549 | | |
4550 | | uint32_t irotPropIndex = 0; // 0-based. |
4551 | | uint32_t imirPropIndex = 0; |
4552 | | // Same behavior as avifImageExtractExifOrientationToIrotImir(). |
4553 | | if (orientation == 3 || orientation == 5 || orientation == 6 || orientation == 7 || orientation == 8) { |
4554 | | irotPropIndex = meta->properties.count; // Store index instead of pointer which may be invalidated by avifMetaCreateProperty(). |
4555 | | // Property with fixed 1-based index 9. |
4556 | | assert(irotPropIndex + 1 == 9); |
4557 | | avifProperty * irotProp = avifMetaCreateProperty(meta, "irot"); |
4558 | | AVIF_CHECKERR(irotProp, AVIF_RESULT_OUT_OF_MEMORY); |
4559 | | irotProp->u.irot.angle = orientation == 3 ? 2 : (orientation == 5 || orientation == 8) ? 1 : 3; |
4560 | | } else { |
4561 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); // Placeholder. |
4562 | | } |
4563 | | if (orientation == 2 || orientation == 4 || orientation == 5 || orientation == 7) { |
4564 | | imirPropIndex = meta->properties.count; |
4565 | | // Property with fixed 1-based index 10. |
4566 | | assert(imirPropIndex + 1 == 10); |
4567 | | avifProperty * imirProp = avifMetaCreateProperty(meta, "imir"); |
4568 | | AVIF_CHECKERR(imirProp, AVIF_RESULT_OUT_OF_MEMORY); |
4569 | | imirProp->u.imir.axis = orientation == 2 ? 1 : 0; |
4570 | | } else { |
4571 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); // Placeholder. |
4572 | | } |
4573 | | |
4574 | | if (hasClli) { |
4575 | | // Property with fixed index 11. |
4576 | | avifProperty * clliProp = avifMetaCreateProperty(meta, "clli"); |
4577 | | AVIF_CHECKERR(clliProp, AVIF_RESULT_OUT_OF_MEMORY); |
4578 | | clliProp->u.clli = clli; |
4579 | | AVIF_CHECKERR(avifDecoderItemAddProperty(colorItem, clliProp), AVIF_RESULT_OUT_OF_MEMORY); |
4580 | | } else { |
4581 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); // Placeholder. |
4582 | | } |
4583 | | // Properties with fixed indices 12 to 16 are ignored by libavif (mdcv, cclv, amve, reve and ndwt). |
4584 | | for (int i = 12; i <= 16; ++i) { |
4585 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); // Placeholder. |
4586 | | } |
4587 | | |
4588 | | if (gainmapItemCodecConfigSize != 0) { |
4589 | | // Property with fixed index 17. |
4590 | | avifProperty * gainmapCodecConfigProp = avifMetaCreateProperty(meta, (const char *)codecConfigType); |
4591 | | AVIF_CHECKERR(gainmapCodecConfigProp, AVIF_RESULT_OUT_OF_MEMORY); |
4592 | | gainmapCodecConfigProp->u.av1C = gainmapItemCodecConfig; |
4593 | | AVIF_CHECKERR(avifDecoderItemAddProperty(gainmapItem, gainmapCodecConfigProp), AVIF_RESULT_OUT_OF_MEMORY); |
4594 | | } else { |
4595 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); // Placeholder. |
4596 | | } |
4597 | | |
4598 | | if (gainmapItemDataSize != 0) { |
4599 | | // Property with fixed index 18. |
4600 | | avifProperty * gainmapIspeProp = avifMetaCreateProperty(meta, "ispe"); |
4601 | | AVIF_CHECKERR(gainmapIspeProp, AVIF_RESULT_OUT_OF_MEMORY); |
4602 | | gainmapIspeProp->u.ispe.width = gainmapWidth; |
4603 | | gainmapIspeProp->u.ispe.height = gainmapHeight; |
4604 | | AVIF_CHECKERR(avifDecoderItemAddProperty(gainmapItem, gainmapIspeProp), AVIF_RESULT_OUT_OF_MEMORY); |
4605 | | |
4606 | | // Property with fixed index 19. |
4607 | | avifProperty * gainmapPixiProp = avifMetaCreateProperty(meta, "pixi"); |
4608 | | AVIF_CHECKERR(gainmapPixiProp, AVIF_RESULT_OUT_OF_MEMORY); |
4609 | | memcpy(gainmapPixiProp->type, "pixi", 4); |
4610 | | gainmapPixiProp->u.pixi.planeCount = gainmapChromaSubsampling == 0 ? 1 : 3; |
4611 | | for (uint8_t plane = 0; plane < gainmapPixiProp->u.pixi.planeCount; ++plane) { |
4612 | | gainmapPixiProp->u.pixi.planeDepths[plane] = (uint8_t)gainmapBitDepth; |
4613 | | } |
4614 | | AVIF_CHECKERR(avifDecoderItemAddProperty(gainmapItem, gainmapPixiProp), AVIF_RESULT_OUT_OF_MEMORY); |
4615 | | |
4616 | | // Property with fixed index 20. |
4617 | | avifProperty * gainmapColrPropNCLX = avifMetaCreateProperty(meta, "colr"); |
4618 | | AVIF_CHECKERR(gainmapColrPropNCLX, AVIF_RESULT_OUT_OF_MEMORY); |
4619 | | gainmapColrPropNCLX->u.colr.hasNCLX = AVIF_TRUE; // colour_type "nclx" |
4620 | | gainmapColrPropNCLX->u.colr.colorPrimaries = AVIF_COLOR_PRIMARIES_UNSPECIFIED; // 2 |
4621 | | gainmapColrPropNCLX->u.colr.transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED; // 2 |
4622 | | gainmapColrPropNCLX->u.colr.matrixCoefficients = (avifMatrixCoefficients)gainmapMatrixCoefficients; |
4623 | | gainmapColrPropNCLX->u.colr.range = gainmapFullRange ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED; |
4624 | | AVIF_CHECKERR(avifDecoderItemAddProperty(gainmapItem, gainmapColrPropNCLX), AVIF_RESULT_OUT_OF_MEMORY); |
4625 | | } else { |
4626 | | // Placeholders 18, 19 and 20. |
4627 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); |
4628 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); |
4629 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); |
4630 | | } |
4631 | | |
4632 | | if (hasGainmap) { |
4633 | | // Property with fixed index 21. |
4634 | | avifProperty * tmapIspeProp = avifMetaCreateProperty(meta, "ispe"); |
4635 | | AVIF_CHECKERR(tmapIspeProp, AVIF_RESULT_OUT_OF_MEMORY); |
4636 | | tmapIspeProp->u.ispe.width = orientation <= 4 ? width : height; |
4637 | | tmapIspeProp->u.ispe.height = orientation <= 4 ? height : width; |
4638 | | AVIF_CHECKERR(avifDecoderItemAddProperty(tmapItem, tmapIspeProp), AVIF_RESULT_OUT_OF_MEMORY); |
4639 | | } else { |
4640 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); // Placeholder. |
4641 | | } |
4642 | | |
4643 | | if (hasGainmap && (tmapHasExplicitCicp || !tmapHasIcc)) { |
4644 | | // Property with fixed index 22. |
4645 | | avifProperty * tmapColrPropNCLX = avifMetaCreateProperty(meta, "colr"); |
4646 | | AVIF_CHECKERR(tmapColrPropNCLX, AVIF_RESULT_OUT_OF_MEMORY); |
4647 | | tmapColrPropNCLX->u.colr.hasNCLX = AVIF_TRUE; // colour_type "nclx" |
4648 | | tmapColrPropNCLX->u.colr.colorPrimaries = (avifColorPrimaries)tmapColorPrimaries; |
4649 | | tmapColrPropNCLX->u.colr.transferCharacteristics = (avifTransferCharacteristics)tmapTransferCharacteristics; |
4650 | | tmapColrPropNCLX->u.colr.matrixCoefficients = (avifMatrixCoefficients)tmapMatrixCoefficients; |
4651 | | tmapColrPropNCLX->u.colr.range = tmapFullRange ? AVIF_RANGE_FULL : AVIF_RANGE_LIMITED; |
4652 | | AVIF_CHECKERR(avifDecoderItemAddProperty(tmapItem, tmapColrPropNCLX), AVIF_RESULT_OUT_OF_MEMORY); |
4653 | | } else { |
4654 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); // Placeholder. |
4655 | | } |
4656 | | |
4657 | | if (tmapIccDataSize != 0) { |
4658 | | // Property with fixed index 23. |
4659 | | avifProperty * tmapColrPropICC = avifMetaCreateProperty(meta, "colr"); |
4660 | | AVIF_CHECKERR(tmapColrPropICC, AVIF_RESULT_OUT_OF_MEMORY); |
4661 | | tmapColrPropICC->u.colr.hasICC = AVIF_TRUE; // colour_type "rICC" or "prof" |
4662 | | tmapColrPropICC->u.colr.iccOffset = rawOffset + avifROStreamOffset(&s); |
4663 | | tmapColrPropICC->u.colr.iccSize = tmapIccDataSize; |
4664 | | AVIF_CHECKERR(avifROStreamSkip(&s, tmapColrPropICC->u.colr.iccSize), AVIF_RESULT_BMFF_PARSE_FAILED); |
4665 | | AVIF_CHECKERR(avifDecoderItemAddProperty(colorItem, tmapColrPropICC), AVIF_RESULT_OUT_OF_MEMORY); |
4666 | | } else { |
4667 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); // Placeholder. |
4668 | | } |
4669 | | |
4670 | | if (tmapHasClli) { |
4671 | | // Property with fixed index 24. |
4672 | | avifProperty * tmapClliProp = avifMetaCreateProperty(meta, "clli"); |
4673 | | AVIF_CHECKERR(tmapClliProp, AVIF_RESULT_OUT_OF_MEMORY); |
4674 | | tmapClliProp->u.clli = tmapClli; |
4675 | | AVIF_CHECKERR(avifDecoderItemAddProperty(tmapItem, tmapClliProp), AVIF_RESULT_OUT_OF_MEMORY); |
4676 | | } else { |
4677 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); // Placeholder. |
4678 | | } |
4679 | | // Properties with fixed indices 25 to 29 are ignored by libavif (mdcv, cclv, amve, reve and ndwt). |
4680 | | for (int i = 25; i <= 29; ++i) { |
4681 | | AVIF_CHECKERR(avifMetaCreateProperty(meta, "skip"), AVIF_RESULT_OUT_OF_MEMORY); // Placeholder. |
4682 | | } |
4683 | | AVIF_ASSERT_OR_RETURN(meta->properties.count == 29); |
4684 | | |
4685 | | // ISO/IEC 23008-12 Section 6.5.1: |
4686 | | // Writers should arrange the descriptive properties specified in 6.5 prior to any other properties in the |
4687 | | // sequence associating properties with an item. |
4688 | | // |
4689 | | // irot and imir are transformative properties, so associate them last. |
4690 | | if (irotPropIndex != 0) { |
4691 | | const avifProperty * irotProp = &meta->properties.prop[irotPropIndex]; |
4692 | | AVIF_CHECKERR(avifDecoderItemAddProperty(colorItem, irotProp), AVIF_RESULT_OUT_OF_MEMORY); |
4693 | | if (hasAlpha) { |
4694 | | AVIF_CHECKERR(avifDecoderItemAddProperty(alphaItem, irotProp), AVIF_RESULT_OUT_OF_MEMORY); |
4695 | | } |
4696 | | if (gainmapItemDataSize != 0) { |
4697 | | AVIF_CHECKERR(avifDecoderItemAddProperty(gainmapItem, irotProp), AVIF_RESULT_OUT_OF_MEMORY); |
4698 | | } |
4699 | | } |
4700 | | if (imirPropIndex != 0) { |
4701 | | const avifProperty * imirProp = &meta->properties.prop[imirPropIndex]; |
4702 | | AVIF_CHECKERR(avifDecoderItemAddProperty(colorItem, imirProp), AVIF_RESULT_OUT_OF_MEMORY); |
4703 | | if (hasAlpha) { |
4704 | | AVIF_CHECKERR(avifDecoderItemAddProperty(alphaItem, imirProp), AVIF_RESULT_OUT_OF_MEMORY); |
4705 | | } |
4706 | | if (gainmapItemDataSize != 0) { |
4707 | | AVIF_CHECKERR(avifDecoderItemAddProperty(gainmapItem, imirProp), AVIF_RESULT_OUT_OF_MEMORY); |
4708 | | } |
4709 | | } |
4710 | | |
4711 | | // Extents. |
4712 | | |
4713 | | if (gainmapMetadataSize != 0) { |
4714 | | // Prepend the version field to the GainMapMetadata to form the ToneMapImage syntax. |
4715 | | tmapItem->size = gainmapMetadataSize + 1; |
4716 | | AVIF_CHECKRES(avifRWDataRealloc(&tmapItem->mergedExtents, tmapItem->size)); |
4717 | | tmapItem->ownsMergedExtents = AVIF_TRUE; |
4718 | | tmapItem->mergedExtents.data[0] = 0; // unsigned int(8) version = 0; |
4719 | | AVIF_CHECKERR(avifROStreamRead(&s, tmapItem->mergedExtents.data + 1, gainmapMetadataSize), AVIF_RESULT_BMFF_PARSE_FAILED); |
4720 | | } |
4721 | | |
4722 | | if (hasAlpha) { |
4723 | | avifExtent * alphaExtent = (avifExtent *)avifArrayPush(&alphaItem->extents); |
4724 | | AVIF_CHECKERR(alphaExtent, AVIF_RESULT_OUT_OF_MEMORY); |
4725 | | alphaExtent->offset = rawOffset + avifROStreamOffset(&s); |
4726 | | alphaExtent->size = alphaItemDataSize; |
4727 | | AVIF_CHECKERR(avifROStreamSkip(&s, alphaExtent->size), AVIF_RESULT_BMFF_PARSE_FAILED); |
4728 | | alphaItem->size = alphaExtent->size; |
4729 | | } |
4730 | | |
4731 | | if (gainmapItemDataSize != 0) { |
4732 | | avifExtent * gainmapExtent = (avifExtent *)avifArrayPush(&gainmapItem->extents); |
4733 | | AVIF_CHECKERR(gainmapExtent, AVIF_RESULT_OUT_OF_MEMORY); |
4734 | | gainmapExtent->offset = rawOffset + avifROStreamOffset(&s); |
4735 | | gainmapExtent->size = gainmapItemDataSize; |
4736 | | AVIF_CHECKERR(avifROStreamSkip(&s, gainmapExtent->size), AVIF_RESULT_BMFF_PARSE_FAILED); |
4737 | | gainmapItem->size = gainmapExtent->size; |
4738 | | } |
4739 | | |
4740 | | avifExtent * colorExtent = (avifExtent *)avifArrayPush(&colorItem->extents); |
4741 | | AVIF_CHECKERR(colorExtent, AVIF_RESULT_OUT_OF_MEMORY); |
4742 | | colorExtent->offset = rawOffset + avifROStreamOffset(&s); |
4743 | | colorExtent->size = mainItemDataSize; |
4744 | | AVIF_CHECKERR(avifROStreamSkip(&s, colorExtent->size), AVIF_RESULT_BMFF_PARSE_FAILED); |
4745 | | colorItem->size = colorExtent->size; |
4746 | | |
4747 | | if (hasExif) { |
4748 | | avifDecoderItem * exifItem; |
4749 | | AVIF_CHECKRES(avifMetaFindOrCreateItem(meta, /*itemID=*/6, &exifItem)); |
4750 | | memcpy(exifItem->type, "Exif", 4); |
4751 | | exifItem->descForID = colorItem->id; // 'cdsc' |
4752 | | |
4753 | | avifExtent * exifExtent = (avifExtent *)avifArrayPush(&exifItem->extents); |
4754 | | AVIF_CHECKERR(exifExtent, AVIF_RESULT_OUT_OF_MEMORY); |
4755 | | exifExtent->offset = rawOffset + avifROStreamOffset(&s); |
4756 | | exifExtent->size = exifDataSize; // Does not include unsigned int(32) exif_tiff_header_offset; |
4757 | | AVIF_CHECKERR(avifROStreamSkip(&s, exifExtent->size), AVIF_RESULT_BMFF_PARSE_FAILED); |
4758 | | exifItem->size = exifExtent->size; |
4759 | | } |
4760 | | |
4761 | | if (hasXmp) { |
4762 | | avifDecoderItem * xmpItem; |
4763 | | AVIF_CHECKRES(avifMetaFindOrCreateItem(meta, /*itemID=*/7, &xmpItem)); |
4764 | | memcpy(xmpItem->type, "mime", 4); |
4765 | | memcpy(xmpItem->contentType.contentType, AVIF_CONTENT_TYPE_XMP, sizeof(AVIF_CONTENT_TYPE_XMP)); |
4766 | | xmpItem->descForID = colorItem->id; // 'cdsc' |
4767 | | |
4768 | | avifExtent * xmpExtent = (avifExtent *)avifArrayPush(&xmpItem->extents); |
4769 | | AVIF_CHECKERR(xmpExtent, AVIF_RESULT_OUT_OF_MEMORY); |
4770 | | xmpExtent->offset = rawOffset + avifROStreamOffset(&s); |
4771 | | xmpExtent->size = xmpDataSize; |
4772 | | AVIF_CHECKERR(avifROStreamSkip(&s, xmpExtent->size), AVIF_RESULT_BMFF_PARSE_FAILED); |
4773 | | xmpItem->size = xmpExtent->size; |
4774 | | } |
4775 | | return AVIF_RESULT_OK; |
4776 | | } |
4777 | | #endif // AVIF_ENABLE_EXPERIMENTAL_MINI |
4778 | | |
4779 | | static avifBool avifParseFileTypeBox(avifFileType * ftyp, const uint8_t * raw, size_t rawLen, avifDiagnostics * diag) |
4780 | 49.4k | { |
4781 | 49.4k | BEGIN_STREAM(s, raw, rawLen, diag, "Box[ftyp]"); |
4782 | | |
4783 | 49.4k | AVIF_CHECK(avifROStreamRead(&s, ftyp->majorBrand, 4)); |
4784 | 49.4k | AVIF_CHECK(avifROStreamRead(&s, ftyp->minorVersion, 4)); |
4785 | | |
4786 | 49.4k | size_t compatibleBrandsBytes = avifROStreamRemainingBytes(&s); |
4787 | 49.4k | if ((compatibleBrandsBytes % 4) != 0) { |
4788 | 7 | avifDiagnosticsPrintf(diag, "Box[ftyp] contains a compatible brands section that isn't divisible by 4 [%zu]", compatibleBrandsBytes); |
4789 | 7 | return AVIF_FALSE; |
4790 | 7 | } |
4791 | 49.4k | ftyp->compatibleBrands = avifROStreamCurrent(&s); |
4792 | 49.4k | AVIF_CHECK(avifROStreamSkip(&s, compatibleBrandsBytes)); |
4793 | 49.4k | ftyp->compatibleBrandsCount = (int)compatibleBrandsBytes / 4; |
4794 | | |
4795 | 49.4k | return AVIF_TRUE; |
4796 | 49.4k | } |
4797 | | |
4798 | | static avifBool avifFileTypeHasBrand(avifFileType * ftyp, const char * brand); |
4799 | | static avifBool avifFileTypeIsCompatible(avifFileType * ftyp); |
4800 | | |
4801 | | static avifResult avifParse(avifDecoder * decoder) |
4802 | 16.5k | { |
4803 | | // Note: this top-level function is the only avifParse*() function that returns avifResult instead of avifBool. |
4804 | | // Be sure to use AVIF_CHECKERR() in this function with an explicit error result instead of simply using AVIF_CHECK(). |
4805 | | |
4806 | 16.5k | avifResult readResult; |
4807 | 16.5k | uint64_t parseOffset = 0; |
4808 | 16.5k | avifDecoderData * data = decoder->data; |
4809 | 16.5k | avifBool ftypSeen = AVIF_FALSE; |
4810 | 16.5k | avifBool metaSeen = AVIF_FALSE; |
4811 | 16.5k | avifBool metaIsSizeZero = AVIF_FALSE; |
4812 | 16.5k | avifBool moovSeen = AVIF_FALSE; |
4813 | 16.5k | avifBool needsMeta = AVIF_FALSE; |
4814 | 16.5k | avifBool needsMoov = AVIF_FALSE; |
4815 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
4816 | | avifBool miniSeen = AVIF_FALSE; |
4817 | | avifBool needsMini = AVIF_FALSE; |
4818 | | #endif |
4819 | 16.5k | avifBool needsTmap = AVIF_FALSE; |
4820 | 16.5k | avifBool tmapSeen = AVIF_FALSE; |
4821 | 16.5k | avifFileType ftyp = { 0 }; |
4822 | | |
4823 | 37.4k | for (;;) { |
4824 | | // Read just enough to get the next box header (a max of 32 bytes) |
4825 | 37.4k | avifROData headerContents; |
4826 | 37.4k | if ((decoder->io->sizeHint > 0) && (parseOffset > decoder->io->sizeHint)) { |
4827 | 149 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
4828 | 149 | } |
4829 | 37.2k | readResult = decoder->io->read(decoder->io, 0, parseOffset, 32, &headerContents); |
4830 | 37.2k | if (readResult != AVIF_RESULT_OK) { |
4831 | 0 | return readResult; |
4832 | 0 | } |
4833 | 37.2k | if (!headerContents.size) { |
4834 | | // If we got AVIF_RESULT_OK from the reader but received 0 bytes, |
4835 | | // we've reached the end of the file with no errors. Hooray! |
4836 | 36 | break; |
4837 | 36 | } |
4838 | | |
4839 | | // Parse the header, and find out how many bytes it actually was |
4840 | 37.2k | BEGIN_STREAM(headerStream, headerContents.data, headerContents.size, &decoder->diag, "File-level box header"); |
4841 | 37.2k | avifBoxHeader header; |
4842 | 37.2k | AVIF_CHECKERR(avifROStreamReadBoxHeaderPartial(&headerStream, &header, /*topLevel=*/AVIF_TRUE), AVIF_RESULT_BMFF_PARSE_FAILED); |
4843 | 37.2k | parseOffset += avifROStreamOffset(&headerStream); |
4844 | 37.2k | AVIF_ASSERT_OR_RETURN(decoder->io->sizeHint == 0 || parseOffset <= decoder->io->sizeHint); |
4845 | | |
4846 | | // Try to get the remainder of the box, if necessary |
4847 | 37.2k | uint64_t boxOffset = 0; |
4848 | 37.2k | avifROData boxContents = AVIF_DATA_EMPTY; |
4849 | | |
4850 | 37.2k | avifBool isFtyp = AVIF_FALSE, isMeta = AVIF_FALSE, isMoov = AVIF_FALSE; |
4851 | 37.2k | avifBool isNonSkippableVariableLengthBox = AVIF_FALSE; |
4852 | 37.2k | if (!memcmp(header.type, "ftyp", 4)) { |
4853 | 16.5k | isFtyp = AVIF_TRUE; |
4854 | 16.5k | isNonSkippableVariableLengthBox = AVIF_TRUE; |
4855 | 20.6k | } else if (!memcmp(header.type, "meta", 4)) { |
4856 | 15.6k | isMeta = AVIF_TRUE; |
4857 | 15.6k | isNonSkippableVariableLengthBox = AVIF_TRUE; |
4858 | 15.6k | metaIsSizeZero = header.isSizeZeroBox; |
4859 | 15.6k | } else if (!memcmp(header.type, "moov", 4)) { |
4860 | 946 | isMoov = AVIF_TRUE; |
4861 | 946 | isNonSkippableVariableLengthBox = AVIF_TRUE; |
4862 | 946 | } |
4863 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
4864 | | avifBool isMini = AVIF_FALSE; |
4865 | | if (!isNonSkippableVariableLengthBox && !memcmp(header.type, "mini", 4)) { |
4866 | | isMini = AVIF_TRUE; |
4867 | | isNonSkippableVariableLengthBox = AVIF_TRUE; |
4868 | | } |
4869 | | #endif |
4870 | | |
4871 | 37.2k | if (!isFtyp && (isNonSkippableVariableLengthBox || !memcmp(header.type, "free", 4) || !memcmp(header.type, "skip", 4) || |
4872 | 18.3k | !memcmp(header.type, "mdat", 4))) { |
4873 | | // Section 6.3.4 of ISO/IEC 14496-12: |
4874 | | // The FileTypeBox shall occur before any variable-length box (e.g. movie, free space, media data). |
4875 | 18.3k | AVIF_CHECKERR(ftypSeen, AVIF_RESULT_BMFF_PARSE_FAILED); |
4876 | 18.3k | } |
4877 | | |
4878 | 37.2k | if (isNonSkippableVariableLengthBox) { |
4879 | 33.1k | boxOffset = parseOffset; |
4880 | 33.1k | size_t sizeToRead; |
4881 | 33.1k | if (header.isSizeZeroBox) { |
4882 | | // The box body goes till the end of the file. |
4883 | 330 | if (decoder->io->sizeHint != 0 && decoder->io->sizeHint - parseOffset < SIZE_MAX) { |
4884 | 330 | sizeToRead = (size_t)(decoder->io->sizeHint - parseOffset); |
4885 | 330 | } else { |
4886 | 0 | sizeToRead = SIZE_MAX; // This will get truncated. See the documentation of avifIOReadFunc. |
4887 | 0 | } |
4888 | 32.8k | } else { |
4889 | 32.8k | sizeToRead = header.size; |
4890 | 32.8k | } |
4891 | 33.1k | readResult = decoder->io->read(decoder->io, 0, parseOffset, sizeToRead, &boxContents); |
4892 | 33.1k | if (readResult != AVIF_RESULT_OK) { |
4893 | 0 | return readResult; |
4894 | 0 | } |
4895 | 33.1k | if (header.isSizeZeroBox) { |
4896 | 330 | header.size = boxContents.size; |
4897 | 32.8k | } else if (boxContents.size != header.size) { |
4898 | | // A truncated box, bail out |
4899 | 118 | return AVIF_RESULT_TRUNCATED_DATA; |
4900 | 118 | } |
4901 | 33.1k | } else if (header.isSizeZeroBox) { |
4902 | | // An unknown top level box with size 0 was found. If we reach here it means we haven't completed parsing successfully |
4903 | | // since there are no further boxes left. |
4904 | 6 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
4905 | 4.05k | } else if (header.size > (UINT64_MAX - parseOffset)) { |
4906 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
4907 | 1 | } |
4908 | 37.1k | parseOffset += header.size; |
4909 | | |
4910 | 37.1k | if (isFtyp) { |
4911 | 16.5k | AVIF_CHECKERR(!ftypSeen, AVIF_RESULT_BMFF_PARSE_FAILED); |
4912 | 16.5k | AVIF_CHECKERR(avifParseFileTypeBox(&ftyp, boxContents.data, boxContents.size, data->diag), AVIF_RESULT_BMFF_PARSE_FAILED); |
4913 | 16.5k | AVIF_CHECKERR(avifFileTypeIsCompatible(&ftyp), AVIF_RESULT_INVALID_FTYP); |
4914 | 16.5k | ftypSeen = AVIF_TRUE; |
4915 | 16.5k | memcpy(data->majorBrand, ftyp.majorBrand, 4); // Remember the major brand for future AVIF_DECODER_SOURCE_AUTO decisions |
4916 | 16.5k | if (ftyp.compatibleBrandsCount > 0) { |
4917 | 15.1k | AVIF_CHECKERR(avifArrayCreate(&data->compatibleBrands, sizeof(avifBrand), ftyp.compatibleBrandsCount), |
4918 | 15.1k | AVIF_RESULT_OUT_OF_MEMORY); |
4919 | 15.1k | memcpy(data->compatibleBrands.brand, ftyp.compatibleBrands, sizeof(avifBrand) * ftyp.compatibleBrandsCount); |
4920 | 15.1k | data->compatibleBrands.count = ftyp.compatibleBrandsCount; |
4921 | 15.1k | } |
4922 | 16.5k | needsMeta = avifFileTypeHasBrand(&ftyp, "avif"); |
4923 | 16.5k | needsMoov = avifFileTypeHasBrand(&ftyp, "avis"); |
4924 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
4925 | | needsMini = avifFileTypeHasBrand(&ftyp, "mif3"); |
4926 | | if (needsMini) { |
4927 | | AVIF_CHECKERR(!needsMeta, AVIF_RESULT_INVALID_FTYP); |
4928 | | // Section O.2.1.2 of ISO/IEC 23008-12:2014, CDAM 2: |
4929 | | // When the 'mif3' brand is present as the major_brand of the FileTypeBox, |
4930 | | // the minor_version of the FileTypeBox shall be 0 or a brand that is either |
4931 | | // structurally compatible with the 'mif3' brand, such as a codec brand |
4932 | | // complying with the 'mif3' structural brand, or a brand to which the file |
4933 | | // conforms after the equivalent MetaBox has been transformed from |
4934 | | // MinimizedImageBox as specified in Clause O.4. |
4935 | | AVIF_CHECKERR(!memcmp(ftyp.minorVersion, "\0\0\0\0", 4) || !memcmp(ftyp.minorVersion, "avif", 4), |
4936 | | AVIF_RESULT_BMFF_PARSE_FAILED); |
4937 | | } |
4938 | | #endif // AVIF_ENABLE_EXPERIMENTAL_MINI |
4939 | 16.5k | needsTmap = avifFileTypeHasBrand(&ftyp, "tmap"); |
4940 | 16.5k | if (needsTmap) { |
4941 | 52 | needsMeta = AVIF_TRUE; |
4942 | 52 | } |
4943 | 20.5k | } else if (isMeta) { |
4944 | 15.6k | AVIF_CHECKERR(!metaSeen, AVIF_RESULT_BMFF_PARSE_FAILED); |
4945 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
4946 | | AVIF_CHECKERR(!miniSeen, AVIF_RESULT_BMFF_PARSE_FAILED); |
4947 | | #endif |
4948 | 15.6k | AVIF_CHECKRES(avifParseMetaBox(data->meta, boxOffset, boxContents.data, boxContents.size, data->diag)); |
4949 | 14.8k | metaSeen = AVIF_TRUE; |
4950 | | |
4951 | 36.5k | for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) { |
4952 | 21.7k | if (!memcmp(data->meta->items.item[itemIndex]->type, "tmap", 4)) { |
4953 | 17 | tmapSeen = AVIF_TRUE; |
4954 | 17 | break; |
4955 | 17 | } |
4956 | 21.7k | } |
4957 | | |
4958 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
4959 | | } else if (isMini) { |
4960 | | AVIF_CHECKERR(!metaSeen, AVIF_RESULT_BMFF_PARSE_FAILED); |
4961 | | AVIF_CHECKERR(!miniSeen, AVIF_RESULT_BMFF_PARSE_FAILED); |
4962 | | const avifBool isAvifAccordingToMinorVersion = !memcmp(ftyp.minorVersion, "avif", 4); |
4963 | | AVIF_CHECKRES( |
4964 | | avifParseMinimizedImageBox(data, boxOffset, boxContents.data, boxContents.size, isAvifAccordingToMinorVersion, data->diag)); |
4965 | | miniSeen = AVIF_TRUE; |
4966 | | #endif |
4967 | 14.8k | } else if (isMoov) { |
4968 | 918 | AVIF_CHECKERR(!moovSeen, AVIF_RESULT_BMFF_PARSE_FAILED); |
4969 | 917 | AVIF_CHECKRES( |
4970 | 917 | avifParseMovieBox(data, boxOffset, boxContents.data, boxContents.size, decoder->imageSizeLimit, decoder->imageDimensionLimit)); |
4971 | 336 | moovSeen = AVIF_TRUE; |
4972 | 336 | decoder->imageSequenceTrackPresent = AVIF_TRUE; |
4973 | 336 | } |
4974 | | |
4975 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
4976 | | if (ftypSeen && !needsMini) { |
4977 | | // When MinimizedImageBox is present in a file, the 'mif3' brand or a derived brand that implies the 'mif3' |
4978 | | // brand shall be the major brand or present among the compatible brands in the FileTypeBox. |
4979 | | AVIF_CHECKERR(!miniSeen, AVIF_RESULT_BMFF_PARSE_FAILED); |
4980 | | } |
4981 | | #endif // AVIF_ENABLE_EXPERIMENTAL_MINI |
4982 | | |
4983 | | // See if there is enough information to consider Parse() a success and early-out: |
4984 | | // * If the brand 'avif' is present, require a meta box |
4985 | | // * If the brand 'avis' is present, require a moov box |
4986 | | // * If AVIF_ENABLE_EXPERIMENTAL_MINI is defined and the brand 'mif3' is present, require a mini box |
4987 | 35.7k | avifBool sawEverythingNeeded = ftypSeen && (!needsMeta || metaSeen) && (!needsMoov || moovSeen) && (!needsTmap || tmapSeen); |
4988 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
4989 | | sawEverythingNeeded = sawEverythingNeeded && (!needsMini || miniSeen); |
4990 | | #endif |
4991 | 35.7k | if (sawEverythingNeeded) { |
4992 | 14.8k | return AVIF_RESULT_OK; |
4993 | 14.8k | } |
4994 | 35.7k | } |
4995 | 36 | if (!ftypSeen) { |
4996 | 0 | return AVIF_RESULT_INVALID_FTYP; |
4997 | 0 | } |
4998 | 36 | if ((needsMeta && !metaSeen) || (needsMoov && !moovSeen)) { |
4999 | 33 | return AVIF_RESULT_TRUNCATED_DATA; |
5000 | 33 | } |
5001 | 3 | if (needsTmap && !tmapSeen) { |
5002 | 3 | return metaIsSizeZero ? AVIF_RESULT_TRUNCATED_DATA : AVIF_RESULT_BMFF_PARSE_FAILED; |
5003 | 3 | } |
5004 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
5005 | | if (needsMini && !miniSeen) { |
5006 | | return AVIF_RESULT_TRUNCATED_DATA; |
5007 | | } |
5008 | | #endif |
5009 | 0 | return AVIF_RESULT_OK; |
5010 | 3 | } |
5011 | | |
5012 | | // --------------------------------------------------------------------------- |
5013 | | |
5014 | | static avifBool avifFileTypeHasBrand(avifFileType * ftyp, const char * brand) |
5015 | 101k | { |
5016 | 101k | if (!memcmp(ftyp->majorBrand, brand, 4)) { |
5017 | 47.7k | return AVIF_TRUE; |
5018 | 47.7k | } |
5019 | | |
5020 | 287k | for (int compatibleBrandIndex = 0; compatibleBrandIndex < ftyp->compatibleBrandsCount; ++compatibleBrandIndex) { |
5021 | 252k | const uint8_t * compatibleBrand = &ftyp->compatibleBrands[4 * compatibleBrandIndex]; |
5022 | 252k | if (!memcmp(compatibleBrand, brand, 4)) { |
5023 | 18.4k | return AVIF_TRUE; |
5024 | 18.4k | } |
5025 | 252k | } |
5026 | 35.0k | return AVIF_FALSE; |
5027 | 53.5k | } |
5028 | | |
5029 | | static avifBool avifFileTypeIsCompatible(avifFileType * ftyp) |
5030 | 49.4k | { |
5031 | 49.4k | return avifFileTypeHasBrand(ftyp, "avif") || avifFileTypeHasBrand(ftyp, "avis") |
5032 | | #if defined(AVIF_ENABLE_EXPERIMENTAL_MINI) |
5033 | | || avifFileTypeHasBrand(ftyp, "mif3") |
5034 | | #endif // AVIF_ENABLE_EXPERIMENTAL_MINI |
5035 | 49.4k | ; |
5036 | 49.4k | } |
5037 | | |
5038 | | avifBool avifPeekCompatibleFileType(const avifROData * input) |
5039 | 33.3k | { |
5040 | 33.3k | BEGIN_STREAM(s, input->data, input->size, NULL, NULL); |
5041 | | |
5042 | 33.3k | avifBoxHeader header; |
5043 | 33.3k | if (!avifROStreamReadBoxHeaderPartial(&s, &header, /*topLevel=*/AVIF_TRUE) || memcmp(header.type, "ftyp", 4)) { |
5044 | 159 | return AVIF_FALSE; |
5045 | 159 | } |
5046 | 33.1k | if (header.isSizeZeroBox) { |
5047 | | // The ftyp box goes on till the end of the file. Either there is no brand requiring anything in the file but a |
5048 | | // FileTypebox (so not AVIF), or it is invalid. |
5049 | 3 | return AVIF_FALSE; |
5050 | 3 | } |
5051 | 33.1k | AVIF_CHECK(avifROStreamHasBytesLeft(&s, header.size)); |
5052 | | |
5053 | 32.9k | avifFileType ftyp; |
5054 | 32.9k | memset(&ftyp, 0, sizeof(avifFileType)); |
5055 | 32.9k | avifBool parsed = avifParseFileTypeBox(&ftyp, avifROStreamCurrent(&s), header.size, NULL); |
5056 | 32.9k | if (!parsed) { |
5057 | 15 | return AVIF_FALSE; |
5058 | 15 | } |
5059 | 32.9k | return avifFileTypeIsCompatible(&ftyp); |
5060 | 32.9k | } |
5061 | | |
5062 | | static avifBool avifBrandArrayHasBrand(avifBrandArray * brands, const char * brand) |
5063 | 14.1k | { |
5064 | 59.4k | for (uint32_t brandIndex = 0; brandIndex < brands->count; ++brandIndex) { |
5065 | 45.3k | if (!memcmp(brands->brand[brandIndex], brand, 4)) { |
5066 | 13 | return AVIF_TRUE; |
5067 | 13 | } |
5068 | 45.3k | } |
5069 | 14.1k | return AVIF_FALSE; |
5070 | 14.1k | } |
5071 | | |
5072 | | // --------------------------------------------------------------------------- |
5073 | | |
5074 | | avifDecoder * avifDecoderCreate(void) |
5075 | 16.5k | { |
5076 | 16.5k | avifDecoder * decoder = (avifDecoder *)avifAlloc(sizeof(avifDecoder)); |
5077 | 16.5k | if (decoder == NULL) { |
5078 | 0 | return NULL; |
5079 | 0 | } |
5080 | 16.5k | memset(decoder, 0, sizeof(avifDecoder)); |
5081 | 16.5k | decoder->maxThreads = 1; |
5082 | 16.5k | decoder->imageSizeLimit = AVIF_DEFAULT_IMAGE_SIZE_LIMIT; |
5083 | 16.5k | decoder->imageDimensionLimit = AVIF_DEFAULT_IMAGE_DIMENSION_LIMIT; |
5084 | 16.5k | decoder->imageCountLimit = AVIF_DEFAULT_IMAGE_COUNT_LIMIT; |
5085 | 16.5k | decoder->strictFlags = AVIF_STRICT_ENABLED; |
5086 | 16.5k | decoder->imageContentToDecode = AVIF_IMAGE_CONTENT_DECODE_DEFAULT; |
5087 | 16.5k | return decoder; |
5088 | 16.5k | } |
5089 | | |
5090 | | static void avifDecoderCleanup(avifDecoder * decoder) |
5091 | 33.0k | { |
5092 | 33.0k | if (decoder->data) { |
5093 | 16.5k | avifDecoderDataDestroy(decoder->data); |
5094 | 16.5k | decoder->data = NULL; |
5095 | 16.5k | } |
5096 | | |
5097 | 33.0k | if (decoder->image) { |
5098 | 14.6k | avifImageDestroy(decoder->image); |
5099 | 14.6k | decoder->image = NULL; |
5100 | 14.6k | } |
5101 | 33.0k | avifDiagnosticsClearError(&decoder->diag); |
5102 | 33.0k | } |
5103 | | |
5104 | | void avifDecoderDestroy(avifDecoder * decoder) |
5105 | 16.5k | { |
5106 | 16.5k | avifDecoderCleanup(decoder); |
5107 | 16.5k | avifIODestroy(decoder->io); |
5108 | 16.5k | avifFree(decoder); |
5109 | 16.5k | } |
5110 | | |
5111 | | avifResult avifDecoderSetSource(avifDecoder * decoder, avifDecoderSource source) |
5112 | 0 | { |
5113 | 0 | decoder->requestedSource = source; |
5114 | 0 | return avifDecoderReset(decoder); |
5115 | 0 | } |
5116 | | |
5117 | | void avifDecoderSetIO(avifDecoder * decoder, avifIO * io) |
5118 | 16.5k | { |
5119 | 16.5k | avifIODestroy(decoder->io); |
5120 | 16.5k | decoder->io = io; |
5121 | 16.5k | } |
5122 | | |
5123 | | avifResult avifDecoderSetIOMemory(avifDecoder * decoder, const uint8_t * data, size_t size) |
5124 | 16.5k | { |
5125 | 16.5k | avifIO * io = avifIOCreateMemoryReader(data, size); |
5126 | 16.5k | AVIF_CHECKERR(io != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
5127 | 16.5k | avifDecoderSetIO(decoder, io); |
5128 | 16.5k | return AVIF_RESULT_OK; |
5129 | 16.5k | } |
5130 | | |
5131 | | avifResult avifDecoderSetIOFile(avifDecoder * decoder, const char * filename) |
5132 | 0 | { |
5133 | 0 | avifIO * io = avifIOCreateFileReader(filename); |
5134 | 0 | if (!io) { |
5135 | 0 | return AVIF_RESULT_IO_ERROR; |
5136 | 0 | } |
5137 | 0 | avifDecoderSetIO(decoder, io); |
5138 | 0 | return AVIF_RESULT_OK; |
5139 | 0 | } |
5140 | | |
5141 | | // 0-byte extents are ignored/overwritten during the merge, as they are the signal from helper |
5142 | | // functions that no extent was necessary for this given sample. If both provided extents are |
5143 | | // >0 bytes, this will set dst to be an extent that bounds both supplied extents. |
5144 | | static avifResult avifExtentMerge(avifExtent * dst, const avifExtent * src) |
5145 | 0 | { |
5146 | 0 | if (!dst->size) { |
5147 | 0 | *dst = *src; |
5148 | 0 | return AVIF_RESULT_OK; |
5149 | 0 | } |
5150 | 0 | if (!src->size) { |
5151 | 0 | return AVIF_RESULT_OK; |
5152 | 0 | } |
5153 | | |
5154 | 0 | const uint64_t minExtent1 = dst->offset; |
5155 | 0 | const uint64_t maxExtent1 = dst->offset + dst->size; |
5156 | 0 | const uint64_t minExtent2 = src->offset; |
5157 | 0 | const uint64_t maxExtent2 = src->offset + src->size; |
5158 | 0 | dst->offset = AVIF_MIN(minExtent1, minExtent2); |
5159 | 0 | const uint64_t extentLength = AVIF_MAX(maxExtent1, maxExtent2) - dst->offset; |
5160 | | #if UINT64_MAX > SIZE_MAX |
5161 | | if (extentLength > SIZE_MAX) { |
5162 | | return AVIF_RESULT_BMFF_PARSE_FAILED; |
5163 | | } |
5164 | | #endif |
5165 | 0 | dst->size = (size_t)extentLength; |
5166 | 0 | return AVIF_RESULT_OK; |
5167 | 0 | } |
5168 | | |
5169 | | avifResult avifDecoderNthImageMaxExtent(const avifDecoder * decoder, uint32_t frameIndex, avifExtent * outExtent) |
5170 | 0 | { |
5171 | 0 | if (!decoder->data) { |
5172 | | // Nothing has been parsed yet |
5173 | 0 | return AVIF_RESULT_NO_CONTENT; |
5174 | 0 | } |
5175 | | |
5176 | 0 | memset(outExtent, 0, sizeof(avifExtent)); |
5177 | |
|
5178 | 0 | uint32_t startFrameIndex = avifDecoderNearestKeyframe(decoder, frameIndex); |
5179 | 0 | uint32_t endFrameIndex = frameIndex; |
5180 | 0 | for (uint32_t currentFrameIndex = startFrameIndex; currentFrameIndex <= endFrameIndex; ++currentFrameIndex) { |
5181 | 0 | for (unsigned int tileIndex = 0; tileIndex < decoder->data->tiles.count; ++tileIndex) { |
5182 | 0 | avifTile * tile = &decoder->data->tiles.tile[tileIndex]; |
5183 | 0 | if (currentFrameIndex >= tile->input->samples.count) { |
5184 | 0 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
5185 | 0 | } |
5186 | | |
5187 | 0 | avifDecodeSample * sample = &tile->input->samples.sample[currentFrameIndex]; |
5188 | 0 | avifExtent sampleExtent; |
5189 | 0 | if (sample->itemID) { |
5190 | | // The data comes from an item. Let avifDecoderItemMaxExtent() do the heavy lifting. |
5191 | |
|
5192 | 0 | avifDecoderItem * item; |
5193 | 0 | AVIF_CHECKRES(avifMetaFindOrCreateItem(decoder->data->meta, sample->itemID, &item)); |
5194 | 0 | avifResult maxExtentResult = avifDecoderItemMaxExtent(item, sample, &sampleExtent); |
5195 | 0 | if (maxExtentResult != AVIF_RESULT_OK) { |
5196 | 0 | return maxExtentResult; |
5197 | 0 | } |
5198 | 0 | } else { |
5199 | | // The data likely comes from a sample table. Use the sample position directly. |
5200 | |
|
5201 | 0 | sampleExtent.offset = sample->offset; |
5202 | 0 | sampleExtent.size = sample->size; |
5203 | 0 | } |
5204 | | |
5205 | 0 | if (sampleExtent.size > UINT64_MAX - sampleExtent.offset) { |
5206 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
5207 | 0 | } |
5208 | | |
5209 | 0 | avifResult extentMergeResult = avifExtentMerge(outExtent, &sampleExtent); |
5210 | 0 | if (extentMergeResult != AVIF_RESULT_OK) { |
5211 | 0 | return extentMergeResult; |
5212 | 0 | } |
5213 | 0 | } |
5214 | 0 | } |
5215 | 0 | return AVIF_RESULT_OK; |
5216 | 0 | } |
5217 | | |
5218 | | static avifResult avifDecoderPrepareSample(avifDecoder * decoder, avifDecodeSample * sample, size_t partialByteCount) |
5219 | 34.5k | { |
5220 | 34.5k | if (!sample->data.size || sample->partialData) { |
5221 | | // This sample hasn't been read from IO or had its extents fully merged yet. |
5222 | | |
5223 | 33.0k | size_t bytesToRead = sample->size; |
5224 | 33.0k | if (partialByteCount && (bytesToRead > partialByteCount)) { |
5225 | 17.8k | bytesToRead = partialByteCount; |
5226 | 17.8k | } |
5227 | | |
5228 | 33.0k | if (sample->itemID) { |
5229 | | // The data comes from an item. Let avifDecoderItemRead() do the heavy lifting. |
5230 | | |
5231 | 32.4k | avifDecoderItem * item; |
5232 | 32.4k | AVIF_CHECKRES(avifMetaFindOrCreateItem(decoder->data->meta, sample->itemID, &item)); |
5233 | 32.4k | avifROData itemContents; |
5234 | | #if UINT64_MAX > SIZE_MAX |
5235 | | if (sample->offset > SIZE_MAX) { |
5236 | | return AVIF_RESULT_BMFF_PARSE_FAILED; |
5237 | | } |
5238 | | #endif |
5239 | 32.4k | size_t offset = (size_t)sample->offset; |
5240 | 32.4k | avifResult readResult = avifDecoderItemRead(item, decoder->io, &itemContents, offset, bytesToRead, &decoder->diag); |
5241 | 32.4k | if (readResult != AVIF_RESULT_OK) { |
5242 | 328 | return readResult; |
5243 | 328 | } |
5244 | | |
5245 | | // avifDecoderItemRead is guaranteed to already be persisted by either the underlying IO |
5246 | | // or by mergedExtents; just reuse the buffer here. |
5247 | 32.1k | sample->data = itemContents; |
5248 | 32.1k | sample->ownsData = AVIF_FALSE; |
5249 | 32.1k | sample->partialData = item->partialMergedExtents; |
5250 | 32.1k | } else { |
5251 | | // The data likely comes from a sample table. Pull the sample and make a copy if necessary. |
5252 | | |
5253 | 575 | avifROData sampleContents; |
5254 | 575 | if ((decoder->io->sizeHint > 0) && (sample->offset > decoder->io->sizeHint)) { |
5255 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
5256 | 0 | } |
5257 | 575 | avifResult readResult = decoder->io->read(decoder->io, 0, sample->offset, bytesToRead, &sampleContents); |
5258 | 575 | if (readResult != AVIF_RESULT_OK) { |
5259 | 0 | return readResult; |
5260 | 0 | } |
5261 | 575 | if (sampleContents.size != bytesToRead) { |
5262 | 0 | return AVIF_RESULT_TRUNCATED_DATA; |
5263 | 0 | } |
5264 | | |
5265 | 575 | sample->ownsData = !decoder->io->persistent; |
5266 | 575 | sample->partialData = (bytesToRead != sample->size); |
5267 | 575 | if (decoder->io->persistent) { |
5268 | 575 | sample->data = sampleContents; |
5269 | 575 | } else { |
5270 | 0 | AVIF_CHECKRES(avifRWDataSet((avifRWData *)&sample->data, sampleContents.data, sampleContents.size)); |
5271 | 0 | } |
5272 | 575 | } |
5273 | 33.0k | } |
5274 | 34.1k | return AVIF_RESULT_OK; |
5275 | 34.5k | } |
5276 | | |
5277 | | // Returns AVIF_TRUE if the item should be skipped. Items should be skipped for one of the following reasons: |
5278 | | // * Size is 0. |
5279 | | // * Has an essential property that isn't supported by libavif. |
5280 | | // * Item is not a single image or a grid. |
5281 | | // * Item is a thumbnail. |
5282 | | static avifBool avifDecoderItemShouldBeSkipped(const avifDecoderItem * item) |
5283 | 56.7k | { |
5284 | 56.7k | return !item->size || item->hasUnsupportedEssentialProperty || |
5285 | 54.4k | (avifGetCodecType(item->type) == AVIF_CODEC_TYPE_UNKNOWN && memcmp(item->type, "grid", 4)) || item->thumbnailForID != 0; |
5286 | 56.7k | } |
5287 | | |
5288 | | avifResult avifDecoderParse(avifDecoder * decoder) |
5289 | 16.5k | { |
5290 | 16.5k | avifDiagnosticsClearError(&decoder->diag); |
5291 | | |
5292 | | // An imageSizeLimit greater than AVIF_DEFAULT_IMAGE_SIZE_LIMIT and the special value of 0 to |
5293 | | // disable the limit are not yet implemented. |
5294 | 16.5k | if ((decoder->imageSizeLimit > AVIF_DEFAULT_IMAGE_SIZE_LIMIT) || (decoder->imageSizeLimit == 0)) { |
5295 | 0 | return AVIF_RESULT_NOT_IMPLEMENTED; |
5296 | 0 | } |
5297 | | // Color only or alpha only is not currently supported. |
5298 | 16.5k | if ((decoder->imageContentToDecode & AVIF_IMAGE_CONTENT_COLOR_AND_ALPHA) != 0 && |
5299 | 16.5k | (decoder->imageContentToDecode & AVIF_IMAGE_CONTENT_COLOR_AND_ALPHA) != AVIF_IMAGE_CONTENT_COLOR_AND_ALPHA) { |
5300 | 0 | avifDiagnosticsPrintf(&decoder->diag, "imageContentToDecode set to only color or only alpha is not supported"); |
5301 | 0 | return AVIF_RESULT_NOT_IMPLEMENTED; |
5302 | 0 | } |
5303 | 16.5k | if (!decoder->io || !decoder->io->read) { |
5304 | 0 | return AVIF_RESULT_IO_NOT_SET; |
5305 | 0 | } |
5306 | | |
5307 | | // Cleanup anything lingering in the decoder |
5308 | 16.5k | avifDecoderCleanup(decoder); |
5309 | | |
5310 | | // ----------------------------------------------------------------------- |
5311 | | // Parse BMFF boxes |
5312 | | |
5313 | 16.5k | decoder->data = avifDecoderDataCreate(); |
5314 | 16.5k | AVIF_CHECKERR(decoder->data != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
5315 | 16.5k | decoder->data->diag = &decoder->diag; |
5316 | | |
5317 | 16.5k | AVIF_CHECKRES(avifParse(decoder)); |
5318 | | |
5319 | | // Walk the decoded items (if any) and harvest ispe |
5320 | 14.8k | avifDecoderData * data = decoder->data; |
5321 | 36.3k | for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) { |
5322 | 21.6k | avifDecoderItem * item = data->meta->items.item[itemIndex]; |
5323 | 21.6k | if (avifDecoderItemShouldBeSkipped(item)) { |
5324 | 3.47k | continue; |
5325 | 3.47k | } |
5326 | | |
5327 | 18.1k | const avifProperty * ispeProp = avifPropertyArrayFind(&item->properties, "ispe"); |
5328 | 18.1k | if (ispeProp) { |
5329 | 18.0k | item->width = ispeProp->u.ispe.width; |
5330 | 18.0k | item->height = ispeProp->u.ispe.height; |
5331 | | |
5332 | 18.0k | if ((item->width == 0) || (item->height == 0)) { |
5333 | 2 | avifDiagnosticsPrintf(data->diag, "Item ID [%u] has an invalid size [%ux%u]", item->id, item->width, item->height); |
5334 | 2 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
5335 | 2 | } |
5336 | 18.0k | if (avifDimensionsTooLarge(item->width, item->height, decoder->imageSizeLimit, decoder->imageDimensionLimit)) { |
5337 | 57 | avifDiagnosticsPrintf(data->diag, "Item ID [%u] dimensions are too large [%ux%u]", item->id, item->width, item->height); |
5338 | 57 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
5339 | 57 | } |
5340 | 18.0k | } else { |
5341 | 133 | const avifProperty * auxCProp = avifPropertyArrayFind(&item->properties, "auxC"); |
5342 | 133 | if (auxCProp && isAlphaURN(auxCProp->u.auxC.auxType)) { |
5343 | 58 | if (decoder->strictFlags & AVIF_STRICT_ALPHA_ISPE_REQUIRED) { |
5344 | 0 | avifDiagnosticsPrintf(data->diag, |
5345 | 0 | "[Strict] Alpha auxiliary image item ID [%u] is missing a mandatory ispe property", |
5346 | 0 | item->id); |
5347 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
5348 | 0 | } |
5349 | 75 | } else { |
5350 | 75 | avifDiagnosticsPrintf(data->diag, "Item ID [%u] is missing a mandatory ispe property", item->id); |
5351 | 75 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
5352 | 75 | } |
5353 | 133 | } |
5354 | 18.1k | } |
5355 | 14.6k | return avifDecoderReset(decoder); |
5356 | 14.8k | } |
5357 | | |
5358 | | static avifResult avifCodecCreateInternal(avifCodecChoice choice, const avifTile * tile, avifDiagnostics * diag, avifCodec ** codec) |
5359 | 13.8k | { |
5360 | | #if defined(AVIF_CODEC_AVM) |
5361 | | // AVIF_CODEC_CHOICE_AUTO leads to AVIF_CODEC_TYPE_AV1 by default. Reroute correctly. |
5362 | | if (choice == AVIF_CODEC_CHOICE_AUTO && tile->codecType == AVIF_CODEC_TYPE_AV2) { |
5363 | | choice = AVIF_CODEC_CHOICE_AVM; |
5364 | | } |
5365 | | #endif |
5366 | | |
5367 | 13.8k | const avifCodecType codecTypeFromChoice = avifCodecTypeFromChoice(choice, AVIF_CODEC_FLAG_CAN_DECODE); |
5368 | 13.8k | if (codecTypeFromChoice == AVIF_CODEC_TYPE_UNKNOWN) { |
5369 | 0 | avifDiagnosticsPrintf(diag, |
5370 | 0 | "Tile type is %s but there is no compatible codec available to decode it", |
5371 | 0 | avifGetConfigurationPropertyName(tile->codecType)); |
5372 | 0 | return AVIF_RESULT_NO_CODEC_AVAILABLE; |
5373 | 13.8k | } else if (choice != AVIF_CODEC_CHOICE_AUTO && codecTypeFromChoice != tile->codecType) { |
5374 | 0 | avifDiagnosticsPrintf(diag, |
5375 | 0 | "Tile type is %s but incompatible %s codec was explicitly set as decoding implementation", |
5376 | 0 | avifGetConfigurationPropertyName(tile->codecType), |
5377 | 0 | avifCodecName(choice, AVIF_CODEC_FLAG_CAN_DECODE)); |
5378 | 0 | return AVIF_RESULT_DECODE_COLOR_FAILED; |
5379 | 0 | } |
5380 | | |
5381 | 13.8k | AVIF_CHECKRES(avifCodecCreate(choice, AVIF_CODEC_FLAG_CAN_DECODE, codec)); |
5382 | 13.8k | AVIF_CHECKERR(*codec, AVIF_RESULT_OUT_OF_MEMORY); |
5383 | 13.8k | (*codec)->diag = diag; |
5384 | 13.8k | (*codec)->operatingPoint = tile->operatingPoint; |
5385 | 13.8k | (*codec)->allLayers = tile->input->allLayers; |
5386 | 13.8k | return AVIF_RESULT_OK; |
5387 | 13.8k | } |
5388 | | |
5389 | | static avifBool avifTilesCanBeDecodedWithSameCodecInstance(const avifDecoderData * data) |
5390 | 117 | { |
5391 | 117 | int32_t numImageBuffers = 0, numStolenImageBuffers = 0; |
5392 | 1.05k | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
5393 | 936 | if (data->tileInfos[c].tileCount > 0) { |
5394 | 194 | ++numImageBuffers; |
5395 | 194 | } |
5396 | | // The sample operations require multiple buffers for compositing so no plane is stolen |
5397 | | // when there is a 'sato' Sample Transform derived image item. |
5398 | 936 | if (c >= AVIF_SAMPLE_TRANSFORM_MIN_CATEGORY && c <= AVIF_SAMPLE_TRANSFORM_MAX_CATEGORY && data->tileInfos[c].tileCount > 0) { |
5399 | 0 | continue; |
5400 | 0 | } |
5401 | 936 | if (data->tileInfos[c].tileCount == 1) { |
5402 | 124 | ++numStolenImageBuffers; |
5403 | 124 | } |
5404 | 936 | } |
5405 | 117 | if (numStolenImageBuffers > 0 && numImageBuffers > 1) { |
5406 | | // Single tile image with single tile alpha plane or gain map. In this case each tile needs its own decoder since the planes will be |
5407 | | // "stolen". Stealing either the color or the alpha plane (or gain map) will invalidate the other ones when decode is called the second |
5408 | | // (or third) time. |
5409 | 62 | return AVIF_FALSE; |
5410 | 62 | } |
5411 | 55 | const uint8_t firstTileOperatingPoint = data->tiles.tile[0].operatingPoint; |
5412 | 55 | const avifBool firstTileAllLayers = data->tiles.tile[0].input->allLayers; |
5413 | 1.07k | for (unsigned int i = 1; i < data->tiles.count; ++i) { |
5414 | 1.01k | const avifTile * tile = &data->tiles.tile[i]; |
5415 | 1.01k | if (tile->operatingPoint != firstTileOperatingPoint || tile->input->allLayers != firstTileAllLayers) { |
5416 | 0 | return AVIF_FALSE; |
5417 | 0 | } |
5418 | | // avifDecoderItemValidateProperties() verified during avifDecoderParse() that all tiles |
5419 | | // share the same coding format so no need to check for codecType equality here. |
5420 | 1.01k | } |
5421 | 55 | return AVIF_TRUE; |
5422 | 55 | } |
5423 | | |
5424 | | static avifResult avifDecoderCreateCodecs(avifDecoder * decoder) |
5425 | 13.7k | { |
5426 | 13.7k | avifDecoderData * data = decoder->data; |
5427 | 13.7k | avifDecoderDataResetCodec(data); |
5428 | | |
5429 | 13.7k | if (data->source == AVIF_DECODER_SOURCE_TRACKS) { |
5430 | | // In this case, we will use at most two codec instances (one for the color planes and one for the alpha plane). |
5431 | | // Gain maps are not supported. |
5432 | 244 | AVIF_CHECKRES(avifCodecCreateInternal(decoder->codecChoice, &decoder->data->tiles.tile[0], &decoder->diag, &data->codec)); |
5433 | 244 | data->tiles.tile[0].codec = data->codec; |
5434 | 244 | if (data->tiles.count > 1) { |
5435 | 0 | AVIF_CHECKRES(avifCodecCreateInternal(decoder->codecChoice, &decoder->data->tiles.tile[1], &decoder->diag, &data->codecAlpha)); |
5436 | 0 | data->tiles.tile[1].codec = data->codecAlpha; |
5437 | 0 | } |
5438 | 13.5k | } else { |
5439 | | // In this case, we will use one codec instance when there is only one tile or when all of the following conditions are |
5440 | | // met: |
5441 | | // - The image must have exactly one layer (i.e. decoder->imageCount == 1). |
5442 | | // - All the tiles must have the same operating point (because the codecs take operating point once at initialization |
5443 | | // and do not allow it to be changed later). |
5444 | | // - All the tiles must have the same value for allLayers (because the codecs take allLayers once at initialization |
5445 | | // and do not allow it to be changed later). |
5446 | | // - If the image has a single tile, it must not have a single tile alpha plane (in this case we will steal the planes |
5447 | | // from the decoder, so we cannot use the same decoder for both the color and the alpha planes). |
5448 | | // - All tiles have the same type (AV1 or AV2). |
5449 | | // - No tile buffer access after another tile was decoded (i.e. no Sample Transform compositing because it happens |
5450 | | // after decoding all tiles). |
5451 | | // Otherwise, we will use |tiles.count| decoder instances (one instance for each tile). |
5452 | 13.5k | const avifBool canUseSingleCodecInstance = |
5453 | 13.5k | ((data->tiles.count == 1) || (decoder->imageCount == 1 && avifTilesCanBeDecodedWithSameCodecInstance(data))) && |
5454 | 13.4k | data->sampleTransformNumInputImageItems == 0; |
5455 | 13.5k | if (canUseSingleCodecInstance) { |
5456 | 13.4k | AVIF_CHECKRES(avifCodecCreateInternal(decoder->codecChoice, &decoder->data->tiles.tile[0], &decoder->diag, &data->codec)); |
5457 | 27.9k | for (unsigned int i = 0; i < decoder->data->tiles.count; ++i) { |
5458 | 14.4k | decoder->data->tiles.tile[i].codec = data->codec; |
5459 | 14.4k | } |
5460 | 13.4k | } else { |
5461 | 186 | for (unsigned int i = 0; i < decoder->data->tiles.count; ++i) { |
5462 | 124 | avifTile * tile = &decoder->data->tiles.tile[i]; |
5463 | 124 | AVIF_CHECKRES(avifCodecCreateInternal(decoder->codecChoice, tile, &decoder->diag, &tile->codec)); |
5464 | 124 | } |
5465 | 62 | } |
5466 | 13.5k | } |
5467 | 13.7k | return AVIF_RESULT_OK; |
5468 | 13.7k | } |
5469 | | |
5470 | | // Returns the primary color item if found, or NULL. |
5471 | | static avifDecoderItem * avifMetaFindColorItem(avifMeta * meta) |
5472 | 14.2k | { |
5473 | 14.7k | for (uint32_t itemIndex = 0; itemIndex < meta->items.count; ++itemIndex) { |
5474 | 14.6k | avifDecoderItem * item = meta->items.item[itemIndex]; |
5475 | 14.6k | if (avifDecoderItemShouldBeSkipped(item)) { |
5476 | 338 | continue; |
5477 | 338 | } |
5478 | 14.3k | if (item->id == meta->primaryItemID) { |
5479 | 14.1k | return item; |
5480 | 14.1k | } |
5481 | 14.3k | } |
5482 | 98 | return NULL; |
5483 | 14.2k | } |
5484 | | |
5485 | | // Returns AVIF_TRUE if item is an alpha auxiliary item of the parent color |
5486 | | // item. |
5487 | | static avifBool avifDecoderItemIsAlphaAux(const avifDecoderItem * item, uint32_t colorItemId) |
5488 | 87.9k | { |
5489 | 87.9k | if (item->auxForID != colorItemId) |
5490 | 86.6k | return AVIF_FALSE; |
5491 | 1.27k | const avifProperty * auxCProp = avifPropertyArrayFind(&item->properties, "auxC"); |
5492 | 1.27k | return auxCProp && isAlphaURN(auxCProp->u.auxC.auxType); |
5493 | 87.9k | } |
5494 | | |
5495 | | // Finds the alpha item whose parent item is colorItem and sets it in the alphaItem output parameter. Returns AVIF_RESULT_OK on |
5496 | | // success. Note that *alphaItem can be NULL even if the return value is AVIF_RESULT_OK. If the colorItem is a grid and the alpha |
5497 | | // item is represented as a set of auxl items to each color tile, then a fake item will be created and *isAlphaItemInInput will be |
5498 | | // set to AVIF_FALSE. In this case, the alpha item merely exists to hold the locations of the alpha tile items. The data of this |
5499 | | // item need not be read and the pixi property cannot be validated. Otherwise, *isAlphaItemInInput will be set to AVIF_TRUE when |
5500 | | // *alphaItem is not NULL. |
5501 | | static avifResult avifMetaFindAlphaItem(avifMeta * meta, |
5502 | | const avifDecoderItem * colorItem, |
5503 | | const avifTileInfo * colorInfo, |
5504 | | avifDecoderItem ** alphaItem, |
5505 | | avifTileInfo * alphaInfo, |
5506 | | avifBool * isAlphaItemInInput) |
5507 | 14.1k | { |
5508 | 34.4k | for (uint32_t itemIndex = 0; itemIndex < meta->items.count; ++itemIndex) { |
5509 | 20.4k | avifDecoderItem * item = meta->items.item[itemIndex]; |
5510 | 20.4k | if (avifDecoderItemShouldBeSkipped(item)) { |
5511 | 2.91k | continue; |
5512 | 2.91k | } |
5513 | 17.5k | if (avifDecoderItemIsAlphaAux(item, colorItem->id)) { |
5514 | 72 | *alphaItem = item; |
5515 | 72 | *isAlphaItemInInput = AVIF_TRUE; |
5516 | 72 | return AVIF_RESULT_OK; |
5517 | 72 | } |
5518 | 17.5k | } |
5519 | 14.0k | if (memcmp(colorItem->type, "grid", 4)) { |
5520 | 13.8k | *alphaItem = NULL; |
5521 | 13.8k | *isAlphaItemInInput = AVIF_FALSE; |
5522 | 13.8k | return AVIF_RESULT_OK; |
5523 | 13.8k | } |
5524 | | // If color item is a grid, check if there is an alpha channel which is represented as an auxl item to each color tile item. |
5525 | 172 | const uint32_t tileCount = colorInfo->grid.rows * colorInfo->grid.columns; |
5526 | 172 | if (tileCount == 0) { |
5527 | 0 | *alphaItem = NULL; |
5528 | 0 | *isAlphaItemInInput = AVIF_FALSE; |
5529 | 0 | return AVIF_RESULT_OK; |
5530 | 0 | } |
5531 | | // Keep the same 'dimg' order as it defines where each tile is located in the reconstructed image. |
5532 | 172 | uint32_t * dimgIdxToAlphaItemIdx = (uint32_t *)avifAlloc(tileCount * sizeof(uint32_t)); |
5533 | 172 | AVIF_CHECKERR(dimgIdxToAlphaItemIdx != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
5534 | 172 | const uint32_t itemIndexNotSet = UINT32_MAX; |
5535 | 1.93k | for (uint32_t dimgIdx = 0; dimgIdx < tileCount; ++dimgIdx) { |
5536 | 1.75k | dimgIdxToAlphaItemIdx[dimgIdx] = itemIndexNotSet; |
5537 | 1.75k | } |
5538 | 172 | uint32_t alphaItemCount = 0; |
5539 | 2.71k | for (uint32_t i = 0; i < meta->items.count; ++i) { |
5540 | 2.67k | const avifDecoderItem * const item = meta->items.item[i]; |
5541 | 2.67k | if (item->dimgForID == colorItem->id) { |
5542 | 1.23k | avifBool seenAlphaForCurrentItem = AVIF_FALSE; |
5543 | 71.6k | for (uint32_t j = 0; j < meta->items.count; ++j) { |
5544 | 70.4k | avifDecoderItem * auxlItem = meta->items.item[j]; |
5545 | 70.4k | if (avifDecoderItemIsAlphaAux(auxlItem, item->id)) { |
5546 | 1.10k | if (seenAlphaForCurrentItem || auxlItem->dimgForID != 0 || item->dimgIdx >= tileCount || |
5547 | 1.09k | dimgIdxToAlphaItemIdx[item->dimgIdx] != itemIndexNotSet) { |
5548 | | // One of the following invalid cases: |
5549 | | // * Multiple items are claiming to be the alpha auxiliary of the current item. |
5550 | | // * Alpha auxiliary is dimg for another item. |
5551 | | // * There are too many items in the dimg array (also checked later in avifFillDimgIdxToItemIdxArray()). |
5552 | | // * There is a repetition in the dimg array (also checked later in avifFillDimgIdxToItemIdxArray()). |
5553 | 3 | avifFree(dimgIdxToAlphaItemIdx); |
5554 | 3 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
5555 | 3 | } |
5556 | 1.09k | dimgIdxToAlphaItemIdx[item->dimgIdx] = j; |
5557 | 1.09k | ++alphaItemCount; |
5558 | 1.09k | seenAlphaForCurrentItem = AVIF_TRUE; |
5559 | 1.09k | } |
5560 | 70.4k | } |
5561 | 1.22k | if (!seenAlphaForCurrentItem) { |
5562 | | // No alpha auxiliary item was found for the current item. Treat this as an image without alpha. |
5563 | 130 | avifFree(dimgIdxToAlphaItemIdx); |
5564 | 130 | *alphaItem = NULL; |
5565 | 130 | *isAlphaItemInInput = AVIF_FALSE; |
5566 | 130 | return AVIF_RESULT_OK; |
5567 | 130 | } |
5568 | 1.22k | } |
5569 | 2.67k | } |
5570 | 39 | if (alphaItemCount != tileCount) { |
5571 | 0 | avifFree(dimgIdxToAlphaItemIdx); |
5572 | 0 | return AVIF_RESULT_INVALID_IMAGE_GRID; |
5573 | 0 | } |
5574 | | // Find an unused ID. |
5575 | 39 | avifResult result; |
5576 | 39 | if (meta->items.count >= UINT32_MAX - 1) { |
5577 | | // In the improbable case where all IDs are used. |
5578 | 0 | result = AVIF_RESULT_DECODE_ALPHA_FAILED; |
5579 | 39 | } else { |
5580 | 39 | uint32_t newItemID = 0; |
5581 | 39 | avifBool isUsed; |
5582 | 2.25k | do { |
5583 | 2.25k | ++newItemID; |
5584 | 2.25k | isUsed = AVIF_FALSE; |
5585 | 73.2k | for (uint32_t i = 0; i < meta->items.count; ++i) { |
5586 | 73.1k | if (meta->items.item[i]->id == newItemID) { |
5587 | 2.22k | isUsed = AVIF_TRUE; |
5588 | 2.22k | break; |
5589 | 2.22k | } |
5590 | 73.1k | } |
5591 | 2.25k | } while (isUsed && newItemID != 0); |
5592 | 39 | result = avifMetaFindOrCreateItem(meta, newItemID, alphaItem); // Create new empty item. |
5593 | 39 | } |
5594 | 39 | if (result != AVIF_RESULT_OK) { |
5595 | 0 | avifFree(dimgIdxToAlphaItemIdx); |
5596 | 0 | return result; |
5597 | 0 | } |
5598 | 39 | memcpy((*alphaItem)->type, "grid", 4); // Make it a grid and register alpha items as its tiles. |
5599 | 39 | (*alphaItem)->width = colorItem->width; |
5600 | 39 | (*alphaItem)->height = colorItem->height; |
5601 | 1.09k | for (uint32_t dimgIdx = 0; dimgIdx < tileCount; ++dimgIdx) { |
5602 | 1.05k | if (dimgIdxToAlphaItemIdx[dimgIdx] >= meta->items.count) { |
5603 | 0 | avifFree(dimgIdxToAlphaItemIdx); |
5604 | 0 | AVIF_ASSERT_NOT_REACHED_OR_RETURN; |
5605 | 0 | } |
5606 | 1.05k | avifDecoderItem * alphaTileItem = meta->items.item[dimgIdxToAlphaItemIdx[dimgIdx]]; |
5607 | 1.05k | alphaTileItem->dimgForID = (*alphaItem)->id; |
5608 | 1.05k | alphaTileItem->dimgIdx = dimgIdx; |
5609 | 1.05k | } |
5610 | 39 | avifFree(dimgIdxToAlphaItemIdx); |
5611 | 39 | *isAlphaItemInInput = AVIF_FALSE; |
5612 | 39 | alphaInfo->grid = colorInfo->grid; |
5613 | 39 | return AVIF_RESULT_OK; |
5614 | 39 | } |
5615 | | |
5616 | | // If cicpSet is not NULL, the caller must set |*cicpSet| to AVIF_FALSE before |
5617 | | // calling this function. |
5618 | | // On success, this function returns AVIF_RESULT_OK and does the following: |
5619 | | // * If a nclx property was found in |properties|: |
5620 | | // - Set |*colorPrimaries|, |*transferCharacteristics|, |*matrixCoefficients| |
5621 | | // and |*yuvRange|. |
5622 | | // - If cicpSet is not NULL, set |*cicpSet| to AVIF_TRUE. |
5623 | | // This function fails if more than one nclx property is found in |properties|. |
5624 | | // The output parameters may be populated even in case of failure and must be |
5625 | | // ignored. |
5626 | | static avifResult avifReadColorNclxProperty(const avifPropertyArray * properties, |
5627 | | avifColorPrimaries * colorPrimaries, |
5628 | | avifTransferCharacteristics * transferCharacteristics, |
5629 | | avifMatrixCoefficients * matrixCoefficients, |
5630 | | avifRange * yuvRange, |
5631 | | avifBool * cicpSet) |
5632 | 14.0k | { |
5633 | 14.0k | assert(cicpSet == NULL || *cicpSet == AVIF_FALSE); |
5634 | 14.0k | avifBool colrNCLXSeen = AVIF_FALSE; |
5635 | 65.7k | for (uint32_t propertyIndex = 0; propertyIndex < properties->count; ++propertyIndex) { |
5636 | 51.6k | avifProperty * prop = &properties->prop[propertyIndex]; |
5637 | 51.6k | if (!memcmp(prop->type, "colr", 4) && prop->u.colr.hasNCLX) { |
5638 | 1.22k | if (colrNCLXSeen) { |
5639 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
5640 | 1 | } |
5641 | 1.22k | colrNCLXSeen = AVIF_TRUE; |
5642 | 1.22k | if (cicpSet != NULL) { |
5643 | 1.22k | *cicpSet = AVIF_TRUE; |
5644 | 1.22k | } |
5645 | 1.22k | *colorPrimaries = prop->u.colr.colorPrimaries; |
5646 | 1.22k | *transferCharacteristics = prop->u.colr.transferCharacteristics; |
5647 | 1.22k | *matrixCoefficients = prop->u.colr.matrixCoefficients; |
5648 | 1.22k | *yuvRange = prop->u.colr.range; |
5649 | 1.22k | } |
5650 | 51.6k | } |
5651 | 14.0k | return AVIF_RESULT_OK; |
5652 | 14.0k | } |
5653 | | |
5654 | | // On success, this function returns AVIF_RESULT_OK and does the following: |
5655 | | // * If a colr property was found in |properties|: |
5656 | | // - Read the icc data into |icc| from |io|. |
5657 | | // - Sets the CICP values as documented in avifReadColorNclxProperty(). |
5658 | | // This function fails if more than one icc or nclx property is found in |
5659 | | // |properties|. The output parameters may be populated even in case of failure |
5660 | | // and must be ignored (and the |icc| object may need to be freed). |
5661 | | static avifResult avifReadColorProperties(avifIO * io, |
5662 | | const avifPropertyArray * properties, |
5663 | | avifRWData * icc, |
5664 | | avifColorPrimaries * colorPrimaries, |
5665 | | avifTransferCharacteristics * transferCharacteristics, |
5666 | | avifMatrixCoefficients * matrixCoefficients, |
5667 | | avifRange * yuvRange, |
5668 | | avifBool * cicpSet) |
5669 | 14.0k | { |
5670 | | // Find and adopt all colr boxes "at most one for a given value of colour type" (HEIF 6.5.5.1, from Amendment 3) |
5671 | | // Accept one of each type, and bail out if more than one of a given type is provided. |
5672 | 14.0k | avifBool colrICCSeen = AVIF_FALSE; |
5673 | 65.7k | for (uint32_t propertyIndex = 0; propertyIndex < properties->count; ++propertyIndex) { |
5674 | 51.6k | avifProperty * prop = &properties->prop[propertyIndex]; |
5675 | 51.6k | if (!memcmp(prop->type, "colr", 4) && prop->u.colr.hasICC) { |
5676 | 1.14k | if (colrICCSeen) { |
5677 | 1 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
5678 | 1 | } |
5679 | 1.14k | avifROData iccRead; |
5680 | 1.14k | AVIF_CHECKRES(io->read(io, 0, prop->u.colr.iccOffset, prop->u.colr.iccSize, &iccRead)); |
5681 | 1.14k | colrICCSeen = AVIF_TRUE; |
5682 | 1.14k | AVIF_CHECKRES(avifRWDataSet(icc, iccRead.data, iccRead.size)); |
5683 | 1.14k | } |
5684 | 51.6k | } |
5685 | 14.0k | return avifReadColorNclxProperty(properties, colorPrimaries, transferCharacteristics, matrixCoefficients, yuvRange, cicpSet); |
5686 | 14.0k | } |
5687 | | |
5688 | | // Finds a 'tmap' (tone mapped image item) box associated with the given 'colorItem'. |
5689 | | // If found, fills 'toneMappedImageItem' and sets 'gainMapItemID' to the id of the gain map |
5690 | | // item associated with the box. Otherwise, sets 'toneMappedImageItem' to NULL. |
5691 | | // Returns AVIF_RESULT_OK if no errors were encountered (whether or not a tmap box was found). |
5692 | | // Assumes that there is a single tmap item, and not, e.g., a grid of tmap items. |
5693 | | // TODO(maryla): add support for files with multiple tmap items if it gets allowed by the spec. |
5694 | | static avifResult avifDecoderDataFindToneMappedImageItem(const avifDecoderData * data, |
5695 | | const avifDecoderItem * colorItem, |
5696 | | avifDecoderItem ** toneMappedImageItem, |
5697 | | uint32_t * gainMapItemID) |
5698 | 13 | { |
5699 | 81 | for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) { |
5700 | 72 | avifDecoderItem * item = data->meta->items.item[itemIndex]; |
5701 | 72 | if (!item->size || item->hasUnsupportedEssentialProperty || item->thumbnailForID != 0) { |
5702 | 43 | continue; |
5703 | 43 | } |
5704 | 29 | if (!memcmp(item->type, "tmap", 4)) { |
5705 | | // The tmap box should be associated (via 'iref'->'dimg') to two items: |
5706 | | // the first one is the base image, the second one is the gain map. |
5707 | 4 | uint32_t dimgItemIDs[2] = { 0, 0 }; |
5708 | 4 | uint32_t numDimgItemIDs = 0; |
5709 | 25 | for (uint32_t otherItemIndex = 0; otherItemIndex < data->meta->items.count; ++otherItemIndex) { |
5710 | 21 | avifDecoderItem * otherItem = data->meta->items.item[otherItemIndex]; |
5711 | 21 | if (otherItem->dimgForID != item->id) { |
5712 | 21 | continue; |
5713 | 21 | } |
5714 | 0 | if (otherItem->dimgIdx < 2) { |
5715 | 0 | AVIF_ASSERT_OR_RETURN(dimgItemIDs[otherItem->dimgIdx] == 0); |
5716 | 0 | dimgItemIDs[otherItem->dimgIdx] = otherItem->id; |
5717 | 0 | } |
5718 | 0 | numDimgItemIDs++; |
5719 | 0 | } |
5720 | | // Even with numDimgItemIDs == 2, one of the ids could be 0 if there are duplicate entries in the 'dimg' box. |
5721 | 4 | if (numDimgItemIDs != 2 || dimgItemIDs[0] == 0 || dimgItemIDs[1] == 0) { |
5722 | 4 | avifDiagnosticsPrintf(data->diag, "box[dimg] for 'tmap' item %d must have exactly 2 entries with distinct ids", item->id); |
5723 | 4 | return AVIF_RESULT_INVALID_TONE_MAPPED_IMAGE; |
5724 | 4 | } |
5725 | 0 | if (dimgItemIDs[0] != colorItem->id) { |
5726 | 0 | continue; |
5727 | 0 | } |
5728 | | |
5729 | 0 | *toneMappedImageItem = item; |
5730 | 0 | *gainMapItemID = dimgItemIDs[1]; |
5731 | 0 | return AVIF_RESULT_OK; |
5732 | 0 | } |
5733 | 29 | } |
5734 | 9 | *toneMappedImageItem = NULL; |
5735 | 9 | *gainMapItemID = 0; |
5736 | 9 | return AVIF_RESULT_OK; |
5737 | 13 | } |
5738 | | |
5739 | | // Returns AVIF_TRUE if the two entity ids (usually item ids) are part of an |
5740 | | // 'altr' group (representing entities that are alternatives of each other) |
5741 | | // with 'id1' appearing before 'id2' (meaning that 'id1' should be preferred). |
5742 | | static avifBool avifIsPreferredAlternativeTo(const avifDecoderData * data, uint32_t id1, uint32_t id2) |
5743 | 4 | { |
5744 | 4 | for (uint32_t i = 0; i < data->meta->entityToGroups.count; ++i) { |
5745 | 0 | avifEntityToGroup * group = &data->meta->entityToGroups.groups[i]; |
5746 | 0 | if (memcmp(group->groupingType, "altr", 4) != 0) { |
5747 | 0 | continue; |
5748 | 0 | } |
5749 | 0 | avifBool id1Found = AVIF_FALSE; |
5750 | 0 | for (uint32_t j = 0; j < group->entityIDs.count; ++j) { |
5751 | 0 | if (group->entityIDs.ids[j] == id1) { |
5752 | 0 | id1Found = AVIF_TRUE; |
5753 | 0 | } else if (group->entityIDs.ids[j] == id2) { |
5754 | | // Assume id2 is only present in one altr group, as per ISO/IEC 14496-12:2022 |
5755 | | // Section 8.15.3.1: |
5756 | | // Any entity_id value shall be mapped to only one grouping of type 'altr'. |
5757 | 0 | return id1Found; |
5758 | 0 | } |
5759 | 0 | } |
5760 | 0 | } |
5761 | 4 | return AVIF_FALSE; |
5762 | 4 | } |
5763 | | |
5764 | | // Finds a 'tmap' (tone mapped image item) box associated with the given 'colorItem', |
5765 | | // then finds the associated gain map image. |
5766 | | // If found, fills 'gainMapItem' and 'gainMapCodecType', and allocates and fills in |
5767 | | // decoder->image->gainMap. |
5768 | | // Otherwise, sets 'gainMapItem' to NULL and gainMapCodecType to AVIF_CODEC_TYPE_UNKNOWN. |
5769 | | // Returns AVIF_RESULT_OK if no errors were encountered (whether or not a gain map was found). |
5770 | | // Assumes that there is a single tmap item, and not, e.g., a grid of tmap items. |
5771 | | static avifResult avifDecoderFindGainMapItem(const avifDecoder * decoder, |
5772 | | const avifDecoderItem * colorItem, |
5773 | | avifDecoderItem ** gainMapItem, |
5774 | | avifCodecType * gainMapCodecType) |
5775 | 13 | { |
5776 | 13 | *gainMapItem = NULL; |
5777 | 13 | *gainMapCodecType = AVIF_CODEC_TYPE_UNKNOWN; |
5778 | | |
5779 | 13 | avifDecoderData * data = decoder->data; |
5780 | | |
5781 | | // Find tmap and gain map item ids. |
5782 | 13 | uint32_t gainMapItemID; |
5783 | 13 | avifDecoderItem * toneMappedImageItemTmp; |
5784 | 13 | AVIF_CHECKRES(avifDecoderDataFindToneMappedImageItem(data, colorItem, &toneMappedImageItemTmp, &gainMapItemID)); |
5785 | 9 | if (!toneMappedImageItemTmp || !gainMapItemID) { |
5786 | 9 | return AVIF_RESULT_OK; |
5787 | 9 | } |
5788 | | |
5789 | 0 | if (!avifIsPreferredAlternativeTo(data, toneMappedImageItemTmp->id, colorItem->id)) { |
5790 | 0 | return AVIF_RESULT_OK; |
5791 | 0 | } |
5792 | | |
5793 | | // Parse tmap item data (containing the gain map metadata). |
5794 | 0 | avifROData tmapData; |
5795 | 0 | AVIF_CHECKRES(avifDecoderItemRead(toneMappedImageItemTmp, decoder->io, &tmapData, 0, 0, data->diag)); |
5796 | | // Allocate avifGainMap on the stack instead of using avifGainMapCreate() to simplify error handling. |
5797 | 0 | avifGainMap gainMapTmp; |
5798 | 0 | avifGainMapSetDefaults(&gainMapTmp); |
5799 | 0 | avifResult result = avifParseToneMappedImageBox(&gainMapTmp, tmapData.data, tmapData.size, data->diag); |
5800 | 0 | if (result == AVIF_RESULT_NOT_IMPLEMENTED) { |
5801 | | // Unsupported gain map version. Simply ignore the gain map. |
5802 | 0 | return AVIF_RESULT_OK; |
5803 | 0 | } |
5804 | 0 | AVIF_CHECKRES(result); |
5805 | | |
5806 | 0 | avifDecoderItem * gainMapItemTmp; |
5807 | 0 | AVIF_CHECKRES(avifMetaFindOrCreateItem(data->meta, gainMapItemID, &gainMapItemTmp)); |
5808 | 0 | if (avifDecoderItemShouldBeSkipped(gainMapItemTmp)) { |
5809 | 0 | return AVIF_RESULT_NOT_IMPLEMENTED; |
5810 | 0 | } |
5811 | | |
5812 | 0 | avifCodecType gainMapCodecTypeTmp; |
5813 | 0 | result = avifDecoderItemReadAndParse(decoder, |
5814 | 0 | gainMapItemTmp, |
5815 | 0 | /*isItemInInput=*/AVIF_TRUE, |
5816 | 0 | &data->tileInfos[AVIF_ITEM_GAIN_MAP].grid, |
5817 | 0 | &gainMapCodecTypeTmp); |
5818 | 0 | if (result == AVIF_RESULT_NOT_IMPLEMENTED) { |
5819 | 0 | return AVIF_RESULT_OK; |
5820 | 0 | } |
5821 | 0 | AVIF_CHECKRES(result); |
5822 | | |
5823 | | // This may allocate gainMapTmp.altICC which must be freed in case of error. |
5824 | 0 | result = avifReadColorProperties(decoder->io, |
5825 | 0 | &toneMappedImageItemTmp->properties, |
5826 | 0 | &gainMapTmp.altICC, |
5827 | 0 | &gainMapTmp.altColorPrimaries, |
5828 | 0 | &gainMapTmp.altTransferCharacteristics, |
5829 | 0 | &gainMapTmp.altMatrixCoefficients, |
5830 | 0 | &gainMapTmp.altYUVRange, |
5831 | 0 | /*cicpSet=*/NULL); |
5832 | 0 | if (result != AVIF_RESULT_OK) { |
5833 | 0 | avifRWDataFree(&gainMapTmp.altICC); |
5834 | 0 | return result; |
5835 | 0 | } |
5836 | | |
5837 | 0 | const avifProperty * clliProp = avifPropertyArrayFind(&toneMappedImageItemTmp->properties, "clli"); |
5838 | 0 | if (clliProp) { |
5839 | 0 | gainMapTmp.altCLLI = clliProp->u.clli; |
5840 | 0 | } |
5841 | |
|
5842 | 0 | const avifProperty * pixiProp = avifPropertyArrayFind(&toneMappedImageItemTmp->properties, "pixi"); |
5843 | 0 | if (pixiProp) { |
5844 | 0 | gainMapTmp.altPlaneCount = pixiProp->u.pixi.planeCount; |
5845 | 0 | gainMapTmp.altDepth = pixiProp->u.pixi.planeDepths[0]; |
5846 | 0 | } |
5847 | |
|
5848 | 0 | const avifProperty * ispeProp = avifPropertyArrayFind(&toneMappedImageItemTmp->properties, "ispe"); |
5849 | 0 | if (!ispeProp) { |
5850 | | // HEIF (ISO/IEC 23008-12:2022), Section 6.5.3.1: |
5851 | | // Every image item shall be associated with one property of this type, prior to the association |
5852 | | // of all transformative properties. |
5853 | 0 | avifDiagnosticsPrintf(data->diag, "Box[tmap] missing mandatory ispe property"); |
5854 | 0 | avifRWDataFree(&gainMapTmp.altICC); |
5855 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
5856 | 0 | } |
5857 | 0 | if (ispeProp->u.ispe.width != colorItem->width || ispeProp->u.ispe.height != colorItem->height) { |
5858 | 0 | avifDiagnosticsPrintf(data->diag, "Box[tmap] ispe property width/height does not match base image"); |
5859 | 0 | avifRWDataFree(&gainMapTmp.altICC); |
5860 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
5861 | 0 | } |
5862 | | |
5863 | 0 | if (avifPropertyArrayFind(&toneMappedImageItemTmp->properties, "pasp") || |
5864 | 0 | avifPropertyArrayFind(&toneMappedImageItemTmp->properties, "clap") || |
5865 | 0 | avifPropertyArrayFind(&toneMappedImageItemTmp->properties, "irot") || |
5866 | 0 | avifPropertyArrayFind(&toneMappedImageItemTmp->properties, "imir")) { |
5867 | | // libavif requires the bitstream contain the same pasp, clap, irot, imir |
5868 | | // properties for both the base and gain map image items used as input to |
5869 | | // the tone-mapped derived image item. libavif also requires the tone-mapped |
5870 | | // derived image item itself not be associated with these properties. This is |
5871 | | // enforced at encoding. Other patterns are rejected at decoding. |
5872 | 0 | avifDiagnosticsPrintf(data->diag, |
5873 | 0 | "Box[tmap] 'pasp', 'clap', 'irot' and 'imir' properties must be associated with base and gain map items instead of 'tmap'"); |
5874 | 0 | avifRWDataFree(&gainMapTmp.altICC); |
5875 | 0 | return AVIF_RESULT_INVALID_TONE_MAPPED_IMAGE; |
5876 | 0 | } |
5877 | | |
5878 | 0 | avifColorPrimaries colorPrimaries = AVIF_COLOR_PRIMARIES_UNSPECIFIED; |
5879 | 0 | avifTransferCharacteristics transferCharacteristics = AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED; |
5880 | 0 | avifMatrixCoefficients matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_UNSPECIFIED; |
5881 | 0 | avifRange yuvRange = AVIF_RANGE_FULL; |
5882 | 0 | avifBool cicpSet = AVIF_FALSE; |
5883 | | // Look for a colr nclx box. Other colr box types (e.g. ICC) are not supported. |
5884 | 0 | result = |
5885 | 0 | avifReadColorNclxProperty(&gainMapItemTmp->properties, &colorPrimaries, &transferCharacteristics, &matrixCoefficients, &yuvRange, &cicpSet); |
5886 | 0 | if (result != AVIF_RESULT_OK) { |
5887 | 0 | avifRWDataFree(&gainMapTmp.altICC); |
5888 | 0 | return result; |
5889 | 0 | } |
5890 | | |
5891 | | // -- Everything is valid, do memory allocations and fill in output data. -- |
5892 | | |
5893 | 0 | decoder->image->gainMap = avifGainMapCreate(); |
5894 | 0 | if (!decoder->image->gainMap) { |
5895 | 0 | avifRWDataFree(&gainMapTmp.altICC); |
5896 | 0 | return AVIF_RESULT_OUT_OF_MEMORY; |
5897 | 0 | } |
5898 | | |
5899 | 0 | if (decoder->imageContentToDecode & AVIF_IMAGE_CONTENT_GAIN_MAP) { |
5900 | 0 | avifImage * image = avifImageCreateEmpty(); |
5901 | 0 | if (!image) { |
5902 | 0 | avifRWDataFree(&gainMapTmp.altICC); |
5903 | 0 | return AVIF_RESULT_OUT_OF_MEMORY; |
5904 | 0 | } |
5905 | 0 | if (cicpSet) { |
5906 | 0 | image->colorPrimaries = colorPrimaries; |
5907 | 0 | image->transferCharacteristics = transferCharacteristics; |
5908 | 0 | image->matrixCoefficients = matrixCoefficients; |
5909 | 0 | image->yuvRange = yuvRange; |
5910 | 0 | } |
5911 | 0 | gainMapTmp.image = image; |
5912 | 0 | } |
5913 | | |
5914 | | // Only set the output pointers after everything has been validated. |
5915 | 0 | *decoder->image->gainMap = gainMapTmp; |
5916 | 0 | *gainMapItem = gainMapItemTmp; |
5917 | 0 | *gainMapCodecType = gainMapCodecTypeTmp; |
5918 | 0 | return AVIF_RESULT_OK; |
5919 | 0 | } |
5920 | | |
5921 | | static avifResult avifDecoderCheckAlphaProperties(avifDecoder * decoder, const avifPropertyArray * alphaProperties) |
5922 | 85 | { |
5923 | 85 | const avifImage * image = decoder->image; |
5924 | | // The 'clap', 'irot' and 'imir' transformative properties should be applied to the alpha |
5925 | | // auxiliary image item before considering it a plane of the color image item. |
5926 | | // Alternatively, inequality with the transformative properties attached to the color image item |
5927 | | // should be treated as AVIF_RESULT_NOT_IMPLEMENTED. |
5928 | | // The latter is easier and is the behavior of libavif. |
5929 | | |
5930 | 85 | const avifProperty * clapProp = avifPropertyArrayFind(alphaProperties, "clap"); |
5931 | 85 | const avifProperty * irotProp = avifPropertyArrayFind(alphaProperties, "irot"); |
5932 | 85 | const avifProperty * imirProp = avifPropertyArrayFind(alphaProperties, "imir"); |
5933 | 85 | if (clapProp == NULL && irotProp == NULL && imirProp == NULL) { |
5934 | | // However, libavif up to version 1.3.0 generated images lacking transformative property |
5935 | | // associations with alpha auxiliary image items, so be lenient on their absence for |
5936 | | // backward compatibility with previously generated images. |
5937 | 85 | return AVIF_RESULT_OK; |
5938 | 85 | } |
5939 | | |
5940 | | // HEIF (ISO/IEC 23008-12), Section 6.9.1: |
5941 | | // When the width or the height of the alpha plane differs from the width or the height of the |
5942 | | // master image, respectively, the alpha plane is resized to have the same width and height as |
5943 | | // those of the master image. |
5944 | | // There is no need to enforce specific 'ispe' values describing the alpha item because |
5945 | | // the alpha item must be resized to the dimensions of the associated color item. |
5946 | | |
5947 | 0 | if (!clapProp != !(image->transformFlags & AVIF_TRANSFORM_CLAP) || |
5948 | 0 | (clapProp && (clapProp->u.clap.widthN != image->clap.widthN || clapProp->u.clap.widthD != image->clap.widthD || |
5949 | 0 | clapProp->u.clap.heightN != image->clap.heightN || clapProp->u.clap.heightD != image->clap.heightD || |
5950 | 0 | clapProp->u.clap.horizOffN != image->clap.horizOffN || clapProp->u.clap.horizOffD != image->clap.horizOffD || |
5951 | 0 | clapProp->u.clap.vertOffN != image->clap.vertOffN || clapProp->u.clap.vertOffD != image->clap.vertOffD))) { |
5952 | 0 | avifDiagnosticsPrintf(&decoder->diag, "Clean aperture property mismatch between alpha auxiliary image item and color item"); |
5953 | 0 | return AVIF_RESULT_NOT_IMPLEMENTED; |
5954 | 0 | } |
5955 | 0 | if (!irotProp != !(image->transformFlags & AVIF_TRANSFORM_IROT) || (irotProp && irotProp->u.irot.angle != image->irot.angle)) { |
5956 | 0 | avifDiagnosticsPrintf(&decoder->diag, "Rotation property mismatch between alpha auxiliary image item and color item"); |
5957 | 0 | return AVIF_RESULT_NOT_IMPLEMENTED; |
5958 | 0 | } |
5959 | 0 | if (!imirProp != !(image->transformFlags & AVIF_TRANSFORM_IMIR) || (imirProp && imirProp->u.imir.axis != image->imir.axis)) { |
5960 | 0 | avifDiagnosticsPrintf(&decoder->diag, "Mirroring property mismatch between alpha auxiliary image item and color item"); |
5961 | 0 | return AVIF_RESULT_NOT_IMPLEMENTED; |
5962 | 0 | } |
5963 | 0 | return AVIF_RESULT_OK; |
5964 | 0 | } |
5965 | | |
5966 | | static avifResult avifDecoderCheckGainMapProperties(avifDecoder * decoder, const avifPropertyArray * gainMapProperties) |
5967 | 0 | { |
5968 | 0 | const avifImage * image = decoder->image; |
5969 | | // libavif requires the bitstream contain the same 'pasp', 'clap', 'irot', 'imir' |
5970 | | // properties for both the base and gain map image items used as input to |
5971 | | // the tone-mapped derived image item. libavif also requires the tone-mapped |
5972 | | // derived image item itself not be associated with these properties. This is |
5973 | | // enforced at encoding. Other patterns are rejected at decoding. |
5974 | 0 | const avifProperty * paspProp = avifPropertyArrayFind(gainMapProperties, "pasp"); |
5975 | 0 | if (!paspProp != !(image->transformFlags & AVIF_TRANSFORM_PASP) || |
5976 | 0 | (paspProp && (paspProp->u.pasp.hSpacing != image->pasp.hSpacing || paspProp->u.pasp.vSpacing != image->pasp.vSpacing))) { |
5977 | 0 | avifDiagnosticsPrintf(&decoder->diag, |
5978 | 0 | "Pixel aspect ratio property mismatch between input items of tone-mapping derived image item"); |
5979 | 0 | return AVIF_RESULT_DECODE_GAIN_MAP_FAILED; |
5980 | 0 | } |
5981 | 0 | const avifProperty * clapProp = avifPropertyArrayFind(gainMapProperties, "clap"); |
5982 | 0 | if (!clapProp != !(image->transformFlags & AVIF_TRANSFORM_CLAP) || |
5983 | 0 | (clapProp && (clapProp->u.clap.widthN != image->clap.widthN || clapProp->u.clap.widthD != image->clap.widthD || |
5984 | 0 | clapProp->u.clap.heightN != image->clap.heightN || clapProp->u.clap.heightD != image->clap.heightD || |
5985 | 0 | clapProp->u.clap.horizOffN != image->clap.horizOffN || clapProp->u.clap.horizOffD != image->clap.horizOffD || |
5986 | 0 | clapProp->u.clap.vertOffN != image->clap.vertOffN || clapProp->u.clap.vertOffD != image->clap.vertOffD))) { |
5987 | 0 | avifDiagnosticsPrintf(&decoder->diag, "Clean aperture property mismatch between input items of tone-mapping derived image item"); |
5988 | 0 | return AVIF_RESULT_DECODE_GAIN_MAP_FAILED; |
5989 | 0 | } |
5990 | 0 | const avifProperty * irotProp = avifPropertyArrayFind(gainMapProperties, "irot"); |
5991 | 0 | if (!irotProp != !(image->transformFlags & AVIF_TRANSFORM_IROT) || (irotProp && irotProp->u.irot.angle != image->irot.angle)) { |
5992 | 0 | avifDiagnosticsPrintf(&decoder->diag, "Rotation property mismatch between input items of tone-mapping derived image item"); |
5993 | 0 | return AVIF_RESULT_DECODE_GAIN_MAP_FAILED; |
5994 | 0 | } |
5995 | 0 | const avifProperty * imirProp = avifPropertyArrayFind(gainMapProperties, "imir"); |
5996 | 0 | if (!imirProp != !(image->transformFlags & AVIF_TRANSFORM_IMIR) || (imirProp && imirProp->u.imir.axis != image->imir.axis)) { |
5997 | 0 | avifDiagnosticsPrintf(&decoder->diag, "Mirroring property mismatch between input items of tone-mapping derived image item"); |
5998 | 0 | return AVIF_RESULT_DECODE_GAIN_MAP_FAILED; |
5999 | 0 | } |
6000 | 0 | return AVIF_RESULT_OK; |
6001 | 0 | } |
6002 | | |
6003 | | // Finds any 'sato' Sample Transform derived image item, distinct from the primary image item, |
6004 | | // and in the same 'altr' group as the primary image item. Returns NULL otherwise. |
6005 | | static avifDecoderItem * avifDecoderDataFindSampleTransformImageItem(avifDecoderData * data) |
6006 | 14.1k | { |
6007 | 34.4k | for (uint32_t itemIndex = 0; itemIndex < data->meta->items.count; ++itemIndex) { |
6008 | 20.3k | avifDecoderItem * item = data->meta->items.item[itemIndex]; |
6009 | 20.3k | if (!memcmp(item->type, "sato", 4) && item->id != data->meta->primaryItemID && item->size != 0 && |
6010 | 6 | !item->hasUnsupportedEssentialProperty && item->thumbnailForID == 0 && |
6011 | 4 | avifIsPreferredAlternativeTo(data, item->id, data->meta->primaryItemID)) { |
6012 | 0 | return item; |
6013 | 0 | } |
6014 | 20.3k | } |
6015 | 14.1k | return NULL; |
6016 | 14.1k | } |
6017 | | |
6018 | | static avifResult avifDecoderGenerateImageTiles(avifDecoder * decoder, avifTileInfo * info, avifDecoderItem * item, avifItemCategory itemCategory) |
6019 | 14.1k | { |
6020 | 14.1k | const uint32_t previousTileCount = decoder->data->tiles.count; |
6021 | 14.1k | if ((info->grid.rows > 0) && (info->grid.columns > 0)) { |
6022 | | // The number of tiles was verified in avifDecoderItemReadAndParse(). |
6023 | 119 | const uint32_t numTiles = info->grid.rows * info->grid.columns; |
6024 | 119 | uint32_t * dimgIdxToItemIdx = (uint32_t *)avifAlloc(numTiles * sizeof(uint32_t)); |
6025 | 119 | AVIF_CHECKERR(dimgIdxToItemIdx != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
6026 | 119 | avifResult result = avifFillDimgIdxToItemIdxArray(dimgIdxToItemIdx, numTiles, item); |
6027 | 119 | if (result == AVIF_RESULT_OK) { |
6028 | 119 | result = avifDecoderGenerateImageGridTiles(decoder, item, itemCategory, dimgIdxToItemIdx, numTiles); |
6029 | 119 | } |
6030 | 119 | avifFree(dimgIdxToItemIdx); |
6031 | 119 | AVIF_CHECKRES(result); |
6032 | 13.9k | } else { |
6033 | 13.9k | AVIF_CHECKERR(item->size != 0, AVIF_RESULT_MISSING_IMAGE_ITEM); |
6034 | | |
6035 | 13.9k | const avifCodecType codecType = avifGetCodecType(item->type); |
6036 | 13.9k | AVIF_ASSERT_OR_RETURN(codecType != AVIF_CODEC_TYPE_UNKNOWN); |
6037 | 13.9k | avifTile * tile = |
6038 | 13.9k | avifDecoderDataCreateTile(decoder->data, codecType, item->width, item->height, avifDecoderItemOperatingPoint(item)); |
6039 | 13.9k | AVIF_CHECKERR(tile, AVIF_RESULT_OUT_OF_MEMORY); |
6040 | 13.9k | AVIF_CHECKRES(avifCodecDecodeInputFillFromDecoderItem(tile->input, |
6041 | 13.9k | item, |
6042 | 13.9k | decoder->allowProgressive, |
6043 | 13.9k | decoder->imageCountLimit, |
6044 | 13.9k | decoder->io->sizeHint, |
6045 | 13.9k | &decoder->diag)); |
6046 | 13.8k | tile->input->itemCategory = itemCategory; |
6047 | 13.8k | } |
6048 | 13.9k | info->tileCount = decoder->data->tiles.count - previousTileCount; |
6049 | 13.9k | return AVIF_RESULT_OK; |
6050 | 14.1k | } |
6051 | | |
6052 | | // Populates depth, yuvFormat and yuvChromaSamplePosition fields on 'image' based on data from the codec config property (e.g. "av1C"). |
6053 | | static avifResult avifReadCodecConfigProperty(avifImage * image, const avifPropertyArray * properties, avifCodecType codecType) |
6054 | 13.7k | { |
6055 | 13.7k | const avifProperty * configProp = avifPropertyArrayFind(properties, avifGetConfigurationPropertyName(codecType)); |
6056 | 13.7k | if (configProp) { |
6057 | 13.7k | image->depth = avifCodecConfigurationBoxGetDepth(&configProp->u.av1C); |
6058 | 13.7k | if (configProp->u.av1C.monochrome) { |
6059 | 1.78k | image->yuvFormat = AVIF_PIXEL_FORMAT_YUV400; |
6060 | 11.9k | } else { |
6061 | 11.9k | if (configProp->u.av1C.chromaSubsamplingX && configProp->u.av1C.chromaSubsamplingY) { |
6062 | 947 | image->yuvFormat = AVIF_PIXEL_FORMAT_YUV420; |
6063 | 11.0k | } else if (configProp->u.av1C.chromaSubsamplingX) { |
6064 | 524 | image->yuvFormat = AVIF_PIXEL_FORMAT_YUV422; |
6065 | 10.5k | } else { |
6066 | 10.5k | image->yuvFormat = AVIF_PIXEL_FORMAT_YUV444; |
6067 | 10.5k | } |
6068 | 11.9k | } |
6069 | 13.7k | image->yuvChromaSamplePosition = (avifChromaSamplePosition)configProp->u.av1C.chromaSamplePosition; |
6070 | 13.7k | } else { |
6071 | | // A configuration property box is mandatory in all valid AVIF configurations. Bail out. |
6072 | 14 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
6073 | 14 | } |
6074 | 13.7k | return AVIF_RESULT_OK; |
6075 | 13.7k | } |
6076 | | |
6077 | | avifResult avifDecoderReset(avifDecoder * decoder) |
6078 | 14.6k | { |
6079 | 14.6k | avifDiagnosticsClearError(&decoder->diag); |
6080 | | |
6081 | 14.6k | avifDecoderData * data = decoder->data; |
6082 | 14.6k | if (!data) { |
6083 | | // Nothing to reset. |
6084 | 0 | return AVIF_RESULT_OK; |
6085 | 0 | } |
6086 | | |
6087 | 132k | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
6088 | 117k | memset(&data->tileInfos[c].grid, 0, sizeof(data->tileInfos[c].grid)); |
6089 | 117k | } |
6090 | 14.6k | avifDecoderDataClearTiles(data); |
6091 | | |
6092 | | // Prepare / cleanup decoded image state |
6093 | 14.6k | if (decoder->image) { |
6094 | 0 | avifImageDestroy(decoder->image); |
6095 | 0 | } |
6096 | 14.6k | decoder->image = avifImageCreateEmpty(); |
6097 | 14.6k | AVIF_CHECKERR(decoder->image, AVIF_RESULT_OUT_OF_MEMORY); |
6098 | 14.6k | decoder->progressiveState = AVIF_PROGRESSIVE_STATE_UNAVAILABLE; |
6099 | 14.6k | data->cicpSet = AVIF_FALSE; |
6100 | | |
6101 | 14.6k | memset(&decoder->ioStats, 0, sizeof(decoder->ioStats)); |
6102 | | |
6103 | | // Color only or alpha only is not currently supported. |
6104 | 14.6k | if ((decoder->imageContentToDecode & AVIF_IMAGE_CONTENT_COLOR_AND_ALPHA) != 0 && |
6105 | 14.6k | (decoder->imageContentToDecode & AVIF_IMAGE_CONTENT_COLOR_AND_ALPHA) != AVIF_IMAGE_CONTENT_COLOR_AND_ALPHA) { |
6106 | 0 | avifDiagnosticsPrintf(&decoder->diag, "imageContentToDecode set to only color or only alpha is not supported"); |
6107 | 0 | return AVIF_RESULT_NOT_IMPLEMENTED; |
6108 | 0 | } |
6109 | | |
6110 | | // ----------------------------------------------------------------------- |
6111 | | // Build decode input |
6112 | | |
6113 | 14.6k | data->sourceSampleTable = NULL; // Reset |
6114 | 14.6k | if (decoder->requestedSource == AVIF_DECODER_SOURCE_AUTO) { |
6115 | | // Honor the major brand (avif or avis) if present, otherwise prefer avis (tracks) if possible. |
6116 | 14.6k | if (!memcmp(data->majorBrand, "avis", 4)) { |
6117 | 261 | data->source = AVIF_DECODER_SOURCE_TRACKS; |
6118 | 14.4k | } else if (!memcmp(data->majorBrand, "avif", 4)) { |
6119 | 10.2k | data->source = AVIF_DECODER_SOURCE_PRIMARY_ITEM; |
6120 | 10.2k | } else if (data->tracks.count > 0) { |
6121 | 66 | data->source = AVIF_DECODER_SOURCE_TRACKS; |
6122 | 4.07k | } else { |
6123 | 4.07k | data->source = AVIF_DECODER_SOURCE_PRIMARY_ITEM; |
6124 | 4.07k | } |
6125 | 14.6k | } else { |
6126 | 0 | data->source = decoder->requestedSource; |
6127 | 0 | } |
6128 | | |
6129 | 14.6k | avifCodecType colorCodecType = AVIF_CODEC_TYPE_UNKNOWN; |
6130 | 14.6k | const avifPropertyArray * colorProperties = NULL; |
6131 | 14.6k | const avifPropertyArray * alphaProperties = NULL; |
6132 | 14.6k | const avifPropertyArray * gainMapProperties = NULL; |
6133 | 14.6k | if (data->source == AVIF_DECODER_SOURCE_TRACKS) { |
6134 | 327 | avifTrack * colorTrack = NULL; |
6135 | 327 | avifTrack * alphaTrack = NULL; |
6136 | | |
6137 | | // Find primary track - this probably needs some better detection |
6138 | 327 | uint32_t colorTrackIndex = 0; |
6139 | 352 | for (; colorTrackIndex < data->tracks.count; ++colorTrackIndex) { |
6140 | 337 | avifTrack * track = &data->tracks.track[colorTrackIndex]; |
6141 | 337 | if (!track->sampleTable) { |
6142 | 6 | continue; |
6143 | 6 | } |
6144 | 331 | if (!track->id) { // trak box might be missing a tkhd box inside, skip it |
6145 | 4 | continue; |
6146 | 4 | } |
6147 | 327 | if (!track->sampleTable->chunks.count) { |
6148 | 5 | continue; |
6149 | 5 | } |
6150 | 322 | colorCodecType = avifSampleTableGetCodecType(track->sampleTable); |
6151 | 322 | if (colorCodecType == AVIF_CODEC_TYPE_UNKNOWN) { |
6152 | 10 | continue; |
6153 | 10 | } |
6154 | 312 | if (track->auxForID != 0) { |
6155 | 0 | continue; |
6156 | 0 | } |
6157 | | // HEIF (ISO/IEC 23008-12:2022), Section 7.1: |
6158 | | // In order to distinguish image sequences from video, the handler type in the |
6159 | | // HandlerBox of the track is 'pict' to indicate an image sequence track. |
6160 | | // But we do not check the handler type because it may break some existing files. |
6161 | | |
6162 | | // Found one! |
6163 | 312 | break; |
6164 | 312 | } |
6165 | 327 | if (colorTrackIndex == data->tracks.count) { |
6166 | 15 | avifDiagnosticsPrintf(&decoder->diag, "Failed to find AV1 color track"); |
6167 | 15 | return AVIF_RESULT_NO_CONTENT; |
6168 | 15 | } |
6169 | 312 | colorTrack = &data->tracks.track[colorTrackIndex]; |
6170 | | |
6171 | 312 | colorProperties = avifSampleTableGetProperties(colorTrack->sampleTable, colorCodecType); |
6172 | 312 | if (!colorProperties) { |
6173 | 0 | avifDiagnosticsPrintf(&decoder->diag, "Failed to find AV1 color track's color properties"); |
6174 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
6175 | 0 | } |
6176 | | |
6177 | | // Find Exif and/or XMP metadata, if any |
6178 | 312 | if (colorTrack->meta) { |
6179 | | // See the comment above avifDecoderFindMetadata() for the explanation of using 0 here |
6180 | 312 | avifResult findResult = avifDecoderFindMetadata(decoder, colorTrack->meta, decoder->image, 0); |
6181 | 312 | if (findResult != AVIF_RESULT_OK) { |
6182 | 0 | return findResult; |
6183 | 0 | } |
6184 | 312 | } |
6185 | | |
6186 | 312 | uint32_t alphaTrackIndex = 0; |
6187 | 312 | avifCodecType alphaCodecType = AVIF_CODEC_TYPE_UNKNOWN; |
6188 | 649 | for (; alphaTrackIndex < data->tracks.count; ++alphaTrackIndex) { |
6189 | 337 | avifTrack * track = &data->tracks.track[alphaTrackIndex]; |
6190 | 337 | if (!track->sampleTable) { |
6191 | 19 | continue; |
6192 | 19 | } |
6193 | 318 | if (!track->id) { |
6194 | 2 | continue; |
6195 | 2 | } |
6196 | 316 | if (!track->sampleTable->chunks.count) { |
6197 | 1 | continue; |
6198 | 1 | } |
6199 | 315 | alphaCodecType = avifSampleTableGetCodecType(track->sampleTable); |
6200 | 315 | if (alphaCodecType == AVIF_CODEC_TYPE_UNKNOWN) { |
6201 | 2 | continue; |
6202 | 2 | } |
6203 | 313 | const avifPropertyArray * properties = avifSampleTableGetProperties(track->sampleTable, alphaCodecType); |
6204 | 313 | const avifProperty * auxiProp = properties ? avifPropertyArrayFind(properties, "auxi") : NULL; |
6205 | | // If auxi is present, check that it contains the alpha URN. |
6206 | | // If auxi is not present, assume that the track is alpha. This is for backward compatibility with |
6207 | | // old versions of libavif that did not write this property, see |
6208 | | // https://github.com/AOMediaCodec/libavif/commit/98faa17 |
6209 | 313 | if (auxiProp && !isAlphaURN(auxiProp->u.auxC.auxType)) { |
6210 | 1 | continue; |
6211 | 1 | } |
6212 | | // Do not check the track's handlerType. It should be "auxv" according to |
6213 | | // HEIF (ISO/IEC 23008-12:2022), Section 7.5.3.1, but old versions of libavif used to write |
6214 | | // "pict" instead. See https://github.com/AOMediaCodec/libavif/commit/65d0af9 |
6215 | | |
6216 | 312 | if (track->auxForID == colorTrack->id) { |
6217 | | // Found it! |
6218 | 0 | alphaProperties = properties; |
6219 | 0 | break; |
6220 | 0 | } |
6221 | 312 | } |
6222 | 312 | if (alphaTrackIndex != data->tracks.count) { |
6223 | 0 | alphaTrack = &data->tracks.track[alphaTrackIndex]; |
6224 | 0 | } |
6225 | | |
6226 | 312 | const uint8_t operatingPoint = 0; // No way to set operating point via tracks |
6227 | 312 | avifTile * colorTile = avifDecoderDataCreateTile(data, colorCodecType, colorTrack->width, colorTrack->height, operatingPoint); |
6228 | 312 | AVIF_CHECKERR(colorTile != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
6229 | 312 | AVIF_CHECKRES(avifCodecDecodeInputFillFromSampleTable(colorTile->input, |
6230 | 312 | colorTrack->sampleTable, |
6231 | 312 | decoder->imageCountLimit, |
6232 | 312 | decoder->io->sizeHint, |
6233 | 312 | data->diag)); |
6234 | 259 | data->tileInfos[AVIF_ITEM_COLOR].tileCount = 1; |
6235 | | |
6236 | 259 | if (alphaTrack) { |
6237 | 0 | avifTile * alphaTile = avifDecoderDataCreateTile(data, alphaCodecType, alphaTrack->width, alphaTrack->height, operatingPoint); |
6238 | 0 | AVIF_CHECKERR(alphaTile != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
6239 | 0 | AVIF_CHECKRES(avifCodecDecodeInputFillFromSampleTable(alphaTile->input, |
6240 | 0 | alphaTrack->sampleTable, |
6241 | 0 | decoder->imageCountLimit, |
6242 | 0 | decoder->io->sizeHint, |
6243 | 0 | data->diag)); |
6244 | 0 | alphaTile->input->itemCategory = AVIF_ITEM_ALPHA; |
6245 | 0 | data->tileInfos[AVIF_ITEM_ALPHA].tileCount = 1; |
6246 | 0 | } |
6247 | | |
6248 | | // Stash off sample table for future timing information |
6249 | 259 | data->sourceSampleTable = colorTrack->sampleTable; |
6250 | | |
6251 | | // Image sequence timing |
6252 | 259 | decoder->imageIndex = -1; |
6253 | 259 | decoder->imageCount = (int)colorTile->input->samples.count; |
6254 | 259 | decoder->timescale = colorTrack->mediaTimescale; |
6255 | 259 | decoder->durationInTimescales = colorTrack->mediaDuration; |
6256 | 259 | if (colorTrack->mediaTimescale) { |
6257 | 201 | decoder->duration = (double)decoder->durationInTimescales / (double)colorTrack->mediaTimescale; |
6258 | 201 | } else { |
6259 | 58 | decoder->duration = 0; |
6260 | 58 | } |
6261 | | // If the alphaTrack->repetitionCount and colorTrack->repetitionCount are different, we will simply use the |
6262 | | // colorTrack's repetitionCount. |
6263 | 259 | decoder->repetitionCount = colorTrack->repetitionCount; |
6264 | | |
6265 | 259 | memset(&decoder->imageTiming, 0, sizeof(decoder->imageTiming)); // to be set in avifDecoderNextImage() |
6266 | | |
6267 | 259 | decoder->image->width = colorTrack->width; |
6268 | 259 | decoder->image->height = colorTrack->height; |
6269 | 259 | decoder->alphaPresent = (alphaTrack != NULL); |
6270 | 259 | decoder->image->alphaPremultiplied = decoder->alphaPresent && (colorTrack->premByID == alphaTrack->id); |
6271 | 14.3k | } else { |
6272 | | // Create from items |
6273 | | |
6274 | 14.3k | if (data->meta->primaryItemID == 0) { |
6275 | | // A primary item is required |
6276 | 61 | avifDiagnosticsPrintf(&decoder->diag, "Primary item not specified"); |
6277 | 61 | return AVIF_RESULT_MISSING_IMAGE_ITEM; |
6278 | 61 | } |
6279 | | |
6280 | | // Main item of each group category (top-level item such as grid or single tile), if any. |
6281 | 14.2k | avifDecoderItem * mainItems[AVIF_ITEM_CATEGORY_COUNT]; |
6282 | 14.2k | avifCodecType codecType[AVIF_ITEM_CATEGORY_COUNT]; |
6283 | 128k | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
6284 | 114k | mainItems[c] = NULL; |
6285 | 114k | codecType[c] = AVIF_CODEC_TYPE_UNKNOWN; |
6286 | 114k | } |
6287 | | |
6288 | | // Mandatory primary color item |
6289 | 14.2k | mainItems[AVIF_ITEM_COLOR] = avifMetaFindColorItem(data->meta); |
6290 | 14.2k | if (!mainItems[AVIF_ITEM_COLOR]) { |
6291 | 98 | avifDiagnosticsPrintf(&decoder->diag, "Primary item not found"); |
6292 | 98 | return AVIF_RESULT_MISSING_IMAGE_ITEM; |
6293 | 98 | } |
6294 | 14.1k | AVIF_CHECKRES(avifDecoderItemReadAndParse(decoder, |
6295 | 14.1k | mainItems[AVIF_ITEM_COLOR], |
6296 | 14.1k | /*isItemInInput=*/AVIF_TRUE, |
6297 | 14.1k | &data->tileInfos[AVIF_ITEM_COLOR].grid, |
6298 | 14.1k | &codecType[AVIF_ITEM_COLOR])); |
6299 | 14.1k | colorProperties = &mainItems[AVIF_ITEM_COLOR]->properties; |
6300 | 14.1k | colorCodecType = codecType[AVIF_ITEM_COLOR]; |
6301 | | |
6302 | | // Optional alpha auxiliary item |
6303 | 14.1k | avifBool isAlphaItemInInput; |
6304 | 14.1k | AVIF_CHECKRES(avifMetaFindAlphaItem(data->meta, |
6305 | 14.1k | mainItems[AVIF_ITEM_COLOR], |
6306 | 14.1k | &data->tileInfos[AVIF_ITEM_COLOR], |
6307 | 14.1k | &mainItems[AVIF_ITEM_ALPHA], |
6308 | 14.1k | &data->tileInfos[AVIF_ITEM_ALPHA], |
6309 | 14.1k | &isAlphaItemInInput)); |
6310 | 14.1k | if (mainItems[AVIF_ITEM_ALPHA]) { |
6311 | 111 | AVIF_CHECKRES(avifDecoderItemReadAndParse(decoder, |
6312 | 111 | mainItems[AVIF_ITEM_ALPHA], |
6313 | 111 | isAlphaItemInInput, |
6314 | 111 | &data->tileInfos[AVIF_ITEM_ALPHA].grid, |
6315 | 111 | &codecType[AVIF_ITEM_ALPHA])); |
6316 | 111 | } |
6317 | | |
6318 | | // Section 10.2.6 of 23008-12:2024/AMD 1:2024(E): |
6319 | | // 'tmap' brand |
6320 | | // This brand enables file players to identify and decode HEIF files containing tone-map derived image |
6321 | | // items. When present, this brand shall be among the brands included in the compatible_brands |
6322 | | // array of the FileTypeBox. |
6323 | | // |
6324 | | // If the file contains a 'tmap' item but doesn't have the 'tmap' brand, it is technically invalid. |
6325 | | // However, we don't report any error because in order to do detect this case consistently, we would |
6326 | | // need to remove the early exit in avifParse() to check if a 'tmap' item might be present |
6327 | | // further down the file. Instead, we simply ignore tmap items in files that lack the 'tmap' brand. |
6328 | 14.1k | if (avifBrandArrayHasBrand(&data->compatibleBrands, "tmap")) { |
6329 | 13 | avifDecoderItem * gainMapItem; |
6330 | 13 | avifCodecType gainMapCodecType; |
6331 | 13 | AVIF_CHECKRES(avifDecoderFindGainMapItem(decoder, mainItems[AVIF_ITEM_COLOR], &gainMapItem, &gainMapCodecType)); |
6332 | 9 | if (gainMapItem != NULL && decoder->imageContentToDecode & AVIF_IMAGE_CONTENT_GAIN_MAP) { |
6333 | 0 | mainItems[AVIF_ITEM_GAIN_MAP] = gainMapItem; |
6334 | 0 | codecType[AVIF_ITEM_GAIN_MAP] = gainMapCodecType; |
6335 | 0 | } |
6336 | 9 | } |
6337 | | |
6338 | | // AVIF_ITEM_SAMPLE_TRANSFORM (not used through mainItems because not a coded item (well grids are not coded items either but it's different)). |
6339 | 14.1k | avifDecoderItem * const sampleTransformItem = avifDecoderDataFindSampleTransformImageItem(data); |
6340 | 14.1k | if ((decoder->imageContentToDecode & AVIF_IMAGE_CONTENT_COLOR_AND_ALPHA) && |
6341 | 14.1k | (decoder->imageContentToDecode & AVIF_IMAGE_CONTENT_SAMPLE_TRANSFORMS) && sampleTransformItem != NULL) { |
6342 | 0 | AVIF_ASSERT_OR_RETURN(data->sampleTransformNumInputImageItems == 0); |
6343 | |
|
6344 | 0 | for (uint32_t i = 0; i < data->meta->items.count; ++i) { |
6345 | 0 | avifDecoderItem * inputImageItem = data->meta->items.item[i]; |
6346 | 0 | if (inputImageItem->dimgForID == sampleTransformItem->id) { |
6347 | 0 | ++data->sampleTransformNumInputImageItems; |
6348 | 0 | } |
6349 | 0 | } |
6350 | | // Check max number of input items allowed by the format. |
6351 | 0 | if (data->sampleTransformNumInputImageItems > 32) { |
6352 | 0 | avifDiagnosticsPrintf(data->diag, |
6353 | 0 | "Box[sato] too many input items, format allows up to 32, got %d", |
6354 | 0 | data->sampleTransformNumInputImageItems); |
6355 | 0 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
6356 | 0 | } |
6357 | | // Check max number of input items supported by this implementation. |
6358 | 0 | AVIF_CHECKERR(data->sampleTransformNumInputImageItems <= AVIF_SAMPLE_TRANSFORM_MAX_NUM_INPUT_IMAGE_ITEMS, |
6359 | 0 | AVIF_RESULT_NOT_IMPLEMENTED); |
6360 | | |
6361 | 0 | uint32_t numExtraInputImageItems = 0; |
6362 | 0 | for (uint32_t i = 0; i < data->meta->items.count; ++i) { |
6363 | 0 | avifDecoderItem * inputImageItem = data->meta->items.item[i]; |
6364 | 0 | if (inputImageItem->dimgForID != sampleTransformItem->id) { |
6365 | 0 | continue; |
6366 | 0 | } |
6367 | 0 | if (avifDecoderItemShouldBeSkipped(inputImageItem)) { |
6368 | 0 | avifDiagnosticsPrintf(data->diag, "Box[sato] input item %u is not a supported image type", inputImageItem->id); |
6369 | 0 | return AVIF_RESULT_DECODE_SAMPLE_TRANSFORM_FAILED; |
6370 | 0 | } |
6371 | | |
6372 | 0 | AVIF_ASSERT_OR_RETURN(inputImageItem->dimgIdx < AVIF_SAMPLE_TRANSFORM_MAX_NUM_INPUT_IMAGE_ITEMS); |
6373 | 0 | avifItemCategory * category = &data->sampleTransformInputImageItems[inputImageItem->dimgIdx]; |
6374 | 0 | avifBool foundItem = AVIF_FALSE; |
6375 | 0 | for (int c = AVIF_ITEM_COLOR; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
6376 | 0 | if (mainItems[c] && inputImageItem->id == mainItems[c]->id) { |
6377 | 0 | *category = c; |
6378 | 0 | AVIF_CHECKERR(*category == AVIF_ITEM_COLOR, AVIF_RESULT_NOT_IMPLEMENTED); |
6379 | 0 | foundItem = AVIF_TRUE; |
6380 | 0 | break; |
6381 | 0 | } |
6382 | 0 | } |
6383 | 0 | if (!foundItem) { |
6384 | 0 | AVIF_CHECKERR(numExtraInputImageItems < AVIF_SAMPLE_TRANSFORM_MAX_NUM_EXTRA_INPUT_IMAGE_ITEMS, |
6385 | 0 | AVIF_RESULT_NOT_IMPLEMENTED); |
6386 | 0 | *category = (avifItemCategory)(AVIF_ITEM_SAMPLE_TRANSFORM_INPUT_0_COLOR + numExtraInputImageItems); |
6387 | 0 | const avifItemCategory alphaCategory = |
6388 | 0 | (avifItemCategory)(AVIF_ITEM_SAMPLE_TRANSFORM_INPUT_0_ALPHA + numExtraInputImageItems); |
6389 | 0 | mainItems[*category] = inputImageItem; |
6390 | 0 | ++numExtraInputImageItems; |
6391 | |
|
6392 | 0 | AVIF_CHECKRES(avifDecoderItemReadAndParse(decoder, |
6393 | 0 | inputImageItem, |
6394 | 0 | /*isItemInInput=*/AVIF_TRUE, |
6395 | 0 | &data->tileInfos[*category].grid, |
6396 | 0 | &codecType[*category])); |
6397 | | |
6398 | | // Optional alpha auxiliary item |
6399 | 0 | avifBool isAlphaInputImageItemInInput = AVIF_FALSE; |
6400 | 0 | AVIF_CHECKRES(avifMetaFindAlphaItem(data->meta, |
6401 | 0 | mainItems[*category], |
6402 | 0 | &data->tileInfos[*category], |
6403 | 0 | &mainItems[alphaCategory], |
6404 | 0 | &data->tileInfos[alphaCategory], |
6405 | 0 | &isAlphaInputImageItemInInput)); |
6406 | | |
6407 | 0 | AVIF_CHECKERR(!mainItems[alphaCategory] == !mainItems[AVIF_ITEM_ALPHA], AVIF_RESULT_NOT_IMPLEMENTED); |
6408 | 0 | if (mainItems[alphaCategory] != NULL) { |
6409 | 0 | AVIF_CHECKERR(isAlphaInputImageItemInInput == isAlphaItemInInput, AVIF_RESULT_NOT_IMPLEMENTED); |
6410 | 0 | AVIF_CHECKERR((mainItems[*category]->premByID == mainItems[alphaCategory]->id) == |
6411 | 0 | (mainItems[AVIF_ITEM_COLOR]->premByID == mainItems[AVIF_ITEM_ALPHA]->id), |
6412 | 0 | AVIF_RESULT_NOT_IMPLEMENTED); |
6413 | 0 | AVIF_CHECKRES(avifDecoderItemReadAndParse(decoder, |
6414 | 0 | mainItems[alphaCategory], |
6415 | 0 | isAlphaInputImageItemInInput, |
6416 | 0 | &data->tileInfos[alphaCategory].grid, |
6417 | 0 | &codecType[alphaCategory])); |
6418 | 0 | } |
6419 | 0 | } |
6420 | 0 | } |
6421 | | |
6422 | 0 | AVIF_ASSERT_OR_RETURN(data->meta->sampleTransformExpression.tokens == NULL); |
6423 | 0 | avifROData satoData; |
6424 | 0 | AVIF_CHECKRES(avifDecoderItemRead(sampleTransformItem, decoder->io, &satoData, 0, 0, data->diag)); |
6425 | 0 | AVIF_CHECKRES(avifParseSampleTransformImageBox(satoData.data, |
6426 | 0 | satoData.size, |
6427 | 0 | data->sampleTransformNumInputImageItems, |
6428 | 0 | &data->meta->sampleTransformExpression, |
6429 | 0 | data->diag)); |
6430 | 0 | AVIF_CHECKRES(avifDecoderSampleTransformItemValidateProperties(sampleTransformItem, data->diag)); |
6431 | 0 | const avifProperty * pixiProp = avifPropertyArrayFind(&sampleTransformItem->properties, "pixi"); |
6432 | 0 | AVIF_ASSERT_OR_RETURN(pixiProp != NULL); |
6433 | 0 | data->meta->sampleTransformDepth = pixiProp->u.pixi.planeDepths[0]; |
6434 | 0 | } |
6435 | | |
6436 | | // Find Exif and/or XMP metadata, if any |
6437 | 14.1k | AVIF_CHECKRES(avifDecoderFindMetadata(decoder, data->meta, decoder->image, mainItems[AVIF_ITEM_COLOR]->id)); |
6438 | | |
6439 | | // Set all counts and timing to safe-but-uninteresting values |
6440 | 14.0k | decoder->imageIndex = -1; |
6441 | 14.0k | decoder->imageCount = 1; |
6442 | 14.0k | decoder->imageTiming.timescale = 1; |
6443 | 14.0k | decoder->imageTiming.pts = 0; |
6444 | 14.0k | decoder->imageTiming.ptsInTimescales = 0; |
6445 | 14.0k | decoder->imageTiming.duration = 1; |
6446 | 14.0k | decoder->imageTiming.durationInTimescales = 1; |
6447 | 14.0k | decoder->timescale = 1; |
6448 | 14.0k | decoder->duration = 1; |
6449 | 14.0k | decoder->durationInTimescales = 1; |
6450 | | |
6451 | 124k | for (int c = AVIF_ITEM_COLOR; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
6452 | 110k | if (!mainItems[c]) { |
6453 | 96.6k | continue; |
6454 | 96.6k | } |
6455 | 14.1k | AVIF_ASSERT_OR_RETURN(c != AVIF_ITEM_SAMPLE_TRANSFORM); // See sampleTransformItem. |
6456 | | |
6457 | 14.1k | if (avifIsAlpha((avifItemCategory)c) && !mainItems[c]->width && !mainItems[c]->height) { |
6458 | | // NON-STANDARD: Alpha subimage does not have an ispe property; adopt width/height from color item |
6459 | 42 | AVIF_ASSERT_OR_RETURN(!(decoder->strictFlags & AVIF_STRICT_ALPHA_ISPE_REQUIRED)); |
6460 | 42 | mainItems[c]->width = mainItems[AVIF_ITEM_COLOR]->width; |
6461 | 42 | mainItems[c]->height = mainItems[AVIF_ITEM_COLOR]->height; |
6462 | 42 | } |
6463 | | |
6464 | 14.1k | AVIF_CHECKRES(avifDecoderAdoptGridTileCodecTypeIfNeeded(decoder, mainItems[c], &data->tileInfos[c])); |
6465 | | |
6466 | 14.1k | if (c == AVIF_ITEM_COLOR || c == AVIF_ITEM_ALPHA) { |
6467 | 14.1k | if (!(decoder->imageContentToDecode & AVIF_IMAGE_CONTENT_COLOR_AND_ALPHA)) { |
6468 | 0 | continue; |
6469 | 0 | } |
6470 | 14.1k | } else if (c == AVIF_ITEM_SAMPLE_TRANSFORM_INPUT_0_COLOR || c == AVIF_ITEM_SAMPLE_TRANSFORM_INPUT_1_COLOR || |
6471 | 0 | c == AVIF_ITEM_SAMPLE_TRANSFORM_INPUT_0_ALPHA || c == AVIF_ITEM_SAMPLE_TRANSFORM_INPUT_1_ALPHA) { |
6472 | 0 | AVIF_ASSERT_OR_RETURN((decoder->imageContentToDecode & AVIF_IMAGE_CONTENT_COLOR_AND_ALPHA) && |
6473 | 0 | (decoder->imageContentToDecode & AVIF_IMAGE_CONTENT_SAMPLE_TRANSFORMS)); |
6474 | 0 | } else { |
6475 | 0 | AVIF_ASSERT_OR_RETURN(c == AVIF_ITEM_GAIN_MAP); |
6476 | 0 | if (!(decoder->imageContentToDecode & AVIF_IMAGE_CONTENT_GAIN_MAP)) { |
6477 | 0 | continue; |
6478 | 0 | } |
6479 | 0 | } |
6480 | | |
6481 | 14.1k | AVIF_CHECKRES(avifDecoderGenerateImageTiles(decoder, &data->tileInfos[c], mainItems[c], (avifItemCategory)c)); |
6482 | | |
6483 | 13.9k | avifStrictFlags strictFlags = decoder->strictFlags; |
6484 | 13.9k | if (avifIsAlpha((avifItemCategory)c) && !isAlphaItemInInput) { |
6485 | | // In this case, the made up grid item will not have an associated pixi property. So validate everything else |
6486 | | // but the pixi property. |
6487 | 16 | strictFlags &= ~(avifStrictFlags)AVIF_STRICT_PIXI_REQUIRED; |
6488 | 16 | } |
6489 | 13.9k | AVIF_CHECKRES( |
6490 | 13.9k | avifDecoderItemValidateProperties(mainItems[c], avifGetConfigurationPropertyName(codecType[c]), &decoder->diag, strictFlags)); |
6491 | 13.9k | } |
6492 | | |
6493 | 13.8k | if (mainItems[AVIF_ITEM_COLOR]->progressive) { |
6494 | 7 | decoder->progressiveState = AVIF_PROGRESSIVE_STATE_AVAILABLE; |
6495 | | // data->tileInfos[AVIF_ITEM_COLOR].firstTileIndex is not yet defined but will be set to 0 a few lines below. |
6496 | 7 | const avifTile * colorTile = &data->tiles.tile[0]; |
6497 | 7 | if (colorTile->input->samples.count > 1) { |
6498 | 0 | decoder->progressiveState = AVIF_PROGRESSIVE_STATE_ACTIVE; |
6499 | 0 | decoder->imageCount = (int)colorTile->input->samples.count; |
6500 | 0 | } |
6501 | 7 | } |
6502 | | |
6503 | 13.8k | decoder->image->width = mainItems[AVIF_ITEM_COLOR]->width; |
6504 | 13.8k | decoder->image->height = mainItems[AVIF_ITEM_COLOR]->height; |
6505 | 13.8k | decoder->alphaPresent = (mainItems[AVIF_ITEM_ALPHA] != NULL); |
6506 | 13.8k | decoder->image->alphaPremultiplied = decoder->alphaPresent && |
6507 | 86 | (mainItems[AVIF_ITEM_COLOR]->premByID == mainItems[AVIF_ITEM_ALPHA]->id); |
6508 | | |
6509 | 13.8k | if (mainItems[AVIF_ITEM_ALPHA]) { |
6510 | 86 | alphaProperties = &mainItems[AVIF_ITEM_ALPHA]->properties; |
6511 | 86 | } |
6512 | 13.8k | if (mainItems[AVIF_ITEM_GAIN_MAP]) { |
6513 | 0 | AVIF_ASSERT_OR_RETURN(decoder->image->gainMap && decoder->image->gainMap->image); |
6514 | 0 | decoder->image->gainMap->image->width = mainItems[AVIF_ITEM_GAIN_MAP]->width; |
6515 | 0 | decoder->image->gainMap->image->height = mainItems[AVIF_ITEM_GAIN_MAP]->height; |
6516 | | // Must be called after avifDecoderAdoptGridTileCodecType() which among other things copies the |
6517 | | // codec config property from the first tile of a grid to the grid item (when grids are used). |
6518 | 0 | AVIF_CHECKRES(avifReadCodecConfigProperty(decoder->image->gainMap->image, |
6519 | 0 | &mainItems[AVIF_ITEM_GAIN_MAP]->properties, |
6520 | 0 | codecType[AVIF_ITEM_GAIN_MAP])); |
6521 | 0 | gainMapProperties = &mainItems[AVIF_ITEM_GAIN_MAP]->properties; |
6522 | 0 | } |
6523 | 13.8k | } |
6524 | | |
6525 | 14.0k | uint32_t firstTileIndex = 0; |
6526 | 126k | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
6527 | 112k | data->tileInfos[c].firstTileIndex = firstTileIndex; |
6528 | 112k | firstTileIndex += data->tileInfos[c].tileCount; |
6529 | 112k | } |
6530 | | |
6531 | | // Sanity check tiles |
6532 | 29.2k | for (uint32_t tileIndex = 0; tileIndex < data->tiles.count; ++tileIndex) { |
6533 | 15.1k | avifTile * tile = &data->tiles.tile[tileIndex]; |
6534 | 34.6k | for (uint32_t sampleIndex = 0; sampleIndex < tile->input->samples.count; ++sampleIndex) { |
6535 | 19.4k | avifDecodeSample * sample = &tile->input->samples.sample[sampleIndex]; |
6536 | 19.4k | if (!sample->size) { |
6537 | | // Every sample must have some data |
6538 | 5 | return AVIF_RESULT_BMFF_PARSE_FAILED; |
6539 | 5 | } |
6540 | | |
6541 | 19.4k | if (tile->input->itemCategory == AVIF_ITEM_COLOR) { |
6542 | 18.9k | decoder->ioStats.colorOBUSize += sample->size; |
6543 | 18.9k | } else if (tile->input->itemCategory == AVIF_ITEM_ALPHA) { |
6544 | 520 | decoder->ioStats.alphaOBUSize += sample->size; |
6545 | 520 | } |
6546 | 19.4k | } |
6547 | 15.1k | } |
6548 | | |
6549 | 14.0k | AVIF_CHECKRES(avifReadColorProperties(decoder->io, |
6550 | 14.0k | colorProperties, |
6551 | 14.0k | &decoder->image->icc, |
6552 | 14.0k | &decoder->image->colorPrimaries, |
6553 | 14.0k | &decoder->image->transferCharacteristics, |
6554 | 14.0k | &decoder->image->matrixCoefficients, |
6555 | 14.0k | &decoder->image->yuvRange, |
6556 | 14.0k | &data->cicpSet)); |
6557 | | |
6558 | 14.0k | const avifProperty * clliProp = avifPropertyArrayFind(colorProperties, "clli"); |
6559 | 14.0k | if (clliProp) { |
6560 | 144 | decoder->image->clli = clliProp->u.clli; |
6561 | 144 | } |
6562 | | |
6563 | | // Transformations |
6564 | 14.0k | const avifProperty * paspProp = avifPropertyArrayFind(colorProperties, "pasp"); |
6565 | 14.0k | if (paspProp) { |
6566 | 559 | decoder->image->transformFlags |= AVIF_TRANSFORM_PASP; |
6567 | 559 | decoder->image->pasp = paspProp->u.pasp; |
6568 | 559 | } |
6569 | 14.0k | const avifProperty * clapProp = avifPropertyArrayFind(colorProperties, "clap"); |
6570 | 14.0k | if (clapProp) { |
6571 | 13 | decoder->image->transformFlags |= AVIF_TRANSFORM_CLAP; |
6572 | 13 | decoder->image->clap = clapProp->u.clap; |
6573 | 13 | } |
6574 | 14.0k | const avifProperty * irotProp = avifPropertyArrayFind(colorProperties, "irot"); |
6575 | 14.0k | if (irotProp) { |
6576 | 171 | decoder->image->transformFlags |= AVIF_TRANSFORM_IROT; |
6577 | 171 | decoder->image->irot = irotProp->u.irot; |
6578 | 171 | } |
6579 | 14.0k | const avifProperty * imirProp = avifPropertyArrayFind(colorProperties, "imir"); |
6580 | 14.0k | if (imirProp) { |
6581 | 43 | decoder->image->transformFlags |= AVIF_TRANSFORM_IMIR; |
6582 | 43 | decoder->image->imir = imirProp->u.imir; |
6583 | 43 | } |
6584 | 14.0k | if (alphaProperties) { |
6585 | 85 | AVIF_CHECKRES(avifDecoderCheckAlphaProperties(decoder, alphaProperties)); |
6586 | 85 | } |
6587 | 14.0k | if (gainMapProperties) { |
6588 | 0 | AVIF_CHECKRES(avifDecoderCheckGainMapProperties(decoder, gainMapProperties)); |
6589 | 0 | } |
6590 | | |
6591 | 14.0k | if (!data->cicpSet && (data->tiles.count > 0)) { |
6592 | 12.8k | avifTile * firstTile = &data->tiles.tile[0]; |
6593 | 12.8k | if (firstTile->input->samples.count > 0) { |
6594 | 12.8k | avifDecodeSample * sample = &firstTile->input->samples.sample[0]; |
6595 | | |
6596 | | // Harvest CICP from the AV1's sequence header, which should be very close to the front |
6597 | | // of the first sample. Read in successively larger chunks until we successfully parse the sequence. |
6598 | 12.8k | static const size_t searchSampleChunkIncrement = 64; |
6599 | 12.8k | static const size_t searchSampleSizeMax = 4096; |
6600 | 12.8k | size_t searchSampleSize = 0; |
6601 | 19.5k | do { |
6602 | 19.5k | searchSampleSize += searchSampleChunkIncrement; |
6603 | 19.5k | if (searchSampleSize > sample->size) { |
6604 | 1.60k | searchSampleSize = sample->size; |
6605 | 1.60k | } |
6606 | | |
6607 | 19.5k | avifResult prepareResult = avifDecoderPrepareSample(decoder, sample, searchSampleSize); |
6608 | 19.5k | if (prepareResult != AVIF_RESULT_OK) { |
6609 | 280 | return prepareResult; |
6610 | 280 | } |
6611 | | |
6612 | 19.2k | avifSequenceHeader sequenceHeader; |
6613 | 19.2k | if (avifSequenceHeaderParse(&sequenceHeader, &sample->data, firstTile->codecType)) { |
6614 | 12.0k | data->cicpSet = AVIF_TRUE; |
6615 | 12.0k | decoder->image->colorPrimaries = sequenceHeader.colorPrimaries; |
6616 | 12.0k | decoder->image->transferCharacteristics = sequenceHeader.transferCharacteristics; |
6617 | 12.0k | decoder->image->matrixCoefficients = sequenceHeader.matrixCoefficients; |
6618 | 12.0k | decoder->image->yuvRange = sequenceHeader.range; |
6619 | 12.0k | break; |
6620 | 12.0k | } |
6621 | 19.2k | } while (searchSampleSize != sample->size && searchSampleSize < searchSampleSizeMax); |
6622 | 12.8k | } |
6623 | 12.8k | } |
6624 | | |
6625 | 13.7k | AVIF_CHECKRES(avifReadCodecConfigProperty(decoder->image, colorProperties, colorCodecType)); |
6626 | 13.7k | if (decoder->data->meta->sampleTransformExpression.count > 0) { |
6627 | 0 | AVIF_ASSERT_OR_RETURN(decoder->data->meta->sampleTransformDepth != 0); |
6628 | 0 | decoder->image->depth = decoder->data->meta->sampleTransformDepth; |
6629 | 0 | } |
6630 | | |
6631 | | // Expose as raw bytes all other properties that libavif does not care about. |
6632 | 64.3k | for (size_t i = 0; i < colorProperties->count; ++i) { |
6633 | 50.6k | const avifProperty * property = &colorProperties->prop[i]; |
6634 | 50.6k | if (property->isOpaque) { |
6635 | 15.5k | AVIF_CHECKRES(avifImagePushProperty(decoder->image, |
6636 | 15.5k | property->type, |
6637 | 15.5k | property->u.opaque.usertype, |
6638 | 15.5k | property->u.opaque.boxPayload.data, |
6639 | 15.5k | property->u.opaque.boxPayload.size)); |
6640 | 15.5k | } |
6641 | 50.6k | } |
6642 | | |
6643 | 13.7k | if (gainMapProperties) { |
6644 | 0 | for (size_t i = 0; i < gainMapProperties->count; ++i) { |
6645 | 0 | const avifProperty * property = &gainMapProperties->prop[i]; |
6646 | 0 | if (property->isOpaque) { |
6647 | 0 | AVIF_CHECKRES(avifImagePushProperty(decoder->image->gainMap->image, |
6648 | 0 | property->type, |
6649 | 0 | property->u.opaque.usertype, |
6650 | 0 | property->u.opaque.boxPayload.data, |
6651 | 0 | property->u.opaque.boxPayload.size)); |
6652 | 0 | } |
6653 | 0 | } |
6654 | 0 | } |
6655 | 13.7k | return AVIF_RESULT_OK; |
6656 | 13.7k | } |
6657 | | |
6658 | | static avifResult avifDecoderPrepareTiles(avifDecoder * decoder, uint32_t nextImageIndex, const avifTileInfo * info) |
6659 | 111k | { |
6660 | 126k | for (unsigned int tileIndex = info->decodedTileCount; tileIndex < info->tileCount; ++tileIndex) { |
6661 | 14.9k | avifTile * tile = &decoder->data->tiles.tile[info->firstTileIndex + tileIndex]; |
6662 | | |
6663 | 14.9k | if (nextImageIndex >= tile->input->samples.count) { |
6664 | 0 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
6665 | 0 | } |
6666 | | |
6667 | 14.9k | avifDecodeSample * sample = &tile->input->samples.sample[nextImageIndex]; |
6668 | 14.9k | avifResult prepareResult = avifDecoderPrepareSample(decoder, sample, 0); |
6669 | 14.9k | if (prepareResult != AVIF_RESULT_OK) { |
6670 | 48 | return prepareResult; |
6671 | 48 | } |
6672 | 14.9k | } |
6673 | 111k | return AVIF_RESULT_OK; |
6674 | 111k | } |
6675 | | |
6676 | | static avifResult avifImageLimitedToFullAlpha(avifImage * image) |
6677 | 39 | { |
6678 | 39 | if (image->imageOwnsAlphaPlane) { |
6679 | 0 | return AVIF_RESULT_NOT_IMPLEMENTED; |
6680 | 0 | } |
6681 | | |
6682 | 39 | const uint8_t * alphaPlane = image->alphaPlane; |
6683 | 39 | const uint32_t alphaRowBytes = image->alphaRowBytes; |
6684 | | |
6685 | | // We cannot do the range conversion in place since it will modify the |
6686 | | // codec's internal frame buffers. Allocate memory for the conversion. |
6687 | 39 | image->alphaPlane = NULL; |
6688 | 39 | image->alphaRowBytes = 0; |
6689 | 39 | const avifResult allocationResult = avifImageAllocatePlanes(image, AVIF_PLANES_A); |
6690 | 39 | if (allocationResult != AVIF_RESULT_OK) { |
6691 | 0 | return allocationResult; |
6692 | 0 | } |
6693 | | |
6694 | 39 | if (image->depth > 8) { |
6695 | 563 | for (uint32_t j = 0; j < image->height; ++j) { |
6696 | 545 | const uint8_t * srcRow = &alphaPlane[(size_t)j * alphaRowBytes]; |
6697 | 545 | uint8_t * dstRow = &image->alphaPlane[(size_t)j * image->alphaRowBytes]; |
6698 | 15.5k | for (uint32_t i = 0; i < image->width; ++i) { |
6699 | 15.0k | int srcAlpha = *((const uint16_t *)&srcRow[i * 2]); |
6700 | 15.0k | int dstAlpha = avifLimitedToFullY(image->depth, srcAlpha); |
6701 | 15.0k | *((uint16_t *)&dstRow[i * 2]) = (uint16_t)dstAlpha; |
6702 | 15.0k | } |
6703 | 545 | } |
6704 | 21 | } else { |
6705 | 1.21k | for (uint32_t j = 0; j < image->height; ++j) { |
6706 | 1.19k | const uint8_t * srcRow = &alphaPlane[(size_t)j * alphaRowBytes]; |
6707 | 1.19k | uint8_t * dstRow = &image->alphaPlane[(size_t)j * image->alphaRowBytes]; |
6708 | 90.0k | for (uint32_t i = 0; i < image->width; ++i) { |
6709 | 88.8k | int srcAlpha = srcRow[i]; |
6710 | 88.8k | int dstAlpha = avifLimitedToFullY(image->depth, srcAlpha); |
6711 | 88.8k | dstRow[i] = (uint8_t)dstAlpha; |
6712 | 88.8k | } |
6713 | 1.19k | } |
6714 | 21 | } |
6715 | 39 | return AVIF_RESULT_OK; |
6716 | 39 | } |
6717 | | |
6718 | | static avifResult avifGetErrorForItemCategory(avifItemCategory itemCategory) |
6719 | 11.5k | { |
6720 | 11.5k | if (itemCategory == AVIF_ITEM_GAIN_MAP) { |
6721 | 0 | return AVIF_RESULT_DECODE_GAIN_MAP_FAILED; |
6722 | 0 | } |
6723 | 11.5k | if (itemCategory >= AVIF_SAMPLE_TRANSFORM_MIN_CATEGORY && itemCategory <= AVIF_SAMPLE_TRANSFORM_MAX_CATEGORY) { |
6724 | 0 | return AVIF_RESULT_DECODE_SAMPLE_TRANSFORM_FAILED; |
6725 | 0 | } |
6726 | 11.5k | return avifIsAlpha(itemCategory) ? AVIF_RESULT_DECODE_ALPHA_FAILED : AVIF_RESULT_DECODE_COLOR_FAILED; |
6727 | 11.5k | } |
6728 | | |
6729 | | static avifResult avifDecoderDecodeTiles(avifDecoder * decoder, uint32_t nextImageIndex, avifTileInfo * info) |
6730 | 31.0k | { |
6731 | 31.0k | const unsigned int oldDecodedTileCount = info->decodedTileCount; |
6732 | 33.8k | for (unsigned int tileIndex = oldDecodedTileCount; tileIndex < info->tileCount; ++tileIndex) { |
6733 | 14.3k | avifTile * tile = &decoder->data->tiles.tile[info->firstTileIndex + tileIndex]; |
6734 | | |
6735 | 14.3k | const avifDecodeSample * sample = &tile->input->samples.sample[nextImageIndex]; |
6736 | 14.3k | if (sample->data.size < sample->size) { |
6737 | 0 | AVIF_ASSERT_OR_RETURN(decoder->allowIncremental); |
6738 | | // Data is missing but there is no error yet. Output available pixel rows. |
6739 | 0 | return AVIF_RESULT_OK; |
6740 | 0 | } |
6741 | | |
6742 | 14.3k | avifBool isLimitedRangeAlpha = AVIF_FALSE; |
6743 | 14.3k | tile->codec->maxThreads = decoder->maxThreads; |
6744 | 14.3k | tile->codec->imageSizeLimit = decoder->imageSizeLimit; |
6745 | 14.3k | tile->codec->imageDimensionLimit = decoder->imageDimensionLimit; |
6746 | 14.3k | if (!tile->codec->getNextImage(tile->codec, sample, avifIsAlpha(tile->input->itemCategory), &isLimitedRangeAlpha, tile->image)) { |
6747 | 11.5k | avifDiagnosticsPrintf(&decoder->diag, "tile->codec->getNextImage() failed"); |
6748 | 11.5k | return avifGetErrorForItemCategory(tile->input->itemCategory); |
6749 | 11.5k | } |
6750 | | |
6751 | | // Section 2.3.4 of AV1 Codec ISO Media File Format Binding v1.2.0 says: |
6752 | | // the full_range_flag in the colr box shall match the color_range |
6753 | | // flag in the Sequence Header OBU. |
6754 | | // See https://aomediacodec.github.io/av1-isobmff/v1.2.0.html#av1codecconfigurationbox-semantics. |
6755 | | // If a 'colr' box of colour_type 'nclx' was parsed, a mismatch between |
6756 | | // the 'colr' decoder->image->yuvRange and the AV1 OBU |
6757 | | // tile->image->yuvRange should be treated as an error. |
6758 | | // However codec_svt.c was not encoding the color_range field for |
6759 | | // multiple years, so there probably are files in the wild that will |
6760 | | // fail decoding if this is enforced. Thus this pattern is allowed. |
6761 | | // Section 12.1.5.1 of ISO 14496-12 (ISOBMFF) says: |
6762 | | // If colour information is supplied in both this [colr] box, and also |
6763 | | // in the video bitstream, this box takes precedence, and over-rides |
6764 | | // the information in the bitstream. |
6765 | | // So decoder->image->yuvRange is kept because it was either the 'colr' |
6766 | | // value set when the 'colr' box was parsed, or it was the AV1 OBU value |
6767 | | // extracted from the sequence header OBU of the first tile of the first |
6768 | | // frame (if no 'colr' box of colour_type 'nclx' was found). |
6769 | | |
6770 | | // Alpha plane with limited range is not allowed by the latest revision |
6771 | | // of the specification. However, it was allowed in version 1.0.0 of the |
6772 | | // specification. To allow such files, simply convert the alpha plane to |
6773 | | // full range. |
6774 | 2.86k | if (avifIsAlpha(tile->input->itemCategory) && isLimitedRangeAlpha) { |
6775 | 39 | avifResult result = avifImageLimitedToFullAlpha(tile->image); |
6776 | 39 | if (result != AVIF_RESULT_OK) { |
6777 | 0 | avifDiagnosticsPrintf(&decoder->diag, "avifImageLimitedToFullAlpha failed"); |
6778 | 0 | return result; |
6779 | 0 | } |
6780 | 39 | } |
6781 | | |
6782 | | // Scale the decoded image so that it corresponds to this tile's output dimensions |
6783 | 2.86k | if ((tile->width != tile->image->width) || (tile->height != tile->image->height)) { |
6784 | 1.57k | if (avifImageScaleWithLimit(tile->image, |
6785 | 1.57k | tile->width, |
6786 | 1.57k | tile->height, |
6787 | 1.57k | decoder->imageSizeLimit, |
6788 | 1.57k | decoder->imageDimensionLimit, |
6789 | 1.57k | &decoder->diag) != AVIF_RESULT_OK) { |
6790 | 0 | return avifGetErrorForItemCategory(tile->input->itemCategory); |
6791 | 0 | } |
6792 | 1.57k | } |
6793 | | |
6794 | 2.86k | ++info->decodedTileCount; |
6795 | | |
6796 | 2.86k | const avifBool isGrid = (info->grid.rows > 0) && (info->grid.columns > 0); |
6797 | 2.86k | avifBool stealPlanes = !isGrid; |
6798 | 2.86k | if (decoder->data->meta->sampleTransformExpression.count > 0) { |
6799 | | // Keep everything as a copy for now. |
6800 | 0 | stealPlanes = AVIF_FALSE; |
6801 | 0 | } |
6802 | 2.86k | if (tile->input->itemCategory >= AVIF_SAMPLE_TRANSFORM_MIN_CATEGORY && |
6803 | 0 | tile->input->itemCategory <= AVIF_SAMPLE_TRANSFORM_MAX_CATEGORY) { |
6804 | | // Keep Sample Transform input image item samples in tiles. |
6805 | | // The expression will be applied in avifDecoderNextImage() below instead, once all the tiles are available. |
6806 | 0 | continue; |
6807 | 0 | } |
6808 | | |
6809 | 2.86k | if (!stealPlanes) { |
6810 | 371 | avifImage * dstImage = decoder->image; |
6811 | 371 | if (tile->input->itemCategory == AVIF_ITEM_GAIN_MAP) { |
6812 | 0 | AVIF_ASSERT_OR_RETURN(dstImage->gainMap && dstImage->gainMap->image); |
6813 | 0 | dstImage = dstImage->gainMap->image; |
6814 | 0 | } |
6815 | 371 | if (tileIndex == 0) { |
6816 | 51 | AVIF_CHECKRES(avifDecoderDataAllocateImagePlanes(decoder->data, info, dstImage, &decoder->data->cicpSet)); |
6817 | 51 | } |
6818 | 364 | AVIF_CHECKRES(avifDecoderDataCopyTileToImage(decoder->data, info, dstImage, tile, tileIndex)); |
6819 | 2.49k | } else { |
6820 | 2.49k | AVIF_ASSERT_OR_RETURN(info->tileCount == 1); |
6821 | 2.49k | AVIF_ASSERT_OR_RETURN(tileIndex == 0); |
6822 | 2.49k | avifImage * src = tile->image; |
6823 | | |
6824 | 2.49k | if (tile->input->itemCategory == AVIF_ITEM_GAIN_MAP) { |
6825 | 0 | AVIF_ASSERT_OR_RETURN(decoder->image->gainMap && decoder->image->gainMap->image); |
6826 | 0 | decoder->image->gainMap->image->width = src->width; |
6827 | 0 | decoder->image->gainMap->image->height = src->height; |
6828 | 0 | decoder->image->gainMap->image->depth = src->depth; |
6829 | 2.49k | } else { |
6830 | 2.49k | if ((decoder->image->width != src->width) || (decoder->image->height != src->height) || |
6831 | 2.48k | (decoder->image->depth != src->depth)) { |
6832 | 752 | if (avifIsAlpha(tile->input->itemCategory)) { |
6833 | 3 | avifDiagnosticsPrintf(&decoder->diag, |
6834 | 3 | "The color image item does not match the alpha image item in width, height, or bit depth"); |
6835 | 3 | return AVIF_RESULT_DECODE_ALPHA_FAILED; |
6836 | 3 | } |
6837 | 749 | avifImageFreePlanes(decoder->image, AVIF_PLANES_ALL); |
6838 | | |
6839 | 749 | decoder->image->width = src->width; |
6840 | 749 | decoder->image->height = src->height; |
6841 | 749 | decoder->image->depth = src->depth; |
6842 | 749 | } |
6843 | 2.49k | } |
6844 | | |
6845 | 2.48k | if (avifIsAlpha(tile->input->itemCategory)) { |
6846 | 49 | avifImageStealPlanes(decoder->image, src, AVIF_PLANES_A); |
6847 | 2.43k | } else if (tile->input->itemCategory == AVIF_ITEM_GAIN_MAP) { |
6848 | 0 | AVIF_ASSERT_OR_RETURN(decoder->image->gainMap && decoder->image->gainMap->image); |
6849 | 0 | avifImageStealPlanes(decoder->image->gainMap->image, src, AVIF_PLANES_YUV); |
6850 | 2.43k | } else { // AVIF_ITEM_COLOR |
6851 | 2.43k | avifImageStealPlanes(decoder->image, src, AVIF_PLANES_YUV); |
6852 | 2.43k | } |
6853 | 2.48k | } |
6854 | 2.86k | } |
6855 | 19.5k | return AVIF_RESULT_OK; |
6856 | 31.0k | } |
6857 | | |
6858 | | // Returns AVIF_FALSE if there is currently a partially decoded frame. |
6859 | | static avifBool avifDecoderDataFrameFullyDecoded(const avifDecoderData * data) |
6860 | 16.4k | { |
6861 | 37.8k | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
6862 | 35.1k | if (data->tileInfos[c].decodedTileCount != data->tileInfos[c].tileCount) { |
6863 | 13.7k | return AVIF_FALSE; |
6864 | 13.7k | } |
6865 | 35.1k | } |
6866 | 2.67k | return AVIF_TRUE; |
6867 | 16.4k | } |
6868 | | |
6869 | | // Composites hidden image items and/or the primary image item into the dstImage. |
6870 | | // Tiles are aggregated into temporary buffers (reconstructedInputImages) |
6871 | | // covering the whole dstImage dimensions in case of grids. |
6872 | | // Non-null elements of reconstructedInputImages must be destroyed after calling this function. |
6873 | | static avifResult avifDecoderApplySampleTransformForPlanesImpl(const avifDecoder * decoder, |
6874 | | avifPlanesFlag planes, |
6875 | | avifImage * dstImage, |
6876 | | avifImage * reconstructedInputImages[AVIF_SAMPLE_TRANSFORM_MAX_NUM_INPUT_IMAGE_ITEMS]) |
6877 | 0 | { |
6878 | 0 | AVIF_ASSERT_OR_RETURN(decoder->data->sampleTransformNumInputImageItems != 0); |
6879 | 0 | AVIF_ASSERT_OR_RETURN(decoder->data->sampleTransformNumInputImageItems <= AVIF_SAMPLE_TRANSFORM_MAX_NUM_INPUT_IMAGE_ITEMS); |
6880 | 0 | const avifImage * inputImages[AVIF_SAMPLE_TRANSFORM_MAX_NUM_INPUT_IMAGE_ITEMS]; |
6881 | 0 | for (uint32_t i = 0; i < decoder->data->sampleTransformNumInputImageItems; ++i) { |
6882 | 0 | avifItemCategory category = decoder->data->sampleTransformInputImageItems[i]; |
6883 | 0 | if (category == AVIF_ITEM_COLOR) { |
6884 | | // If the primary image item was a grid, it was already aggregated |
6885 | | // into this single output buffer in avifDecoderDecodeTiles(). |
6886 | 0 | inputImages[i] = decoder->image; |
6887 | 0 | } else { |
6888 | 0 | AVIF_ASSERT_OR_RETURN(category >= AVIF_ITEM_SAMPLE_TRANSFORM_INPUT_0_COLOR && |
6889 | 0 | category < AVIF_ITEM_SAMPLE_TRANSFORM_INPUT_0_COLOR + |
6890 | 0 | AVIF_SAMPLE_TRANSFORM_MAX_NUM_EXTRA_INPUT_IMAGE_ITEMS); |
6891 | 0 | if (planes == AVIF_PLANES_A) { |
6892 | 0 | category += AVIF_SAMPLE_TRANSFORM_MAX_NUM_EXTRA_INPUT_IMAGE_ITEMS; |
6893 | 0 | } |
6894 | 0 | const avifTileInfo * info = &decoder->data->tileInfos[category]; |
6895 | 0 | AVIF_ASSERT_OR_RETURN(info != NULL); |
6896 | 0 | const avifTile * firstTile = &decoder->data->tiles.tile[info->firstTileIndex]; |
6897 | 0 | AVIF_ASSERT_OR_RETURN(firstTile != NULL && firstTile->image != NULL); |
6898 | 0 | if (info->tileCount == 1) { |
6899 | 0 | inputImages[i] = firstTile->image; |
6900 | 0 | } else { |
6901 | | // Combine the tiles into a single buffer used as one of the input images in avifImageApplyExpression(). |
6902 | 0 | reconstructedInputImages[i] = avifImageCreateEmpty(); |
6903 | 0 | AVIF_CHECKERR(reconstructedInputImages[i] != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
6904 | 0 | avifImageCopyNoAlloc(reconstructedInputImages[i], firstTile->image); |
6905 | 0 | reconstructedInputImages[i]->width = decoder->image->width; |
6906 | 0 | reconstructedInputImages[i]->height = decoder->image->height; |
6907 | 0 | avifBool cicpSet = AVIF_TRUE; |
6908 | 0 | AVIF_CHECKRES(avifDecoderDataAllocateImagePlanes(decoder->data, info, reconstructedInputImages[i], &cicpSet)); |
6909 | 0 | for (unsigned int tileIndex = 0; tileIndex < info->tileCount; ++tileIndex) { |
6910 | 0 | const avifTile * tile = firstTile + tileIndex; |
6911 | 0 | AVIF_CHECKRES(avifDecoderDataCopyTileToImage(decoder->data, info, reconstructedInputImages[i], tile, tileIndex)); |
6912 | 0 | } |
6913 | 0 | inputImages[i] = reconstructedInputImages[i]; |
6914 | 0 | } |
6915 | 0 | } |
6916 | 0 | } |
6917 | 0 | AVIF_CHECKRES(avifImageApplyExpression(dstImage, |
6918 | 0 | AVIF_SAMPLE_TRANSFORM_BIT_DEPTH_32, |
6919 | 0 | &decoder->data->meta->sampleTransformExpression, |
6920 | 0 | decoder->data->sampleTransformNumInputImageItems, |
6921 | 0 | inputImages, |
6922 | 0 | planes)); |
6923 | 0 | return AVIF_RESULT_OK; |
6924 | 0 | } |
6925 | | |
6926 | | // Intermediate function used to safely destroy temporary buffers even in case of error. |
6927 | | static avifResult avifDecoderApplySampleTransformForPlanes(const avifDecoder * decoder, avifPlanesFlag planes, avifImage * dstImage) |
6928 | 0 | { |
6929 | 0 | avifImage * toDestroy[AVIF_SAMPLE_TRANSFORM_MAX_NUM_INPUT_IMAGE_ITEMS] = { NULL }; |
6930 | 0 | const avifResult result = avifDecoderApplySampleTransformForPlanesImpl(decoder, planes, dstImage, toDestroy); |
6931 | 0 | for (uint32_t i = 0; i < AVIF_SAMPLE_TRANSFORM_MAX_NUM_INPUT_IMAGE_ITEMS; ++i) { |
6932 | 0 | if (toDestroy[i] != NULL) { |
6933 | 0 | avifImageDestroy(toDestroy[i]); |
6934 | 0 | } |
6935 | 0 | } |
6936 | 0 | return result; |
6937 | 0 | } |
6938 | | |
6939 | | static avifResult avifDecoderApplySampleTransform(const avifDecoder * decoder, avifImage * dstImage) |
6940 | 0 | { |
6941 | 0 | if (dstImage->depth != decoder->data->meta->sampleTransformDepth) { |
6942 | 0 | AVIF_ASSERT_OR_RETURN(dstImage->yuvPlanes[0] != NULL); |
6943 | 0 | AVIF_ASSERT_OR_RETURN(dstImage->imageOwnsYUVPlanes); |
6944 | | |
6945 | | // Use a temporary buffer because dstImage may point to decoder->image, which could be an input image. |
6946 | 0 | avifImage * dstImageWithCorrectDepth = |
6947 | 0 | avifImageCreate(dstImage->width, dstImage->height, decoder->data->meta->sampleTransformDepth, dstImage->yuvFormat); |
6948 | 0 | AVIF_CHECKERR(dstImageWithCorrectDepth != NULL, AVIF_RESULT_OUT_OF_MEMORY); |
6949 | 0 | dstImageWithCorrectDepth->yuvRange = dstImage->yuvRange; |
6950 | 0 | avifResult result = |
6951 | 0 | avifImageAllocatePlanes(dstImageWithCorrectDepth, dstImage->alphaPlane != NULL ? AVIF_PLANES_ALL : AVIF_PLANES_YUV); |
6952 | 0 | if (result == AVIF_RESULT_OK) { |
6953 | 0 | result = avifDecoderApplySampleTransform(decoder, dstImageWithCorrectDepth); |
6954 | 0 | if (result == AVIF_RESULT_OK) { |
6955 | | // Keep the same dstImage object rather than swapping decoder->image, in case the user already accessed it. |
6956 | 0 | avifImageFreePlanes(dstImage, AVIF_PLANES_ALL); |
6957 | 0 | dstImage->depth = dstImageWithCorrectDepth->depth; |
6958 | 0 | avifImageStealPlanes(dstImage, dstImageWithCorrectDepth, AVIF_PLANES_ALL); |
6959 | 0 | } |
6960 | 0 | } |
6961 | 0 | avifImageDestroy(dstImageWithCorrectDepth); |
6962 | 0 | return result; |
6963 | 0 | } |
6964 | | |
6965 | 0 | AVIF_CHECKRES(avifDecoderApplySampleTransformForPlanes(decoder, AVIF_PLANES_YUV, dstImage)); |
6966 | 0 | if (decoder->alphaPresent) { |
6967 | 0 | AVIF_CHECKRES(avifDecoderApplySampleTransformForPlanes(decoder, AVIF_PLANES_A, dstImage)); |
6968 | 0 | } |
6969 | 0 | return AVIF_RESULT_OK; |
6970 | 0 | } |
6971 | | |
6972 | | avifResult avifDecoderNextImage(avifDecoder * decoder) |
6973 | 14.0k | { |
6974 | 14.0k | avifDiagnosticsClearError(&decoder->diag); |
6975 | | |
6976 | 14.0k | if (!decoder->data || decoder->data->tiles.count == 0) { |
6977 | | // Nothing has been parsed yet |
6978 | 0 | return AVIF_RESULT_NO_CONTENT; |
6979 | 0 | } |
6980 | | |
6981 | 14.0k | if (!decoder->io || !decoder->io->read) { |
6982 | 0 | return AVIF_RESULT_IO_NOT_SET; |
6983 | 0 | } |
6984 | | |
6985 | 14.0k | if (avifDecoderDataFrameFullyDecoded(decoder->data)) { |
6986 | | // A frame was decoded during the last avifDecoderNextImage() call. |
6987 | 2.14k | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
6988 | 1.90k | decoder->data->tileInfos[c].decodedTileCount = 0; |
6989 | 1.90k | } |
6990 | 238 | } |
6991 | | |
6992 | 14.0k | AVIF_ASSERT_OR_RETURN(decoder->data->tiles.count == (decoder->data->tileInfos[AVIF_ITEM_CATEGORY_COUNT - 1].firstTileIndex + |
6993 | 14.0k | decoder->data->tileInfos[AVIF_ITEM_CATEGORY_COUNT - 1].tileCount)); |
6994 | | |
6995 | 14.0k | const uint32_t nextImageIndex = (uint32_t)(decoder->imageIndex + 1); |
6996 | | |
6997 | | // Ensure that we have created the codecs before proceeding with the decoding. |
6998 | 14.0k | if (!decoder->data->tiles.tile[0].codec) { |
6999 | 13.7k | AVIF_CHECKRES(avifDecoderCreateCodecs(decoder)); |
7000 | 13.7k | } |
7001 | | |
7002 | | // Acquire all sample data for the current image first, allowing for any read call to bail out |
7003 | | // with AVIF_RESULT_WAITING_ON_IO harmlessly / idempotently, unless decoder->allowIncremental. |
7004 | 14.0k | avifResult prepareTileResult[AVIF_ITEM_CATEGORY_COUNT]; |
7005 | 125k | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
7006 | 111k | prepareTileResult[c] = avifDecoderPrepareTiles(decoder, nextImageIndex, &decoder->data->tileInfos[c]); |
7007 | 111k | if (!decoder->allowIncremental || (prepareTileResult[c] != AVIF_RESULT_WAITING_ON_IO)) { |
7008 | 111k | AVIF_CHECKRES(prepareTileResult[c]); |
7009 | 111k | } |
7010 | 111k | } |
7011 | | |
7012 | | // Decode all available color tiles now, then all available alpha tiles, then all available bit |
7013 | | // depth extension tiles. The order of appearance of the tiles in the bitstream is left to the |
7014 | | // encoder's choice, and decoding as many as possible of each category in parallel is beneficial |
7015 | | // for incremental decoding, as pixel rows need all channels to be decoded before being |
7016 | | // accessible to the user. |
7017 | 33.4k | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
7018 | 31.0k | AVIF_CHECKRES(avifDecoderDecodeTiles(decoder, nextImageIndex, &decoder->data->tileInfos[c])); |
7019 | 31.0k | } |
7020 | | |
7021 | 2.43k | if (!avifDecoderDataFrameFullyDecoded(decoder->data)) { |
7022 | 0 | AVIF_ASSERT_OR_RETURN(decoder->allowIncremental); |
7023 | | // The image is not completely decoded. There should be no error unrelated to missing bytes, |
7024 | | // and at least some missing bytes. |
7025 | 0 | avifResult firstNonOkResult = AVIF_RESULT_OK; |
7026 | 0 | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
7027 | 0 | AVIF_ASSERT_OR_RETURN(prepareTileResult[c] == AVIF_RESULT_OK || prepareTileResult[c] == AVIF_RESULT_WAITING_ON_IO); |
7028 | 0 | if (firstNonOkResult == AVIF_RESULT_OK) { |
7029 | 0 | firstNonOkResult = prepareTileResult[c]; |
7030 | 0 | } |
7031 | 0 | } |
7032 | 0 | AVIF_ASSERT_OR_RETURN(firstNonOkResult != AVIF_RESULT_OK); |
7033 | | // Return the "not enough bytes" status now instead of moving on to the next frame. |
7034 | 0 | return AVIF_RESULT_WAITING_ON_IO; |
7035 | 0 | } |
7036 | 21.9k | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
7037 | 19.5k | AVIF_ASSERT_OR_RETURN(prepareTileResult[c] == AVIF_RESULT_OK); |
7038 | 19.5k | } |
7039 | | |
7040 | | // If decoder->data->tileInfos[AVIF_ITEM_COLOR].tileCount == 0, it means |
7041 | | // decoder->imageContentToDecode & AVIF_IMAGE_CONTENT_COLOR_AND_ALPHA was equal to 0. |
7042 | | // Only apply Sample Transforms if there is a color item to apply it onto. |
7043 | 2.43k | if (decoder->data->tileInfos[AVIF_ITEM_COLOR].tileCount != 0 && decoder->data->meta->sampleTransformExpression.count > 0) { |
7044 | 0 | AVIF_CHECKRES(avifDecoderApplySampleTransform(decoder, decoder->image)); |
7045 | 0 | } |
7046 | | |
7047 | | // Only advance decoder->imageIndex once the image is completely decoded, so that |
7048 | | // avifDecoderNthImage(decoder, decoder->imageIndex + 1) is equivalent to avifDecoderNextImage(decoder) |
7049 | | // if the previous call to avifDecoderNextImage() returned AVIF_RESULT_WAITING_ON_IO. |
7050 | 2.43k | decoder->imageIndex = (int)nextImageIndex; |
7051 | | // The decoded tile counts will be reset to 0 the next time avifDecoderNextImage() is called, |
7052 | | // for avifDecoderDecodedRowCount() to work until then. |
7053 | 2.43k | if (decoder->data->sourceSampleTable) { |
7054 | | // Decoding from a track! Provide timing information. |
7055 | | |
7056 | 290 | avifResult timingResult = avifDecoderNthImageTiming(decoder, decoder->imageIndex, &decoder->imageTiming); |
7057 | 290 | if (timingResult != AVIF_RESULT_OK) { |
7058 | 0 | return timingResult; |
7059 | 0 | } |
7060 | 290 | } |
7061 | 2.43k | return AVIF_RESULT_OK; |
7062 | 2.43k | } |
7063 | | |
7064 | | avifResult avifDecoderNthImageTiming(const avifDecoder * decoder, uint32_t frameIndex, avifImageTiming * outTiming) |
7065 | 290 | { |
7066 | 290 | if (!decoder->data) { |
7067 | | // Nothing has been parsed yet |
7068 | 0 | return AVIF_RESULT_NO_CONTENT; |
7069 | 0 | } |
7070 | | |
7071 | 290 | if ((frameIndex > INT_MAX) || ((int)frameIndex >= decoder->imageCount)) { |
7072 | | // Impossible index |
7073 | 0 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
7074 | 0 | } |
7075 | | |
7076 | 290 | if (!decoder->data->sourceSampleTable) { |
7077 | | // There isn't any real timing associated with this decode, so |
7078 | | // just hand back the defaults chosen in avifDecoderReset(). |
7079 | 0 | *outTiming = decoder->imageTiming; |
7080 | 0 | return AVIF_RESULT_OK; |
7081 | 0 | } |
7082 | | |
7083 | 290 | outTiming->timescale = decoder->timescale; |
7084 | 290 | outTiming->ptsInTimescales = 0; |
7085 | 340 | for (uint32_t imageIndex = 0; imageIndex < frameIndex; ++imageIndex) { |
7086 | 50 | outTiming->ptsInTimescales += avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, imageIndex); |
7087 | 50 | } |
7088 | 290 | outTiming->durationInTimescales = avifSampleTableGetImageDelta(decoder->data->sourceSampleTable, frameIndex); |
7089 | | |
7090 | 290 | if (outTiming->timescale > 0) { |
7091 | 224 | outTiming->pts = (double)outTiming->ptsInTimescales / (double)outTiming->timescale; |
7092 | 224 | outTiming->duration = (double)outTiming->durationInTimescales / (double)outTiming->timescale; |
7093 | 224 | } else { |
7094 | 66 | outTiming->pts = 0.0; |
7095 | 66 | outTiming->duration = 0.0; |
7096 | 66 | } |
7097 | 290 | return AVIF_RESULT_OK; |
7098 | 290 | } |
7099 | | |
7100 | | avifResult avifDecoderNthImage(avifDecoder * decoder, uint32_t frameIndex) |
7101 | 0 | { |
7102 | 0 | avifDiagnosticsClearError(&decoder->diag); |
7103 | |
|
7104 | 0 | if (!decoder->data) { |
7105 | | // Nothing has been parsed yet |
7106 | 0 | return AVIF_RESULT_NO_CONTENT; |
7107 | 0 | } |
7108 | | |
7109 | 0 | if ((frameIndex > INT_MAX) || ((int)frameIndex >= decoder->imageCount)) { |
7110 | | // Impossible index |
7111 | 0 | return AVIF_RESULT_NO_IMAGES_REMAINING; |
7112 | 0 | } |
7113 | | |
7114 | 0 | int requestedIndex = (int)frameIndex; |
7115 | 0 | if (requestedIndex == (decoder->imageIndex + 1)) { |
7116 | | // It's just the next image (already partially decoded or not at all), nothing special here |
7117 | 0 | return avifDecoderNextImage(decoder); |
7118 | 0 | } |
7119 | | |
7120 | 0 | if (requestedIndex == decoder->imageIndex) { |
7121 | 0 | if (avifDecoderDataFrameFullyDecoded(decoder->data)) { |
7122 | | // The current fully decoded image (decoder->imageIndex) is requested, nothing to do |
7123 | 0 | return AVIF_RESULT_OK; |
7124 | 0 | } |
7125 | | // The next image (decoder->imageIndex + 1) is partially decoded but |
7126 | | // the previous image (decoder->imageIndex) is requested. |
7127 | | // Fall through to resetting the decoder data and start decoding from |
7128 | | // the nearest key frame. |
7129 | 0 | } |
7130 | | |
7131 | 0 | int nearestKeyFrame = (int)avifDecoderNearestKeyframe(decoder, frameIndex); |
7132 | 0 | if ((nearestKeyFrame > (decoder->imageIndex + 1)) || (requestedIndex <= decoder->imageIndex)) { |
7133 | | // If we get here, we need to start decoding from the nearest key frame. |
7134 | | // So discard the unused decoder state and its previous frames. This |
7135 | | // will force the setup of new AV1 decoder (avifCodec) instances in |
7136 | | // avifDecoderNextImage(). |
7137 | 0 | decoder->imageIndex = nearestKeyFrame - 1; // prepare to read nearest keyframe |
7138 | 0 | avifDecoderDataResetCodec(decoder->data); |
7139 | 0 | } |
7140 | 0 | for (;;) { |
7141 | 0 | avifResult result = avifDecoderNextImage(decoder); |
7142 | 0 | if (result != AVIF_RESULT_OK) { |
7143 | 0 | return result; |
7144 | 0 | } |
7145 | | |
7146 | 0 | if (requestedIndex == decoder->imageIndex) { |
7147 | 0 | break; |
7148 | 0 | } |
7149 | 0 | } |
7150 | 0 | return AVIF_RESULT_OK; |
7151 | 0 | } |
7152 | | |
7153 | | avifBool avifDecoderIsKeyframe(const avifDecoder * decoder, uint32_t frameIndex) |
7154 | 0 | { |
7155 | 0 | if (!decoder->data || (decoder->data->tiles.count == 0)) { |
7156 | | // Nothing has been parsed yet |
7157 | 0 | return AVIF_FALSE; |
7158 | 0 | } |
7159 | | |
7160 | | // *All* tiles for the requested frameIndex must be keyframes in order for |
7161 | | // avifDecoderIsKeyframe() to return true, otherwise we may seek to a frame in which the color |
7162 | | // planes are a keyframe but the alpha plane isn't a keyframe, which will cause an alpha plane |
7163 | | // decode failure. |
7164 | 0 | for (unsigned int i = 0; i < decoder->data->tiles.count; ++i) { |
7165 | 0 | const avifTile * tile = &decoder->data->tiles.tile[i]; |
7166 | 0 | if ((frameIndex >= tile->input->samples.count) || !tile->input->samples.sample[frameIndex].sync) { |
7167 | 0 | return AVIF_FALSE; |
7168 | 0 | } |
7169 | 0 | } |
7170 | 0 | return AVIF_TRUE; |
7171 | 0 | } |
7172 | | |
7173 | | uint32_t avifDecoderNearestKeyframe(const avifDecoder * decoder, uint32_t frameIndex) |
7174 | 0 | { |
7175 | 0 | if (!decoder->data) { |
7176 | | // Nothing has been parsed yet |
7177 | 0 | return 0; |
7178 | 0 | } |
7179 | | |
7180 | 0 | for (; frameIndex != 0; --frameIndex) { |
7181 | 0 | if (avifDecoderIsKeyframe(decoder, frameIndex)) { |
7182 | 0 | break; |
7183 | 0 | } |
7184 | 0 | } |
7185 | 0 | return frameIndex; |
7186 | 0 | } |
7187 | | |
7188 | | // Returns the number of available rows in decoder->image given a color or alpha subimage. |
7189 | | static uint32_t avifGetDecodedRowCount(const avifDecoder * decoder, const avifTileInfo * info, const avifImage * image) |
7190 | 0 | { |
7191 | 0 | if (info->decodedTileCount == info->tileCount) { |
7192 | 0 | return image->height; |
7193 | 0 | } |
7194 | 0 | if (info->decodedTileCount == 0) { |
7195 | 0 | return 0; |
7196 | 0 | } |
7197 | | |
7198 | 0 | if (decoder->data->meta->sampleTransformExpression.count > 0) { |
7199 | | // TODO(yguyon): Support incremental Sample Transforms |
7200 | 0 | return 0; |
7201 | 0 | } |
7202 | | |
7203 | 0 | if ((info->grid.rows > 0) && (info->grid.columns > 0)) { |
7204 | | // Grid of AVIF tiles (not to be confused with AV1 tiles). |
7205 | 0 | const uint32_t tileHeight = decoder->data->tiles.tile[info->firstTileIndex].height; |
7206 | 0 | return AVIF_MIN((info->decodedTileCount / info->grid.columns) * tileHeight, image->height); |
7207 | 0 | } else { |
7208 | | // Non-grid image. |
7209 | 0 | return image->height; |
7210 | 0 | } |
7211 | 0 | } |
7212 | | |
7213 | | uint32_t avifDecoderDecodedRowCount(const avifDecoder * decoder) |
7214 | 0 | { |
7215 | 0 | if (decoder->data->tileInfos[AVIF_ITEM_COLOR].tileCount == 0) { |
7216 | | // decoder->imageContentToDecode & AVIF_IMAGE_CONTENT_COLOR_AND_ALPHA |
7217 | | // was likely 0 when avifDecoderNextImage() was called. |
7218 | | // avifDecoderDecodedRowCount() only describes decoder->image->yuvPlanes[0]. |
7219 | | // There is no available luma plane, so return 0 decoded rows. |
7220 | 0 | return 0; |
7221 | 0 | } |
7222 | | |
7223 | 0 | uint32_t minRowCount = decoder->image->height; |
7224 | 0 | for (int c = 0; c < AVIF_ITEM_CATEGORY_COUNT; ++c) { |
7225 | 0 | if (c == AVIF_ITEM_GAIN_MAP) { |
7226 | 0 | const avifImage * const gainMap = decoder->image->gainMap ? decoder->image->gainMap->image : NULL; |
7227 | 0 | if (gainMap != NULL && gainMap->height != 0 && decoder->data->tileInfos[AVIF_ITEM_GAIN_MAP].tileCount != 0) { |
7228 | 0 | uint32_t gainMapRowCount = avifGetDecodedRowCount(decoder, &decoder->data->tileInfos[AVIF_ITEM_GAIN_MAP], gainMap); |
7229 | 0 | if (gainMap->height != decoder->image->height) { |
7230 | 0 | const uint32_t scaledGainMapRowCount = |
7231 | 0 | (uint32_t)floorf((float)gainMapRowCount / gainMap->height * decoder->image->height); |
7232 | | // Make sure it matches the formula described in the comment of avifDecoderDecodedRowCount() in avif.h. |
7233 | 0 | AVIF_CHECKERR((uint32_t)lround((double)scaledGainMapRowCount / decoder->image->height * |
7234 | 0 | decoder->image->gainMap->image->height) <= gainMapRowCount, |
7235 | 0 | 0); |
7236 | 0 | gainMapRowCount = scaledGainMapRowCount; |
7237 | 0 | } |
7238 | 0 | minRowCount = AVIF_MIN(minRowCount, gainMapRowCount); |
7239 | 0 | } |
7240 | 0 | continue; |
7241 | 0 | } |
7242 | 0 | const uint32_t rowCount = avifGetDecodedRowCount(decoder, &decoder->data->tileInfos[c], decoder->image); |
7243 | 0 | minRowCount = AVIF_MIN(minRowCount, rowCount); |
7244 | 0 | } |
7245 | 0 | return minRowCount; |
7246 | 0 | } |
7247 | | |
7248 | | avifResult avifDecoderRead(avifDecoder * decoder, avifImage * image) |
7249 | 0 | { |
7250 | 0 | avifResult result = avifDecoderParse(decoder); |
7251 | 0 | if (result != AVIF_RESULT_OK) { |
7252 | 0 | return result; |
7253 | 0 | } |
7254 | 0 | result = avifDecoderNextImage(decoder); |
7255 | 0 | if (result != AVIF_RESULT_OK) { |
7256 | 0 | return result; |
7257 | 0 | } |
7258 | | // If decoder->image->imageOwnsYUVPlanes is true and decoder->image is not used after this call, |
7259 | | // the ownership of the planes in decoder->image could be transferred here instead of copied. |
7260 | | // However most codec_*.c implementations allocate the output buffer themselves and return a |
7261 | | // view, unless some postprocessing is applied (container-level grid reconstruction for |
7262 | | // example), so the first condition rarely holds. |
7263 | | // The second condition does not hold either: it is not required by the documentation in avif.h. |
7264 | 0 | return avifImageCopy(image, decoder->image, AVIF_PLANES_ALL); |
7265 | 0 | } |
7266 | | |
7267 | | avifResult avifDecoderReadMemory(avifDecoder * decoder, avifImage * image, const uint8_t * data, size_t size) |
7268 | 0 | { |
7269 | 0 | avifDiagnosticsClearError(&decoder->diag); |
7270 | 0 | avifResult result = avifDecoderSetIOMemory(decoder, data, size); |
7271 | 0 | if (result != AVIF_RESULT_OK) { |
7272 | 0 | return result; |
7273 | 0 | } |
7274 | 0 | return avifDecoderRead(decoder, image); |
7275 | 0 | } |
7276 | | |
7277 | | avifResult avifDecoderReadFile(avifDecoder * decoder, avifImage * image, const char * filename) |
7278 | 0 | { |
7279 | 0 | avifDiagnosticsClearError(&decoder->diag); |
7280 | 0 | avifResult result = avifDecoderSetIOFile(decoder, filename); |
7281 | 0 | if (result != AVIF_RESULT_OK) { |
7282 | 0 | return result; |
7283 | 0 | } |
7284 | 0 | return avifDecoderRead(decoder, image); |
7285 | 0 | } |