mp4forge 0.8.0

Rust library and CLI for inspecting, probing, extracting, muxing, and rewriting MP4 structures
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
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
//! Feature-gated mux planning, real MP4 container assembly, and sample-reader helpers.
//!
//! The additive `mux` feature exposes two layers:
//! - low-level staged media-item planning plus payload-copy helpers
//! - higher-level real MP4 mux helpers that assemble `ftyp`, `moov`, and `mdat`
//!
//! Internally, both layers build on one mux event graph that carries stream descriptions, ordered
//! sample events, and boundary events. The task-level sample-reader helpers live under
//! [`crate::mux::sample_reader`], the public direct-ingest inspection and export helpers plus the
//! additive packet-focused report surface live under [`crate::mux::inspect`], the public
//! elementary sample rewrite helpers and elementary export helpers live under
//! [`crate::mux::rewrite`], and the real file-backed mux surface builds actual MP4 container
//! output on top of the same internal event flow.

use std::collections::BTreeMap;
use std::error::Error;
use std::fmt;
use std::fs::File;
use std::io::{self, BufWriter, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::str::FromStr;

#[cfg(feature = "async")]
use tokio::fs::File as TokioFile;
#[cfg(feature = "async")]
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};

use crate::FourCc;
#[cfg(feature = "async")]
use crate::async_io::{AsyncReadForward, AsyncReadSeek, AsyncWrite, AsyncWriteForward};
use crate::codec::CodecError;
use crate::header::HeaderError;
use crate::queue::{OrderedWorkQueue, QueueWorkItem};
use crate::writer::WriterError;

mod coordination;
mod demux;
pub(crate) mod event;
mod import;
/// Feature-gated direct-ingest inspection and export helpers built on native mux parsing.
#[cfg_attr(docsrs, doc(cfg(feature = "mux")))]
pub mod inspect;
mod mp4;
/// Feature-gated elementary sample rewrite helpers built on landed mux codec logic.
#[cfg_attr(docsrs, doc(cfg(feature = "mux")))]
pub mod rewrite;
/// Feature-gated planned sample-reader helpers built on mux plans.
#[cfg_attr(docsrs, doc(cfg(feature = "mux")))]
pub mod sample_reader;

use coordination::MuxCoordinationPlan;
pub(crate) use coordination::{
    MuxDurationBoundaryKind, TrackCoordinationDirective, build_capped_duration_chunk_sample_counts,
    build_duration_chunk_sample_counts, build_duration_chunk_sample_counts_with_start_time,
    build_fragmented_duration_chunk_sample_counts_with_start_time,
    build_sync_aligned_fragmented_duration_chunk_sample_counts,
    build_sync_aligned_segment_chunk_sample_counts,
    rebalance_small_multi_audio_chunk_sample_counts,
};
pub(crate) use event::{MuxEventCursor, MuxEventGraph, MuxSampleEvent};
pub use import::mux_fragmented_to_paths;
#[cfg(feature = "async")]
pub use import::mux_fragmented_to_paths_async;
pub use import::mux_into_path;
#[cfg(feature = "async")]
pub use import::mux_into_path_async;
pub use import::mux_to_path;
#[cfg(feature = "async")]
pub use import::mux_to_path_async;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum MuxRawCodec {
    /// AV1 elementary input.
    Av1,
    /// MPEG-2 elementary video input.
    Mpeg2v,
    /// MPEG-4 Part 2 elementary input.
    Mp4v,
    /// H.263 elementary input.
    H263,
    /// H.264 or AVC elementary input.
    H264,
    /// H.265 or HEVC elementary input.
    H265,
    /// H.266 or VVC elementary input.
    Vvc,
    /// VP8 elementary input.
    Vp8,
    /// VP9 elementary input.
    Vp9,
    /// VP10 elementary input.
    Vp10,
    /// AAC input.
    Aac,
    /// AAC LATM input.
    Latm,
    /// MP3 input.
    Mp3,
    /// AC-3 input.
    Ac3,
    /// E-AC-3 input.
    Eac3,
    /// AC-4 input.
    Ac4,
    /// AMR narrowband input.
    Amr,
    /// AMR wideband input.
    AmrWb,
    /// QCP-wrapped voice input carrying QCELP, EVRC, or SMV frames.
    Qcp,
    /// JPEG still-image input.
    Jpeg,
    /// PNG still-image input.
    Png,
    /// BMP still-image input.
    Bmp,
    /// Raw ProRes input.
    Prores,
    /// Self-describing YUV4MPEG input.
    Y4m,
    /// JPEG 2000 image or codestream input.
    J2k,
    /// WAVE or PCM input.
    Pcm,
    /// DTS core input.
    Dts,
    /// Dolby TrueHD input.
    Truehd,
    /// ALAC input.
    Alac,
    /// FLAC input.
    Flac,
    /// IAMF elementary input.
    Iamf,
    /// MPEG-H AudioMux input.
    MpegH,
    /// Opus input.
    Opus,
    /// Vorbis input.
    Vorbis,
    /// Speex input.
    Speex,
    /// Theora input.
    Theora,
}

impl MuxRawCodec {
    pub const fn prefix(&self) -> &'static str {
        match self {
            Self::Av1 => "av1",
            Self::Mpeg2v => "mpeg2v",
            Self::Mp4v => "mp4v",
            Self::H263 => "h263",
            Self::H264 => "h264",
            Self::H265 => "h265",
            Self::Vvc => "vvc",
            Self::Vp8 => "vp8",
            Self::Vp9 => "vp9",
            Self::Vp10 => "vp10",
            Self::Aac => "aac",
            Self::Latm => "latm",
            Self::Mp3 => "mp3",
            Self::Ac3 => "ac3",
            Self::Eac3 => "ec3",
            Self::Ac4 => "ac4",
            Self::Amr => "amr",
            Self::AmrWb => "amr-wb",
            Self::Qcp => "qcp",
            Self::Jpeg => "jpeg",
            Self::Png => "png",
            Self::Bmp => "bmp",
            Self::Prores => "prores",
            Self::Y4m => "y4m",
            Self::J2k => "j2k",
            Self::Pcm => "pcm",
            Self::Dts => "dts",
            Self::Truehd => "truehd",
            Self::Alac => "alac",
            Self::Flac => "flac",
            Self::Iamf => "iamf",
            Self::MpegH => "mhas",
            Self::Opus => "opus",
            Self::Vorbis => "vorbis",
            Self::Speex => "speex",
            Self::Theora => "theora",
        }
    }
}

/// One MP4-side track selector accepted by widened `mux` track specs.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MuxMp4TrackSelector {
    /// Select the first video track from one MP4 source.
    Video,
    /// Select one audio track occurrence from one MP4 source.
    ///
    /// The occurrence index is one-based in the public surface, so `1` means the first audio
    /// track in file order and `2` means the second.
    Audio { occurrence: u32 },
    /// Select one text-track occurrence from one MP4 source.
    ///
    /// The occurrence index is one-based in the public surface.
    Text { occurrence: u32 },
    /// Select one specific track identifier from one MP4 source.
    TrackId { track_id: u32 },
}

/// One validated public track specification for the mux task surface.
///
/// The current path-first `mux` grammar uses one repeated track-spec model for both CLI and
/// library callers:
/// - path-only imports: `PATH`
/// - path plus selector: `PATH#video`, `PATH#audio`, `PATH#audio:N`, `PATH#text`,
///   `PATH#text:N`, `PATH#track:ID`
/// - explicit bare raw-video imports: `PATH#rawvideo:size=WIDTHxHEIGHT,spfmt=PIXFMT,fps=NUM/DEN`
///
/// The raw-video form is intentionally explicit. Unlike self-describing YUV4MPEG streams, bare
/// raw video needs out-of-band geometry, pixel-format, and frame-rate metadata before `mp4forge`
/// can author a truthful `uncv` sample entry.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MuxRawVideoPixelFormat {
    /// Planar 8-bit YUV 4:2:0.
    Yuv420p8,
    /// Planar 8-bit YVU 4:2:0.
    Yvu420p8,
    /// Planar 10-bit YUV 4:2:0 stored in 16-bit words.
    Yuv420p10,
    /// Planar 8-bit YUV 4:2:2.
    Yuv422p8,
    /// Planar 10-bit YUV 4:2:2 stored in 16-bit words.
    Yuv422p10,
    /// Planar 8-bit YUV 4:4:4.
    Yuv444p8,
    /// Planar 10-bit YUV 4:4:4 stored in 16-bit words.
    Yuv444p10,
    /// Planar 8-bit YUV 4:2:0 with alpha.
    Yuva420p8,
    /// Planar 8-bit YUV 4:2:0 with depth.
    Yuvd420p8,
    /// Planar 8-bit YUV 4:4:4 with alpha.
    Yuva444p8,
    /// Semi-planar 8-bit NV12.
    Nv12p8,
    /// Semi-planar 8-bit NV21.
    Nv21p8,
    /// Semi-planar 10-bit NV12 stored in 16-bit words.
    Nv12p10,
    /// Semi-planar 10-bit NV21 stored in 16-bit words.
    Nv21p10,
    /// Packed 8-bit UYVY 4:2:2.
    Uyvy422p8,
    /// Packed 8-bit VYUY 4:2:2.
    Vyuy422p8,
    /// Packed 8-bit YUYV 4:2:2.
    Yuyv422p8,
    /// Packed 8-bit YVYU 4:2:2.
    Yvyu422p8,
    /// Packed 10-bit UYVY 4:2:2 stored in 16-bit words.
    Uyvy422p10,
    /// Packed 10-bit VYUY 4:2:2 stored in 16-bit words.
    Vyuy422p10,
    /// Packed 10-bit YUYV 4:2:2 stored in 16-bit words.
    Yuyv422p10,
    /// Packed 10-bit YVYU 4:2:2 stored in 16-bit words.
    Yvyu422p10,
    /// Packed 8-bit YUV 4:4:4.
    Yuv444Packed8,
    /// Packed 8-bit VYU 4:4:4.
    Vyu444Packed8,
    /// Packed 8-bit YUV 4:4:4 with alpha.
    Yuva444Packed8,
    /// Packed 8-bit UYV 4:4:4 with alpha.
    Uyva444Packed8,
    /// Packed 10-bit UYV 4:4:4 little-endian.
    Yuv444Packed10,
    /// Packed 10-bit v210 4:2:2 little-endian.
    V210,
    /// 8-bit greyscale.
    Grey8,
    /// 8-bit alpha followed by 8-bit greyscale.
    AlphaGrey8,
    /// 8-bit greyscale followed by 8-bit alpha.
    GreyAlpha8,
    /// Packed RGB 3:3:2.
    Rgb332,
    /// Packed RGB 4:4:4 stored in 16 bits.
    Rgb444,
    /// Packed RGB 5:5:5 stored in 16 bits.
    Rgb555,
    /// Packed RGB 5:6:5 stored in 16 bits.
    Rgb565,
    /// Packed 24-bit RGB in byte order `R-G-B`.
    Rgb24,
    /// Packed 24-bit RGB in byte order `B-G-R`.
    Bgr24,
    /// Packed 32-bit RGB in byte order `R-G-B-X`.
    Rgbx32,
    /// Packed 32-bit RGB in byte order `B-G-R-X`.
    Bgrx32,
    /// Packed 32-bit RGB in byte order `X-R-G-B`.
    Xrgb32,
    /// Packed 32-bit RGB in byte order `X-B-G-R`.
    Xbgr32,
    /// Packed 32-bit RGBA in byte order `A-R-G-B`.
    Argb32,
    /// Packed 32-bit RGBA in byte order `R-G-B-A`.
    Rgba32,
    /// Packed 32-bit RGBA in byte order `B-G-R-A`.
    Bgra32,
    /// Packed 32-bit RGBA in byte order `A-B-G-R`.
    Abgr32,
    /// Packed 32-bit RGB with depth.
    Rgbd32,
    /// Packed 32-bit RGB with depth and bit-shape.
    Rgbds32,
}

impl MuxRawVideoPixelFormat {
    /// Returns the canonical raw-video pixel-format label.
    pub const fn canonical_name(self) -> &'static str {
        match self {
            Self::Yuv420p8 => "yuv420",
            Self::Yvu420p8 => "yvu420",
            Self::Yuv420p10 => "yuv420_10",
            Self::Yuv422p8 => "yuv422",
            Self::Yuv422p10 => "yuv422_10",
            Self::Yuv444p8 => "yuv444",
            Self::Yuv444p10 => "yuv444_10",
            Self::Yuva420p8 => "yuva",
            Self::Yuvd420p8 => "yuvd",
            Self::Yuva444p8 => "yuv444a",
            Self::Nv12p8 => "nv12",
            Self::Nv21p8 => "nv21",
            Self::Nv12p10 => "nv12_10",
            Self::Nv21p10 => "nv21_10",
            Self::Uyvy422p8 => "uyvy",
            Self::Vyuy422p8 => "vyuy",
            Self::Yuyv422p8 => "yuyv",
            Self::Yvyu422p8 => "yvyu",
            Self::Uyvy422p10 => "uyvl",
            Self::Vyuy422p10 => "vyul",
            Self::Yuyv422p10 => "yuyl",
            Self::Yvyu422p10 => "yvyl",
            Self::Yuv444Packed8 => "yuv444p",
            Self::Vyu444Packed8 => "v308",
            Self::Yuva444Packed8 => "yuv444ap",
            Self::Uyva444Packed8 => "v408",
            Self::Yuv444Packed10 => "v410",
            Self::V210 => "v210",
            Self::Grey8 => "grey",
            Self::AlphaGrey8 => "algr",
            Self::GreyAlpha8 => "gral",
            Self::Rgb332 => "rgb8",
            Self::Rgb444 => "rgb4",
            Self::Rgb555 => "rgb5",
            Self::Rgb565 => "rgb6",
            Self::Rgb24 => "rgb",
            Self::Bgr24 => "bgr",
            Self::Rgbx32 => "rgbx",
            Self::Bgrx32 => "bgrx",
            Self::Xrgb32 => "xrgb",
            Self::Xbgr32 => "xbgr",
            Self::Argb32 => "argb",
            Self::Rgba32 => "rgba",
            Self::Bgra32 => "bgra",
            Self::Abgr32 => "abgr",
            Self::Rgbd32 => "rgbd",
            Self::Rgbds32 => "rgbds",
        }
    }

    fn parse(spec: &str, value: &str) -> Result<Self, MuxError> {
        match value {
            "yuv420" | "yuv" => Ok(Self::Yuv420p8),
            "yvu420" | "yvu" => Ok(Self::Yvu420p8),
            "yuv420_10" | "yuvl" => Ok(Self::Yuv420p10),
            "yuv422" | "yuv2" => Ok(Self::Yuv422p8),
            "yuv422_10" | "yp2l" => Ok(Self::Yuv422p10),
            "yuv444" | "yuv4" => Ok(Self::Yuv444p8),
            "yuv444_10" | "yp4l" => Ok(Self::Yuv444p10),
            "yuva" => Ok(Self::Yuva420p8),
            "yuvd" => Ok(Self::Yuvd420p8),
            "yuv444a" | "yp4a" => Ok(Self::Yuva444p8),
            "nv12" => Ok(Self::Nv12p8),
            "nv21" => Ok(Self::Nv21p8),
            "nv12_10" | "nv1l" => Ok(Self::Nv12p10),
            "nv21_10" | "nv2l" => Ok(Self::Nv21p10),
            "uyvy" => Ok(Self::Uyvy422p8),
            "vyuy" => Ok(Self::Vyuy422p8),
            "yuyv" => Ok(Self::Yuyv422p8),
            "yvyu" => Ok(Self::Yvyu422p8),
            "uyvl" => Ok(Self::Uyvy422p10),
            "vyul" => Ok(Self::Vyuy422p10),
            "yuyl" => Ok(Self::Yuyv422p10),
            "yvyl" => Ok(Self::Yvyu422p10),
            "yuv444p" | "yv4p" => Ok(Self::Yuv444Packed8),
            "v308" => Ok(Self::Vyu444Packed8),
            "yuv444ap" | "y4ap" => Ok(Self::Yuva444Packed8),
            "v408" => Ok(Self::Uyva444Packed8),
            "v410" => Ok(Self::Yuv444Packed10),
            "v210" => Ok(Self::V210),
            "grey" => Ok(Self::Grey8),
            "algr" => Ok(Self::AlphaGrey8),
            "gral" => Ok(Self::GreyAlpha8),
            "rgb8" => Ok(Self::Rgb332),
            "rgb4" => Ok(Self::Rgb444),
            "rgb5" => Ok(Self::Rgb555),
            "rgb6" => Ok(Self::Rgb565),
            "rgb" => Ok(Self::Rgb24),
            "bgr" => Ok(Self::Bgr24),
            "rgbx" => Ok(Self::Rgbx32),
            "bgrx" => Ok(Self::Bgrx32),
            "xrgb" => Ok(Self::Xrgb32),
            "xbgr" => Ok(Self::Xbgr32),
            "argb" => Ok(Self::Argb32),
            "rgba" => Ok(Self::Rgba32),
            "bgra" => Ok(Self::Bgra32),
            "abgr" => Ok(Self::Abgr32),
            "rgbd" => Ok(Self::Rgbd32),
            "rgbds" => Ok(Self::Rgbds32),
            _ => Err(MuxError::InvalidTrackSpec {
                spec: spec.to_string(),
                message: format!(
                    "unsupported rawvideo `spfmt={value}`; expected one of the rawvideo pixel formats supported by mp4forge"
                ),
            }),
        }
    }
}

