Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
/*
 * Copyright (c) Radzivon Bartoshyk 6/2026. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1.  Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2.  Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3.  Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

//! # AVIF / AV2 File Format Parser and Decoder
//!
//! This module parses AVIF image files (ISO/IEC 23000-22) — an ISOBMFF container
//! (ISO 14496-12) that wraps AV2-compressed image samples — and feeds the extracted
//! bitstream into [`Decoder`] to produce decoded [`Picture`]s.
//!
//! ## Container structure
//!
//! ```text
//! AVIF file
//! ├── ftyp  — file-type box (brand = 'avif' or 'avis')
//! ├── meta  — metadata container (FullBox)
//! │   ├── hdlr  — handler = 'pict'
//! │   ├── iloc  — item location (maps item_id → byte range in mdat)
//! │   ├── iinf  — item info (maps item_id → type 'av01'/'av02'/'grid')
//! │   ├── iprp  — item properties
//! │   │   ├── ipco  — property container
//! │   │   │   ├── ispe  — image spatial extents (width / height)
//! │   │   │   ├── av1C / av2C — codec configuration
//! │   │   │   ├── colr  — color information
//! │   │   │   ├── pixi  — pixel information (bits per channel)
//! │   │   │   ├── pasp  — pixel aspect ratio
//! │   │   │   ├── irot  — image rotation (0 / 90 / 180 / 270° anti-clockwise)
//! │   │   │   ├── imir  — image mirror (horizontal-axis / vertical-axis)
//! │   │   │   └── clap  — clean aperture (visible crop rectangle)
//! │   │   └── ipma  — item property association
//! │   └── iref  — item references (dimg / thmb / cdsc)
//! └── mdat  — media data (raw AV2 OBU samples, one per item)
//! ```
//!

use crate::data::Data;
use crate::decoder::{Decoder, Settings};
use crate::error::TealdustError;
use crate::getbits::GetBits;
use crate::headers::{ContentLightLevel, ObuType, PixelLayout};
use crate::levels::ObuMetaType;
use crate::picture::Picture;
use crate::{ColorPrimaries, MatrixCoefficients, TransferCharacteristics};
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;

/// Default number of items accepted from an `iinf` box.
pub const DEFAULT_AVIF_MAX_ITEMS: u32 = 1024;
/// Default number of property entries accepted from an `ipco` box.
pub const DEFAULT_AVIF_MAX_IPCO_PROPS: usize = 256;
/// Default number of `ipma` entry associations per item.
pub const DEFAULT_AVIF_MAX_IPMA_ENTRIES: u32 = 4096;
/// Default extents per `iloc` item.
pub const DEFAULT_AVIF_MAX_EXTENTS_PER_ITEM: u16 = 32;
/// Default total OBU bytes assembled from `iloc` extents before feeding the
/// decoder.
pub const DEFAULT_AVIF_MAX_OBU_BYTES: usize = 64 * 1024 * 1024; // 64 MiB
/// Default image dimension accepted from `ispe`.
pub const DEFAULT_AVIF_MAX_IMAGE_DIMENSION: u32 = 65536;
/// Default allowed `iloc` item count (v0/v1 = u16, v2 = u32; cap both).
pub const DEFAULT_AVIF_MAX_ILOC_ITEMS: u32 = 1024;
/// Default number of `ftyp` compatible brands scanned (prevents O(n) on junk).
pub const DEFAULT_AVIF_MAX_COMPAT_BRANDS: usize = 64;
/// Default byte length of an `auxC` URN string accepted from the bitstream.
pub const DEFAULT_AVIF_MAX_AUXC_URN_LEN: usize = 512;
/// Default number of item references consumed from one `iref` type-ref box.
pub const DEFAULT_AVIF_MAX_IREF_REFS: u16 = 256;

/// High-level AVIF parser/decoder configuration.
///
/// This wraps the AV2 [`Settings`] used internally by [`Decoder`] and the
/// container-level caps that protect AVIF box parsing and OBU assembly. The
/// default is suitable for untrusted still images: it keeps the existing parser
/// limits, enables reconstruction, and uses the host's available parallelism.
/// Set an individual limit to `0` to disable that specific container cap.
#[derive(Debug, Clone)]
pub struct AvifSettings {
    /// Settings passed to the underlying AV2 decoder.
    ///
    /// [`Settings::run_decode`] is forced to `true` while decoding AVIF because
    /// the high-level API always returns pixels. Other fields, including
    /// `n_threads`, `frame_size_limit`, filters, strictness, and grain policy are
    /// passed through as provided.
    pub decoder_settings: Settings,
    /// Maximum total OBU bytes assembled from codec-config OBUs plus item extents.
    pub max_obu_bytes: usize,
    /// Maximum image width or height accepted from `ispe`.
    pub max_image_dimension: u32,
    /// Maximum number of items accepted from an `iinf` box.
    pub max_items: u32,
    /// Maximum number of property entries accepted from an `ipco` box.
    pub max_ipco_props: usize,
    /// Maximum number of `ipma` entries accepted.
    pub max_ipma_entries: u32,
    /// Maximum number of extents accepted for a single `iloc` item.
    pub max_extents_per_item: u16,
    /// Maximum number of items accepted from an `iloc` box.
    pub max_iloc_items: u32,
    /// Maximum number of compatible brands scanned in `ftyp`.
    pub max_compat_brands: usize,
    /// Maximum byte length of an `auxC` URN.
    pub max_auxc_urn_len: usize,
    /// Maximum number of references consumed from one `iref` type-ref box.
    pub max_iref_refs: u16,
}

impl Default for AvifSettings {
    fn default() -> Self {
        let mut decoder_settings = Settings::default();
        decoder_settings.n_threads = std::thread::available_parallelism()
            .map(|n| n.get() as u32)
            .unwrap_or(1);
        decoder_settings.run_decode = true;

        Self {
            decoder_settings,
            max_obu_bytes: DEFAULT_AVIF_MAX_OBU_BYTES,
            max_image_dimension: DEFAULT_AVIF_MAX_IMAGE_DIMENSION,
            max_items: DEFAULT_AVIF_MAX_ITEMS,
            max_ipco_props: DEFAULT_AVIF_MAX_IPCO_PROPS,
            max_ipma_entries: DEFAULT_AVIF_MAX_IPMA_ENTRIES,
            max_extents_per_item: DEFAULT_AVIF_MAX_EXTENTS_PER_ITEM,
            max_iloc_items: DEFAULT_AVIF_MAX_ILOC_ITEMS,
            max_compat_brands: DEFAULT_AVIF_MAX_COMPAT_BRANDS,
            max_auxc_urn_len: DEFAULT_AVIF_MAX_AUXC_URN_LEN,
            max_iref_refs: DEFAULT_AVIF_MAX_IREF_REFS,
        }
    }
}

/// Errors that can occur when parsing or decoding an AVIF file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AvifError {
    /// The file is too short to contain a valid AVIF container.
    TooShort,
    /// The `ftyp` box is missing or does not carry an AVIF-compatible brand.
    NotAvif,
    /// A required metadata box (`meta`, `iloc`, `iinf`, …) is missing.
    MissingBox(&'static str),
    /// A box header or field value is out of range or otherwise invalid.
    InvalidBox,
    /// The primary item is not a supported codec type.
    UnsupportedCodec,
    /// Grid (AVIF sequence / tiled image) decoding is not yet supported.
    GridNotSupported,
    /// An `iloc` extent references bytes outside the file.
    ExtentOutOfBounds,
    /// The underlying AV2 bitstream decoder returned an error.
    DecodeError(TealdustError),
    /// The `av2C` / `av1C` codec-config box is present but malformed.
    InvalidCodecConfig,
    /// An attacker-controlled count or size exceeded a safety limit.
    LimitExceeded,
    /// A fallible heap allocation failed while parsing/container-copying.
    OutOfMemory,
}

impl fmt::Display for AvifError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::TooShort => write!(f, "file too short"),
            Self::NotAvif => write!(f, "not an AVIF file (missing avif/avis brand)"),
            Self::MissingBox(b) => write!(f, "required box '{b}' not found"),
            Self::InvalidBox => write!(f, "malformed ISOBMFF box"),
            Self::UnsupportedCodec => write!(f, "unsupported item codec type"),
            Self::GridNotSupported => write!(f, "grid/sequence AVIF items not yet supported"),
            Self::ExtentOutOfBounds => write!(f, "iloc extent references bytes outside the file"),
            Self::DecodeError(e) => write!(f, "AV2 decode error: {e}"),
            Self::InvalidCodecConfig => write!(f, "invalid av2C/av1C codec config box"),
            Self::LimitExceeded => write!(f, "parser limit exceeded"),
            Self::OutOfMemory => write!(f, "out of memory"),
        }
    }
}

impl std::error::Error for AvifError {}

impl From<TealdustError> for AvifError {
    fn from(e: TealdustError) -> Self {
        Self::DecodeError(e)
    }
}

type Result<T> = std::result::Result<T, AvifError>;

/// Color information extracted from the `colr` item property.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ColorInfo {
    /// Color primaries (ISO 23091-2 / H.273).
    pub color_primaries: ColorPrimaries,
    /// Transfer characteristics.
    pub transfer_characteristics: TransferCharacteristics,
    /// Matrix coefficients.
    pub matrix_coefficients: MatrixCoefficients,
    /// Full-range flag (`0` = limited, `1` = full).
    pub full_range: bool,
}

/// The standard URN identifying an alpha auxiliary image item.
///
/// Defined in HEIF (ISO 23008-12) §6.10.3 and referenced by the AVIF spec.
pub const ALPHA_AUXILIARY_URN: &str = "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha";

/// Identifies the purpose of an auxiliary image item, parsed from the `auxC`
/// item property.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AuxiliaryType {
    /// The item is an alpha (transparency) plane.
    Alpha,
    /// The item is a depth map.
    Depth,
    /// Some other auxiliary type (URN stored for inspection).
    Other(String),
}

impl AuxiliaryType {
    /// Parse from a null-terminated URN byte string (the `auxC` payload after
    /// the FullBox 4-byte header).
    fn from_urn_bytes(bytes: &[u8]) -> Self {
        // Trim a trailing NUL if present.
        let s = bytes
            .iter()
            .position(|&b| b == 0)
            .map_or(bytes, |i| &bytes[..i]);
        match s {
            b"urn:mpeg:mpegB:cicp:systems:auxiliary:alpha" => Self::Alpha,
            b"urn:mpeg:mpegB:cicp:systems:auxiliary:depth" => Self::Depth,
            _ => Self::Other(String::from_utf8_lossy(s).into_owned()),
        }
    }

    /// Returns `true` if this auxiliary item carries alpha transparency data.
    #[inline]
    pub fn is_alpha(&self) -> bool {
        *self == Self::Alpha
    }
}

/// Parsed `av2C` (or `av1C`) codec-configuration record.
///
/// The `av2C` box stores a sequence of OBUs (Sequence Header + Metadata) that
/// precede every sample.  We hold the raw bytes and feed them to the decoder
/// before the sample OBUs.
#[derive(Debug, Clone)]
pub struct CodecConfig {
    /// Raw `configOBUs` bytes (zero or more OBUs).
    pub config_obus: Vec<u8>,
    /// High bit-depth flag from the config record.
    pub high_bitdepth: bool,
    /// Twelve-bit flag.
    pub twelve_bit: bool,
    /// Monochrome flag.
    pub monochrome: bool,
    /// Chroma subsampling: x.
    pub chroma_subsampling_x: u8,
    /// Chroma subsampling: y.
    pub chroma_subsampling_y: u8,
    /// Chroma sample position.
    pub chroma_sample_position: u8,
}

impl CodecConfig {
    /// Derive the [`PixelLayout`] that the config record implies.
    pub fn pixel_layout(&self) -> PixelLayout {
        if self.monochrome {
            PixelLayout::I400
        } else if self.chroma_subsampling_x != 0 && self.chroma_subsampling_y != 0 {
            PixelLayout::I420
        } else if self.chroma_subsampling_x != 0 {
            PixelLayout::I422
        } else {
            PixelLayout::I444
        }
    }

    /// Bits per component implied by the config record.
    pub fn bits_per_component(&self) -> u8 {
        if self.twelve_bit {
            12
        } else if self.high_bitdepth {
            10
        } else {
            8
        }
    }
}

/// Image dimensions from the `ispe` item property.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct SpatialExtents {
    /// Image width in pixels (bounded by [`AvifSettings::max_image_dimension`]).
    pub width: u32,
    /// Image height in pixels (bounded by [`AvifSettings::max_image_dimension`]).
    pub height: u32,
}

/// Pixel-aspect-ratio from the `pasp` item property.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct PixelAspectRatio {
    pub h_spacing: u32,
    pub v_spacing: u32,
}

/// Display orientation, using the eight EXIF orientation values. HEIF expresses
/// orientation with a rotation property (`irot`, anticlockwise multiples of 90°)
/// and a mirror property (`imir`); the diagonal EXIF values map to a mirror
/// followed by a rotation.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum Orientation {
    /// 1 — upright, no transform.
    #[default]
    Normal,
    /// 2 — mirrored horizontally.
    FlipH,
    /// 3 — rotated 180°.
    Rotate180,
    /// 4 — mirrored vertically.
    FlipV,
    /// 5 — transpose (mirror H then rotate 90° clockwise).
    Transpose,
    /// 6 — rotated 90° clockwise.
    Rotate90,
    /// 7 — transverse (mirror H then rotate 90° anticlockwise).
    Transverse,
    /// 8 — rotated 90° anticlockwise.
    Rotate270,
}

impl Orientation {
    /// Map a raw EXIF Orientation value (1..=8) to an [`Orientation`]; anything
    /// out of range is treated as `Normal`.
    pub fn from_exif(v: u16) -> Self {
        match v {
            2 => Orientation::FlipH,
            3 => Orientation::Rotate180,
            4 => Orientation::FlipV,
            5 => Orientation::Transpose,
            6 => Orientation::Rotate90,
            7 => Orientation::Transverse,
            8 => Orientation::Rotate270,
            _ => Orientation::Normal,
        }
    }

    /// True when no orientation transform is needed (so neither `irot` nor
    /// `imir` is written).
    pub fn is_identity(self) -> bool {
        self.irot_steps() == 0 && self.imir_axis().is_none()
    }

    /// The `imir` axis when a mirror is part of the transform: `Some(false)` for
    /// a vertical mirroring axis (left-right flip), `Some(true)` for a
    /// horizontal mirroring axis (top-bottom flip), or `None` for no mirror.
    /// (HEIF `imir`: `axis == 0` mirrors about a vertical axis.)
    pub(crate) fn imir_axis(self) -> Option<bool> {
        match self {
            Orientation::FlipH | Orientation::Transpose | Orientation::Transverse => Some(false),
            Orientation::FlipV => Some(true),
            _ => None,
        }
    }

    /// `irot` rotation in anticlockwise 90° steps (0..=3).
    pub(crate) fn irot_steps(self) -> u8 {
        match self {
            Orientation::Normal | Orientation::FlipH | Orientation::FlipV => 0,
            Orientation::Rotate180 => 2,
            Orientation::Rotate90 => 3,
            Orientation::Rotate270 => 1,
            Orientation::Transpose => 3,
            Orientation::Transverse => 1,
        }
    }

    /// Reconstruct an [`Orientation`] from the `irot` step count (0..=3) and the
    /// `imir` axis (`None`, `Some(false)` = vertical axis / left-right flip,
    /// `Some(true)` = horizontal axis / top-bottom flip). Inverse of the
    /// `irot_steps` / `imir_axis` pair this crate writes.
    pub(crate) fn from_irot_imir(steps: u8, axis: Option<bool>) -> Self {
        match (steps & 3, axis) {
            (0, None) => Orientation::Normal,
            (2, None) => Orientation::Rotate180,
            (3, None) => Orientation::Rotate90,
            (1, None) => Orientation::Rotate270,
            (0, Some(false)) => Orientation::FlipH,
            (0, Some(true)) => Orientation::FlipV,
            (3, Some(false)) => Orientation::Transpose,
            (1, Some(false)) => Orientation::Transverse,
            _ => Orientation::Normal,
        }
    }
}

/// A rational number as a (numerator, denominator) pair.
///
/// Denominators come directly from the bitstream and **must be checked for
/// zero before dividing**.  The parser validates them at parse time and
/// returns [`AvifError::InvalidBox`] for zero denominators.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Rational {
    pub numerator: i32,
    pub denominator: i32,
}

impl Rational {
    /// Evaluate to an `f64`.  Panics if `denominator == 0`.
    #[inline]
    pub fn to_f64(self) -> f64 {
        self.numerator as f64 / self.denominator as f64
    }
}

/// Clean aperture from the `clap` item property (ISO 14496-12 §12.1.4).
///
/// Defines the visible sub-rectangle of the coded image in *sample* coordinates
/// relative to the image centre.  All four fields are rational numbers so that
/// an integer pixel grid can be addressed exactly even after sub-sample offsets.
///
/// The visible rectangle can be computed as:
///
/// ```text
/// x_centre = (coded_width  - 1) / 2  +  horiz_off
/// y_centre = (coded_height - 1) / 2  +  vert_off
///
/// left   = x_centre - (clean_width  - 1) / 2
/// top    = y_centre - (clean_height - 1) / 2
/// right  = left + clean_width
/// bottom = top  + clean_height
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CleanAperture {
    /// Clean aperture width as a rational (numerator / denominator > 0).
    pub width: Rational,
    /// Clean aperture height as a rational.
    pub height: Rational,
    /// Horizontal offset of the aperture centre from the image centre.
    pub horiz_off: Rational,
    /// Vertical offset of the aperture centre from the image centre.
    pub vert_off: Rational,
}

impl CleanAperture {
    /// Compute the integer pixel crop rectangle `(x, y, w, h)` in the coded
    /// image for the given `coded_width × coded_height`, rounding to the
    /// nearest pixel.  Returns `None` if any denominator is zero or if the
    /// resulting rectangle falls outside `[0, coded_width) × [0, coded_height)`.
    pub fn to_crop_rect(self, coded_width: u32, coded_height: u32) -> Option<(u32, u32, u32, u32)> {
        let cw = coded_width as f64;
        let ch = coded_height as f64;

        let clean_w = self.width.to_f64();
        let clean_h = self.height.to_f64();
        let h_off = self.horiz_off.to_f64();
        let v_off = self.vert_off.to_f64();

        if clean_w <= 0.0 || clean_h <= 0.0 {
            return None;
        }

        let x_centre = (cw - 1.0) / 2.0 + h_off;
        let y_centre = (ch - 1.0) / 2.0 + v_off;
        let left = x_centre - (clean_w - 1.0) / 2.0;
        let top = y_centre - (clean_h - 1.0) / 2.0;

        let x = left.round() as i64;
        let y = top.round() as i64;
        let w = clean_w.round() as i64;
        let h = clean_h.round() as i64;

        if x < 0 || y < 0 || w <= 0 || h <= 0 {
            return None;
        }
        if x + w > coded_width as i64 {
            return None;
        }
        if y + h > coded_height as i64 {
            return None;
        }

        Some((x as u32, y as u32, w as u32, h as u32))
    }
}

/// The four-character-code type of `iinf` item.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ItemType {
    /// AV1 image item (AVIF using AV1 codec).
    Av01,
    /// AV2 image item (AVIF using AV2 codec).
    Av02,
    /// Grid image (tiled/sequence composite).
    Grid,
    /// Thumbnail.
    Thumb,
    /// ICC color profile embedded as an item.
    Prof,
    /// Any other item type (ignored by the decoder).
    Other([u8; 4]),
}

impl ItemType {
    fn from_fourcc(b: [u8; 4]) -> Self {
        match &b {
            b"av01" => Self::Av01,
            b"av02" => Self::Av02,
            b"grid" => Self::Grid,
            b"thmb" => Self::Thumb,
            b"prof" => Self::Prof,
            _ => Self::Other(b),
        }
    }

    /// Returns `true` if this item holds an AV1 or AV2 compressed image.
    pub fn is_image(&self) -> bool {
        matches!(self, Self::Av02)
    }
}

/// Describes a single item inside the AVIF `meta` box.
#[derive(Debug, Clone)]
pub struct AvifItem {
    pub item_id: u16,
    pub item_type: ItemType,
    /// Decoded spatial extents from `ispe`.
    pub extents: Option<SpatialExtents>,
    /// Codec configuration from `av2C` / `av1C`.
    pub codec_config: Option<CodecConfig>,
    /// Color information from `colr`.
    pub color_info: Option<ColorInfo>,
    /// Pixel-aspect-ratio from `pasp`.
    pub pixel_aspect_ratio: Option<PixelAspectRatio>,
    /// Byte extents inside the file (from `iloc`).
    pub iloc_extents: Vec<(u64, u64)>,
    /// Auxiliary image type from `auxC` (present on alpha / depth items).
    pub auxiliary_type: Option<AuxiliaryType>,
    /// Whether the color channels in the primary item are premultiplied by
    /// this alpha item's values.  Set when the `prem` item reference is
    /// present from the primary item to this alpha item.
    pub premultiplied_alpha: bool,
    /// Display orientation from `irot` and/or `imir` item properties.
    /// `None` means the decoder found neither property (treat as identity).
    pub orientation: Option<Orientation>,
    /// Visible crop rectangle from the `clap` item property.
    /// `None` if the property is absent (the full coded image is displayed).
    pub clean_aperture: Option<CleanAperture>,
    /// ICC profile from a `colr` box of type `rICC` or `prof`.
    pub icc_profile: Option<Vec<u8>>,
}

impl AvifItem {
    fn new(item_id: u16, item_type: ItemType) -> Self {
        Self {
            item_id,
            item_type,
            extents: None,
            codec_config: None,
            color_info: None,
            pixel_aspect_ratio: None,
            iloc_extents: Vec::new(),
            auxiliary_type: None,
            premultiplied_alpha: false,
            orientation: None,
            clean_aperture: None,
            icc_profile: None,
        }
    }
}

/// Metadata about a decoded AVIF image, available before or alongside pixel data.
#[derive(Debug, Clone)]
pub struct AvifImageInfo {
    /// Width in pixels (from `ispe`).
    pub width: u32,
    /// Height in pixels (from `ispe`).
    pub height: u32,
    /// Pixel layout derived from the codec config.
    pub pixel_layout: PixelLayout,
    /// Bits per component.
    pub bits_per_component: u8,
    /// color information, if present.
    pub color_info: Option<ColorInfo>,
    /// Content light level metadata from HDR CLL metadata OBUs, if present.
    pub content_light_level: Option<ContentLightLevel>,
    /// Pixel-aspect-ratio, if present.
    pub pixel_aspect_ratio: Option<PixelAspectRatio>,
    /// Item type of the primary image.
    pub item_type: ItemType,
    /// Whether the file contains a separate alpha (transparency) item linked
    /// to the primary item via an `auxl` `iref` reference.
    pub has_alpha: bool,
    /// Whether the color channels are premultiplied by alpha (`prem` iref).
    pub premultiplied_alpha: bool,
    /// Display orientation from `irot` / `imir` properties; `None` = identity.
    pub orientation: Option<Orientation>,
    /// Visible crop rectangle from the `clap` property
    pub clean_aperture: Option<CleanAperture>,
    /// ICC profile, if the `colr` box carried `rICC` or `prof` data.
    pub icc_profile: Option<Vec<u8>>,
}

/// A fully parsed AVIF container, ready for decoding.
///
/// Produced by [`AvifParser::parse`].
#[derive(Debug)]
pub struct AvifContainer {
    /// All items found in `iinf`.
    pub items: HashMap<u16, AvifItem>,
    /// `pitm` primary item ID.
    pub primary_item_id: u16,
    /// Brand string from `ftyp` (e.g. `avif`).
    pub brand: [u8; 4],
    /// Item ID of the alpha auxiliary image linked to the primary item via
    /// an `auxl` `iref` reference, if any.
    pub alpha_item_id: Option<u16>,
}

impl AvifContainer {
    /// Return a reference to the primary image item.
    pub fn primary_item(&self) -> Option<&AvifItem> {
        self.items.get(&self.primary_item_id)
    }

    /// Return a reference to the alpha auxiliary item, if present.
    pub fn alpha_item(&self) -> Option<&AvifItem> {
        self.alpha_item_id.and_then(|id| self.items.get(&id))
    }
}
#[derive(Debug)]
struct Reader<'a> {
    data: &'a [u8],
    pos: usize,
}

impl<'a> Reader<'a> {
    fn new(data: &'a [u8]) -> Self {
        Self { data, pos: 0 }
    }

    #[inline]
    fn remaining(&self) -> usize {
        self.data.len().saturating_sub(self.pos)
    }

    #[inline]
    fn is_empty(&self) -> bool {
        self.pos >= self.data.len()
    }

    fn read_u8(&mut self) -> Result<u8> {
        let v = self
            .data
            .get(self.pos)
            .copied()
            .ok_or(AvifError::TooShort)?;
        self.pos += 1;
        Ok(v)
    }

    fn read_u16_be(&mut self) -> Result<u16> {
        let b = self.read_bytes::<2>()?;
        Ok(u16::from_be_bytes(b))
    }

    fn read_u32_be(&mut self) -> Result<u32> {
        let b = self.read_bytes::<4>()?;
        Ok(u32::from_be_bytes(b))
    }

    fn read_u64_be(&mut self) -> Result<u64> {
        let b = self.read_bytes::<8>()?;
        Ok(u64::from_be_bytes(b))
    }

    fn read_bytes<const N: usize>(&mut self) -> Result<[u8; N]> {
        let end = self.pos.checked_add(N).ok_or(AvifError::TooShort)?;
        if end > self.data.len() {
            return Err(AvifError::TooShort);
        }
        let mut buf = [0u8; N];
        buf.copy_from_slice(&self.data[self.pos..end]);
        self.pos = end;
        Ok(buf)
    }

    fn read_slice(&mut self, n: usize) -> Result<&'a [u8]> {
        let end = self.pos.checked_add(n).ok_or(AvifError::TooShort)?;
        if end > self.data.len() {
            return Err(AvifError::TooShort);
        }
        let sl = &self.data[self.pos..end];
        self.pos = end;
        Ok(sl)
    }

    /// Create a sub-reader for exactly `len` bytes starting at the current position.
    fn sub_reader(&mut self, len: usize) -> Result<Reader<'a>> {
        let end = self.pos.checked_add(len).ok_or(AvifError::TooShort)?;
        if end > self.data.len() {
            return Err(AvifError::TooShort);
        }
        let sl = &self.data[self.pos..end];
        self.pos = end;
        Ok(Reader { data: sl, pos: 0 })
    }
}

#[derive(Debug, Clone, Copy)]
struct BoxHeader {
    /// Four-character-code.
    fourcc: [u8; 4],
    /// Header byte size (8 for normal, 16 for extended).
    #[allow(dead_code)]
    header_size: u64,
}

/// Read the next ISOBMFF box header from `r`, returning header + a sub-reader
/// strictly limited to the payload bytes.
fn read_box_header<'a>(r: &mut Reader<'a>) -> Result<(BoxHeader, Reader<'a>)> {
    if r.remaining() < 8 {
        return Err(AvifError::TooShort);
    }

    let size_field = r.read_u32_be()?;
    let fourcc = r.read_bytes::<4>()?;

    let (total_size, header_size): (u64, u64) = match size_field {
        0 => return Err(AvifError::InvalidBox),

        1 => {
            // Extended 64-bit size: next 8 bytes hold the *total* box size.
            let ext = r.read_u64_be()?;
            if ext < 16 {
                return Err(AvifError::InvalidBox);
            }
            (ext, 16)
        }

        n if n < 8 => return Err(AvifError::InvalidBox),

        n => (n as u64, 8),
    };

    let payload_size = total_size
        .checked_sub(header_size)
        .ok_or(AvifError::InvalidBox)?;

    let payload_size_usize = usize::try_from(payload_size).map_err(|_| AvifError::InvalidBox)?;

    // sub_reader will return TooShort if payload_size_usize > r.remaining(),
    // so no further bounds check is needed here.
    let payload = r.sub_reader(payload_size_usize)?;

    Ok((
        BoxHeader {
            fourcc,
            header_size,
        },
        payload,
    ))
}

fn read_fullbox_header(r: &mut Reader<'_>) -> Result<(u8, u32)> {
    let version = r.read_u8()?;
    let b = r.read_bytes::<3>()?;
    let flags = ((b[0] as u32) << 16) | ((b[1] as u32) << 8) | (b[2] as u32);
    Ok((version, flags))
}

/// Stateless AVIF container parser.
///
/// Call [`AvifParser::parse`] with the raw file bytes to obtain an
/// [`AvifContainer`] that can then be passed to [`AvifDecoder`].
pub struct AvifParser;

impl AvifParser {
    /// Parse an AVIF file from `data` and return the container metadata.
    ///
    /// No pixel decoding occurs here; only the box structure is walked.
    pub fn parse(data: &[u8]) -> Result<AvifContainer> {
        Self::parse_with_settings(data, &AvifSettings::default())
    }

    /// Parse an AVIF file with explicit parser limits.
    ///
    /// No pixel decoding occurs here; only the box structure is walked.
    pub fn parse_with_settings(data: &[u8], settings: &AvifSettings) -> Result<AvifContainer> {
        let mut r = Reader::new(data);

        let mut brand = [0u8; 4];
        let mut found_ftyp = false;
        let mut primary_item_id: u16 = 0;
        let mut items: HashMap<u16, AvifItem> = HashMap::new();

        // Defer iloc/ipma/iref parsing until iinf and ipco are complete.
        let mut iloc_raw: Option<Vec<u8>> = None;
        let mut ipco_props: Vec<([u8; 4], Vec<u8>)> = Vec::new();
        let mut ipma_raw: Option<Vec<u8>> = None;
        // iref raw bytes: deferred so item IDs are populated first.
        let mut iref_raw: Option<Vec<u8>> = None;

        let mut seen_meta = false;

        while !r.is_empty() {
            let (hdr, mut payload) = match read_box_header(&mut r) {
                Ok(v) => v,
                Err(AvifError::TooShort) => break,
                Err(e) => return Err(e),
            };

            match &hdr.fourcc {
                b"ftyp" => {
                    if !found_ftyp {
                        brand = payload.read_bytes::<4>()?;
                        let _minor_version = payload.read_u32_be()?;

                        let mut avif_brand = is_avif_brand(&brand);

                        let mut brand_count = 0usize;
                        while payload.remaining() >= 4
                            && (settings.max_compat_brands == 0
                                || brand_count < settings.max_compat_brands)
                        {
                            let compat = payload.read_bytes::<4>()?;
                            if is_avif_brand(&compat) {
                                avif_brand = true;
                            }
                            brand_count += 1;
                        }

                        if !avif_brand {
                            return Err(AvifError::NotAvif);
                        }
                        found_ftyp = true;
                    }
                }

                b"meta" if !seen_meta => {
                    seen_meta = true;
                    let (_ver, _flags) = read_fullbox_header(&mut payload)?;
                    Self::parse_meta(
                        &mut payload,
                        &mut primary_item_id,
                        &mut items,
                        &mut iloc_raw,
                        &mut ipco_props,
                        &mut ipma_raw,
                        &mut iref_raw,
                        settings,
                    )?;
                }

                // mdat and all other top-level boxes are consumed implicitly
                // by the sub-reader (their bytes are skipped).
                _ => {}
            }
        }

        if !found_ftyp {
            return Err(AvifError::NotAvif);
        }

        // Resolve iloc offsets into absolute file positions.
        if let Some(iloc_bytes) = iloc_raw {
            Self::apply_iloc(&iloc_bytes, &mut items, data.len() as u64, settings)?;
        }

        // Apply ipco properties to items via ipma associations.
        if !ipco_props.is_empty() {
            if let Some(ipma_bytes) = ipma_raw {
                Self::apply_ipma(&ipma_bytes, &ipco_props, &mut items, settings)?;
            }
        }

        // Resolve iref references to find the alpha item for the primary item.
        let alpha_item_id = if let Some(iref_bytes) = iref_raw {
            Self::resolve_alpha_from_iref(&iref_bytes, primary_item_id, &mut items, settings)?
        } else {
            None
        };

        Ok(AvifContainer {
            items,
            primary_item_id,
            brand,
            alpha_item_id,
        })
    }

    fn parse_meta(
        r: &mut Reader<'_>,
        primary_item_id: &mut u16,
        items: &mut HashMap<u16, AvifItem>,
        iloc_raw: &mut Option<Vec<u8>>,
        ipco_props: &mut Vec<([u8; 4], Vec<u8>)>,
        ipma_raw: &mut Option<Vec<u8>>,
        iref_raw: &mut Option<Vec<u8>>,
        settings: &AvifSettings,
    ) -> Result<()> {
        let mut seen_pitm = false;
        let mut seen_iinf = false;
        let mut seen_iloc = false;
        let mut seen_iprp = false;
        let mut seen_iref = false;

        while !r.is_empty() {
            let (hdr, mut payload) = match read_box_header(r) {
                Ok(v) => v,
                Err(AvifError::TooShort) => break,
                Err(e) => return Err(e),
            };

            match &hdr.fourcc {
                b"pitm" if !seen_pitm => {
                    seen_pitm = true;
                    let (ver, _flags) = read_fullbox_header(&mut payload)?;
                    *primary_item_id = if ver == 0 {
                        payload.read_u16_be()?
                    } else {
                        let v = payload.read_u32_be()?;
                        // pitm v1 uses a u32 item_ID; our internal representation
                        // is u16 so IDs > 65535 are clamped to 0 (invalid sentinel).
                        u16::try_from(v).unwrap_or(0)
                    };
                }

                b"iinf" if !seen_iinf => {
                    seen_iinf = true;
                    let (ver, _flags) = read_fullbox_header(&mut payload)?;
                    let raw_count: u32 = if ver == 0 {
                        payload.read_u16_be()? as u32
                    } else {
                        payload.read_u32_be()?
                    };

                    if settings.max_items != 0 && raw_count > settings.max_items {
                        return Err(AvifError::LimitExceeded);
                    }
                    let count = raw_count;

                    for _ in 0..count {
                        let (_, mut ie_payload) = read_box_header(&mut payload)?;
                        let (iv, _) = read_fullbox_header(&mut ie_payload)?;
                        // infe item_id field width (ISO 14496-12 §8.11.6.2):
                        //   v0 / v1 / v2 → item_ID is u16
                        //   v3           → item_ID is u32
                        let item_id: u16 = if iv < 3 {
                            ie_payload.read_u16_be()?
                        } else {
                            let v = ie_payload.read_u32_be()?;
                            // u32 item IDs > 65535 can't be represented; skip
                            // silently — they belong to a feature we don't support.
                            u16::try_from(v).unwrap_or(0)
                        };
                        // item_protection_index (u16) — ignored.
                        let _ = ie_payload.read_u16_be()?;
                        // item_type is present in v1/v2/v3 but not v0.
                        // For v0 we'd need to read a name string; since AVIF
                        // always uses v2+, treat v0 as having no type.
                        let item_type_bytes = if iv >= 1 {
                            ie_payload.read_bytes::<4>()?
                        } else {
                            *b"\0\0\0\0"
                        };
                        let item_type = ItemType::from_fourcc(item_type_bytes);
                        items
                            .entry(item_id)
                            .or_insert_with(|| AvifItem::new(item_id, item_type));
                    }
                }

                b"iloc" if !seen_iloc => {
                    seen_iloc = true;
                    // Store raw bytes; resolved after iinf is fully parsed.
                    let raw = payload.read_slice(payload.remaining())?;
                    *iloc_raw = Some(raw.to_vec());
                }

                b"iprp" if !seen_iprp => {
                    seen_iprp = true;
                    Self::parse_iprp(&mut payload, ipco_props, ipma_raw, settings)?;
                }

                b"iref" if !seen_iref => {
                    seen_iref = true;
                    // Store raw bytes (includes the FullBox header); resolved
                    // after ipma has applied auxC properties to items.
                    let raw = payload.read_slice(payload.remaining())?;
                    *iref_raw = Some(raw.to_vec());
                }

                // All other singleton-once boxes and unknown boxes are skipped.
                _ => {}
            }
        }
        Ok(())
    }

    fn parse_iprp(
        r: &mut Reader<'_>,
        ipco_props: &mut Vec<([u8; 4], Vec<u8>)>,
        ipma_raw: &mut Option<Vec<u8>>,
        settings: &AvifSettings,
    ) -> Result<()> {
        let mut seen_ipco = false;
        let mut seen_ipma = false;

        while !r.is_empty() {
            let (hdr, mut payload) = match read_box_header(r) {
                Ok(v) => v,
                Err(AvifError::TooShort) => break,
                Err(e) => return Err(e),
            };

            match &hdr.fourcc {
                b"ipco" if !seen_ipco => {
                    seen_ipco = true;
                    while !payload.is_empty() {
                        let (phdr, mut ppayload) = match read_box_header(&mut payload) {
                            Ok(v) => v,
                            Err(AvifError::TooShort) => break,
                            Err(e) => return Err(e),
                        };

                        if settings.max_ipco_props != 0
                            && ipco_props.len() >= settings.max_ipco_props
                        {
                            return Err(AvifError::LimitExceeded);
                        }

                        let raw = ppayload.read_slice(ppayload.remaining())?;
                        ipco_props.push((phdr.fourcc, raw.to_vec()));
                    }
                }

                b"ipma" if !seen_ipma => {
                    seen_ipma = true;
                    let raw = payload.read_slice(payload.remaining())?;
                    *ipma_raw = Some(raw.to_vec());
                }

                _ => {}
            }
        }
        Ok(())
    }

    fn apply_iloc(
        iloc_bytes: &[u8],
        items: &mut HashMap<u16, AvifItem>,
        file_len: u64,
        settings: &AvifSettings,
    ) -> Result<()> {
        let mut r = Reader::new(iloc_bytes);
        let (ver, _flags) = read_fullbox_header(&mut r)?;

        let byte = r.read_u8()?;
        let byte2 = r.read_u8()?;

        let offset_size = (byte >> 4) & 0x0F;
        let length_size = byte & 0x0F;
        let base_offset_size = (byte2 >> 4) & 0x0F;
        let index_size = if ver >= 1 { byte2 & 0x0F } else { 0 };

        for &sz in &[offset_size, length_size, base_offset_size, index_size] {
            if !matches!(sz, 0 | 1 | 2 | 4 | 8) {
                return Err(AvifError::InvalidBox);
            }
        }

        let raw_item_count: u32 = if ver < 2 {
            r.read_u16_be()? as u32
        } else {
            r.read_u32_be()?
        };

        if settings.max_iloc_items != 0 && raw_item_count > settings.max_iloc_items {
            return Err(AvifError::LimitExceeded);
        }

        for _ in 0..raw_item_count {
            let item_id: u16 = if ver < 2 {
                r.read_u16_be()?
            } else {
                let v = r.read_u32_be()?;
                u16::try_from(v).map_err(|_| AvifError::InvalidBox)?
            };

            // construction_method (version 1/2 only).
            if ver >= 1 {
                let cm = r.read_u16_be()?;
                // Method 0 = file offset; 1 = idat; 2 = item.
                // Only method 0 is supported; skip and consume the rest of
                // this entry for non-zero methods.
                if cm & 0x0F != 0 {
                    let _ = r.read_u16_be()?; // data_reference_index
                    let _ = read_sized_int(&mut r, base_offset_size)?; // base_offset
                    let ec = r.read_u16_be()?;
                    if settings.max_extents_per_item != 0 && ec > settings.max_extents_per_item {
                        return Err(AvifError::LimitExceeded);
                    }
                    for _ in 0..ec {
                        if index_size > 0 {
                            let _ = read_sized_int(&mut r, index_size)?;
                        }
                        let _ = read_sized_int(&mut r, offset_size)?;
                        let _ = read_sized_int(&mut r, length_size)?;
                    }
                    continue;
                }
            }

            let _ = r.read_u16_be()?; // data_reference_index
            let base_offset = read_sized_int(&mut r, base_offset_size)?;
            let extent_count = r.read_u16_be()?;

            if settings.max_extents_per_item != 0 && extent_count > settings.max_extents_per_item {
                return Err(AvifError::LimitExceeded);
            }

            let mut extents: Vec<(u64, u64)> = Vec::new();
            extents
                .try_reserve_exact(extent_count as usize)
                .map_err(|_| AvifError::OutOfMemory)?;

            for _ in 0..extent_count {
                if index_size > 0 {
                    let _ = read_sized_int(&mut r, index_size)?;
                }
                let ext_offset = read_sized_int(&mut r, offset_size)?;
                let ext_length = read_sized_int(&mut r, length_size)?;

                let abs_offset = base_offset
                    .checked_add(ext_offset)
                    .ok_or(AvifError::InvalidBox)?;

                let end = abs_offset
                    .checked_add(ext_length)
                    .ok_or(AvifError::InvalidBox)?;

                if end > file_len {
                    return Err(AvifError::ExtentOutOfBounds);
                }

                extents.push((abs_offset, ext_length));
            }

            if let Some(item) = items.get_mut(&item_id) {
                item.iloc_extents = extents;
            }
        }
        Ok(())
    }

    fn apply_ipma(
        ipma_bytes: &[u8],
        ipco_props: &[([u8; 4], Vec<u8>)],
        items: &mut HashMap<u16, AvifItem>,
        settings: &AvifSettings,
    ) -> Result<()> {
        let mut r = Reader::new(ipma_bytes);
        let (ver, flags) = read_fullbox_header(&mut r)?;

        let entry_count = r.read_u32_be()?;

        if settings.max_ipma_entries != 0 && entry_count > settings.max_ipma_entries {
            return Err(AvifError::LimitExceeded);
        }

        for _ in 0..entry_count {
            let item_id: u16 = if ver < 1 {
                r.read_u16_be()?
            } else {
                let v = r.read_u32_be()?;
                // v1 ipma uses u32 item IDs; cap to u16 since that's our
                // internal representation.  IDs > 65535 are skipped below.
                u16::try_from(v).unwrap_or(0)
            };

            let assoc_count = r.read_u8()?;
            // assoc_count is u8, so at most 255 iterations — no cap needed.
            for _ in 0..assoc_count {
                // Bit 15 (or 7): essential flag; remaining bits: 1-based property index.
                let prop_idx: u16 = if flags & 1 != 0 {
                    let word = r.read_u16_be()?;
                    word & 0x7FFF
                } else {
                    let byte = r.read_u8()?;
                    (byte & 0x7F) as u16
                };

                if prop_idx == 0 {
                    continue; // 0 = no property, per spec
                }

                let idx = (prop_idx as usize).wrapping_sub(1); // convert to 0-based

                if idx >= ipco_props.len() {
                    continue;
                }

                let (fourcc, raw_prop) = &ipco_props[idx];
                if let Some(item) = items.get_mut(&item_id) {
                    Self::apply_property(item, fourcc, raw_prop, settings)?;
                }
            }
        }
        Ok(())
    }

    fn apply_property(
        item: &mut AvifItem,
        fourcc: &[u8; 4],
        raw: &[u8],
        settings: &AvifSettings,
    ) -> Result<()> {
        match fourcc {
            b"ispe" => {
                // ispe: FullBox (version=0, flags=0), image_width u32, image_height u32.
                // Total: 4 (fullbox) + 4 (width) + 4 (height) = 12 bytes minimum.
                if raw.len() < 12 {
                    return Err(AvifError::InvalidBox);
                }
                // raw[0..4] = version(1) + flags(3); raw[4..8] = width; raw[8..12] = height.
                let width = u32::from_be_bytes([raw[4], raw[5], raw[6], raw[7]]);
                let height = u32::from_be_bytes([raw[8], raw[9], raw[10], raw[11]]);

                if width == 0 || height == 0 {
                    return Err(AvifError::InvalidBox);
                }
                if settings.max_image_dimension != 0
                    && (width > settings.max_image_dimension
                        || height > settings.max_image_dimension)
                {
                    return Err(AvifError::LimitExceeded);
                }

                item.extents = Some(SpatialExtents { width, height });
            }

            b"av2C" | b"av1C" => {
                // av2C/av1C layout (AOM HEIF-AV1/AV2 spec §2.2.1):
                //   byte 0: marker(1=MSB) | version(7)
                //   byte 1: seq_profile(3) | seq_level_idx_0(5)
                //   byte 2: seq_tier_0(1) | high_bitdepth(1) | twelve_bit(1) | monochrome(1)
                //           | chroma_subsampling_x(1) | chroma_subsampling_y(1)
                //           | chroma_sample_position(2)
                //   byte 3: reserved(3) | initial_presentation_delay_present(1) | ...
                //   bytes 4+ (or 5+ if ipd_present): configOBUs
                if raw.len() < 4 {
                    return Err(AvifError::InvalidCodecConfig);
                }

                if raw[0] & 0x80 == 0 {
                    return Err(AvifError::InvalidCodecConfig);
                }

                let b2 = raw[2];
                let b3 = raw[3];

                let high_bitdepth = (b2 >> 6) & 1 != 0;
                let twelve_bit = (b2 >> 5) & 1 != 0;
                let monochrome = (b2 >> 4) & 1 != 0;
                let chroma_subsampling_x = (b2 >> 3) & 1;
                let chroma_subsampling_y = (b2 >> 2) & 1;
                let chroma_sample_position = b2 & 0x3;

                let ipd_present = (b3 >> 4) & 1 != 0;
                let config_obu_start = if ipd_present { 5 } else { 4 };

                let config_obus = raw.get(config_obu_start..).unwrap_or(&[]).to_vec();

                item.codec_config = Some(CodecConfig {
                    config_obus,
                    high_bitdepth,
                    twelve_bit,
                    monochrome,
                    chroma_subsampling_x,
                    chroma_subsampling_y,
                    chroma_sample_position,
                });
            }

            b"colr" => {
                // colr: color_type (4 bytes) + type-specific payload.
                // Three subtypes are defined:
                //   "nclx" — CICP color info (primaries, TRC, matrix, range)
                //   "rICC" — restricted ICC profile (ICC.1 v2/v4, embedded)
                //   "prof" — unrestricted ICC profile (ICC.1 v4, embedded)
                if raw.len() < 4 {
                    return Err(AvifError::InvalidBox);
                }
                match &raw[..4] {
                    b"nclx" => {
                        // nclx: color_type(4) + primaries(2) + trc(2) + matrix(2)
                        //       + full_range(1) = 11 bytes minimum.
                        if raw.len() < 11 {
                            return Err(AvifError::InvalidBox);
                        }
                        let cp = u16::from_be_bytes([raw[4], raw[5]]);
                        let tc = u16::from_be_bytes([raw[6], raw[7]]);
                        let mc = u16::from_be_bytes([raw[8], raw[9]]);
                        let full_range = (raw[10] >> 7) & 1;
                        item.color_info = Some(ColorInfo {
                            color_primaries: ColorPrimaries::from(cp),
                            transfer_characteristics: TransferCharacteristics::from(tc),
                            matrix_coefficients: MatrixCoefficients::from(mc),
                            full_range: full_range != 0,
                        });
                    }
                    b"rICC" | b"prof" => {
                        // rICC / prof: color_type(4) + raw ICC profile bytes.
                        // We need at least the 4-byte type code; an empty profile
                        // is technically invalid but we tolerate it gracefully.
                        let profile_bytes = raw[4..].to_vec();
                        item.icc_profile = Some(profile_bytes);
                    }
                    _ => {
                        // Unknown color type — ignore silently per spec §12.
                    }
                }
            }

            b"pasp" => {
                // pasp: hSpacing(4) + vSpacing(4) = 8 bytes.
                if raw.len() < 8 {
                    return Err(AvifError::InvalidBox);
                }
                let h = u32::from_be_bytes([raw[0], raw[1], raw[2], raw[3]]);
                let v = u32::from_be_bytes([raw[4], raw[5], raw[6], raw[7]]);

                if h == 0 || v == 0 {
                    return Err(AvifError::InvalidBox);
                }

                item.pixel_aspect_ratio = Some(PixelAspectRatio {
                    h_spacing: h,
                    v_spacing: v,
                });
            }

            b"auxC" => {
                // auxC: FullBox (4 bytes) + null-terminated UTF-8 URN string.
                // Used to identify alpha, depth, and other auxiliary item types.
                // Minimum: 4 (fullbox) + 1 (at least one URN byte or NUL) = 5 bytes.
                if raw.len() < 5 {
                    return Err(AvifError::InvalidBox);
                }
                // raw[0..4] = version(1) + flags(3); rest is the URN.
                let urn_bytes = &raw[4..];

                if settings.max_auxc_urn_len != 0 && urn_bytes.len() > settings.max_auxc_urn_len {
                    return Err(AvifError::LimitExceeded);
                }

                item.auxiliary_type = Some(AuxiliaryType::from_urn_bytes(urn_bytes));
            }

            b"irot" => {
                // irot: a single byte.  Bits [1:0] encode the anticlockwise
                // rotation in 90° steps: 0=0°, 1=90°, 2=180°, 3=270°.
                // No FullBox header — the payload is exactly 1 byte.
                if raw.is_empty() {
                    return Err(AvifError::InvalidBox);
                }
                let steps = raw[0] & 0x03;
                // Preserve any previously-parsed imir axis on this item.
                let axis = item.orientation.and_then(|o| o.imir_axis());
                item.orientation = Some(Orientation::from_irot_imir(steps, axis));
            }

            b"imir" => {
                // imir: a single byte.  Bit 0 is the axis:
                //   0 = vertical axis   → left-right (horizontal) flip
                //   1 = horizontal axis → top-bottom (vertical) flip
                // No FullBox header.
                if raw.is_empty() {
                    return Err(AvifError::InvalidBox);
                }
                // false = vertical axis (left-right flip), true = horizontal axis.
                let axis = Some(raw[0] & 0x01 != 0);
                // Preserve any previously-parsed irot steps on this item.
                let steps = item.orientation.map_or(0, |o| o.irot_steps());
                item.orientation = Some(Orientation::from_irot_imir(steps, axis));
            }

            b"clap" => {
                // clap: 8 × u32 big-endian = 4 rational pairs.
                // Layout: cleanApertureWidthN, cleanApertureWidthD,
                //         cleanApertureHeightN, cleanApertureHeightD,
                //         horizOffN, horizOffD, vertOffN, vertOffD.
                // Total: 32 bytes.  No FullBox header.
                if raw.len() < 32 {
                    return Err(AvifError::InvalidBox);
                }
                let read_u32 = |off: usize| -> i32 {
                    i32::from_be_bytes([raw[off], raw[off + 1], raw[off + 2], raw[off + 3]])
                };
                let width_n = read_u32(0);
                let width_d = read_u32(4);
                let height_n = read_u32(8);
                let height_d = read_u32(12);
                let hoff_n = read_u32(16);
                let hoff_d = read_u32(20);
                let voff_n = read_u32(24);
                let voff_d = read_u32(28);

                if width_d == 0 || height_d == 0 || hoff_d == 0 || voff_d == 0 {
                    return Err(AvifError::InvalidBox);
                }
                if width_n <= 0 || height_n <= 0 {
                    return Err(AvifError::InvalidBox);
                }

                item.clean_aperture = Some(CleanAperture {
                    width: Rational {
                        numerator: width_n,
                        denominator: width_d,
                    },
                    height: Rational {
                        numerator: height_n,
                        denominator: height_d,
                    },
                    horiz_off: Rational {
                        numerator: hoff_n,
                        denominator: hoff_d,
                    },
                    vert_off: Rational {
                        numerator: voff_n,
                        denominator: voff_d,
                    },
                });
            }

            // All other property types are ignored.
            _ => {}
        }
        Ok(())
    }

    /// Walk an `iref` box and return the item ID of the alpha auxiliary image
    /// associated with `primary_item_id`, if any.
    ///
    /// ## How AVIF alpha references work
    ///
    /// The `iref` box contains one or more typed reference sub-boxes.  Each
    /// sub-box has a four-character reference type (`auxl`, `thmb`, `dimg`, …)
    /// and a list of `(from_item_id, to_item_id…)` pairs.  For alpha:
    ///
    /// ```text
    /// iref
    ///   auxl  from_item=<alpha_item_id>  to_item=<primary_item_id>
    ///   prem  from_item=<primary_item_id> to_item=<alpha_item_id>   (optional)
    /// ```
    ///
    /// So the alpha item *points at* the primary item, not the other way round.
    /// We scan every `auxl` entry; when `to_item_id == primary_item_id` and the
    /// `from_item` has an `AuxiliaryType::Alpha` property (set by `auxC` via
    /// `ipma`), we record it.  We also scan `prem` to set
    /// `premultiplied_alpha` on the alpha item.
    ///
    /// ## Fuzzer hardening
    ///
    /// - `iref` is a FullBox; version drives the item-ID field width (0 → u16,
    ///   1 → u32, down-cast to u16).
    /// - The reference count inside each typed-reference sub-box is u16 (up to
    ///   65535 per spec); we cap it with [`AvifSettings::max_iref_refs`].
    /// - Unknown reference types are skipped by consuming their payload via the
    ///   sub-reader (already bounded by box size).
    fn resolve_alpha_from_iref(
        iref_bytes: &[u8],
        primary_item_id: u16,
        items: &mut HashMap<u16, AvifItem>,
        settings: &AvifSettings,
    ) -> Result<Option<u16>> {
        let mut r = Reader::new(iref_bytes);
        let (ver, _flags) = read_fullbox_header(&mut r)?;

        let mut alpha_item_id: Option<u16> = None;

        while !r.is_empty() {
            let (hdr, mut payload) = match read_box_header(&mut r) {
                Ok(v) => v,
                Err(AvifError::TooShort) => break,
                Err(e) => return Err(e),
            };

            let ref_type = &hdr.fourcc;
            let is_auxl = ref_type == b"auxl";
            let is_prem = ref_type == b"prem";

            if !is_auxl && !is_prem {
                // Skip unknown reference types; their bytes are already
                // consumed by the sub-reader.
                continue;
            }

            // from_item_id: the item that *has* the reference.
            let from_item_id: u16 = if ver == 0 {
                payload.read_u16_be()?
            } else {
                let v = payload.read_u32_be()?;
                u16::try_from(v).unwrap_or(0)
            };

            // reference_count: number of target item IDs that follow.
            let ref_count = payload.read_u16_be()?;

            if settings.max_iref_refs != 0 && ref_count > settings.max_iref_refs {
                return Err(AvifError::LimitExceeded);
            }

            for _ in 0..ref_count {
                let to_item_id: u16 = if ver == 0 {
                    payload.read_u16_be()?
                } else {
                    let v = payload.read_u32_be()?;
                    u16::try_from(v).unwrap_or(0)
                };

                if is_auxl && to_item_id == primary_item_id {
                    // `from_item_id` claims to be an auxiliary image for the
                    // primary.  Accept it as alpha only if its auxC property
                    // (set during ipma application) identifies it as alpha.
                    if let Some(item) = items.get(&from_item_id) {
                        if item.auxiliary_type.as_ref().is_some_and(|a| a.is_alpha()) {
                            // Last writer wins if multiple alpha items declare
                            // themselves; real encoders only emit one.
                            alpha_item_id = Some(from_item_id);
                        }
                    }
                }

                if is_prem && from_item_id == primary_item_id {
                    // `to_item_id` is the alpha item whose channel data
                    // the primary is premultiplied against.
                    if let Some(item) = items.get_mut(&to_item_id) {
                        item.premultiplied_alpha = true;
                    }
                }
            }
        }

        Ok(alpha_item_id)
    }
}

/// Read an unsigned integer of `size` bytes from `r`.
///
/// Only sizes 0, 1, 2, 4, and 8 are accepted; other values are rejected.
/// (The caller in `apply_iloc` already validates the sizes from the bitstream,
/// so this function is a second defensive layer.)
fn read_sized_int(r: &mut Reader<'_>, size: u8) -> Result<u64> {
    match size {
        0 => Ok(0),
        1 => Ok(r.read_u8()? as u64),
        2 => Ok(r.read_u16_be()? as u64),
        4 => Ok(r.read_u32_be()? as u64),
        8 => Ok(r.read_u64_be()?),
        _ => Err(AvifError::InvalidBox),
    }
}

#[inline]
fn is_avif_brand(b: &[u8; 4]) -> bool {
    matches!(b, b"avif" | b"avis" | b"av02")
}

/// Decoded alpha (transparency) plane from the auxiliary alpha item.
///
/// Presence as `Some` on [`AvifImage::alpha`] is the authoritative signal
/// that the image has an alpha channel; callers never need to check length.
/// Dimensions always equal the primary image dimensions.
#[derive(Debug, Clone)]
pub struct AlphaPlane {
    /// Raw plane bytes, row-major.  For 8-bit alpha each sample is one byte;
    /// for 10/12-bit alpha each sample is two bytes in little-endian order
    /// (matching the layout used by the AV2 decoder for HBD planes).
    pub data: Vec<u8>,
    /// Row stride in bytes.
    pub stride: usize,
    /// Bits per alpha sample (8, 10, or 12).
    pub bits_per_component: u8,
}

/// A fully decoded AVIF image: metadata + raw plane data.
pub struct AvifImage {
    /// Width in pixels.
    pub width: u32,
    /// Height in pixels.
    pub height: u32,
    /// Pixel layout (I400, I420, I422, I444).
    pub pixel_layout: PixelLayout,
    /// Bits per component (8, 10, or 12).
    pub bits_per_component: u8,
    /// color information, if present in the container.
    pub color_info: Option<ColorInfo>,
    /// Content light level metadata from HDR CLL metadata OBUs, if present.
    pub content_light_level: Option<ContentLightLevel>,
    /// Pixel-aspect-ratio, if present.
    pub pixel_aspect_ratio: Option<PixelAspectRatio>,
    /// Luma (Y) plane bytes (row-major, stride = width * bytes_per_sample).
    pub planes: [Vec<u8>; 3],
    /// Row stride for each plane, in bytes.
    pub strides: [usize; 2],
    /// Alpha (transparency) plane, or `None` if the image has no alpha channel.
    ///
    /// When `Some`, the plane dimensions always equal `width × height` and the
    /// sample layout matches the primary image's bit depth convention (8-bit →
    /// one byte per sample; 10/12-bit → two LE bytes per sample).
    pub alpha: Option<AlphaPlane>,
    /// Whether the color planes in `planes` are premultiplied by `alpha`.
    /// Consumers must un-premultiply before compositing if this is `true`.
    pub premultiplied_alpha: bool,
    /// Display orientation from `irot` / `imir`; [`Orientation::Normal`]
    /// when neither property is present.
    pub orientation: Orientation,
    /// Visible crop rectangle from the `clap` property.
    /// `None` means the property was absent and the full coded frame is visible.
    pub clean_aperture: Option<CleanAperture>,
    /// ICC profile, if the `colr` box carried `rICC` or `prof` data.
    pub icc_profile: Option<Vec<u8>>,
    /// The raw underlying [`Picture`] from the AV2 decoder.
    pub picture: Arc<Picture>,
}

impl fmt::Debug for AvifImage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AvifImage")
            .field("width", &self.width)
            .field("height", &self.height)
            .field("pixel_layout", &self.pixel_layout)
            .field("bits_per_component", &self.bits_per_component)
            .field("color_info", &self.color_info)
            .field("content_light_level", &self.content_light_level)
            .field("has_alpha", &self.alpha.is_some())
            .field("premultiplied_alpha", &self.premultiplied_alpha)
            .field("orientation", &self.orientation)
            .field("clean_aperture", &self.clean_aperture)
            .field("has_icc_profile", &self.icc_profile.is_some())
            .finish_non_exhaustive()
    }
}

/// High-level AVIF / AV2 image decoder.
///
/// Wraps [`AvifParser`] (container parsing) and [`Decoder`] (AV2 bitstream
/// decoding) into a single convenient API.
pub struct AvifDecoder<'a> {
    file: &'a [u8],
    container: AvifContainer,
    settings: AvifSettings,
}

impl<'a> AvifDecoder<'a> {
    /// Parse the AVIF container from `data` (the raw file bytes).
    ///
    /// This is a fast metadata-only pass; no pixel decoding happens here.
    pub fn new(data: &'a [u8]) -> Result<Self> {
        Self::with_settings(data, AvifSettings::default())
    }

    /// Parse the AVIF container from `data` with explicit parser and decoder
    /// settings.
    ///
    /// This is a fast metadata-only pass; no pixel decoding happens here.
    pub fn with_settings(data: &'a [u8], settings: AvifSettings) -> Result<Self> {
        let container = AvifParser::parse_with_settings(data, &settings)?;
        Ok(Self {
            file: data,
            container,
            settings,
        })
    }

    /// Return image metadata for the primary item without decoding pixels.
    pub fn image_info(&self) -> Result<AvifImageInfo> {
        let item = self
            .container
            .primary_item()
            .ok_or(AvifError::MissingBox("pitm"))?;

        let extents = item.extents.ok_or(AvifError::MissingBox("ispe"))?;
        let cfg = item
            .codec_config
            .as_ref()
            .ok_or(AvifError::MissingBox("av2C"))?;

        let has_alpha = self.container.alpha_item_id.is_some();
        let premultiplied_alpha = self
            .container
            .alpha_item()
            .is_some_and(|a| a.premultiplied_alpha);

        Ok(AvifImageInfo {
            width: extents.width,
            height: extents.height,
            pixel_layout: cfg.pixel_layout(),
            bits_per_component: cfg.bits_per_component(),
            color_info: item.color_info,
            content_light_level: self.item_content_light_level(item)?,
            pixel_aspect_ratio: item.pixel_aspect_ratio,
            item_type: item.item_type,
            has_alpha,
            premultiplied_alpha,
            orientation: item.orientation,
            clean_aperture: item.clean_aperture,
            icc_profile: item.icc_profile.clone(),
        })
    }

    /// Decode the primary image item and return its pixel data.
    ///
    /// The caller receives an [`AvifImage`] whose `planes` vectors contain
    /// copies of the plane data (safe to use without lifetime concerns).
    ///
    /// When the container carries a separate alpha auxiliary item (linked via
    /// an `auxl` `iref` reference with an `auxC` property set to the alpha
    /// URN), it is decoded with a second AV2 decoder pass and its luma plane
    /// is returned in [`AvifImage::alpha`].  The dimensions of the alpha plane
    /// are validated to match the primary image; a mismatch is a hard error.
    pub fn decode(&mut self) -> Result<AvifImage> {
        let item = self
            .container
            .primary_item()
            .ok_or(AvifError::MissingBox("pitm"))?;

        if item.item_type == ItemType::Grid {
            return Err(AvifError::GridNotSupported);
        }
        if !item.item_type.is_image() {
            return Err(AvifError::UnsupportedCodec);
        }

        let extents = item.extents.ok_or(AvifError::MissingBox("ispe"))?;
        let cfg = item
            .codec_config
            .as_ref()
            .ok_or(AvifError::MissingBox("av2C"))?;
        let alpha_item_id = self.container.alpha_item_id;
        let premultiplied_alpha = alpha_item_id
            .and_then(|id| self.container.items.get(&id))
            .is_some_and(|a| a.premultiplied_alpha);

        let primary_obu = self.assemble_obu(item)?;
        if primary_obu.is_empty() {
            return Err(AvifError::MissingBox("mdat"));
        }

        let picture = self.run_decoder(primary_obu)?;

        let layout = picture.p.layout;
        let bpc = u8::try_from(picture.p.bpc).map_err(|_| AvifError::InvalidCodecConfig)?;
        let bps: usize = picture.bytes_per_sample();

        if cfg.pixel_layout() != layout || cfg.bits_per_component() != bpc {
            return Err(AvifError::InvalidCodecConfig);
        }

        let pic_w = picture.p.w as usize;
        let pic_h = picture.p.h as usize;

        let y_stride = picture.stride[0].unsigned_abs();
        let uv_stride = picture.stride[1].unsigned_abs();

        let uv_h = match layout {
            PixelLayout::I420 => pic_h.div_ceil(2),
            PixelLayout::I400 => 0,
            _ => pic_h,
        };
        let uv_w = match layout {
            PixelLayout::I420 | PixelLayout::I422 => pic_w.div_ceil(2),
            PixelLayout::I400 => 0,
            _ => pic_w,
        };

        let plane_y = copy_plane(picture.plane_bytes(0), y_stride, pic_h, pic_w, bps)?;
        let plane_u = copy_plane(picture.plane_bytes(1), uv_stride, uv_h, uv_w, bps)?;
        let plane_v = copy_plane(picture.plane_bytes(2), uv_stride, uv_h, uv_w, bps)?;

        // AvifImage::strides are byte strides for the compact output buffers.
        // For high-bit-depth output this must be width * 2, not width.
        let out_y_stride = pic_w.checked_mul(bps).ok_or(AvifError::InvalidBox)?;
        let out_uv_stride = uv_w.checked_mul(bps).ok_or(AvifError::InvalidBox)?;

        let content_light_level = picture.content_light_level;
        let picture_arc = Arc::new(picture);

        let alpha: Option<AlphaPlane> = if let Some(aid) = alpha_item_id {
            let alpha_item = self
                .container
                .items
                .get(&aid)
                .ok_or(AvifError::MissingBox("alpha item"))?;

            if !alpha_item.item_type.is_image() {
                return Err(AvifError::UnsupportedCodec);
            }

            let alpha_extents = alpha_item
                .extents
                .ok_or(AvifError::MissingBox("alpha ispe"))?;

            // Validate alpha dimensions match the primary image.
            // The AVIF spec (§4.8.2) requires them to be identical.
            if alpha_extents.width != extents.width || alpha_extents.height != extents.height {
                return Err(AvifError::InvalidBox);
            }

            let alpha_cfg = alpha_item
                .codec_config
                .as_ref()
                .ok_or(AvifError::MissingBox("alpha av2C"))?;

            let alpha_obu = self.assemble_obu(alpha_item)?;
            if alpha_obu.is_empty() {
                return Err(AvifError::MissingBox("alpha mdat"));
            }

            let alpha_picture = self.run_decoder(alpha_obu)?;

            // Validate decoded dimensions against the declared extents.
            if alpha_picture.p.w as u32 != extents.width
                || alpha_picture.p.h as u32 != extents.height
            {
                return Err(AvifError::InvalidBox);
            }

            let alpha_bpc =
                u8::try_from(alpha_picture.p.bpc).map_err(|_| AvifError::InvalidCodecConfig)?;
            let alpha_bps = alpha_picture.bytes_per_sample();
            if alpha_cfg.pixel_layout() != PixelLayout::I400
                || alpha_picture.p.layout != PixelLayout::I400
                || alpha_cfg.bits_per_component() != alpha_bpc
            {
                return Err(AvifError::InvalidCodecConfig);
            }

            let a_w = alpha_picture.p.w as usize;
            let a_h = alpha_picture.p.h as usize;
            let a_stride = alpha_picture.stride[0].unsigned_abs();

            // Alpha is encoded as a monochrome (I400) AV2 stream; the
            // transparency values live in the luma plane.
            let data = copy_plane(alpha_picture.plane_bytes(0), a_stride, a_h, a_w, alpha_bps)?;
            let alpha_out_stride = a_w.checked_mul(alpha_bps).ok_or(AvifError::InvalidBox)?;

            Some(AlphaPlane {
                data,
                stride: alpha_out_stride,
                bits_per_component: alpha_bpc,
            })
        } else {
            None
        };

        Ok(AvifImage {
            width: extents.width,
            height: extents.height,
            pixel_layout: layout,
            bits_per_component: bpc,
            color_info: self.container.primary_item().and_then(|i| i.color_info),
            content_light_level,
            pixel_aspect_ratio: self
                .container
                .primary_item()
                .and_then(|i| i.pixel_aspect_ratio),
            planes: [plane_y, plane_u, plane_v],
            strides: [out_y_stride, out_uv_stride],
            alpha,
            premultiplied_alpha,
            orientation: self
                .container
                .primary_item()
                .and_then(|i| i.orientation)
                .unwrap_or(Orientation::Normal),
            clean_aperture: self.container.primary_item().and_then(|i| i.clean_aperture),
            icc_profile: self
                .container
                .primary_item()
                .and_then(|i| i.icc_profile.clone()),
            picture: picture_arc,
        })
    }

    /// Return content light level metadata for the primary item, if present.
    ///
    /// This scans the AV2 OBU stream for HDR CLL metadata without decoding
    /// pixels. The same value is also copied to [`AvifImage::content_light_level`]
    /// after [`decode`](Self::decode).
    pub fn content_light_level(&self) -> Result<Option<ContentLightLevel>> {
        let item = self
            .container
            .primary_item()
            .ok_or(AvifError::MissingBox("pitm"))?;
        self.item_content_light_level(item)
    }

    /// Return a reference to the parsed container metadata.
    pub fn container(&self) -> &AvifContainer {
        &self.container
    }

    /// Return the parser/decoder settings used by this decoder.
    pub fn settings(&self) -> &AvifSettings {
        &self.settings
    }

    fn item_content_light_level(&self, item: &AvifItem) -> Result<Option<ContentLightLevel>> {
        let obu_data = self.assemble_obu(item)?;
        scan_content_light_level_from_obus(&obu_data)
    }

    /// Assemble a contiguous OBU byte stream from an item's `configOBUs` and
    /// `iloc` extents, subject to [`AvifSettings::max_obu_bytes`].
    fn assemble_obu(&self, item: &AvifItem) -> Result<Vec<u8>> {
        let cfg = item
            .codec_config
            .as_ref()
            .ok_or(AvifError::MissingBox("av2C"))?;

        let config_len = cfg.config_obus.len();
        let sample_len: u64 = item
            .iloc_extents
            .iter()
            .try_fold(0u64, |acc, &(_, len)| acc.checked_add(len))
            .ok_or(AvifError::InvalidBox)?;

        let total_obu_len: u64 = (config_len as u64)
            .checked_add(sample_len)
            .ok_or(AvifError::InvalidBox)?;

        if self.settings.max_obu_bytes != 0 && total_obu_len > self.settings.max_obu_bytes as u64 {
            return Err(AvifError::LimitExceeded);
        }

        let mut obu_data: Vec<u8> = Vec::new();
        obu_data
            .try_reserve_exact(total_obu_len as usize)
            .map_err(|_| AvifError::OutOfMemory)?;
        if !cfg.config_obus.is_empty() {
            obu_data.extend_from_slice(&cfg.config_obus);
        }
        for &(abs_offset, ext_len) in &item.iloc_extents {
            let start = usize::try_from(abs_offset).map_err(|_| AvifError::ExtentOutOfBounds)?;
            let len = usize::try_from(ext_len).map_err(|_| AvifError::ExtentOutOfBounds)?;
            let end = start.checked_add(len).ok_or(AvifError::ExtentOutOfBounds)?;

            let slice = self
                .file
                .get(start..end)
                .ok_or(AvifError::ExtentOutOfBounds)?;
            obu_data.extend_from_slice(slice);
        }
        Ok(obu_data)
    }

    /// Open an AV2 decoder, feed it `obu_data`, drain one [`Picture`].
    fn run_decoder(&self, obu_data: Vec<u8>) -> Result<Picture> {
        let mut settings = self.settings.decoder_settings.clone();
        settings.run_decode = true;

        let mut decoder = Decoder::open(&settings).map_err(AvifError::DecodeError)?;
        decoder
            .send_data(Some(Data::wrap(obu_data)))
            .map_err(AvifError::DecodeError)?;

        // Signal end-of-stream, then drain one picture.
        let _ = decoder.send_data(None);

        match decoder.get_picture() {
            Ok(p) => Ok(p),
            Err(TealdustError::Again) => {
                let _ = decoder.send_data(None);
                match decoder.get_picture() {
                    Ok(p) => Ok(p),
                    Err(e) => Err(AvifError::DecodeError(e)),
                }
            }
            Err(e) => Err(AvifError::DecodeError(e)),
        }
    }
}

fn scan_content_light_level_from_obus(obu_data: &[u8]) -> Result<Option<ContentLightLevel>> {
    let mut pos = 0usize;
    let mut content_light_level = None;

    while pos < obu_data.len() {
        let (obu_len, leb_len) = read_leb128_usize(&obu_data[pos..])?;
        pos = pos.checked_add(leb_len).ok_or(AvifError::InvalidBox)?;

        let end = pos.checked_add(obu_len).ok_or(AvifError::InvalidBox)?;
        let body = obu_data.get(pos..end).ok_or(AvifError::InvalidBox)?;
        pos = end;

        if body.is_empty() {
            continue;
        }

        let mut gb = GetBits::new(body);
        let has_extension = gb.get_bit() != 0;
        let obu_type_raw = gb.get_bits(5);
        let _temporal_layer_id = gb.get_bits(2);

        if has_extension {
            let _multilayer_id = gb.get_bits(3);
            let _extension_layer_id = gb.get_bits(5);
        }

        if gb.has_error() {
            return Err(AvifError::InvalidCodecConfig);
        }

        if obu_type_raw == ObuType::Metadata as u32 {
            let meta_type = gb.get_uleb128();
            if gb.has_error() {
                return Err(AvifError::InvalidCodecConfig);
            }

            if meta_type == ObuMetaType::HdrCll as u32 {
                let cll = crate::obu::parse_cll(&mut gb);
                if gb.has_error() {
                    return Err(AvifError::InvalidCodecConfig);
                }
                content_light_level = Some(cll);
            }
        }
    }

    Ok(content_light_level)
}

fn read_leb128_usize(data: &[u8]) -> Result<(usize, usize)> {
    let mut value: u64 = 0;
    let mut shift = 0u32;

    for (i, &byte) in data.iter().take(8).enumerate() {
        value |= ((byte & 0x7f) as u64) << shift;
        if byte & 0x80 == 0 {
            let len = i + 1;
            let value = usize::try_from(value).map_err(|_| AvifError::LimitExceeded)?;
            return Ok((value, len));
        }
        shift += 7;
    }

    Err(AvifError::InvalidBox)
}

fn copy_plane(
    plane: Option<&[u8]>,
    stride: usize,
    h: usize,
    w: usize,
    bps: usize,
) -> Result<Vec<u8>> {
    let Some(plane) = plane else {
        return Ok(Vec::new());
    };
    if h == 0 || w == 0 {
        return Ok(Vec::new());
    }

    let w_bytes = w.checked_mul(bps).ok_or(AvifError::InvalidBox)?;
    let total = h.checked_mul(w_bytes).ok_or(AvifError::InvalidBox)?;

    let mut out = Vec::new();
    out.try_reserve_exact(total)
        .map_err(|_| AvifError::OutOfMemory)?;
    for row in 0..h {
        let row_offset = row.checked_mul(stride).ok_or(AvifError::InvalidBox)?;
        let row_end = row_offset
            .checked_add(w_bytes)
            .ok_or(AvifError::InvalidBox)?;
        let row_slice = plane
            .get(row_offset..row_end)
            .ok_or(AvifError::InvalidBox)?;
        out.extend_from_slice(row_slice);
    }
    Ok(out)
}

/// Quick probe: returns `true` if `data` begins with an ISOBMFF `ftyp` box
/// carrying a recognised AVIF brand (`avif`, `avis`, or `av02`).
///
/// This is intentionally lenient — it only inspects the first 12 bytes.
pub fn probe_avif(data: &[u8]) -> bool {
    if data.len() < 12 {
        return false;
    }
    // Expect ftyp as first box.
    if &data[4..8] != b"ftyp" {
        return false;
    }
    let brand = [data[8], data[9], data[10], data[11]];
    is_avif_brand(&brand)
}