/// One explicit bare raw-video import description for the mux surface.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct MuxRawVideoParams {
    width: u32,
    height: u32,
    pixel_format: MuxRawVideoPixelFormat,
    fps_num: u32,
    fps_den: u32,
}

impl MuxRawVideoParams {
    /// Validates one explicit raw-video import description.
    pub fn new(
        width: u32,
        height: u32,
        pixel_format: MuxRawVideoPixelFormat,
        fps_num: u32,
        fps_den: u32,
    ) -> Result<Self, MuxError> {
        if width == 0 || height == 0 {
            return Err(MuxError::InvalidTrackSpec {
                spec: "rawvideo".to_string(),
                message: "rawvideo `size` must declare non-zero width and height".to_string(),
            });
        }
        if fps_num == 0 || fps_den == 0 {
            return Err(MuxError::InvalidTrackSpec {
                spec: "rawvideo".to_string(),
                message: "rawvideo `fps` must declare non-zero numerator and denominator"
                    .to_string(),
            });
        }
        Ok(Self {
            width,
            height,
            pixel_format,
            fps_num,
            fps_den,
        })
    }

    /// Returns the declared frame width in pixels.
    pub const fn width(&self) -> u32 {
        self.width
    }

    /// Returns the declared frame height in pixels.
    pub const fn height(&self) -> u32 {
        self.height
    }

    /// Returns the declared pixel format.
    pub const fn pixel_format(&self) -> MuxRawVideoPixelFormat {
        self.pixel_format
    }

    /// Returns the declared frame-rate numerator.
    pub const fn fps_num(&self) -> u32 {
        self.fps_num
    }

    /// Returns the declared frame-rate denominator.
    pub const fn fps_den(&self) -> u32 {
        self.fps_den
    }

    fn format_suffix(&self) -> String {
        format!(
            "rawvideo:size={}x{},spfmt={},fps={}/{}",
            self.width,
            self.height,
            self.pixel_format.canonical_name(),
            self.fps_num,
            self.fps_den
        )
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum MuxTrackSpec {
    /// Import one input path, optionally selecting one track when the source is containerized.
    Path {
        /// The filesystem path to import.
        path: PathBuf,
        /// The optional public selector to resolve inside that source.
        selector: Option<MuxMp4TrackSelector>,
    },
    /// Import one bare raw-video input using explicit out-of-band geometry and frame-rate data.
    RawVideo {
        /// The filesystem path to import.
        path: PathBuf,
        /// The explicit raw-video parameters.
        params: MuxRawVideoParams,
    },
}

impl MuxTrackSpec {
    /// Creates one path-first track specification from `path`.
    pub fn path(path: impl Into<PathBuf>) -> Self {
        Self::Path {
            path: path.into(),
            selector: None,
        }
    }

    /// Creates one path-first track specification from `path` and `selector`.
    pub fn selected(path: impl Into<PathBuf>, selector: MuxMp4TrackSelector) -> Self {
        Self::Path {
            path: path.into(),
            selector: Some(selector),
        }
    }

    /// Creates one compatibility selected track specification from `path` and `selector`.
    pub fn mp4(path: impl Into<PathBuf>, selector: MuxMp4TrackSelector) -> Self {
        Self::selected(path, selector)
    }

    /// Creates one explicit bare raw-video track specification from `path` and `params`.
    pub fn raw_video(path: impl Into<PathBuf>, params: MuxRawVideoParams) -> Self {
        Self::RawVideo {
            path: path.into(),
            params,
        }
    }

    /// Returns the filesystem path referenced by this track specification.
    pub fn input_path(&self) -> &Path {
        match self {
            Self::Path { path, .. } => path.as_path(),
            Self::RawVideo { path, .. } => path.as_path(),
        }
    }
}

impl FromStr for MuxTrackSpec {
    type Err = MuxError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        if value.is_empty() {
            return Err(MuxError::InvalidTrackSpec {
                spec: value.to_string(),
                message: "missing input path".to_string(),
            });
        }

        if let Some((path, selector_text)) = value.rsplit_once('#') {
            if path.is_empty() {
                return Err(MuxError::InvalidTrackSpec {
                    spec: value.to_string(),
                    message: "missing input path before `#`".to_string(),
                });
            }
            if let Some(rawvideo_text) = selector_text.strip_prefix("rawvideo:") {
                let params = parse_raw_video_params(value, rawvideo_text)?;
                return Ok(Self::RawVideo {
                    path: PathBuf::from(path),
                    params,
                });
            }
            let selector = parse_mp4_track_selector(value, selector_text)?;
            return Ok(Self::Path {
                path: PathBuf::from(path),
                selector: Some(selector),
            });
        }

        Ok(Self::path(value))
    }
}

fn parse_mp4_track_selector(spec: &str, selector: &str) -> Result<MuxMp4TrackSelector, MuxError> {
    if selector.is_empty() {
        return Err(MuxError::InvalidTrackSpec {
            spec: spec.to_string(),
            message:
                "expected one selector after `#`, such as `video`, `audio`, `text`, or `track:ID`"
                    .to_string(),
        });
    }
    if selector.contains('=') || selector.contains(',') {
        return Err(MuxError::InvalidTrackSpec {
            spec: spec.to_string(),
            message: "public mux track specs only allow selector suffixes such as `#video`, `#audio`, `#text`, or `#track:ID`; raw `#name=value` parameters are no longer accepted".to_string(),
        });
    }
    if selector == "video" {
        return Ok(MuxMp4TrackSelector::Video);
    }
    if selector == "audio" {
        return Ok(MuxMp4TrackSelector::Audio { occurrence: 1 });
    }
    if selector == "text" {
        return Ok(MuxMp4TrackSelector::Text { occurrence: 1 });
    }
    if let Some(index) = selector.strip_prefix("audio:") {
        let occurrence = index
            .parse::<u32>()
            .map_err(|_| MuxError::InvalidTrackSpec {
                spec: spec.to_string(),
                message: format!("invalid audio occurrence `{index}`"),
            })?;
        if occurrence == 0 {
            return Err(MuxError::InvalidTrackSpec {
                spec: spec.to_string(),
                message: "audio occurrences are one-based; `audio:0` is invalid".to_string(),
            });
        }
        return Ok(MuxMp4TrackSelector::Audio { occurrence });
    }
    if let Some(index) = selector.strip_prefix("text:") {
        let occurrence = index
            .parse::<u32>()
            .map_err(|_| MuxError::InvalidTrackSpec {
                spec: spec.to_string(),
                message: format!("invalid text occurrence `{index}`"),
            })?;
        if occurrence == 0 {
            return Err(MuxError::InvalidTrackSpec {
                spec: spec.to_string(),
                message: "text occurrences are one-based; `text:0` is invalid".to_string(),
            });
        }
        return Ok(MuxMp4TrackSelector::Text { occurrence });
    }
    if let Some(track_id) = selector.strip_prefix("track:") {
        let track_id = track_id
            .parse::<u32>()
            .map_err(|_| MuxError::InvalidTrackSpec {
                spec: spec.to_string(),
                message: format!("invalid track id `{track_id}`"),
            })?;
        if track_id == 0 {
            return Err(MuxError::InvalidTrackSpec {
                spec: spec.to_string(),
                message: "track ids are one-based; `track:0` is invalid".to_string(),
            });
        }
        return Ok(MuxMp4TrackSelector::TrackId { track_id });
    }

    Err(MuxError::InvalidTrackSpec {
        spec: spec.to_string(),
        message: format!(
            "unsupported MP4 track selector `{selector}`; expected `video`, `audio`, `audio:N`, `text`, `text:N`, or `track:ID`"
        ),
    })
}

fn parse_raw_video_params(spec: &str, rawvideo_text: &str) -> Result<MuxRawVideoParams, MuxError> {
    if rawvideo_text.is_empty() {
        return Err(MuxError::InvalidTrackSpec {
            spec: spec.to_string(),
            message:
                "expected rawvideo parameters after `#rawvideo:`, such as `size=1920x1080,spfmt=yuv420,fps=25/1`"
                    .to_string(),
        });
    }

    let mut width = None::<u32>;
    let mut height = None::<u32>;
    let mut pixel_format = None::<MuxRawVideoPixelFormat>;
    let mut fps_num = None::<u32>;
    let mut fps_den = None::<u32>;

    for token in rawvideo_text.split(',') {
        let (name, value) = token.split_once('=').ok_or_else(|| MuxError::InvalidTrackSpec {
            spec: spec.to_string(),
            message: format!(
                "invalid rawvideo parameter `{token}`; expected `name=value` pairs separated by commas"
            ),
        })?;
        match name {
            "size" => {
                let (parsed_width, parsed_height) =
                    value
                        .split_once('x')
                        .ok_or_else(|| MuxError::InvalidTrackSpec {
                            spec: spec.to_string(),
                            message: "rawvideo `size` must use `WIDTHxHEIGHT`".to_string(),
                        })?;
                width =
                    Some(
                        parsed_width
                            .parse::<u32>()
                            .map_err(|_| MuxError::InvalidTrackSpec {
                                spec: spec.to_string(),
                                message: format!("invalid rawvideo width `{parsed_width}`"),
                            })?,
                    );
                height =
                    Some(
                        parsed_height
                            .parse::<u32>()
                            .map_err(|_| MuxError::InvalidTrackSpec {
                                spec: spec.to_string(),
                                message: format!("invalid rawvideo height `{parsed_height}`"),
                            })?,
                    );
            }
            "spfmt" => {
                pixel_format = Some(MuxRawVideoPixelFormat::parse(spec, value)?);
            }
            "fps" => {
                let (parsed_num, parsed_den) =
                    value
                        .split_once('/')
                        .ok_or_else(|| MuxError::InvalidTrackSpec {
                            spec: spec.to_string(),
                            message: "rawvideo `fps` must use `NUM/DEN`".to_string(),
                        })?;
                fps_num =
                    Some(
                        parsed_num
                            .parse::<u32>()
                            .map_err(|_| MuxError::InvalidTrackSpec {
                                spec: spec.to_string(),
                                message: format!(
                                    "invalid rawvideo frame-rate numerator `{parsed_num}`"
                                ),
                            })?,
                    );
                fps_den =
                    Some(
                        parsed_den
                            .parse::<u32>()
                            .map_err(|_| MuxError::InvalidTrackSpec {
                                spec: spec.to_string(),
                                message: format!(
                                    "invalid rawvideo frame-rate denominator `{parsed_den}`"
                                ),
                            })?,
                    );
            }
            _ => {
                return Err(MuxError::InvalidTrackSpec {
                    spec: spec.to_string(),
                    message: format!(
                        "unsupported rawvideo parameter `{name}`; expected `size`, `spfmt`, or `fps`"
                    ),
                });
            }
        }
    }

    let width = width.ok_or_else(|| MuxError::InvalidTrackSpec {
        spec: spec.to_string(),
        message: "rawvideo track specs must declare `size=WIDTHxHEIGHT`".to_string(),
    })?;
    let height = height.ok_or_else(|| MuxError::InvalidTrackSpec {
        spec: spec.to_string(),
        message: "rawvideo track specs must declare `size=WIDTHxHEIGHT`".to_string(),
    })?;
    let pixel_format = pixel_format.ok_or_else(|| MuxError::InvalidTrackSpec {
        spec: spec.to_string(),
        message: "rawvideo track specs must declare `spfmt=PIXFMT`".to_string(),
    })?;
    let fps_num = fps_num.ok_or_else(|| MuxError::InvalidTrackSpec {
        spec: spec.to_string(),
        message: "rawvideo track specs must declare `fps=NUM/DEN`".to_string(),
    })?;
    let fps_den = fps_den.ok_or_else(|| MuxError::InvalidTrackSpec {
        spec: spec.to_string(),
        message: "rawvideo track specs must declare `fps=NUM/DEN`".to_string(),
    })?;
    MuxRawVideoParams::new(width, height, pixel_format, fps_num, fps_den).map_err(|error| {
        match error {
            MuxError::InvalidTrackSpec { message, .. } => MuxError::InvalidTrackSpec {
                spec: spec.to_string(),
                message,
            },
            other => other,
        }
    })
}

/// Duration-boundary mode for the narrowed public mux surface.
///
/// The current `mp4forge` mux follow-on keeps the public duration surface intentionally narrow:
/// callers may request exactly one boundary mode for fragmented output when the current one-file
/// MP4 output can model it correctly.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum MuxDurationMode {
    /// Coordinate track chunks around one target segment duration in seconds.
    Segment { seconds: f64 },
    /// Coordinate track chunks around one target fragment duration in seconds.
    Fragment { seconds: f64 },
}

impl MuxDurationMode {
    /// Returns the public mode label used by diagnostics and CLI help.
    pub const fn label(&self) -> &'static str {
        match self {
            Self::Segment { .. } => "segment_duration",
            Self::Fragment { .. } => "fragment_duration",
        }
    }

    /// Returns the requested duration in seconds.
    pub const fn seconds(&self) -> f64 {
        match self {
            Self::Segment { seconds } | Self::Fragment { seconds } => *seconds,
        }
    }
}

/// Container layout used by the public mux request surface.
///
/// The default `mp4forge` mux behavior remains one flat `ftyp + moov + mdat` file. Fragmented
/// output is additive and explicit so callers do not accidentally change container structure just
/// by supplying one duration mode.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub enum MuxOutputLayout {
    /// Write one flat self-contained MP4 with `ftyp`, `moov`, and `mdat`.
    #[default]
    Flat,
    /// Write one fragmented MP4 with `sidx` plus one or more `moof`/`mdat` pairs.
    Fragmented,
}

impl MuxOutputLayout {
    /// Returns the public layout label used by CLI parsing and diagnostics.
    pub const fn label(&self) -> &'static str {
        match self {
            Self::Flat => "flat",
            Self::Fragmented => "fragmented",
        }
    }
}

/// Destination mode used by the public mux request surface.
///
/// The force-new mode writes one newly created output file to a caller-supplied path. The
/// destination-path mode follows an update-or-create model: if the destination already exists as
/// an MP4, its tracks are preserved and additional tracks are imported into it; otherwise the same
/// path is treated as the newly created output file.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub enum MuxDestinationMode {
    /// Write one newly created output file supplied separately to the file-backed helpers.
    #[default]
    CreateNew,
    /// Preserve one destination MP4 when it already exists, or create it at the same path.
    UpdateOrCreateDestination,
}

impl MuxDestinationMode {
    /// Returns the public destination-mode label used by CLI parsing and diagnostics.
    pub const fn label(&self) -> &'static str {
        match self {
            Self::CreateNew => "create-new",
            Self::UpdateOrCreateDestination => "update-or-create-destination",
        }
    }
}

/// Event-message metadata to emit before one fragmented media fragment.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MuxFragmentEventMessage {
    fragment_index: u32,
    version: u8,
    scheme_id_uri: String,
    value: String,
    timescale: u32,
    presentation_time_delta: u32,
    presentation_time: u64,
    event_duration: u32,
    id: u32,
    message_data: Vec<u8>,
}

impl MuxFragmentEventMessage {
    /// Creates one version-0 event message for the zero-based fragment index.
    #[allow(clippy::too_many_arguments)]
    pub fn new_v0<S, V, M>(
        fragment_index: u32,
        scheme_id_uri: S,
        value: V,
        timescale: u32,
        presentation_time_delta: u32,
        event_duration: u32,
        id: u32,
        message_data: M,
    ) -> Self
    where
        S: Into<String>,
        V: Into<String>,
        M: Into<Vec<u8>>,
    {
        Self {
            fragment_index,
            version: 0,
            scheme_id_uri: scheme_id_uri.into(),
            value: value.into(),
            timescale,
            presentation_time_delta,
            presentation_time: 0,
            event_duration,
            id,
            message_data: message_data.into(),
        }
    }

    /// Creates one version-1 event message for the zero-based fragment index.
    #[allow(clippy::too_many_arguments)]
    pub fn new_v1<S, V, M>(
        fragment_index: u32,
        scheme_id_uri: S,
        value: V,
        timescale: u32,
        presentation_time: u64,
        event_duration: u32,
        id: u32,
        message_data: M,
    ) -> Self
    where
        S: Into<String>,
        V: Into<String>,
        M: Into<Vec<u8>>,
    {
        Self {
            fragment_index,
            version: 1,
            scheme_id_uri: scheme_id_uri.into(),
            value: value.into(),
            timescale,
            presentation_time_delta: 0,
            presentation_time,
            event_duration,
            id,
            message_data: message_data.into(),
        }
    }

    /// Returns the zero-based output fragment index this message belongs to.
    pub const fn fragment_index(&self) -> u32 {
        self.fragment_index
    }

    /// Returns the encoded `emsg` version.
    pub const fn version(&self) -> u8 {
        self.version
    }

    /// Returns the event scheme identifier.
    pub fn scheme_id_uri(&self) -> &str {
        &self.scheme_id_uri
    }

    /// Returns the event value string.
    pub fn value(&self) -> &str {
        &self.value
    }

    /// Returns the event timescale.
    pub const fn timescale(&self) -> u32 {
        self.timescale
    }

    /// Returns the version-0 presentation time delta.
    pub const fn presentation_time_delta(&self) -> u32 {
        self.presentation_time_delta
    }

    /// Returns the version-1 presentation time.
    pub const fn presentation_time(&self) -> u64 {
        self.presentation_time
    }

    /// Returns the event duration.
    pub const fn event_duration(&self) -> u32 {
        self.event_duration
    }

    /// Returns the event identifier.
    pub const fn id(&self) -> u32 {
        self.id
    }

    /// Returns the event payload bytes.
    pub fn message_data(&self) -> &[u8] {
        &self.message_data
    }
}

/// Producer-reference-time metadata to emit before one fragmented media fragment.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MuxProducerReferenceTime {
    fragment_index: u32,
    version: u8,
    flags: u32,
    reference_track_id: u32,
    ntp_timestamp: u64,
    media_time: u64,
}

impl MuxProducerReferenceTime {
    /// Creates one version-1 producer-reference-time entry for the zero-based fragment index.
    pub const fn new(
        fragment_index: u32,
        reference_track_id: u32,
        ntp_timestamp: u64,
        media_time: u64,
    ) -> Self {
        Self {
            fragment_index,
            version: 1,
            flags: 0,
            reference_track_id,
            ntp_timestamp,
            media_time,
        }
    }

    /// Returns a copy of this entry with an explicit encoded `prft` version.
    pub const fn with_version(mut self, version: u8) -> Self {
        self.version = version;
        self
    }

    /// Returns a copy of this entry with explicit `prft` flags.
    pub const fn with_flags(mut self, flags: u32) -> Self {
        self.flags = flags;
        self
    }

    /// Returns the zero-based output fragment index this entry belongs to.
    pub const fn fragment_index(&self) -> u32 {
        self.fragment_index
    }

    /// Returns the encoded `prft` version.
    pub const fn version(&self) -> u8 {
        self.version
    }

    /// Returns the encoded `prft` flags.
    pub const fn flags(&self) -> u32 {
        self.flags
    }

    /// Returns the referenced track identifier.
    pub const fn reference_track_id(&self) -> u32 {
        self.reference_track_id
    }

    /// Returns the NTP timestamp payload.
    pub const fn ntp_timestamp(&self) -> u64 {
        self.ntp_timestamp
    }

    /// Returns the media-time payload before version-specific narrowing.
    pub const fn media_time(&self) -> u64 {
        self.media_time
    }
}

/// One high-level mux request aligned with the public CLI surface.
///
/// The narrowed public `mux` surface now centers on repeated [`MuxTrackSpec`] values, one
/// caller-supplied destination path, one explicit output layout, and at most one
/// duration-boundary mode.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct MuxRequest {
    tracks: Vec<MuxTrackSpec>,
    output_layout: MuxOutputLayout,
    destination_mode: MuxDestinationMode,
    duration_mode: Option<MuxDurationMode>,
    preserve_flat_authority_layout: bool,
    fragment_event_messages: Vec<MuxFragmentEventMessage>,
    producer_reference_times: Vec<MuxProducerReferenceTime>,
}

impl MuxRequest {
    /// Creates one mux request from repeated public track specs.
    pub fn new(tracks: Vec<MuxTrackSpec>) -> Self {
        Self {
            tracks,
            output_layout: MuxOutputLayout::Flat,
            destination_mode: MuxDestinationMode::CreateNew,
            duration_mode: None,
            preserve_flat_authority_layout: false,
            fragment_event_messages: Vec::new(),
            producer_reference_times: Vec::new(),
        }
    }

    /// Returns the public track specs carried by this request.
    pub fn tracks(&self) -> &[MuxTrackSpec] {
        &self.tracks
    }

    /// Returns the explicit container layout requested by the caller.
    pub const fn output_layout(&self) -> MuxOutputLayout {
        self.output_layout
    }

    /// Returns the destination mode requested by the caller.
    pub const fn destination_mode(&self) -> MuxDestinationMode {
        self.destination_mode
    }

    /// Returns the configured public duration-boundary mode, if any.
    pub const fn duration_mode(&self) -> Option<MuxDurationMode> {
        self.duration_mode
    }

    pub(crate) const fn preserve_flat_authority_layout(&self) -> bool {
        self.preserve_flat_authority_layout
    }

    /// Returns configured event messages for fragmented output.
    pub fn fragment_event_messages(&self) -> &[MuxFragmentEventMessage] {
        &self.fragment_event_messages
    }

    /// Returns configured producer-reference-time entries for fragmented output.
    pub fn producer_reference_times(&self) -> &[MuxProducerReferenceTime] {
        &self.producer_reference_times
    }

    /// Returns a copy of this request with one explicit container layout configured.
    pub const fn with_output_layout(mut self, output_layout: MuxOutputLayout) -> Self {
        self.output_layout = output_layout;
        self
    }

    /// Returns a copy of this request with one explicit destination mode configured.
    pub const fn with_destination_mode(mut self, destination_mode: MuxDestinationMode) -> Self {
        self.destination_mode = destination_mode;
        self
    }

    /// Returns a copy of this request with one public duration-boundary mode configured.
    pub const fn with_duration_mode(mut self, duration_mode: MuxDurationMode) -> Self {
        self.duration_mode = Some(duration_mode);
        self
    }

    pub(crate) const fn with_preserve_flat_authority_layout(
        mut self,
        preserve_flat_authority_layout: bool,
    ) -> Self {
        self.preserve_flat_authority_layout = preserve_flat_authority_layout;
        self
    }

    /// Returns a copy of this request with one appended fragmented event message.
    pub fn with_fragment_event_message(mut self, message: MuxFragmentEventMessage) -> Self {
        self.fragment_event_messages.push(message);
        self
    }

    /// Returns a copy of this request with one appended fragmented producer-reference-time entry.
    pub fn with_producer_reference_time(mut self, entry: MuxProducerReferenceTime) -> Self {
        self.producer_reference_times.push(entry);
        self
    }
}

/// Interleave policy used when ordering staged media items into one output payload.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub enum MuxInterleavePolicy {
    /// Orders staged items by normalized decode time while keeping ties stable by source and
    /// source-offset order.
    #[default]
    DecodeTime,
    /// Orders coordinated chunks by chunk ordinal first, then keeps ties stable by source and
    /// source-offset order. This is used only on preserved-authority flat carry paths where the
    /// authority layout dictates the interleave window sequence directly.
    ChunkOrdinalThenSource,
}

/// One staged media item that a later mux step can schedule into one output payload.
///
/// The current foundation expects `decode_time` to already be normalized onto one interleave
/// timeline across every staged source involved in the plan. Future phases can widen the staging
/// model with richer timeline normalization once full container assembly lands.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MuxStagedMediaItem {
    source_index: usize,
    track_id: u32,
    decode_time: u64,
    composition_time_offset: i32,
    duration: u32,
    data_offset: u64,
    data_size: u32,
    is_sync_sample: bool,
    sample_description_index: u32,
}

impl MuxStagedMediaItem {
    /// Creates one staged media item for a later mux payload plan.
    pub const fn new(
        source_index: usize,
        track_id: u32,
        decode_time: u64,
        duration: u32,
        data_offset: u64,
        data_size: u32,
    ) -> Self {
        Self {
            source_index,
            track_id,
            decode_time,
            composition_time_offset: 0,
            duration,
            data_offset,
            data_size,
            is_sync_sample: false,
            sample_description_index: 1,
        }
    }

    /// Returns the staged source slot this item will read from during payload copy.
    pub const fn source_index(&self) -> usize {
        self.source_index
    }

    /// Returns the destination track identifier for this item.
    pub const fn track_id(&self) -> u32 {
        self.track_id
    }

    /// Returns the normalized decode time used by the current interleave planner.
    pub const fn decode_time(&self) -> u64 {
        self.decode_time
    }

    /// Returns the composition offset carried with this item.
    pub const fn composition_time_offset(&self) -> i32 {
        self.composition_time_offset
    }

    /// Returns this item's decode duration on the staged mux timeline.
    pub const fn duration(&self) -> u32 {
        self.duration
    }

    /// Returns the source byte offset for this item's sample payload.
    pub const fn data_offset(&self) -> u64 {
        self.data_offset
    }

    /// Returns the number of bytes to copy for this item's sample payload.
    pub const fn data_size(&self) -> u32 {
        self.data_size
    }

    /// Returns whether the staged item is marked as a sync sample.
    pub const fn is_sync_sample(&self) -> bool {
        self.is_sync_sample
    }

    /// Returns the one-based sample-description index used for this staged sample.
    pub const fn sample_description_index(&self) -> u32 {
        self.sample_description_index
    }

    /// Returns a copy of this item with a non-zero composition offset.
    pub const fn with_composition_time_offset(mut self, composition_time_offset: i32) -> Self {
        self.composition_time_offset = composition_time_offset;
        self
    }

    /// Returns a copy of this item with an explicit sync-sample marker.
    pub const fn with_sync_sample(mut self, is_sync_sample: bool) -> Self {
        self.is_sync_sample = is_sync_sample;
        self
    }

    /// Returns a copy of this item with an explicit one-based sample-description index.
    pub const fn with_sample_description_index(mut self, sample_description_index: u32) -> Self {
        self.sample_description_index = sample_description_index;
        self
    }
}

/// One planned media item with its final output payload placement.
///
/// This is the current mux-side boundary surface for future higher-level work: one item carries
/// the sample order, the source byte range, the decode interval, and the output payload span
/// without exposing the crate-private queue internals directly.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MuxPlannedMediaItem {
    staged: MuxStagedMediaItem,
    output_offset: u64,
}

impl MuxPlannedMediaItem {
    /// Returns the original staged media item.
    pub const fn staged(&self) -> &MuxStagedMediaItem {
        &self.staged
    }

    /// Returns the byte offset this item occupies in the final payload order.
    pub const fn output_offset(&self) -> u64 {
        self.output_offset
    }

    /// Returns the first byte offset after this item's payload in the final output order.
    pub const fn output_end_offset(&self) -> u64 {
        self.output_offset + self.staged.data_size as u64
    }

    /// Returns the decode end time of this item on the planned mux timeline.
    pub const fn decode_end_time(&self) -> u64 {
        self.staged.decode_time + self.staged.duration as u64
    }
}

/// Aggregate per-track timing and item-count information for a mux plan.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MuxTrackPlan {
    track_id: u32,
    item_count: u32,
    first_decode_time: u64,
    end_decode_time: u64,
}

impl MuxTrackPlan {
    /// Returns the track identifier summarized by this plan entry.
    pub const fn track_id(&self) -> u32 {
        self.track_id
    }

    /// Returns the number of staged items scheduled for this track.
    pub const fn item_count(&self) -> u32 {
        self.item_count
    }

    /// Returns the earliest decode time assigned to this track in the current plan.
    pub const fn first_decode_time(&self) -> u64 {
        self.first_decode_time
    }

    /// Returns the decode end time of the last staged item scheduled for this track.
    pub const fn end_decode_time(&self) -> u64 {
        self.end_decode_time
    }
}

/// Planned mux payload order and per-track timing summaries.
///
/// The stable task-level plan view intentionally mirrors the internal mux event graph. Callers
/// continue to consume planned items and per-track summaries, while the crate-private event graph
/// drives the current payload-copy, chunk coordination, and planned sample-reader helpers
/// underneath.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MuxPlan {
    interleave_policy: MuxInterleavePolicy,
    planned_items: Vec<MuxPlannedMediaItem>,
    track_plans: Vec<MuxTrackPlan>,
    total_payload_size: u64,
    coordination: MuxCoordinationPlan,
    event_graph: MuxEventGraph,
}

impl MuxPlan {
    /// Returns the interleave policy used when building this plan.
    pub const fn interleave_policy(&self) -> MuxInterleavePolicy {
        self.interleave_policy
    }

    /// Returns the staged items in final payload order.
    ///
    /// This slice is the stable task-level view of the current mux event graph. Callers that need
    /// sample-order timing or payload spans should build on these planned items instead of
    /// depending on the crate-private event graph directly.
    pub fn planned_items(&self) -> &[MuxPlannedMediaItem] {
        &self.planned_items
    }

    /// Returns the per-track summaries collected during planning.
    pub fn track_plans(&self) -> &[MuxTrackPlan] {
        &self.track_plans
    }

    /// Returns the total number of bytes the planned payload copy will emit.
    pub const fn total_payload_size(&self) -> u64 {
        self.total_payload_size
    }

    pub(crate) fn chunk_sample_counts(&self, track_id: u32) -> Result<&[u32], MuxError> {
        self.coordination.chunk_sample_counts(track_id)
    }

    pub(crate) fn event_graph(&self) -> &MuxEventGraph {
        &self.event_graph
    }
}

/// File-level MP4 mux configuration for the real container-writing surface.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MuxFileConfig {
    movie_timescale: u32,
    major_brand: FourCc,
    minor_version: u32,
    compatible_brands: Vec<FourCc>,
    auto_flat_profile: bool,
    allow_audio_only_iods: bool,
    keep_flat_free_box: bool,
    keep_flat_authority_brands: bool,
    preserve_auto_flat_movie_timescale: bool,
    emit_default_flat_tool_metadata: bool,
    flat_source_encoding_metadata: Option<String>,
    flat_source_encoder_metadata: Option<String>,
    flat_source_movie_creation_time: Option<u64>,
    flat_source_movie_modification_time: Option<u64>,
    preserved_flat_prefix_bytes: Vec<u8>,
    preserved_flat_iods_bytes: Option<Vec<u8>>,
    preserved_flat_udta_bytes: Option<Vec<u8>>,
    fragment_event_messages: Vec<MuxFragmentEventMessage>,
    producer_reference_times: Vec<MuxProducerReferenceTime>,
}

impl MuxFileConfig {
    /// Creates one MP4 mux configuration with the supplied movie timescale.
    ///
    /// The default brand layout is `isom` plus `mp42` compatibility.
    pub fn new(movie_timescale: u32) -> Self {
        Self {
            movie_timescale,
            major_brand: FourCc::from_bytes(*b"isom"),
            minor_version: 0,
            compatible_brands: vec![FourCc::from_bytes(*b"isom"), FourCc::from_bytes(*b"mp42")],
            auto_flat_profile: false,
            allow_audio_only_iods: false,
            keep_flat_free_box: false,
            keep_flat_authority_brands: false,
            preserve_auto_flat_movie_timescale: false,
            emit_default_flat_tool_metadata: true,
            flat_source_encoding_metadata: None,
            flat_source_encoder_metadata: None,
            flat_source_movie_creation_time: None,
            flat_source_movie_modification_time: None,
            preserved_flat_prefix_bytes: Vec::new(),
            preserved_flat_iods_bytes: None,
            preserved_flat_udta_bytes: None,
            fragment_event_messages: Vec::new(),
            producer_reference_times: Vec::new(),
        }
    }

    /// Returns the movie timescale used for `mvhd` and `tkhd` durations.
    pub const fn movie_timescale(&self) -> u32 {
        self.movie_timescale
    }

    /// Returns the file's major brand.
    pub const fn major_brand(&self) -> FourCc {
        self.major_brand
    }

    /// Returns the file's minor version.
    pub const fn minor_version(&self) -> u32 {
        self.minor_version
    }

    /// Returns the compatible brands written into `ftyp`.
    pub fn compatible_brands(&self) -> &[FourCc] {
        &self.compatible_brands
    }

    /// Returns a copy of this configuration with a different major brand.
    pub const fn with_major_brand(mut self, major_brand: FourCc) -> Self {
        self.major_brand = major_brand;
        self
    }

    /// Returns a copy of this configuration with a different minor version.
    pub const fn with_minor_version(mut self, minor_version: u32) -> Self {
        self.minor_version = minor_version;
        self
    }

    /// Adds `brand` to the compatibility list if it is not already present.
    pub fn add_compatible_brand(&mut self, brand: FourCc) {
        if !self.compatible_brands.contains(&brand) {
            self.compatible_brands.push(brand);
        }
    }

    /// Returns a copy of this configuration with one extra compatible brand.
    pub fn with_compatible_brand(mut self, brand: FourCc) -> Self {
        self.add_compatible_brand(brand);
        self
    }

    pub(crate) fn with_compatible_brands(mut self, compatible_brands: Vec<FourCc>) -> Self {
        self.compatible_brands = compatible_brands;
        self
    }

    pub(crate) const fn auto_flat_profile(&self) -> bool {
        self.auto_flat_profile
    }

    pub(crate) const fn with_auto_flat_profile(mut self, auto_flat_profile: bool) -> Self {
        self.auto_flat_profile = auto_flat_profile;
        self
    }

    pub(crate) const fn allow_audio_only_iods(&self) -> bool {
        self.allow_audio_only_iods
    }

    pub(crate) const fn with_allow_audio_only_iods(mut self, allow_audio_only_iods: bool) -> Self {
        self.allow_audio_only_iods = allow_audio_only_iods;
        self
    }

    pub(crate) const fn keep_flat_free_box(&self) -> bool {
        self.keep_flat_free_box
    }

    pub(crate) const fn with_keep_flat_free_box(mut self, keep_flat_free_box: bool) -> Self {
        self.keep_flat_free_box = keep_flat_free_box;
        self
    }

    pub(crate) const fn keep_flat_authority_brands(&self) -> bool {
        self.keep_flat_authority_brands
    }

    pub(crate) const fn with_keep_flat_authority_brands(
        mut self,
        keep_flat_authority_brands: bool,
    ) -> Self {
        self.keep_flat_authority_brands = keep_flat_authority_brands;
        self
    }

    pub(crate) const fn preserve_auto_flat_movie_timescale(&self) -> bool {
        self.preserve_auto_flat_movie_timescale
    }

    pub(crate) const fn with_preserve_auto_flat_movie_timescale(
        mut self,
        preserve_auto_flat_movie_timescale: bool,
    ) -> Self {
        self.preserve_auto_flat_movie_timescale = preserve_auto_flat_movie_timescale;
        self
    }

    pub(crate) const fn emit_default_flat_tool_metadata(&self) -> bool {
        self.emit_default_flat_tool_metadata
    }

    pub(crate) const fn with_emit_default_flat_tool_metadata(
        mut self,
        emit_default_flat_tool_metadata: bool,
    ) -> Self {
        self.emit_default_flat_tool_metadata = emit_default_flat_tool_metadata;
        self
    }

    pub(crate) fn flat_source_encoding_metadata(&self) -> Option<&str> {
        self.flat_source_encoding_metadata.as_deref()
    }

    pub(crate) fn with_flat_source_encoding_metadata(
        mut self,
        flat_source_encoding_metadata: Option<String>,
    ) -> Self {
        self.flat_source_encoding_metadata = flat_source_encoding_metadata;
        self
    }

    pub(crate) fn flat_source_encoder_metadata(&self) -> Option<&str> {
        self.flat_source_encoder_metadata.as_deref()
    }

    pub(crate) fn with_flat_source_encoder_metadata(
        mut self,
        flat_source_encoder_metadata: Option<String>,
    ) -> Self {
        self.flat_source_encoder_metadata = flat_source_encoder_metadata;
        self
    }

    pub(crate) const fn flat_source_movie_creation_time(&self) -> Option<u64> {
        self.flat_source_movie_creation_time
    }

    pub(crate) const fn with_flat_source_movie_creation_time(
        mut self,
        flat_source_movie_creation_time: Option<u64>,
    ) -> Self {
        self.flat_source_movie_creation_time = flat_source_movie_creation_time;
        self
    }

    pub(crate) const fn flat_source_movie_modification_time(&self) -> Option<u64> {
        self.flat_source_movie_modification_time
    }

    pub(crate) const fn with_flat_source_movie_modification_time(
        mut self,
        flat_source_movie_modification_time: Option<u64>,
    ) -> Self {
        self.flat_source_movie_modification_time = flat_source_movie_modification_time;
        self
    }

    pub(crate) fn preserved_flat_prefix_bytes(&self) -> &[u8] {
        &self.preserved_flat_prefix_bytes
    }

    pub(crate) fn with_preserved_flat_prefix_bytes(
        mut self,
        preserved_flat_prefix_bytes: Vec<u8>,
    ) -> Self {
        self.preserved_flat_prefix_bytes = preserved_flat_prefix_bytes;
        self
    }

    pub(crate) fn preserved_flat_iods_bytes(&self) -> Option<&[u8]> {
        self.preserved_flat_iods_bytes.as_deref()
    }

    pub(crate) fn with_preserved_flat_iods_bytes(
        mut self,
        preserved_flat_iods_bytes: Option<Vec<u8>>,
    ) -> Self {
        self.preserved_flat_iods_bytes = preserved_flat_iods_bytes;
        self
    }

    pub(crate) fn preserved_flat_udta_bytes(&self) -> Option<&[u8]> {
        self.preserved_flat_udta_bytes.as_deref()
    }

    pub(crate) fn with_preserved_flat_udta_bytes(
        mut self,
        preserved_flat_udta_bytes: Option<Vec<u8>>,
    ) -> Self {
        self.preserved_flat_udta_bytes = preserved_flat_udta_bytes;
        self
    }

    pub(crate) fn fragment_event_messages(&self) -> &[MuxFragmentEventMessage] {
        &self.fragment_event_messages
    }

    pub(crate) fn with_fragment_event_messages(
        mut self,
        fragment_event_messages: Vec<MuxFragmentEventMessage>,
    ) -> Self {
        self.fragment_event_messages = fragment_event_messages;
        self
    }

    pub(crate) fn producer_reference_times(&self) -> &[MuxProducerReferenceTime] {
        &self.producer_reference_times
    }

    pub(crate) fn with_producer_reference_times(
        mut self,
        producer_reference_times: Vec<MuxProducerReferenceTime>,
    ) -> Self {
        self.producer_reference_times = producer_reference_times;
        self
    }
}

/// Track kind used by the real MP4 mux surface.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MuxTrackKind {
    /// Sound track with `smhd`, `soun`, and non-zero default track volume.
    Audio,
    /// Visual track with `vmhd`, `vide`, width, and height metadata.
    Video,
    /// Timed text track with `nmhd`, `text`, and zero default track volume.
    Text,
    /// Timed subtitle track with `sthd`, `subt`, and zero default track volume.
    Subtitle,
}

impl MuxTrackKind {
    /// Returns whether this track kind is audio.
    pub const fn is_audio(self) -> bool {
        matches!(self, Self::Audio)
    }

    /// Returns whether this track kind is video.
    pub const fn is_video(self) -> bool {
        matches!(self, Self::Video)
    }

    /// Returns whether this track kind is one of the timed-text families.
    pub const fn is_textual(self) -> bool {
        matches!(self, Self::Text | Self::Subtitle)
    }
}

const DEFAULT_TKHD_FLAGS: u32 = 0x0000_0001 | 0x0000_0002 | 0x0000_0004;
const DEFAULT_AUDIO_ALTERNATE_GROUP: i16 = 1;
const DEFAULT_SUBTITLE_ALTERNATE_GROUP: i16 = 0;
const DEFAULT_TKHD_MATRIX: [i32; 9] = [0x0001_0000, 0, 0, 0, 0x0001_0000, 0, 0, 0, 0x4000_0000];

const fn default_alternate_group_for_kind(kind: MuxTrackKind) -> i16 {
    match kind {
        MuxTrackKind::Audio => DEFAULT_AUDIO_ALTERNATE_GROUP,
        MuxTrackKind::Subtitle => DEFAULT_SUBTITLE_ALTERNATE_GROUP,
        MuxTrackKind::Video | MuxTrackKind::Text => 0,
    }
}

/// Per-track configuration for the real MP4 mux surface.
///
/// The muxer accepts a primary encoded sample-entry box and can retain additional entries for
/// imported tracks that switch sample descriptions over time.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MuxTrackConfig {
    track_id: u32,
    kind: MuxTrackKind,
    timescale: u32,
    language: [u8; 3],
    handler_name: String,
    track_width: u16,
    track_height: u16,
    track_width_fixed_16_16: Option<u32>,
    track_height_fixed_16_16: Option<u32>,
    tkhd_flags: u32,
    alternate_group: i16,
    volume: i16,
    matrix: [i32; 9],
    edit_media_time: Option<u64>,
    sample_roll_distance: Option<i16>,
    emit_roll_sbgp: bool,
    sample_entry_box: Vec<u8>,
    sample_entry_boxes: Vec<Vec<u8>>,
    sync_sample_table_mode: SyncSampleTableMode,
    stts_run_encoding_mode: SttsRunEncodingMode,
    stsc_run_encoding_mode: StscRunEncodingMode,
    flat_timing_override: Option<FlatTimingOverride>,
    flat_audio_profile_level_indication: Option<u8>,
    fragmented_decode_time_offset: Option<u64>,
    fragmented_reference_group_fragment_counts: Option<Vec<u32>>,
    flat_source_track_creation_time: Option<u64>,
    flat_source_track_modification_time: Option<u64>,
    flat_source_media_creation_time: Option<u64>,
    flat_source_media_modification_time: Option<u64>,
    omit_flat_iods: bool,
    flat_stsc_override: Option<crate::boxes::iso14496_12::Stsc>,
    preserved_flat_stbl_boxes: Vec<Vec<u8>>,
    preserved_flat_trak_boxes: Vec<Vec<u8>>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum SyncSampleTableMode {
    Auto,
    ForceEmpty,
    ForceFirstOnly,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum StscRunEncodingMode {
    CollapseIdentical,
    PreserveTerminalBoundary,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum SttsRunEncodingMode {
    CollapseIdentical,
    PreservePerSample,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct FlatTimingOverride {
    pub(crate) sample_durations: Vec<u32>,
    pub(crate) composition_offsets: Vec<i32>,
    pub(crate) media_duration: u64,
    pub(crate) presentation_duration: u64,
}

impl MuxTrackConfig {
    /// Creates one audio-track configuration with a full encoded sample-entry box.
    pub fn new_audio(track_id: u32, timescale: u32, sample_entry_box: Vec<u8>) -> Self {
        Self {
            track_id,
            kind: MuxTrackKind::Audio,
            timescale,
            language: *b"und",
            handler_name: "SoundHandler".to_string(),
            track_width: 0,
            track_height: 0,
            track_width_fixed_16_16: None,
            track_height_fixed_16_16: None,
            tkhd_flags: DEFAULT_TKHD_FLAGS,
            alternate_group: default_alternate_group_for_kind(MuxTrackKind::Audio),
            volume: 0x0100,
            matrix: DEFAULT_TKHD_MATRIX,
            edit_media_time: None,
            sample_roll_distance: None,
            emit_roll_sbgp: true,
            sample_entry_box: sample_entry_box.clone(),
            sample_entry_boxes: vec![sample_entry_box],
            sync_sample_table_mode: SyncSampleTableMode::Auto,
            stts_run_encoding_mode: SttsRunEncodingMode::CollapseIdentical,
            stsc_run_encoding_mode: StscRunEncodingMode::CollapseIdentical,
            flat_timing_override: None,
            flat_audio_profile_level_indication: None,
            fragmented_decode_time_offset: None,
            fragmented_reference_group_fragment_counts: None,
            flat_source_track_creation_time: None,
            flat_source_track_modification_time: None,
            flat_source_media_creation_time: None,
            flat_source_media_modification_time: None,
            omit_flat_iods: false,
            flat_stsc_override: None,
            preserved_flat_stbl_boxes: Vec::new(),
            preserved_flat_trak_boxes: Vec::new(),
        }
    }

    /// Creates one video-track configuration with a full encoded sample-entry box.
    pub fn new_video(
        track_id: u32,
        timescale: u32,
        width: u16,
        height: u16,
        sample_entry_box: Vec<u8>,
    ) -> Self {
        Self {
            track_id,
            kind: MuxTrackKind::Video,
            timescale,
            language: *b"und",
            handler_name: "VideoHandler".to_string(),
            track_width: width,
            track_height: height,
            track_width_fixed_16_16: None,
            track_height_fixed_16_16: None,
            tkhd_flags: DEFAULT_TKHD_FLAGS,
            alternate_group: default_alternate_group_for_kind(MuxTrackKind::Video),
            volume: 0,
            matrix: DEFAULT_TKHD_MATRIX,
            edit_media_time: None,
            sample_roll_distance: None,
            emit_roll_sbgp: true,
            sample_entry_box: sample_entry_box.clone(),
            sample_entry_boxes: vec![sample_entry_box],
            sync_sample_table_mode: SyncSampleTableMode::Auto,
            stts_run_encoding_mode: SttsRunEncodingMode::CollapseIdentical,
            stsc_run_encoding_mode: StscRunEncodingMode::CollapseIdentical,
            flat_timing_override: None,
            flat_audio_profile_level_indication: None,
            fragmented_decode_time_offset: None,
            fragmented_reference_group_fragment_counts: None,
            flat_source_track_creation_time: None,
            flat_source_track_modification_time: None,
            flat_source_media_creation_time: None,
            flat_source_media_modification_time: None,
            omit_flat_iods: false,
            flat_stsc_override: None,
            preserved_flat_stbl_boxes: Vec::new(),
            preserved_flat_trak_boxes: Vec::new(),
        }
    }

    /// Creates one timed-text track configuration with a full encoded sample-entry box.
    pub fn new_text(
        track_id: u32,
        timescale: u32,
        width: u16,
        height: u16,
        sample_entry_box: Vec<u8>,
    ) -> Self {
        Self {
            track_id,
            kind: MuxTrackKind::Text,
            timescale,
            language: *b"und",
            handler_name: "TextHandler".to_string(),
            track_width: width,
            track_height: height,
            track_width_fixed_16_16: None,
            track_height_fixed_16_16: None,
            tkhd_flags: DEFAULT_TKHD_FLAGS,
            alternate_group: default_alternate_group_for_kind(MuxTrackKind::Text),
            volume: 0,
            matrix: DEFAULT_TKHD_MATRIX,
            edit_media_time: None,
            sample_roll_distance: None,
            emit_roll_sbgp: true,
            sample_entry_box: sample_entry_box.clone(),
            sample_entry_boxes: vec![sample_entry_box],
            sync_sample_table_mode: SyncSampleTableMode::Auto,
            stts_run_encoding_mode: SttsRunEncodingMode::CollapseIdentical,
            stsc_run_encoding_mode: StscRunEncodingMode::CollapseIdentical,
            flat_timing_override: None,
            flat_audio_profile_level_indication: None,
            fragmented_decode_time_offset: None,
            fragmented_reference_group_fragment_counts: None,
            flat_source_track_creation_time: None,
            flat_source_track_modification_time: None,
            flat_source_media_creation_time: None,
            flat_source_media_modification_time: None,
            omit_flat_iods: false,
            flat_stsc_override: None,
            preserved_flat_stbl_boxes: Vec::new(),
            preserved_flat_trak_boxes: Vec::new(),
        }
    }

    /// Creates one timed-subtitle track configuration with a full encoded sample-entry box.
    pub fn new_subtitle(
        track_id: u32,
        timescale: u32,
        width: u16,
        height: u16,
        sample_entry_box: Vec<u8>,
    ) -> Self {
        Self {
            track_id,
            kind: MuxTrackKind::Subtitle,
            timescale,
            language: *b"und",
            handler_name: "SubtitleHandler".to_string(),
            track_width: width,
            track_height: height,
            track_width_fixed_16_16: None,
            track_height_fixed_16_16: None,
            tkhd_flags: DEFAULT_TKHD_FLAGS,
            alternate_group: default_alternate_group_for_kind(MuxTrackKind::Subtitle),
            volume: 0,
            matrix: DEFAULT_TKHD_MATRIX,
            edit_media_time: None,
            sample_roll_distance: None,
            emit_roll_sbgp: true,
            sample_entry_box: sample_entry_box.clone(),
            sample_entry_boxes: vec![sample_entry_box],
            sync_sample_table_mode: SyncSampleTableMode::Auto,
            stts_run_encoding_mode: SttsRunEncodingMode::CollapseIdentical,
            stsc_run_encoding_mode: StscRunEncodingMode::CollapseIdentical,
            flat_timing_override: None,
            flat_audio_profile_level_indication: None,
            fragmented_decode_time_offset: None,
            fragmented_reference_group_fragment_counts: None,
            flat_source_track_creation_time: None,
            flat_source_track_modification_time: None,
            flat_source_media_creation_time: None,
            flat_source_media_modification_time: None,
            omit_flat_iods: false,
            flat_stsc_override: None,
            preserved_flat_stbl_boxes: Vec::new(),
            preserved_flat_trak_boxes: Vec::new(),
        }
    }

    /// Returns the track identifier.
    pub const fn track_id(&self) -> u32 {
        self.track_id
    }

    /// Returns the configured track kind.
    pub const fn kind(&self) -> MuxTrackKind {
        self.kind
    }

    /// Returns the media timescale used by this track's `mdhd` and sample tables.
    pub const fn timescale(&self) -> u32 {
        self.timescale
    }

    /// Returns the three-letter ISO-639-2 language code carried by this track.
    pub const fn language(&self) -> [u8; 3] {
        self.language
    }

    /// Returns the handler name written into `hdlr`.
    pub fn handler_name(&self) -> &str {
        &self.handler_name
    }

    /// Returns the width recorded in `tkhd` for this track.
    pub const fn track_width(&self) -> u16 {
        self.track_width
    }

    /// Returns the height recorded in `tkhd` for this track.
    pub const fn track_height(&self) -> u16 {
        self.track_height
    }

    pub(crate) const fn track_width_fixed_16_16(&self) -> Option<u32> {
        self.track_width_fixed_16_16
    }

    pub(crate) const fn track_height_fixed_16_16(&self) -> Option<u32> {
        self.track_height_fixed_16_16
    }

    pub(crate) const fn tkhd_flags(&self) -> u32 {
        self.tkhd_flags
    }

    pub(crate) const fn flat_source_track_creation_time(&self) -> Option<u64> {
        self.flat_source_track_creation_time
    }

    pub(crate) const fn flat_source_track_modification_time(&self) -> Option<u64> {
        self.flat_source_track_modification_time
    }

    pub(crate) const fn flat_source_media_creation_time(&self) -> Option<u64> {
        self.flat_source_media_creation_time
    }

    pub(crate) const fn flat_source_media_modification_time(&self) -> Option<u64> {
        self.flat_source_media_modification_time
    }

    pub(crate) const fn omit_flat_iods(&self) -> bool {
        self.omit_flat_iods
    }

    pub(crate) const fn alternate_group(&self) -> i16 {
        self.alternate_group
    }

    /// Returns the fixed-point 8.8 track volume written into `tkhd`.
    pub const fn volume(&self) -> i16 {
        self.volume
    }

    pub(crate) const fn matrix(&self) -> [i32; 9] {
        self.matrix
    }

    /// Returns the optional media-time trim that should be written into one edit list.
    pub const fn edit_media_time(&self) -> Option<u64> {
        self.edit_media_time
    }

    pub(crate) const fn sample_roll_distance(&self) -> Option<i16> {
        self.sample_roll_distance
    }

    pub(crate) const fn emit_roll_sbgp(&self) -> bool {
        self.emit_roll_sbgp
    }

    /// Returns the primary full encoded sample-entry box written under `stsd`.
    pub fn sample_entry_box(&self) -> &[u8] {
        &self.sample_entry_box
    }

    /// Returns every encoded sample-entry box written under `stsd`.
    pub fn sample_entry_boxes(&self) -> &[Vec<u8>] {
        &self.sample_entry_boxes
    }

    /// Returns a copy of this configuration with a different language code.
    pub const fn with_language(mut self, language: [u8; 3]) -> Self {
        self.language = language;
        self
    }

    /// Returns a copy of this configuration with a different `hdlr` name.
    pub fn with_handler_name(mut self, handler_name: impl Into<String>) -> Self {
        self.handler_name = handler_name.into();
        self
    }

    pub(crate) const fn with_tkhd_flags(mut self, tkhd_flags: u32) -> Self {
        self.tkhd_flags = tkhd_flags;
        self
    }

    pub(crate) const fn with_flat_source_track_creation_time(
        mut self,
        flat_source_track_creation_time: Option<u64>,
    ) -> Self {
        self.flat_source_track_creation_time = flat_source_track_creation_time;
        self
    }

    pub(crate) const fn with_flat_source_track_modification_time(
        mut self,
        flat_source_track_modification_time: Option<u64>,
    ) -> Self {
        self.flat_source_track_modification_time = flat_source_track_modification_time;
        self
    }

    pub(crate) const fn with_flat_source_media_creation_time(
        mut self,
        flat_source_media_creation_time: Option<u64>,
    ) -> Self {
        self.flat_source_media_creation_time = flat_source_media_creation_time;
        self
    }

    pub(crate) const fn with_flat_source_media_modification_time(
        mut self,
        flat_source_media_modification_time: Option<u64>,
    ) -> Self {
        self.flat_source_media_modification_time = flat_source_media_modification_time;
        self
    }

    pub(crate) const fn with_omit_flat_iods(mut self, omit_flat_iods: bool) -> Self {
        self.omit_flat_iods = omit_flat_iods;
        self
    }

    pub(crate) const fn with_alternate_group(mut self, alternate_group: i16) -> Self {
        self.alternate_group = alternate_group;
        self
    }

    /// Returns a copy of this configuration with a different fixed-point 8.8 track volume.
    pub const fn with_volume(mut self, volume: i16) -> Self {
        self.volume = volume;
        self
    }

    pub(crate) const fn with_matrix(mut self, matrix: [i32; 9]) -> Self {
        self.matrix = matrix;
        self
    }

    pub(crate) const fn with_tkhd_dimensions_fixed_16_16(
        mut self,
        track_width_fixed_16_16: u32,
        track_height_fixed_16_16: u32,
    ) -> Self {
        self.track_width_fixed_16_16 = Some(track_width_fixed_16_16);
        self.track_height_fixed_16_16 = Some(track_height_fixed_16_16);
        self
    }

    /// Returns a copy of this configuration with one edit-list media-time trim.
    pub const fn with_edit_media_time(mut self, edit_media_time: u64) -> Self {
        self.edit_media_time = Some(edit_media_time);
        self
    }

    pub(crate) const fn with_sample_roll_distance(mut self, sample_roll_distance: i16) -> Self {
        self.sample_roll_distance = Some(sample_roll_distance);
        self
    }

    pub(crate) const fn with_emit_roll_sbgp(mut self, emit_roll_sbgp: bool) -> Self {
        self.emit_roll_sbgp = emit_roll_sbgp;
        self
    }

    pub(crate) fn with_sample_entry_boxes(mut self, sample_entry_boxes: Vec<Vec<u8>>) -> Self {
        if let Some(first) = sample_entry_boxes.first() {
            self.sample_entry_box = first.clone();
        }
        self.sample_entry_boxes = sample_entry_boxes;
        self
    }

    pub(crate) const fn with_sync_sample_table_mode(
        mut self,
        sync_sample_table_mode: SyncSampleTableMode,
    ) -> Self {
        self.sync_sample_table_mode = sync_sample_table_mode;
        self
    }

    pub(crate) const fn stts_run_encoding_mode(&self) -> SttsRunEncodingMode {
        self.stts_run_encoding_mode
    }

    pub(crate) const fn with_stts_run_encoding_mode(
        mut self,
        stts_run_encoding_mode: SttsRunEncodingMode,
    ) -> Self {
        self.stts_run_encoding_mode = stts_run_encoding_mode;
        self
    }

    pub(crate) const fn stsc_run_encoding_mode(&self) -> StscRunEncodingMode {
        self.stsc_run_encoding_mode
    }

    pub(crate) const fn with_stsc_run_encoding_mode(
        mut self,
        stsc_run_encoding_mode: StscRunEncodingMode,
    ) -> Self {
        self.stsc_run_encoding_mode = stsc_run_encoding_mode;
        self
    }

    pub(crate) fn flat_timing_override(&self) -> Option<&FlatTimingOverride> {
        self.flat_timing_override.as_ref()
    }

    pub(crate) fn with_flat_timing_override(
        mut self,
        flat_timing_override: FlatTimingOverride,
    ) -> Self {
        self.flat_timing_override = Some(flat_timing_override);
        self
    }

    pub(crate) const fn flat_audio_profile_level_indication(&self) -> Option<u8> {
        self.flat_audio_profile_level_indication
    }

    pub(crate) const fn with_flat_audio_profile_level_indication(
        mut self,
        flat_audio_profile_level_indication: u8,
    ) -> Self {
        self.flat_audio_profile_level_indication = Some(flat_audio_profile_level_indication);
        self
    }

    pub(crate) const fn fragmented_decode_time_offset(&self) -> Option<u64> {
        self.fragmented_decode_time_offset
    }

    pub(crate) const fn with_fragmented_decode_time_offset(
        mut self,
        fragmented_decode_time_offset: u64,
    ) -> Self {
        self.fragmented_decode_time_offset = Some(fragmented_decode_time_offset);
        self
    }

    pub(crate) fn fragmented_reference_group_fragment_counts(&self) -> Option<&[u32]> {
        self.fragmented_reference_group_fragment_counts.as_deref()
    }

    pub(crate) fn with_fragmented_reference_group_fragment_counts(
        mut self,
        fragmented_reference_group_fragment_counts: Vec<u32>,
    ) -> Self {
        self.fragmented_reference_group_fragment_counts =
            Some(fragmented_reference_group_fragment_counts);
        self
    }

    pub(crate) fn flat_stsc_override(&self) -> Option<&crate::boxes::iso14496_12::Stsc> {
        self.flat_stsc_override.as_ref()
    }

    pub(crate) fn with_flat_stsc_override(
        mut self,
        flat_stsc_override: crate::boxes::iso14496_12::Stsc,
    ) -> Self {
        self.flat_stsc_override = Some(flat_stsc_override);
        self
    }

    pub(crate) fn preserved_flat_stbl_boxes(&self) -> &[Vec<u8>] {
        &self.preserved_flat_stbl_boxes
    }

    pub(crate) fn with_preserved_flat_stbl_boxes(
        mut self,
        preserved_flat_stbl_boxes: Vec<Vec<u8>>,
    ) -> Self {
        self.preserved_flat_stbl_boxes = preserved_flat_stbl_boxes;
        self
    }

    pub(crate) fn preserved_flat_trak_boxes(&self) -> &[Vec<u8>] {
        &self.preserved_flat_trak_boxes
    }

    pub(crate) fn with_preserved_flat_trak_boxes(
        mut self,
        preserved_flat_trak_boxes: Vec<Vec<u8>>,
    ) -> Self {
        self.preserved_flat_trak_boxes = preserved_flat_trak_boxes;
        self
    }
}

/// Errors returned by the additive mux foundation helpers.
#[derive(Debug)]
pub enum MuxError {
    /// One public mux track spec did not match the fixed supported grammar.
    InvalidTrackSpec { spec: String, message: String },
    /// The current fragmented mux request selected more than one video track for one output.
    MultipleVideoTracks { count: usize },
    /// The current mux request did not carry any tracks.
    MissingTrackSpecs,
    /// One requested MP4 track selector did not resolve to a matching track.
    MissingTrackSelection { spec: String },
    /// One track import was recognized but is not supported by the current mux follow-on.
    UnsupportedTrackImport { spec: String, message: String },
    /// One duration-boundary mode conflicts with the current request shape or requested value.
    InvalidDurationMode { mode: &'static str, message: String },
    /// One explicit mux output layout conflicts with the current request shape.
    InvalidOutputLayout {
        layout: &'static str,
        message: String,
    },
    /// One explicit destination mode conflicts with the current request shape.
    InvalidDestinationMode { mode: &'static str, message: String },
    /// The output path conflicts with one of the supplied input paths.
    OutputPathConflict { output: PathBuf, input: PathBuf },
    /// One track timeline could not be normalized onto the selected movie timescale exactly.
    IncompatibleTrackTiming {
        track_id: u32,
        track_timescale: u32,
        movie_timescale: u32,
        value: i64,
    },
    /// One chunk or segment coordination plan was internally inconsistent.
    InvalidChunkPlan { track_id: u32, message: String },
    /// The planned payload would overflow a 64-bit output offset or size.
    PayloadSizeOverflow,
    /// One planned item referenced a staged source index that was not provided by the caller.
    MissingSourceIndex {
        source_index: usize,
        source_count: usize,
    },
    /// A progressive source would need to seek backward to satisfy the staged plan.
    NonMonotonicSourceOffset {
        source_index: usize,
        previous_offset: u64,
        next_offset: u64,
    },
    /// A progressive source ended before it reached the requested staged offset.
    IncompleteAdvance {
        source_index: usize,
        expected_offset: u64,
        actual_offset: u64,
    },
    /// A source did not produce the number of bytes described by the plan.
    IncompleteCopy {
        source_index: usize,
        expected_size: u64,
        actual_size: u64,
    },
    /// The real mux surface requires a non-zero movie timescale.
    InvalidMovieTimescale,
    /// One real mux track configuration used a zero or otherwise incompatible media timescale.
    InvalidTrackTimescale { track_id: u32 },
    /// One real mux track language code was not a valid three-letter ISO-639-2 code.
    InvalidTrackLanguage { track_id: u32, language: String },
    /// More than one track configuration used the same track identifier.
    DuplicateTrackId { track_id: u32 },
    /// The plan referenced a track that was not configured for the real mux surface.
    MissingTrackId { track_id: u32 },
    /// One configured track had no planned samples.
    TrackHasNoSamples { track_id: u32 },
    /// One track regressed in decode ordering inside the mux event graph.
    NonMonotonicTrackDecodeTime {
        track_id: u32,
        previous_decode_time: u64,
        next_decode_time: u64,
    },
    /// One configured sample-entry box was not a single valid encoded box.
    InvalidSampleEntryBox { track_id: u32, message: String },
    /// The real mux layout overflowed one container field.
    LayoutOverflow(&'static str),
    /// A typed box payload could not be encoded.
    Codec(CodecError),
    /// A container box could not be written or finalized.
    Writer(WriterError),
    /// A box header could not be parsed or encoded.
    Header(HeaderError),
    /// One typed extract helper failed while importing a track.
    Extract(crate::extract::ExtractError),
    /// One typed probe helper failed while importing a track.
    Probe(crate::probe::ProbeError),
    /// An I/O error occurred while reading staged payloads or writing output bytes.
    Io(io::Error),
}

impl fmt::Display for MuxError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidTrackSpec { spec, message } => {
                write!(f, "invalid mux track spec `{spec}`: {message}")
            }
            Self::MultipleVideoTracks { count } => write!(
                f,
                "fragmented output supports at most one video track per mux output, but {count} were requested"
            ),
            Self::MissingTrackSpecs => {
                write!(
                    f,
                    "the current mux surface requires at least one `--track` input"
                )
            }
            Self::MissingTrackSelection { spec } => {
                write!(
                    f,
                    "mux track spec `{spec}` did not resolve to a matching input track"
                )
            }
            Self::UnsupportedTrackImport { spec, message } => {
                write!(f, "mux track spec `{spec}` is not supported: {message}")
            }
            Self::InvalidDurationMode { mode, message } => {
                write!(f, "invalid mux {mode}: {message}")
            }
            Self::InvalidOutputLayout { layout, message } => {
                write!(f, "invalid mux layout `{layout}`: {message}")
            }
            Self::InvalidDestinationMode { mode, message } => {
                write!(f, "invalid mux destination mode `{mode}`: {message}")
            }
            Self::OutputPathConflict { output, input } => write!(
                f,
                "output path `{}` conflicts with input `{}`",
                output.display(),
                input.display()
            ),
            Self::IncompatibleTrackTiming {
                track_id,
                track_timescale,
                movie_timescale,
                value,
            } => write!(
                f,
                "track {track_id} timing value {value} from timescale {track_timescale} cannot be normalized exactly onto movie timescale {movie_timescale}"
            ),
            Self::InvalidChunkPlan { track_id, message } => {
                write!(
                    f,
                    "track {track_id} produced an invalid chunk plan: {message}"
                )
            }
            Self::PayloadSizeOverflow => {
                write!(f, "planned mux payload size overflowed the supported range")
            }
            Self::MissingSourceIndex {
                source_index,
                source_count,
            } => write!(
                f,
                "mux plan referenced source index {source_index}, but only {source_count} sources were provided"
            ),
            Self::NonMonotonicSourceOffset {
                source_index,
                previous_offset,
                next_offset,
            } => write!(
                f,
                "source index {source_index} would need to move backward from offset {previous_offset} to {next_offset}"
            ),
            Self::IncompleteAdvance {
                source_index,
                expected_offset,
                actual_offset,
            } => write!(
                f,
                "source index {source_index} ended while advancing to offset {expected_offset}; only reached {actual_offset}"
            ),
            Self::IncompleteCopy {
                source_index,
                expected_size,
                actual_size,
            } => write!(
                f,
                "source index {source_index} produced {actual_size} bytes, expected {expected_size}"
            ),
            Self::InvalidMovieTimescale => {
                write!(f, "real mux output requires a non-zero movie timescale")
            }
            Self::InvalidTrackTimescale { track_id } => {
                write!(
                    f,
                    "track {track_id} uses an invalid or incompatible media timescale for the planned mux timeline"
                )
            }
            Self::InvalidTrackLanguage { track_id, language } => write!(
                f,
                "track {track_id} uses invalid language code `{language}`; expected three ASCII letters"
            ),
            Self::DuplicateTrackId { track_id } => {
                write!(f, "duplicate mux track id {track_id}")
            }
            Self::MissingTrackId { track_id } => {
                write!(
                    f,
                    "mux plan referenced track id {track_id}, but no matching track configuration was provided"
                )
            }
            Self::TrackHasNoSamples { track_id } => {
                write!(f, "mux track {track_id} has no planned samples")
            }
            Self::NonMonotonicTrackDecodeTime {
                track_id,
                previous_decode_time,
                next_decode_time,
            } => write!(
                f,
                "track {track_id} regressed in decode order from {previous_decode_time} to {next_decode_time}"
            ),
            Self::InvalidSampleEntryBox { track_id, message } => write!(
                f,
                "track {track_id} provided an invalid sample-entry box: {message}"
            ),
            Self::LayoutOverflow(field) => write!(
                f,
                "real mux layout overflowed the supported range while building {field}"
            ),
            Self::Codec(error) => error.fmt(f),
            Self::Writer(error) => error.fmt(f),
            Self::Header(error) => error.fmt(f),
            Self::Extract(error) => error.fmt(f),
            Self::Probe(error) => error.fmt(f),
            Self::Io(error) => write!(f, "{error}"),
        }
    }
}

impl MuxError {
    /// Stable coarse category label for additive mux diagnostics.
    pub fn category(&self) -> &'static str {
        match self {
            Self::InvalidTrackSpec { .. }
            | Self::MultipleVideoTracks { .. }
            | Self::MissingTrackSpecs
            | Self::MissingTrackSelection { .. }
            | Self::InvalidDurationMode { .. }
            | Self::InvalidOutputLayout { .. }
            | Self::InvalidDestinationMode { .. }
            | Self::OutputPathConflict { .. }
            | Self::InvalidMovieTimescale
            | Self::InvalidTrackTimescale { .. }
            | Self::InvalidTrackLanguage { .. } => "input",
            Self::UnsupportedTrackImport { .. } => "unsupported",
            Self::IncompatibleTrackTiming { .. } | Self::NonMonotonicTrackDecodeTime { .. } => {
                "timing"
            }
            Self::InvalidChunkPlan { .. }
            | Self::PayloadSizeOverflow
            | Self::MissingSourceIndex { .. }
            | Self::NonMonotonicSourceOffset { .. }
            | Self::IncompleteAdvance { .. }
            | Self::IncompleteCopy { .. }
            | Self::DuplicateTrackId { .. }
            | Self::MissingTrackId { .. }
            | Self::TrackHasNoSamples { .. }
            | Self::InvalidSampleEntryBox { .. }
            | Self::LayoutOverflow(_) => "layout",
            Self::Codec(_) | Self::Writer(_) | Self::Header(_) => "writer",
            Self::Extract(_) | Self::Probe(_) => "input",
            Self::Io(_) => "io",
        }
    }

    /// Stable coarse stage label for additive mux diagnostics.
    pub fn stage(&self) -> &'static str {
        match self {
            Self::InvalidTrackSpec { .. }
            | Self::MultipleVideoTracks { .. }
            | Self::MissingTrackSpecs
            | Self::InvalidDurationMode { .. }
            | Self::InvalidOutputLayout { .. }
            | Self::InvalidDestinationMode { .. }
            | Self::OutputPathConflict { .. } => "request",
            Self::MissingTrackSelection { .. }
            | Self::UnsupportedTrackImport { .. }
            | Self::Extract(_)
            | Self::Probe(_) => "import",
            Self::IncompatibleTrackTiming { .. }
            | Self::InvalidChunkPlan { .. }
            | Self::PayloadSizeOverflow
            | Self::MissingSourceIndex { .. }
            | Self::InvalidMovieTimescale
            | Self::InvalidTrackTimescale { .. }
            | Self::InvalidTrackLanguage { .. }
            | Self::DuplicateTrackId { .. }
            | Self::MissingTrackId { .. }
            | Self::TrackHasNoSamples { .. }
            | Self::NonMonotonicTrackDecodeTime { .. }
            | Self::InvalidSampleEntryBox { .. }
            | Self::LayoutOverflow(_) => "plan",
            Self::NonMonotonicSourceOffset { .. }
            | Self::IncompleteAdvance { .. }
            | Self::IncompleteCopy { .. }
            | Self::Io(_) => "payload",
            Self::Codec(_) | Self::Writer(_) | Self::Header(_) => "write",
        }
    }
}

impl Error for MuxError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Codec(error) => Some(error),
            Self::Writer(error) => Some(error),
            Self::Header(error) => Some(error),
            Self::Extract(error) => Some(error),
            Self::Probe(error) => Some(error),
            Self::Io(error) => Some(error),
            _ => None,
        }
    }
}

impl From<io::Error> for MuxError {
    fn from(error: io::Error) -> Self {
        Self::Io(error)
    }
}

impl From<CodecError> for MuxError {
    fn from(error: CodecError) -> Self {
        Self::Codec(error)
    }
}

impl From<WriterError> for MuxError {
    fn from(error: WriterError) -> Self {
        Self::Writer(error)
    }
}

impl From<HeaderError> for MuxError {
    fn from(error: HeaderError) -> Self {
        Self::Header(error)
    }
}

impl From<crate::extract::ExtractError> for MuxError {
    fn from(error: crate::extract::ExtractError) -> Self {
        Self::Extract(error)
    }
}

impl From<crate::probe::ProbeError> for MuxError {
    fn from(error: crate::probe::ProbeError) -> Self {
        Self::Probe(error)
    }
}

/// Plans one output payload order from staged media items using the selected interleave policy.
pub fn plan_staged_media_items(
    items: Vec<MuxStagedMediaItem>,
    interleave_policy: MuxInterleavePolicy,
) -> Result<MuxPlan, MuxError> {
    plan_staged_media_items_with_coordination(items, interleave_policy, Vec::new())
}

/// Plans one output payload order with explicit per-track chunk sample counts.
///
/// The chunk counts are required by fragmented low-level writers because each media fragment is
/// built from one chunk ordinal across the participating tracks. The counts for each track must
/// cover that track's staged samples exactly.
pub fn plan_staged_media_items_with_chunk_sample_counts<I>(
    items: Vec<MuxStagedMediaItem>,
    interleave_policy: MuxInterleavePolicy,
    chunk_sample_counts_by_track: I,
) -> Result<MuxPlan, MuxError>
where
    I: IntoIterator<Item = (u32, Vec<u32>)>,
{
    let coordination = chunk_sample_counts_by_track
        .into_iter()
        .map(|(track_id, chunk_sample_counts)| {
            TrackCoordinationDirective::new(track_id, chunk_sample_counts)
        })
        .collect();
    plan_staged_media_items_with_coordination(items, interleave_policy, coordination)
}

pub(crate) fn plan_staged_media_items_with_coordination(
    items: Vec<MuxStagedMediaItem>,
    interleave_policy: MuxInterleavePolicy,
    coordination_directives: Vec<TrackCoordinationDirective>,
) -> Result<MuxPlan, MuxError> {
    let mut queue_items = items
        .into_iter()
        .map(MuxQueueItem::from_staged)
        .collect::<Vec<_>>();

    match interleave_policy {
        MuxInterleavePolicy::DecodeTime | MuxInterleavePolicy::ChunkOrdinalThenSource => {
            // Keep equal decode-time items stable by source and byte offset before the queue
            // layer applies the decode-time ordering key. This preserves path-first merge order
            // even when a carried track keeps a large external track identifier such as a TS PID.
            queue_items.sort_by_key(|item| {
                (
                    item.staged.source_index,
                    item.staged.data_offset,
                    item.staged.track_id,
                )
            });
        }
    }

    let queue = OrderedWorkQueue::new(queue_items);
    let mut items_by_track = BTreeMap::<u32, Vec<MuxStagedMediaItem>>::new();
    let mut track_state = BTreeMap::<u32, MuxTrackPlanState>::new();

    for item in queue.iter() {
        let end_decode_time = item
            .staged
            .decode_time
            .checked_add(u64::from(item.staged.duration))
            .ok_or(MuxError::PayloadSizeOverflow)?;
        items_by_track
            .entry(item.staged.track_id)
            .or_default()
            .push(item.staged);
        track_state
            .entry(item.staged.track_id)
            .and_modify(|state| {
                state.item_count += 1;
                state.end_decode_time = state.end_decode_time.max(end_decode_time);
                state.first_decode_time = state.first_decode_time.min(item.staged.decode_time);
            })
            .or_insert(MuxTrackPlanState {
                item_count: 1,
                first_decode_time: item.staged.decode_time,
                end_decode_time,
            });
    }

    let track_plans = track_state
        .into_iter()
        .map(|(track_id, state)| MuxTrackPlan {
            track_id,
            item_count: state.item_count,
            first_decode_time: state.first_decode_time,
            end_decode_time: state.end_decode_time,
        })
        .collect::<Vec<_>>();

    let coordination =
        MuxCoordinationPlan::from_track_plans(&track_plans, coordination_directives)?;
    let (planned_items, total_payload_size) =
        build_planned_items_from_tracks(&items_by_track, &coordination, interleave_policy)?;
    let event_graph = MuxEventGraph::from_plan(
        &planned_items,
        &track_plans,
        total_payload_size,
        &coordination,
    );

    Ok(MuxPlan {
        interleave_policy,
        planned_items,
        track_plans,
        total_payload_size,
        coordination,
        event_graph,
    })
}

/// Writes one real MP4 file to `writer` from staged seekable `sources`, `plan`, and track
/// metadata.
///
/// This higher-level mux surface assembles `ftyp`, `moov`, and `mdat` around the staged sample
/// order produced by [`plan_staged_media_items`]. The lower-level payload-copy helpers remain
/// available for callers that only need interleaved raw payload output.
pub fn write_mp4_mux<R, W>(
    sources: &mut [R],
    writer: &mut W,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    R: Read + Seek,
    W: Write,
{
    mp4::write_mp4_mux(sources, writer, file_config, track_configs, plan)
}

/// Opens staged source files and writes one real MP4 file to `output_path`.
pub fn write_mp4_mux_to_path<P, Q>(
    source_paths: &[P],
    output_path: Q,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    P: AsRef<Path>,
    Q: AsRef<Path>,
{
    mp4::write_mp4_mux_to_path(source_paths, output_path, file_config, track_configs, plan)
}

/// Writes one fragmented MP4 to `writer` from staged seekable `sources`, `plan`, and track
/// metadata.
///
/// The emitted byte stream is one initialization section, one top-level index when present, and
/// one or more media fragments in the same order as the high-level fragmented mux request path.
pub fn write_fragmented_mp4_mux<R, W>(
    sources: &mut [R],
    writer: &mut W,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    single_sidx_reference: bool,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    R: Read + Seek,
    W: Write,
{
    mp4::write_fragmented_mp4_mux(
        sources,
        writer,
        file_config,
        track_configs,
        single_sidx_reference,
        plan,
    )
}

/// Opens staged source files and writes one fragmented MP4 file to `output_path`.
pub fn write_fragmented_mp4_mux_to_path<P, Q>(
    source_paths: &[P],
    output_path: Q,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    single_sidx_reference: bool,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    P: AsRef<Path>,
    Q: AsRef<Path>,
{
    let mut sources = source_paths
        .iter()
        .map(File::open)
        .collect::<Result<Vec<_>, _>>()?;
    let mut writer = BufWriter::new(File::create(output_path)?);
    write_fragmented_mp4_mux(
        &mut sources,
        &mut writer,
        file_config,
        track_configs,
        single_sidx_reference,
        plan,
    )
}

/// Writes fragmented initialization bytes and media-fragment bytes to separate writers.
///
/// Concatenating the init writer output followed by the media writer output yields the same byte
/// stream as [`write_fragmented_mp4_mux`] except for volatile creation-time fields.
pub fn write_fragmented_mp4_mux_split<R, I, M>(
    sources: &mut [R],
    init_writer: &mut I,
    media_writer: &mut M,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    single_sidx_reference: bool,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    R: Read + Seek,
    I: Write,
    M: Write,
{
    mp4::write_fragmented_mp4_mux_split(
        sources,
        init_writer,
        media_writer,
        file_config,
        track_configs,
        single_sidx_reference,
        plan,
    )
}

/// Writes fragmented initialization bytes and standalone media-segment bytes to separate writers.
///
/// The media writer receives concatenated standalone media-segment units. Each unit starts with a
/// segment type box, then a local segment index, then the media fragment bytes.
pub fn write_fragmented_mp4_mux_segmented<R, I, M>(
    sources: &mut [R],
    init_writer: &mut I,
    media_writer: &mut M,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    R: Read + Seek,
    I: Write,
    M: Write,
{
    mp4::write_fragmented_mp4_mux_segmented(
        sources,
        init_writer,
        media_writer,
        file_config,
        track_configs,
        plan,
    )
}

/// Opens staged source files and writes fragmented init/media outputs to separate paths.
pub fn write_fragmented_mp4_mux_split_to_paths<P, I, M>(
    source_paths: &[P],
    init_path: I,
    media_path: M,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    single_sidx_reference: bool,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    P: AsRef<Path>,
    I: AsRef<Path>,
    M: AsRef<Path>,
{
    let mut sources = source_paths
        .iter()
        .map(File::open)
        .collect::<Result<Vec<_>, _>>()?;
    let mut init_writer = BufWriter::new(File::create(init_path)?);
    let mut media_writer = BufWriter::new(File::create(media_path)?);
    write_fragmented_mp4_mux_split(
        &mut sources,
        &mut init_writer,
        &mut media_writer,
        file_config,
        track_configs,
        single_sidx_reference,
        plan,
    )
}

/// Writes one fragmented MP4 and flushes the writer after the top-level index and after each
/// media fragment.
///
/// This preserves the same final bytes as [`write_fragmented_mp4_mux`] while exposing deterministic
/// flush points for callers that send the stream incrementally.
pub fn write_fragmented_mp4_mux_chunked<R, W>(
    sources: &mut [R],
    writer: &mut W,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    single_sidx_reference: bool,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    R: Read + Seek,
    W: Write,
{
    mp4::write_fragmented_mp4_mux_chunked(
        sources,
        writer,
        file_config,
        track_configs,
        single_sidx_reference,
        plan,
    )
}

/// Writes one real MP4 file through the additive Tokio-based async mux surface.
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "mux", feature = "async"))))]
pub async fn write_mp4_mux_async<R, W>(
    sources: &mut [R],
    writer: &mut W,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    R: AsyncReadSeek,
    W: AsyncWrite + Unpin,
{
    mp4::write_mp4_mux_async(sources, writer, file_config, track_configs, plan).await
}

/// Opens staged source files asynchronously and writes one real MP4 file to `output_path`.
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "mux", feature = "async"))))]
pub async fn write_mp4_mux_to_path_async<P, Q>(
    source_paths: &[P],
    output_path: Q,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    P: AsRef<Path>,
    Q: AsRef<Path>,
{
    mp4::write_mp4_mux_to_path_async(source_paths, output_path, file_config, track_configs, plan)
        .await
}

/// Writes one fragmented MP4 through the additive Tokio-based async mux surface.
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "mux", feature = "async"))))]
pub async fn write_fragmented_mp4_mux_async<R, W>(
    sources: &mut [R],
    writer: &mut W,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    single_sidx_reference: bool,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    R: AsyncReadSeek,
    W: AsyncWrite + Unpin,
{
    mp4::write_fragmented_mp4_mux_async(
        sources,
        writer,
        file_config,
        track_configs,
        single_sidx_reference,
        plan,
    )
    .await
}

/// Opens staged source files asynchronously and writes one fragmented MP4 to `output_path`.
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "mux", feature = "async"))))]
pub async fn write_fragmented_mp4_mux_to_path_async<P, Q>(
    source_paths: &[P],
    output_path: Q,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    single_sidx_reference: bool,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    P: AsRef<Path>,
    Q: AsRef<Path>,
{
    let mut sources = Vec::with_capacity(source_paths.len());
    for path in source_paths {
        sources.push(TokioFile::open(path).await?);
    }
    let output = TokioFile::create(output_path).await?;
    let mut writer = tokio::io::BufWriter::new(output);
    write_fragmented_mp4_mux_async(
        &mut sources,
        &mut writer,
        file_config,
        track_configs,
        single_sidx_reference,
        plan,
    )
    .await
}

/// Writes fragmented initialization bytes and media-fragment bytes to separate async writers.
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "mux", feature = "async"))))]
pub async fn write_fragmented_mp4_mux_split_async<R, I, M>(
    sources: &mut [R],
    init_writer: &mut I,
    media_writer: &mut M,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    single_sidx_reference: bool,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    R: AsyncReadSeek,
    I: AsyncWrite + Unpin,
    M: AsyncWrite + Unpin,
{
    mp4::write_fragmented_mp4_mux_split_async(
        sources,
        init_writer,
        media_writer,
        file_config,
        track_configs,
        single_sidx_reference,
        plan,
    )
    .await
}

/// Writes fragmented initialization bytes and standalone media-segment bytes to separate async
/// writers.
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "mux", feature = "async"))))]
pub async fn write_fragmented_mp4_mux_segmented_async<R, I, M>(
    sources: &mut [R],
    init_writer: &mut I,
    media_writer: &mut M,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    R: AsyncReadSeek,
    I: AsyncWrite + Unpin,
    M: AsyncWrite + Unpin,
{
    mp4::write_fragmented_mp4_mux_segmented_async(
        sources,
        init_writer,
        media_writer,
        file_config,
        track_configs,
        plan,
    )
    .await
}

/// Writes one fragmented MP4 asynchronously and flushes after the top-level index and each media
/// fragment.
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "mux", feature = "async"))))]
pub async fn write_fragmented_mp4_mux_chunked_async<R, W>(
    sources: &mut [R],
    writer: &mut W,
    file_config: &MuxFileConfig,
    track_configs: &[MuxTrackConfig],
    single_sidx_reference: bool,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    R: AsyncReadSeek,
    W: AsyncWrite + Unpin,
{
    mp4::write_fragmented_mp4_mux_chunked_async(
        sources,
        writer,
        file_config,
        track_configs,
        single_sidx_reference,
        plan,
    )
    .await
}

/// Copies the payload bytes described by `plan` from the staged seekable `sources` into
/// `writer`.
pub fn copy_planned_payloads<R, W>(
    sources: &mut [R],
    writer: &mut W,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    R: Read + Seek,
    W: Write,
{
    let mut cursor = plan.event_graph.cursor();
    while let Some(sample) = cursor.next_sample() {
        let staged = sample.planned_item().staged();
        let Some(source) = sources.get_mut(staged.source_index()) else {
            return Err(MuxError::MissingSourceIndex {
                source_index: staged.source_index(),
                source_count: sources.len(),
            });
        };

        source.seek(SeekFrom::Start(staged.data_offset()))?;
        let mut limited = source.take(u64::from(staged.data_size()));
        let copied = io::copy(&mut limited, writer)?;
        if copied != u64::from(staged.data_size()) {
            return Err(MuxError::IncompleteCopy {
                source_index: staged.source_index(),
                expected_size: u64::from(staged.data_size()),
                actual_size: copied,
            });
        }
    }

    Ok(())
}

/// Copies the payload bytes described by `plan` from staged non-seekable `sources` into `writer`.
///
/// This progressive path keeps one forward-only read cursor per source. It supports plans whose
/// staged items consume each source in monotonic byte-offset order, and it reports a structured
/// error when a caller asks it to seek backward implicitly.
pub fn copy_planned_payloads_progressive<R, W>(
    sources: &mut [R],
    writer: &mut W,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    R: Read,
    W: Write,
{
    let mut source_offsets = vec![0_u64; sources.len()];
    let mut cursor = plan.event_graph.cursor();
    while let Some(sample) = cursor.next_sample() {
        let staged = sample.planned_item().staged();
        let Some(source) = sources.get_mut(staged.source_index()) else {
            return Err(MuxError::MissingSourceIndex {
                source_index: staged.source_index(),
                source_count: sources.len(),
            });
        };

        let source_offset = source_offsets.get_mut(staged.source_index()).unwrap();
        advance_progressive_source(
            source,
            staged.source_index(),
            source_offset,
            staged.data_offset(),
        )?;
        copy_progressive_payload(
            source,
            writer,
            staged.source_index(),
            source_offset,
            u64::from(staged.data_size()),
        )?;
    }

    Ok(())
}

/// Opens staged source files and copies the payload bytes described by `plan` into `output_path`.
pub fn copy_planned_payloads_to_path<P, Q>(
    source_paths: &[P],
    output_path: Q,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    P: AsRef<Path>,
    Q: AsRef<Path>,
{
    let mut sources = source_paths
        .iter()
        .map(File::open)
        .collect::<Result<Vec<_>, _>>()?;
    let mut writer = BufWriter::new(File::create(output_path)?);
    copy_planned_payloads(&mut sources, &mut writer, plan)
}

/// Copies the payload bytes described by `plan` from the staged seekable async `sources` into
/// `writer`.
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "mux", feature = "async"))))]
pub async fn copy_planned_payloads_async<R, W>(
    sources: &mut [R],
    writer: &mut W,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    R: AsyncReadSeek,
    W: AsyncWrite + Unpin,
{
    let mut buffer = vec![0_u8; 16 * 1024];
    let mut cursor = plan.event_graph.cursor();
    while let Some(sample) = cursor.next_sample() {
        let staged = sample.planned_item().staged();
        let Some(source) = sources.get_mut(staged.source_index()) else {
            return Err(MuxError::MissingSourceIndex {
                source_index: staged.source_index(),
                source_count: sources.len(),
            });
        };

        source.seek(SeekFrom::Start(staged.data_offset())).await?;
        let mut remaining = u64::from(staged.data_size());
        let mut copied = 0_u64;
        while remaining > 0 {
            let chunk_len = remaining.min(buffer.len() as u64) as usize;
            let read = source.read(&mut buffer[..chunk_len]).await?;
            if read == 0 {
                break;
            }
            writer.write_all(&buffer[..read]).await?;
            copied += read as u64;
            remaining -= read as u64;
        }

        if copied != u64::from(staged.data_size()) {
            return Err(MuxError::IncompleteCopy {
                source_index: staged.source_index(),
                expected_size: u64::from(staged.data_size()),
                actual_size: copied,
            });
        }
    }

    writer.flush().await?;
    Ok(())
}

/// Copies the payload bytes described by `plan` from staged non-seekable async `sources` into
/// `writer`.
///
/// Like [`copy_planned_payloads_progressive`], this path supports only plans whose staged items
/// consume each source in monotonic byte-offset order.
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "mux", feature = "async"))))]
pub async fn copy_planned_payloads_async_progressive<R, W>(
    sources: &mut [R],
    writer: &mut W,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    R: AsyncReadForward,
    W: AsyncWriteForward,
{
    let mut source_offsets = vec![0_u64; sources.len()];
    let mut buffer = vec![0_u8; 16 * 1024];
    let mut cursor = plan.event_graph.cursor();
    while let Some(sample) = cursor.next_sample() {
        let staged = sample.planned_item().staged();
        let Some(source) = sources.get_mut(staged.source_index()) else {
            return Err(MuxError::MissingSourceIndex {
                source_index: staged.source_index(),
                source_count: sources.len(),
            });
        };

        let source_offset = source_offsets.get_mut(staged.source_index()).unwrap();
        advance_progressive_source_async(
            source,
            staged.source_index(),
            source_offset,
            staged.data_offset(),
            &mut buffer,
        )
        .await?;
        copy_progressive_payload_async(
            source,
            writer,
            staged.source_index(),
            source_offset,
            u64::from(staged.data_size()),
            &mut buffer,
        )
        .await?;
    }

    writer.flush().await?;
    Ok(())
}

/// Opens staged source files asynchronously and copies the payload bytes described by `plan` into
/// `output_path`.
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "mux", feature = "async"))))]
pub async fn copy_planned_payloads_to_path_async<P, Q>(
    source_paths: &[P],
    output_path: Q,
    plan: &MuxPlan,
) -> Result<(), MuxError>
where
    P: AsRef<Path>,
    Q: AsRef<Path>,
{
    let mut sources = Vec::with_capacity(source_paths.len());
    for path in source_paths {
        sources.push(TokioFile::open(path).await?);
    }
    let mut writer = TokioFile::create(output_path).await?;
    copy_planned_payloads_async(&mut sources, &mut writer, plan).await
}

struct MuxQueueItem {
    staged: MuxStagedMediaItem,
}

impl MuxQueueItem {
    fn from_staged(staged: MuxStagedMediaItem) -> Self {
        Self { staged }
    }
}

impl QueueWorkItem for MuxQueueItem {
    fn queue_order_key(&self) -> u64 {
        self.staged.decode_time
    }
}

struct MuxTrackPlanState {
    item_count: u32,
    first_decode_time: u64,
    end_decode_time: u64,
}

#[derive(Clone, Copy)]
struct PlannedChunk {
    chunk_index: usize,
    order_key: PlannedChunkOrderKey,
    track_id: u32,
    start_index: usize,
    end_index: usize,
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct PlannedChunkOrderKey {
    decode_time: u64,
    source_index: usize,
    data_offset: u64,
    track_id: u32,
}

fn build_planned_items_from_tracks(
    items_by_track: &BTreeMap<u32, Vec<MuxStagedMediaItem>>,
    coordination: &MuxCoordinationPlan,
    interleave_policy: MuxInterleavePolicy,
) -> Result<(Vec<MuxPlannedMediaItem>, u64), MuxError> {
    let mut chunks = Vec::new();
    let total_sample_count = items_by_track.values().map(Vec::len).sum();
    for (&track_id, items) in items_by_track {
        let chunk_sample_counts = coordination.chunk_sample_counts(track_id)?;
        let mut start_index = 0_usize;
        for (chunk_index, &samples_per_chunk) in chunk_sample_counts.iter().enumerate() {
            let chunk_len = usize::try_from(samples_per_chunk)
                .map_err(|_| MuxError::LayoutOverflow("chunk sample-count conversion"))?;
            let end_index = start_index
                .checked_add(chunk_len)
                .ok_or(MuxError::LayoutOverflow("chunk sample indexing"))?;
            let first_sample =
                items
                    .get(start_index)
                    .ok_or_else(|| MuxError::InvalidChunkPlan {
                        track_id,
                        message: "chunk boundaries ran past the staged sample count".to_string(),
                    })?;
            chunks.push(PlannedChunk {
                chunk_index,
                order_key: PlannedChunkOrderKey {
                    decode_time: first_sample.decode_time(),
                    source_index: first_sample.source_index(),
                    data_offset: first_sample.data_offset(),
                    track_id,
                },
                track_id,
                start_index,
                end_index,
            });
            start_index = end_index;
        }
        if start_index != items.len() {
            return Err(MuxError::InvalidChunkPlan {
                track_id,
                message: "chunk boundaries did not cover every staged sample".to_string(),
            });
        }
    }

    match interleave_policy {
        MuxInterleavePolicy::DecodeTime => {
            chunks.sort_by_key(|chunk| chunk.order_key);
        }
        MuxInterleavePolicy::ChunkOrdinalThenSource => {
            chunks.sort_by_key(|chunk| {
                (
                    chunk.chunk_index,
                    chunk.order_key.source_index,
                    chunk.order_key.data_offset,
                    chunk.order_key.track_id,
                    chunk.order_key.decode_time,
                )
            });
        }
    }

    let mut planned_items = Vec::with_capacity(total_sample_count);
    let mut total_payload_size = 0_u64;
    for chunk in chunks {
        let items = items_by_track
            .get(&chunk.track_id)
            .ok_or(MuxError::MissingTrackId {
                track_id: chunk.track_id,
            })?;
        for staged in &items[chunk.start_index..chunk.end_index] {
            planned_items.push(MuxPlannedMediaItem {
                staged: *staged,
                output_offset: total_payload_size,
            });
            total_payload_size = total_payload_size
                .checked_add(u64::from(staged.data_size()))
                .ok_or(MuxError::PayloadSizeOverflow)?;
        }
    }

    Ok((planned_items, total_payload_size))
}

fn advance_progressive_source<R>(
    source: &mut R,
    source_index: usize,
    current_offset: &mut u64,
    target_offset: u64,
) -> Result<(), MuxError>
where
    R: Read,
{
    if target_offset < *current_offset {
        return Err(MuxError::NonMonotonicSourceOffset {
            source_index,
            previous_offset: *current_offset,
            next_offset: target_offset,
        });
    }

    let mut remaining = target_offset - *current_offset;
    let mut buffer = [0_u8; 16 * 1024];
    while remaining > 0 {
        let chunk_len = remaining.min(buffer.len() as u64) as usize;
        let read = source.read(&mut buffer[..chunk_len])?;
        if read == 0 {
            return Err(MuxError::IncompleteAdvance {
                source_index,
                expected_offset: target_offset,
                actual_offset: *current_offset,
            });
        }
        *current_offset += read as u64;
        remaining -= read as u64;
    }

    Ok(())
}

fn copy_progressive_payload<R, W>(
    source: &mut R,
    writer: &mut W,
    source_index: usize,
    current_offset: &mut u64,
    size: u64,
) -> Result<(), MuxError>
where
    R: Read,
    W: Write,
{
    let mut remaining = size;
    let mut copied = 0_u64;
    let mut buffer = [0_u8; 16 * 1024];
    while remaining > 0 {
        let chunk_len = remaining.min(buffer.len() as u64) as usize;
        let read = source.read(&mut buffer[..chunk_len])?;
        if read == 0 {
            return Err(MuxError::IncompleteCopy {
                source_index,
                expected_size: size,
                actual_size: copied,
            });
        }
        writer.write_all(&buffer[..read])?;
        *current_offset += read as u64;
        copied += read as u64;
        remaining -= read as u64;
    }

    Ok(())
}

#[cfg(feature = "async")]
async fn advance_progressive_source_async<R>(
    source: &mut R,
    source_index: usize,
    current_offset: &mut u64,
    target_offset: u64,
    buffer: &mut [u8],
) -> Result<(), MuxError>
where
    R: AsyncReadForward,
{
    if target_offset < *current_offset {
        return Err(MuxError::NonMonotonicSourceOffset {
            source_index,
            previous_offset: *current_offset,
            next_offset: target_offset,
        });
    }

    let mut remaining = target_offset - *current_offset;
    while remaining > 0 {
        let chunk_len = remaining.min(buffer.len() as u64) as usize;
        let read = source.read(&mut buffer[..chunk_len]).await?;
        if read == 0 {
            return Err(MuxError::IncompleteAdvance {
                source_index,
                expected_offset: target_offset,
                actual_offset: *current_offset,
            });
        }
        *current_offset += read as u64;
        remaining -= read as u64;
    }

    Ok(())
}

#[cfg(feature = "async")]
async fn copy_progressive_payload_async<R, W>(
    source: &mut R,
    writer: &mut W,
    source_index: usize,
    current_offset: &mut u64,
    size: u64,
    buffer: &mut [u8],
) -> Result<(), MuxError>
where
    R: AsyncReadForward,
    W: AsyncWriteForward,
{
    let mut remaining = size;
    let mut copied = 0_u64;
    while remaining > 0 {
        let chunk_len = remaining.min(buffer.len() as u64) as usize;
        let read = source.read(&mut buffer[..chunk_len]).await?;
        if read == 0 {
            return Err(MuxError::IncompleteCopy {
                source_index,
                expected_size: size,
                actual_size: copied,
            });
        }
        writer.write_all(&buffer[..read]).await?;
        *current_offset += read as u64;
        copied += read as u64;
        remaining -= read as u64;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn coordinated_chunk_plans_keep_multi_sample_chunks_contiguous_in_output_order() {
        let plan = plan_staged_media_items_with_coordination(
            vec![
                MuxStagedMediaItem::new(0, 1, 0, 10, 0, 4),
                MuxStagedMediaItem::new(0, 1, 10, 10, 4, 4),
                MuxStagedMediaItem::new(1, 2, 0, 10, 0, 3),
                MuxStagedMediaItem::new(1, 2, 10, 10, 3, 3),
            ],
            MuxInterleavePolicy::DecodeTime,
            vec![
                TrackCoordinationDirective::new(1, vec![2]),
                TrackCoordinationDirective::new(2, vec![2]),
            ],
        )
        .unwrap();

        let planned = plan.planned_items();
        assert_eq!(planned.len(), 4);
        assert_eq!(planned[0].staged().track_id(), 1);
        assert_eq!(planned[1].staged().track_id(), 1);
        assert_eq!(planned[2].staged().track_id(), 2);
        assert_eq!(planned[3].staged().track_id(), 2);
        assert_eq!(planned[0].output_offset(), 0);
        assert_eq!(planned[1].output_offset(), 4);
        assert_eq!(planned[2].output_offset(), 8);
        assert_eq!(planned[3].output_offset(), 11);
    }

    #[test]
    fn chunk_ordinal_interleave_keeps_aligned_chunks_in_source_pair_order() {
        let plan = plan_staged_media_items_with_coordination(
            vec![
                MuxStagedMediaItem::new(0, 1, 0, 10, 0, 4),
                MuxStagedMediaItem::new(0, 1, 10, 10, 4, 4),
                MuxStagedMediaItem::new(0, 1, 20, 10, 8, 4),
                MuxStagedMediaItem::new(0, 1, 30, 10, 12, 4),
                MuxStagedMediaItem::new(1, 2, 0, 10, 0, 3),
                MuxStagedMediaItem::new(1, 2, 15, 10, 3, 3),
                MuxStagedMediaItem::new(1, 2, 31, 10, 6, 3),
            ],
            MuxInterleavePolicy::ChunkOrdinalThenSource,
            vec![
                TrackCoordinationDirective::new(1, vec![1, 1, 1, 1]),
                TrackCoordinationDirective::new(2, vec![1, 1, 1]),
            ],
        )
        .unwrap();

        let track_order = plan
            .planned_items()
            .iter()
            .map(|item| item.staged().track_id())
            .collect::<Vec<_>>();
        assert_eq!(track_order, vec![1, 2, 1, 2, 1, 2, 1]);
    }
}