mediadecode-ffmpeg 0.9.0

FFmpeg adapter for the `mediadecode` abstraction layer — implements its `VideoAdapter` / `AudioAdapter` / `SubtitleAdapter` traits and the matching push-style decoder traits, with hardware-acceleration auto-probe across VideoToolbox / VAAPI / NVDEC / D3D11VA and software fallback via ffmpeg-next.
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
//! Boundary conversions between FFmpeg's bindgen integers and the
//! unified [`mediadecode`] vocabulary.
//!
//! Centralised so the rest of the crate never compares raw
//! `AVPixelFormat` integers against literals or transmutes back into
//! the bindgen enum (UB hazard when the value isn't in the enum's
//! discriminant set).

use core::{
  ffi::c_int,
  ptr::{addr_of, read_unaligned},
};

use derive_more::{IsVariant, TryUnwrap, Unwrap};
use ffmpeg_next::{Packet, ffi::AVPixelFormat};
use mediadecode::{
  PixelFormat, Timestamp,
  demuxer::{AttachmentPacket, DataPacket},
  frame::{AudioFrame, Dimensions, Plane, SubtitleFrame, VideoFrame},
  packet::{AudioPacket, PacketFlags as MdPacketFlags, SubtitlePacket, VideoPacket},
  subtitle::{SubtitlePayload, Text as SubtitleText},
};
use mediaframe::audio::ChannelLayoutDescription;

use crate::{
  // `buffer::SideDataAlloc` is aliased: this module also defines its own
  // `PacketBuildError::SideDataAlloc` payload (the write-side failure —
  // FFmpeg refusing an allocation while rebuilding an `AVPacket`'s side
  // data), which keeps the bare name since it is native to this file.
  // `BufferSideDataAlloc` is `PacketBufferError`'s read-side counterpart
  // (out of memory copying a side-data entry *out of* an `AVPacket`) —
  // same short name, different struct, different direction.
  buffer::{
    FfmpegBytes, PacketBufferError, SideDataAlloc as BufferSideDataAlloc, SideDataArray,
    SideDataBytes, SideDataEntries, SideDataPayload, UnrepresentableFlags,
  },
  carrier::BodyRoute,
  convert::{SIDE_DATA_MAX_ENTRIES, SIDE_DATA_MAX_TOTAL_BYTES},
  extras::{
    AttachmentPacketExtra, AudioFrameExtra, AudioPacketExtra, DataPacketExtra, SideDataEntry,
    SubtitleFrameExtra, SubtitlePacketExtra, VideoFrameExtra, VideoPacketExtra,
  },
  limits::PacketLimits,
  sample_format::SampleFormat,
};

/// Maps a raw `AVFrame.format` integer (i.e. the value of an
/// `AVPixelFormat` enum variant) onto [`mediadecode::PixelFormat`].
///
/// Returns [`PixelFormat::None`] for raw integers we don't have a
/// mapping for — including `AV_PIX_FMT_NONE` itself and the
/// hardware-frame markers (`AV_PIX_FMT_VIDEOTOOLBOX` / `_VAAPI` /
/// `_CUDA` / `_D3D11` / …), since those never describe CPU-side pixel
/// data and the unified enum intentionally doesn't carry them. Use
/// [`is_hardware_pix_fmt`] to identify HW frames before transferring
/// to a CPU format.
///
/// mediaframe 0.3 struck `PixelFormat::Unknown(u32)`, so the raw
/// integer no longer rides along in the returned value; the caller's
/// own `raw` is the place it survives. Every consumer in this crate
/// already treats the fall-through as "not a deliverable CPU format"
/// (`pixdesc::to_av_pixel_format`, `is_supported_cpu_pix_fmt` and the
/// geometry tables all reject it), so the rejection is unchanged.
///
/// The match never constructs an `AVPixelFormat` from a runtime
/// value; it compares the input against `AVPixelFormat::AV_PIX_FMT_X
/// as i32` constants. Sound regardless of which discriminant set the
/// linked FFmpeg version exposes.
pub const fn from_av_pixel_format(raw: i32) -> PixelFormat {
  // Mirrors `crate::pixdesc::to_av_pixel_format` arm-for-arm (its
  // inverse). Every deliverable CPU format plus the non-deliverable
  // formats `to_av` still resolves a constant for (monochrome / PAL /
  // sub-byte-packed RGB / Bayer) is mapped here, so a frame's raw
  // `format` integer always lands on the same `PixelFormat` the round
  // trip would produce. Deliverability (HWACCEL / BAYER / PAL /
  // BITSTREAM rejection) is enforced separately by
  // `pixdesc::is_deliverable` / the convert layer — this boundary is
  // identity-only.
  //
  // BE-tagged formats map to mediadecode's distinct `*Be` variants
  // (never folded onto the LE canonical). Folding BE onto LE silently
  // corrupted pixel data: each >8-bit sample is byte-swapped between
  // BE and LE, and the convert path exports the AVBufferRef bytes
  // verbatim with no endian conversion, so a consumer reading a
  // BE-tagged frame's planes as LE samples would see every sample
  // byte-reversed. Mapping to the `*Be` variant keeps the format
  // distinct so the convert layer can handle (or reject) it correctly.
  //
  // The match never constructs an `AVPixelFormat` from a runtime
  // value; it compares the input against `AVPixelFormat::AV_PIX_FMT_X
  // as i32` constants. Sound regardless of which discriminant set the
  // linked FFmpeg version exposes.
  match raw {
    // Planar YUV 8-bit.
    x if x == AVPixelFormat::AV_PIX_FMT_YUV420P as i32 => PixelFormat::Yuv420p,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV422P as i32 => PixelFormat::Yuv422p,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV440P as i32 => PixelFormat::Yuv440p,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P as i32 => PixelFormat::Yuv444p,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV411P as i32 => PixelFormat::Yuv411p,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV410P as i32 => PixelFormat::Yuv410p,
    // Deprecated JPEG-range planar YUV (yuvj-family).
    x if x == AVPixelFormat::AV_PIX_FMT_YUVJ411P as i32 => PixelFormat::Yuvj411p,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVJ420P as i32 => PixelFormat::Yuvj420p,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVJ422P as i32 => PixelFormat::Yuvj422p,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVJ440P as i32 => PixelFormat::Yuvj440p,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVJ444P as i32 => PixelFormat::Yuvj444p,
    // Planar YUV 4:2:0 high-bit.
    x if x == AVPixelFormat::AV_PIX_FMT_YUV420P9LE as i32 => PixelFormat::Yuv420p9Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV420P9BE as i32 => PixelFormat::Yuv420p9Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV420P10LE as i32 => PixelFormat::Yuv420p10Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV420P10BE as i32 => PixelFormat::Yuv420p10Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV420P12LE as i32 => PixelFormat::Yuv420p12Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV420P12BE as i32 => PixelFormat::Yuv420p12Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV420P14LE as i32 => PixelFormat::Yuv420p14Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV420P14BE as i32 => PixelFormat::Yuv420p14Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV420P16LE as i32 => PixelFormat::Yuv420p16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV420P16BE as i32 => PixelFormat::Yuv420p16Be,
    // Planar YUV 4:2:2 high-bit.
    x if x == AVPixelFormat::AV_PIX_FMT_YUV422P9LE as i32 => PixelFormat::Yuv422p9Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV422P9BE as i32 => PixelFormat::Yuv422p9Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV422P10LE as i32 => PixelFormat::Yuv422p10Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV422P10BE as i32 => PixelFormat::Yuv422p10Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV422P12LE as i32 => PixelFormat::Yuv422p12Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV422P12BE as i32 => PixelFormat::Yuv422p12Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV422P14LE as i32 => PixelFormat::Yuv422p14Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV422P14BE as i32 => PixelFormat::Yuv422p14Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV422P16LE as i32 => PixelFormat::Yuv422p16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV422P16BE as i32 => PixelFormat::Yuv422p16Be,
    // Planar YUV 4:4:0 high-bit.
    x if x == AVPixelFormat::AV_PIX_FMT_YUV440P10LE as i32 => PixelFormat::Yuv440p10Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV440P10BE as i32 => PixelFormat::Yuv440p10Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV440P12LE as i32 => PixelFormat::Yuv440p12Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV440P12BE as i32 => PixelFormat::Yuv440p12Be,
    // Planar YUV 4:4:4 high-bit.
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P9LE as i32 => PixelFormat::Yuv444p9Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P9BE as i32 => PixelFormat::Yuv444p9Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P10LE as i32 => PixelFormat::Yuv444p10Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P10BE as i32 => PixelFormat::Yuv444p10Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P12LE as i32 => PixelFormat::Yuv444p12Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P12BE as i32 => PixelFormat::Yuv444p12Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P14LE as i32 => PixelFormat::Yuv444p14Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P14BE as i32 => PixelFormat::Yuv444p14Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P16LE as i32 => PixelFormat::Yuv444p16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P16BE as i32 => PixelFormat::Yuv444p16Be,
    // MSB-packed YUV 4:4:4.
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P10MSBLE as i32 => PixelFormat::Yuv444p10MsbLe,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P10MSBBE as i32 => PixelFormat::Yuv444p10MsbBe,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P12MSBLE as i32 => PixelFormat::Yuv444p12MsbLe,
    x if x == AVPixelFormat::AV_PIX_FMT_YUV444P12MSBBE as i32 => PixelFormat::Yuv444p12MsbBe,
    // Planar YUVA.
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA420P as i32 => PixelFormat::Yuva420p,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA422P as i32 => PixelFormat::Yuva422p,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA444P as i32 => PixelFormat::Yuva444p,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA420P9LE as i32 => PixelFormat::Yuva420p9Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA420P9BE as i32 => PixelFormat::Yuva420p9Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA422P9LE as i32 => PixelFormat::Yuva422p9Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA422P9BE as i32 => PixelFormat::Yuva422p9Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA444P9LE as i32 => PixelFormat::Yuva444p9Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA444P9BE as i32 => PixelFormat::Yuva444p9Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA420P10LE as i32 => PixelFormat::Yuva420p10Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA420P10BE as i32 => PixelFormat::Yuva420p10Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA422P10LE as i32 => PixelFormat::Yuva422p10Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA422P10BE as i32 => PixelFormat::Yuva422p10Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA444P10LE as i32 => PixelFormat::Yuva444p10Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA444P10BE as i32 => PixelFormat::Yuva444p10Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA422P12LE as i32 => PixelFormat::Yuva422p12Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA422P12BE as i32 => PixelFormat::Yuva422p12Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA444P12LE as i32 => PixelFormat::Yuva444p12Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA444P12BE as i32 => PixelFormat::Yuva444p12Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA420P16LE as i32 => PixelFormat::Yuva420p16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA420P16BE as i32 => PixelFormat::Yuva420p16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA422P16LE as i32 => PixelFormat::Yuva422p16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA422P16BE as i32 => PixelFormat::Yuva422p16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA444P16LE as i32 => PixelFormat::Yuva444p16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YUVA444P16BE as i32 => PixelFormat::Yuva444p16Be,
    // Semi-planar YUV 8-bit.
    x if x == AVPixelFormat::AV_PIX_FMT_NV12 as i32 => PixelFormat::Nv12,
    x if x == AVPixelFormat::AV_PIX_FMT_NV21 as i32 => PixelFormat::Nv21,
    x if x == AVPixelFormat::AV_PIX_FMT_NV16 as i32 => PixelFormat::Nv16,
    x if x == AVPixelFormat::AV_PIX_FMT_NV24 as i32 => PixelFormat::Nv24,
    x if x == AVPixelFormat::AV_PIX_FMT_NV42 as i32 => PixelFormat::Nv42,
    x if x == AVPixelFormat::AV_PIX_FMT_NV20LE as i32 => PixelFormat::Nv20Le,
    x if x == AVPixelFormat::AV_PIX_FMT_NV20BE as i32 => PixelFormat::Nv20Be,
    // Semi-planar YUV high-bit.
    x if x == AVPixelFormat::AV_PIX_FMT_P010LE as i32 => PixelFormat::P010Le,
    x if x == AVPixelFormat::AV_PIX_FMT_P010BE as i32 => PixelFormat::P010Be,
    x if x == AVPixelFormat::AV_PIX_FMT_P012LE as i32 => PixelFormat::P012Le,
    x if x == AVPixelFormat::AV_PIX_FMT_P012BE as i32 => PixelFormat::P012Be,
    x if x == AVPixelFormat::AV_PIX_FMT_P016LE as i32 => PixelFormat::P016Le,
    x if x == AVPixelFormat::AV_PIX_FMT_P016BE as i32 => PixelFormat::P016Be,
    x if x == AVPixelFormat::AV_PIX_FMT_P210LE as i32 => PixelFormat::P210Le,
    x if x == AVPixelFormat::AV_PIX_FMT_P210BE as i32 => PixelFormat::P210Be,
    x if x == AVPixelFormat::AV_PIX_FMT_P212LE as i32 => PixelFormat::P212Le,
    x if x == AVPixelFormat::AV_PIX_FMT_P212BE as i32 => PixelFormat::P212Be,
    x if x == AVPixelFormat::AV_PIX_FMT_P216LE as i32 => PixelFormat::P216Le,
    x if x == AVPixelFormat::AV_PIX_FMT_P216BE as i32 => PixelFormat::P216Be,
    x if x == AVPixelFormat::AV_PIX_FMT_P410LE as i32 => PixelFormat::P410Le,
    x if x == AVPixelFormat::AV_PIX_FMT_P410BE as i32 => PixelFormat::P410Be,
    x if x == AVPixelFormat::AV_PIX_FMT_P412LE as i32 => PixelFormat::P412Le,
    x if x == AVPixelFormat::AV_PIX_FMT_P412BE as i32 => PixelFormat::P412Be,
    x if x == AVPixelFormat::AV_PIX_FMT_P416LE as i32 => PixelFormat::P416Le,
    x if x == AVPixelFormat::AV_PIX_FMT_P416BE as i32 => PixelFormat::P416Be,
    // Packed YUV 8-bit.
    x if x == AVPixelFormat::AV_PIX_FMT_YUYV422 as i32 => PixelFormat::Yuyv422,
    x if x == AVPixelFormat::AV_PIX_FMT_UYVY422 as i32 => PixelFormat::Uyvy422,
    x if x == AVPixelFormat::AV_PIX_FMT_YVYU422 as i32 => PixelFormat::Yvyu422,
    x if x == AVPixelFormat::AV_PIX_FMT_UYYVYY411 as i32 => PixelFormat::Uyyvyy411,
    // Packed YUV high-bit.
    x if x == AVPixelFormat::AV_PIX_FMT_Y210LE as i32 => PixelFormat::Y210Le,
    x if x == AVPixelFormat::AV_PIX_FMT_Y210BE as i32 => PixelFormat::Y210Be,
    x if x == AVPixelFormat::AV_PIX_FMT_Y212LE as i32 => PixelFormat::Y212Le,
    x if x == AVPixelFormat::AV_PIX_FMT_Y212BE as i32 => PixelFormat::Y212Be,
    x if x == AVPixelFormat::AV_PIX_FMT_Y216LE as i32 => PixelFormat::Y216Le,
    x if x == AVPixelFormat::AV_PIX_FMT_Y216BE as i32 => PixelFormat::Y216Be,
    x if x == AVPixelFormat::AV_PIX_FMT_XV30LE as i32 => PixelFormat::Xv30Le,
    x if x == AVPixelFormat::AV_PIX_FMT_XV30BE as i32 => PixelFormat::Xv30Be,
    x if x == AVPixelFormat::AV_PIX_FMT_V30XLE as i32 => PixelFormat::V30xLe,
    x if x == AVPixelFormat::AV_PIX_FMT_V30XBE as i32 => PixelFormat::V30xBe,
    x if x == AVPixelFormat::AV_PIX_FMT_XV36LE as i32 => PixelFormat::Xv36Le,
    x if x == AVPixelFormat::AV_PIX_FMT_XV36BE as i32 => PixelFormat::Xv36Be,
    x if x == AVPixelFormat::AV_PIX_FMT_XV48LE as i32 => PixelFormat::Xv48Le,
    x if x == AVPixelFormat::AV_PIX_FMT_XV48BE as i32 => PixelFormat::Xv48Be,
    x if x == AVPixelFormat::AV_PIX_FMT_VUYA as i32 => PixelFormat::Vuya,
    x if x == AVPixelFormat::AV_PIX_FMT_VUYX as i32 => PixelFormat::Vuyx,
    x if x == AVPixelFormat::AV_PIX_FMT_AYUV as i32 => PixelFormat::Ayuv,
    x if x == AVPixelFormat::AV_PIX_FMT_AYUV64LE as i32 => PixelFormat::Ayuv64Le,
    x if x == AVPixelFormat::AV_PIX_FMT_AYUV64BE as i32 => PixelFormat::Ayuv64Be,
    x if x == AVPixelFormat::AV_PIX_FMT_UYVA as i32 => PixelFormat::Uyva,
    x if x == AVPixelFormat::AV_PIX_FMT_VYU444 as i32 => PixelFormat::Vyu444,
    // XYZ.
    x if x == AVPixelFormat::AV_PIX_FMT_XYZ12LE as i32 => PixelFormat::Xyz12Le,
    x if x == AVPixelFormat::AV_PIX_FMT_XYZ12BE as i32 => PixelFormat::Xyz12Be,
    // Packed RGB 8-bit.
    x if x == AVPixelFormat::AV_PIX_FMT_RGB24 as i32 => PixelFormat::Rgb24,
    x if x == AVPixelFormat::AV_PIX_FMT_BGR24 as i32 => PixelFormat::Bgr24,
    x if x == AVPixelFormat::AV_PIX_FMT_RGBA as i32 => PixelFormat::Rgba,
    x if x == AVPixelFormat::AV_PIX_FMT_BGRA as i32 => PixelFormat::Bgra,
    x if x == AVPixelFormat::AV_PIX_FMT_ARGB as i32 => PixelFormat::Argb,
    x if x == AVPixelFormat::AV_PIX_FMT_ABGR as i32 => PixelFormat::Abgr,
    x if x == AVPixelFormat::AV_PIX_FMT_RGB0 as i32 => PixelFormat::Rgbx,
    x if x == AVPixelFormat::AV_PIX_FMT_BGR0 as i32 => PixelFormat::Bgrx,
    x if x == AVPixelFormat::AV_PIX_FMT_0RGB as i32 => PixelFormat::Xrgb,
    x if x == AVPixelFormat::AV_PIX_FMT_0BGR as i32 => PixelFormat::Xbgr,
    x if x == AVPixelFormat::AV_PIX_FMT_X2RGB10LE as i32 => PixelFormat::X2Rgb10Le,
    x if x == AVPixelFormat::AV_PIX_FMT_X2RGB10BE as i32 => PixelFormat::X2Rgb10Be,
    x if x == AVPixelFormat::AV_PIX_FMT_X2BGR10LE as i32 => PixelFormat::X2Bgr10Le,
    x if x == AVPixelFormat::AV_PIX_FMT_X2BGR10BE as i32 => PixelFormat::X2Bgr10Be,
    // Gbr24p shares AV_PIX_FMT_GBRP's discriminant; mapped to Gbrp above.
    // Packed RGB high-bit.
    x if x == AVPixelFormat::AV_PIX_FMT_RGB48LE as i32 => PixelFormat::Rgb48Le,
    x if x == AVPixelFormat::AV_PIX_FMT_RGB48BE as i32 => PixelFormat::Rgb48Be,
    x if x == AVPixelFormat::AV_PIX_FMT_BGR48LE as i32 => PixelFormat::Bgr48Le,
    x if x == AVPixelFormat::AV_PIX_FMT_BGR48BE as i32 => PixelFormat::Bgr48Be,
    x if x == AVPixelFormat::AV_PIX_FMT_RGBA64LE as i32 => PixelFormat::Rgba64Le,
    x if x == AVPixelFormat::AV_PIX_FMT_RGBA64BE as i32 => PixelFormat::Rgba64Be,
    x if x == AVPixelFormat::AV_PIX_FMT_BGRA64LE as i32 => PixelFormat::Bgra64Le,
    x if x == AVPixelFormat::AV_PIX_FMT_BGRA64BE as i32 => PixelFormat::Bgra64Be,
    x if x == AVPixelFormat::AV_PIX_FMT_RGB96LE as i32 => PixelFormat::Rgb96Le,
    x if x == AVPixelFormat::AV_PIX_FMT_RGB96BE as i32 => PixelFormat::Rgb96Be,
    x if x == AVPixelFormat::AV_PIX_FMT_RGBA128LE as i32 => PixelFormat::Rgba128Le,
    x if x == AVPixelFormat::AV_PIX_FMT_RGBA128BE as i32 => PixelFormat::Rgba128Be,
    // Packed RGB float / half-float.
    x if x == AVPixelFormat::AV_PIX_FMT_RGBF16LE as i32 => PixelFormat::Rgbf16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_RGBF16BE as i32 => PixelFormat::Rgbf16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_RGBF32LE as i32 => PixelFormat::Rgbf32Le,
    x if x == AVPixelFormat::AV_PIX_FMT_RGBF32BE as i32 => PixelFormat::Rgbf32Be,
    x if x == AVPixelFormat::AV_PIX_FMT_RGBAF16LE as i32 => PixelFormat::Rgbaf16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_RGBAF16BE as i32 => PixelFormat::Rgbaf16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_RGBAF32LE as i32 => PixelFormat::Rgbaf32Le,
    x if x == AVPixelFormat::AV_PIX_FMT_RGBAF32BE as i32 => PixelFormat::Rgbaf32Be,
    // Planar GBR.
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP as i32 => PixelFormat::Gbrp,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP9LE as i32 => PixelFormat::Gbrp9Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP9BE as i32 => PixelFormat::Gbrp9Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP10LE as i32 => PixelFormat::Gbrp10Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP10BE as i32 => PixelFormat::Gbrp10Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP10MSBLE as i32 => PixelFormat::Gbrp10MsbLe,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP10MSBBE as i32 => PixelFormat::Gbrp10MsbBe,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP12LE as i32 => PixelFormat::Gbrp12Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP12BE as i32 => PixelFormat::Gbrp12Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP12MSBLE as i32 => PixelFormat::Gbrp12MsbLe,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP12MSBBE as i32 => PixelFormat::Gbrp12MsbBe,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP14LE as i32 => PixelFormat::Gbrp14Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP14BE as i32 => PixelFormat::Gbrp14Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP16LE as i32 => PixelFormat::Gbrp16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRP16BE as i32 => PixelFormat::Gbrp16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRPF16LE as i32 => PixelFormat::Gbrpf16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRPF16BE as i32 => PixelFormat::Gbrpf16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRPF32LE as i32 => PixelFormat::Gbrpf32Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRPF32BE as i32 => PixelFormat::Gbrpf32Be,
    // Planar GBRA.
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAP as i32 => PixelFormat::Gbrap,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAP10LE as i32 => PixelFormat::Gbrap10Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAP10BE as i32 => PixelFormat::Gbrap10Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAP12LE as i32 => PixelFormat::Gbrap12Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAP12BE as i32 => PixelFormat::Gbrap12Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAP14LE as i32 => PixelFormat::Gbrap14Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAP14BE as i32 => PixelFormat::Gbrap14Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAP16LE as i32 => PixelFormat::Gbrap16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAP16BE as i32 => PixelFormat::Gbrap16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAP32LE as i32 => PixelFormat::Gbrap32Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAP32BE as i32 => PixelFormat::Gbrap32Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAPF16LE as i32 => PixelFormat::Gbrapf16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAPF16BE as i32 => PixelFormat::Gbrapf16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAPF32LE as i32 => PixelFormat::Gbrapf32Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GBRAPF32BE as i32 => PixelFormat::Gbrapf32Be,
    // Greyscale.
    x if x == AVPixelFormat::AV_PIX_FMT_GRAY8 as i32 => PixelFormat::Gray8,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAY9LE as i32 => PixelFormat::Gray9Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAY9BE as i32 => PixelFormat::Gray9Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAY10LE as i32 => PixelFormat::Gray10Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAY10BE as i32 => PixelFormat::Gray10Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAY12LE as i32 => PixelFormat::Gray12Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAY12BE as i32 => PixelFormat::Gray12Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAY14LE as i32 => PixelFormat::Gray14Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAY14BE as i32 => PixelFormat::Gray14Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAY16LE as i32 => PixelFormat::Gray16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAY16BE as i32 => PixelFormat::Gray16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAY32LE as i32 => PixelFormat::Gray32Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAY32BE as i32 => PixelFormat::Gray32Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAYF16LE as i32 => PixelFormat::Grayf16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAYF16BE as i32 => PixelFormat::Grayf16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAYF32LE as i32 => PixelFormat::Grayf32Le,
    x if x == AVPixelFormat::AV_PIX_FMT_GRAYF32BE as i32 => PixelFormat::Grayf32Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YA8 as i32 => PixelFormat::Ya8,
    x if x == AVPixelFormat::AV_PIX_FMT_YA16LE as i32 => PixelFormat::Ya16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YA16BE as i32 => PixelFormat::Ya16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YAF16LE as i32 => PixelFormat::Yaf16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YAF16BE as i32 => PixelFormat::Yaf16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_YAF32LE as i32 => PixelFormat::Yaf32Le,
    x if x == AVPixelFormat::AV_PIX_FMT_YAF32BE as i32 => PixelFormat::Yaf32Be,
    x if x == AVPixelFormat::AV_PIX_FMT_MONOWHITE as i32 => PixelFormat::Monowhite,
    x if x == AVPixelFormat::AV_PIX_FMT_MONOBLACK as i32 => PixelFormat::Monoblack,
    x if x == AVPixelFormat::AV_PIX_FMT_PAL8 as i32 => PixelFormat::Pal8,
    x if x == AVPixelFormat::AV_PIX_FMT_RGB4 as i32 => PixelFormat::Rgb4,
    x if x == AVPixelFormat::AV_PIX_FMT_RGB4_BYTE as i32 => PixelFormat::Rgb4Byte,
    x if x == AVPixelFormat::AV_PIX_FMT_RGB8 as i32 => PixelFormat::Rgb8,
    x if x == AVPixelFormat::AV_PIX_FMT_BGR4 as i32 => PixelFormat::Bgr4,
    x if x == AVPixelFormat::AV_PIX_FMT_BGR4_BYTE as i32 => PixelFormat::Bgr4Byte,
    x if x == AVPixelFormat::AV_PIX_FMT_BGR8 as i32 => PixelFormat::Bgr8,
    x if x == AVPixelFormat::AV_PIX_FMT_RGB444LE as i32 => PixelFormat::Rgb444Le,
    x if x == AVPixelFormat::AV_PIX_FMT_RGB444BE as i32 => PixelFormat::Rgb444Be,
    x if x == AVPixelFormat::AV_PIX_FMT_BGR444LE as i32 => PixelFormat::Bgr444Le,
    x if x == AVPixelFormat::AV_PIX_FMT_BGR444BE as i32 => PixelFormat::Bgr444Be,
    x if x == AVPixelFormat::AV_PIX_FMT_RGB555LE as i32 => PixelFormat::Rgb555Le,
    x if x == AVPixelFormat::AV_PIX_FMT_RGB555BE as i32 => PixelFormat::Rgb555Be,
    x if x == AVPixelFormat::AV_PIX_FMT_BGR555LE as i32 => PixelFormat::Bgr555Le,
    x if x == AVPixelFormat::AV_PIX_FMT_BGR555BE as i32 => PixelFormat::Bgr555Be,
    x if x == AVPixelFormat::AV_PIX_FMT_RGB565LE as i32 => PixelFormat::Rgb565Le,
    x if x == AVPixelFormat::AV_PIX_FMT_RGB565BE as i32 => PixelFormat::Rgb565Be,
    x if x == AVPixelFormat::AV_PIX_FMT_BGR565LE as i32 => PixelFormat::Bgr565Le,
    x if x == AVPixelFormat::AV_PIX_FMT_BGR565BE as i32 => PixelFormat::Bgr565Be,
    x if x == AVPixelFormat::AV_PIX_FMT_BAYER_BGGR8 as i32 => PixelFormat::BayerBggr8,
    x if x == AVPixelFormat::AV_PIX_FMT_BAYER_RGGB8 as i32 => PixelFormat::BayerRggb8,
    x if x == AVPixelFormat::AV_PIX_FMT_BAYER_GBRG8 as i32 => PixelFormat::BayerGbrg8,
    x if x == AVPixelFormat::AV_PIX_FMT_BAYER_GRBG8 as i32 => PixelFormat::BayerGrbg8,
    x if x == AVPixelFormat::AV_PIX_FMT_BAYER_BGGR16LE as i32 => PixelFormat::BayerBggr16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_BAYER_BGGR16BE as i32 => PixelFormat::BayerBggr16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_BAYER_RGGB16LE as i32 => PixelFormat::BayerRggb16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_BAYER_RGGB16BE as i32 => PixelFormat::BayerRggb16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_BAYER_GBRG16LE as i32 => PixelFormat::BayerGbrg16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_BAYER_GBRG16BE as i32 => PixelFormat::BayerGbrg16Be,
    x if x == AVPixelFormat::AV_PIX_FMT_BAYER_GRBG16LE as i32 => PixelFormat::BayerGrbg16Le,
    x if x == AVPixelFormat::AV_PIX_FMT_BAYER_GRBG16BE as i32 => PixelFormat::BayerGrbg16Be,
    _ => PixelFormat::None,
  }
}

/// Returns `true` when `raw` is one of FFmpeg's hardware-frame markers
/// (`AV_PIX_FMT_VIDEOTOOLBOX` / `_VAAPI` / `_CUDA` / `_D3D11` /
/// `_DRM_PRIME` / `_MEDIACODEC` / `_VULKAN`). Used by the HW probe to
/// identify GPU-resident frames before triggering
/// `av_hwframe_transfer_data`.
pub const fn is_hardware_pix_fmt(raw: i32) -> bool {
  matches!(
    raw,
    x if x == AVPixelFormat::AV_PIX_FMT_VIDEOTOOLBOX as i32
      || x == AVPixelFormat::AV_PIX_FMT_VAAPI as i32
      || x == AVPixelFormat::AV_PIX_FMT_CUDA as i32
      || x == AVPixelFormat::AV_PIX_FMT_D3D11 as i32
      || x == AVPixelFormat::AV_PIX_FMT_DRM_PRIME as i32
      || x == AVPixelFormat::AV_PIX_FMT_MEDIACODEC as i32
      || x == AVPixelFormat::AV_PIX_FMT_VULKAN as i32
  )
}

/// Fallible counterpart to ffmpeg-next's `Packet::copy`.
///
/// The upstream helper calls `Packet::new(size)` (which silently
/// truncates `size` to `c_int` and ignores `av_new_packet`'s return
/// code) and then panics via `data_mut().unwrap().write_all(...).unwrap()`
/// if the allocation failed. From a safe public decoder API we want
/// the OOM / oversized-payload paths to surface as
/// `ffmpeg_next::Error` rather than aborting the process — every
/// `send_packet` path goes through this helper.
///
/// Failure modes:
/// * payload larger than `c_int::MAX` (would overflow `AVPacket.size`)
///   → `ffmpeg_next::Error::Other { errno: libc::EINVAL }`.
/// * `av_new_packet` allocation failure (signalled by `data_mut()`
///   returning `None`) → `ffmpeg_next::Error::Other { errno:
///   libc::ENOMEM }`.
pub(crate) fn try_packet_copy(data: &[u8]) -> std::result::Result<Packet, ffmpeg_next::Error> {
  // FFmpeg's `AVPacket.size` is `c_int`. A payload larger than that
  // can't fit in a single packet — refuse rather than truncate via
  // `as c_int` inside `Packet::new`.
  if data.len() > c_int::MAX as usize {
    return Err(ffmpeg_next::Error::Other {
      errno: libc::EINVAL,
    });
  }
  // `Packet::new(size)` calls `av_new_packet(&mut pkt, size as
  // c_int)` and ignores the return code; on OOM it returns a
  // `Packet` whose `.data` is null. We detect that via
  // `data_mut()` (returns `None` on null) and copy via
  // `copy_nonoverlapping` so we never go through `data_mut()
  // .unwrap().write_all().unwrap()` — the upstream `Packet::copy`'s
  // double panic.
  let mut pkt = Packet::new(data.len());
  match pkt.data_mut() {
    Some(slot) if slot.len() == data.len() => {
      // SAFETY: `slot` is a `&mut [u8]` of `data.len()` bytes;
      // `data` is a `&[u8]` of the same length. Non-overlapping
      // because `slot` is a fresh allocation.
      if !data.is_empty() {
        unsafe {
          core::ptr::copy_nonoverlapping(data.as_ptr(), slot.as_mut_ptr(), data.len());
        }
      }
      Ok(pkt)
    }
    _ => Err(ffmpeg_next::Error::Other {
      errno: libc::ENOMEM,
    }),
  }
}

/// Writes every flag the portable packet carries onto the `AVPacket`
/// being rebuilt.
///
/// The one place this crate writes packet flags into FFmpeg. The three
/// stream families rebuild through it, and so does the one-shot image
/// road — which for two releases forgot to, and sent every attachment
/// to libavcodec with a zeroed `flags` field.
///
/// Not through `Packet::set_flags`, which takes `ffmpeg_next`'s `Flags`
/// and so can only spell `KEY` and `CORRUPT`: the bits are written to
/// `AVPacket.flags` directly, so `DISCARD` — and anything else the
/// forward direction retained — reaches the decoder that has to obey
/// it. `PacketFlags` is a `u8` bit set and the field is a `c_int`, so
/// the widening is total and nothing needs deciding here.
///
/// # Safety
///
/// `packet` must own a live `AVPacket`.
pub(crate) unsafe fn write_md_flags(packet: &mut Packet, flags: MdPacketFlags) {
  use ffmpeg_next::packet::Mut;
  unsafe {
    (*packet.as_mut_ptr()).flags = c_int::from(flags.bits());
  }
}

/// Payload for [`PacketBuildError::UnknownSideData`].
///
/// A side-data entry whose type this build of FFmpeg does not name.
///
/// Refused rather than dropped: this crate carries side-data types as
/// the raw integers they are on the wire, and handing an unknown one
/// to C would either form an invalid enum discriminant or attach a
/// type nothing downstream can read. Everything the demuxer captured
/// came from this same build and is in range, so this only answers a
/// hand-built entry.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("side-data type {kind} is not one this FFmpeg build names (0..{limit})")]
pub struct UnknownSideData {
  kind: i32,
  limit: i32,
}

impl UnknownSideData {
  /// Constructs an `UnknownSideData` payload.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(kind: i32, limit: i32) -> Self {
    Self { kind, limit }
  }
  /// The type integer the entry carried.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn kind(&self) -> i32 {
    self.kind
  }
  /// How many side-data types this build names.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn limit(&self) -> i32 {
    self.limit
  }
}

/// Payload for [`PacketBuildError::SideDataAlloc`].
///
/// FFmpeg refused the side-data allocation.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("out of memory attaching {size} bytes of side data of type {kind}")]
pub struct SideDataAlloc {
  kind: i32,
  size: usize,
}

impl SideDataAlloc {
  /// Constructs a `SideDataAlloc` payload.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(kind: i32, size: usize) -> Self {
    Self { kind, size }
  }
  /// The entry's type integer.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn kind(&self) -> i32 {
    self.kind
  }
  /// The entry's payload length.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn size(&self) -> usize {
    self.size
  }
}

/// Payload for [`PacketBuildError::SendPayloadTooLarge`].
///
/// A compressed payload on its way **into** FFmpeg is larger than the
/// session's budget allows.
///
/// Named for the direction: this is the send leg. Its outbound twin is
/// [`PacketBufferError::PacketTooLarge`](crate::PacketBufferError),
/// which judges the same quantity coming the other way, against the
/// same seat.
#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("a {bytes}-byte packet payload exceeds the {limit}-byte budget on the way into FFmpeg")]
pub struct SendPayloadTooLarge {
  bytes: usize,
  limit: usize,
}

impl SendPayloadTooLarge {
  /// Constructs a `SendPayloadTooLarge` payload.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn new(bytes: usize, limit: usize) -> Self {
    Self { bytes, limit }
  }
  /// The payload length the caller handed over.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn bytes(&self) -> usize {
    self.bytes
  }
  /// The budget in force.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub const fn limit(&self) -> usize {
    self.limit
  }
}

/// Why a portable packet could not be rebuilt as an `AVPacket`.
///
/// The reverse direction is what feeds a decoder, so everything the
/// forward direction captured has to survive it or the capture was
/// theatre.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error, IsVariant, Unwrap, TryUnwrap)]
#[unwrap(ref, ref_mut)]
#[try_unwrap(ref, ref_mut)]
pub enum PacketBuildError {
  /// The payload is larger than the session's budget allows. Refused
  /// **before** the `AVPacket` is allocated — the into-FFmpeg leg of
  /// the budget the outbound leg already kept.
  #[error(transparent)]
  SendPayloadTooLarge(#[from] SendPayloadTooLarge),

  /// The packet body could not be allocated or is larger than
  /// `AVPacket.size` can hold.
  #[error(transparent)]
  Ffmpeg(#[from] ffmpeg_next::Error),

  /// A side-data entry whose type this build of FFmpeg does not name.
  #[error(transparent)]
  UnknownSideData(#[from] UnknownSideData),

  /// FFmpeg refused the side-data allocation.
  #[error(transparent)]
  SideDataAlloc(#[from] SideDataAlloc),

  /// The packet is marked `AV_PKT_FLAG_TRUSTED`. See
  /// [`crate::buffer::TrustedPayload`] — the other leg of the same
  /// refusal.
  #[error(transparent)]
  TrustedPayload(#[from] crate::buffer::TrustedPayload),

  /// The packet's side-data list is over the entry or byte cap the read
  /// side has always applied. See [`SendSideDataTooLarge`].
  #[error(transparent)]
  SendSideDataTooLarge(#[from] SendSideDataTooLarge),
}

/// Judges the **whole** side-data list before a byte of it is
/// allocated.
///
/// # One seat, both directions
///
/// The read side has capped frame and packet side data at 64 entries
/// and 256 KiB since it was written; the send side had no cap at all.
/// The preflight checked `body.len()` and then `attach_side_data`
/// allocated every entry the caller handed over, one
/// `av_packet_new_side_data` at a time, with nothing bounding the count
/// or the total — so bytes the demux boundary would have refused coming
/// *out* of a container went straight into libavcodec going *in*. That
/// is the same asymmetry `PacketLimits` was introduced to close for the
/// packet body, one field along.
///
/// So the caps are the read side's, applied here, before anything is
/// allocated — including the body, because a list that cannot be
/// carried should not cost a packet allocation first.
fn check_side_data_budget(entries: &[SideDataEntry]) -> Result<(), PacketBuildError> {
  use crate::convert::{SIDE_DATA_MAX_ENTRIES, SIDE_DATA_MAX_TOTAL_BYTES};

  if entries.len() > SIDE_DATA_MAX_ENTRIES {
    return Err(PacketBuildError::SendSideDataTooLarge(
      SendSideDataTooLarge::new_entries(entries.len(), SIDE_DATA_MAX_ENTRIES),
    ));
  }
  let mut total: usize = 0;
  for entry in entries {
    total = total.saturating_add(entry.data().len());
    if total > SIDE_DATA_MAX_TOTAL_BYTES {
      return Err(PacketBuildError::SendSideDataTooLarge(
        SendSideDataTooLarge::new_bytes(total, SIDE_DATA_MAX_TOTAL_BYTES),
      ));
    }
  }
  Ok(())
}

/// Payload for [`PacketBuildError::SendSideDataTooLarge`].
///
/// A side-data list too long or too large to carry into a decoder.
/// Carries whichever of the two caps was reached, so a caller can tell
/// "too many annotations" from "too much annotation".
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("packet side data is over the {what} ceiling: {value} against {limit}")]
pub struct SendSideDataTooLarge {
  what: &'static str,
  value: usize,
  limit: usize,
}

impl SendSideDataTooLarge {
  /// The entry-count cap was reached.
  #[inline]
  pub const fn new_entries(value: usize, limit: usize) -> Self {
    Self {
      what: "entry-count",
      value,
      limit,
    }
  }
  /// The aggregate-byte cap was reached.
  #[inline]
  pub const fn new_bytes(value: usize, limit: usize) -> Self {
    Self {
      what: "byte",
      value,
      limit,
    }
  }
  /// The count or byte total the list reached.
  #[inline]
  pub const fn value(&self) -> usize {
    self.value
  }
  /// The cap in force.
  #[inline]
  pub const fn limit(&self) -> usize {
    self.limit
  }
  /// Which cap: `"entry-count"` or `"byte"`.
  #[inline]
  pub const fn what(&self) -> &'static str {
    self.what
  }
}

/// Refuses a portable packet whose flags carry `AV_PKT_FLAG_TRUSTED`.
///
/// The rebuild leg of the refusal `payload_of` makes on the way out.
/// Closing only the copy-out leg would leave the loop open: a flag that
/// reached a portable packet by some other route (a caller composing
/// one by hand, a future producer, a graph that round-trips flags it
/// does not interpret) would be written back onto a fresh `AVPacket` by
/// `write_md_flags` and handed to a decoder entitled to believe it.
///
/// See [`crate::buffer::TrustedPayload`] for why the flag makes the
/// payload uncarriable rather than merely suspicious.
fn refuse_trusted(flags: MdPacketFlags, len: usize) -> std::result::Result<(), PacketBuildError> {
  if flags.bits() & crate::buffer::TRUSTED_BIT != 0 {
    return Err(PacketBuildError::TrustedPayload(
      crate::buffer::TrustedPayload::new(len),
    ));
  }
  Ok(())
}

/// Copies `entries` onto an `AVPacket` under construction.
///
/// A decoder learns things only this way: `AV_PKT_DATA_NEW_EXTRADATA`
/// replaces its parameters mid-stream, `AV_PKT_DATA_PARAM_CHANGE` moves
/// its rate or layout, `AV_PKT_DATA_SKIP_SAMPLES` is the encoder-delay
/// trim without which a gapless stream is not gapless. Rebuilding a
/// packet without them hands the decoder a body and a lie.
fn attach_side_data(out: &mut Packet, entries: &[SideDataEntry]) -> Result<(), PacketBuildError> {
  use ffmpeg_next::packet::Mut;
  for entry in entries {
    let kind = entry.kind();
    let size = entry.data().len();
    // SAFETY: `out` owns a live `AVPacket`; `packet_new_side_data`
    // validates the type against this build's own range before handing
    // it to C and reports a failed allocation as `None`.
    let slot = unsafe { crate::ffi::packet_new_side_data(out.as_mut_ptr(), kind, size) };
    let Some(slot) = slot else {
      let limit = crate::ffi::side_data_type_count();
      return Err(if kind < 0 || kind >= limit {
        PacketBuildError::UnknownSideData(UnknownSideData::new(kind, limit))
      } else {
        PacketBuildError::SideDataAlloc(SideDataAlloc::new(kind, size))
      });
    };
    if size > 0 {
      // SAFETY: FFmpeg just allocated `size` bytes at `slot` (plus its
      // padding), and `entry.data()` is a `&[u8]` of exactly that
      // length; the two regions belong to different allocations.
      unsafe { core::ptr::copy_nonoverlapping(entry.data().as_ptr(), slot, size) };
    }
  }
  Ok(())
}

/// Builds an `AVPacket` that **shares** a view carrier's buffer instead
/// of copying it — the send half of the zero-copy chain.
///
/// # What the census found
///
/// 0.8 did not have this. Its reverse builders called the same
/// `try_packet_copy` the owned lane uses, so a packet demuxed
/// zero-copy was copied again on its way back into a decoder. The
/// zero-copy chain 0.8 actually shipped was demux to consumer, and
/// stopped there.
///
/// # The padding proof
///
/// libavcodec is entitled to read `AV_INPUT_BUFFER_PADDING_SIZE` bytes
/// **past** a packet's payload — bitstream readers do it routinely, and
/// libavformat allocates that slack behind every packet it produces. A
/// carrier viewing such a packet inherits the slack; a carrier this
/// crate minted from a slice through
/// [`FfmpegCarrier::from_bytes`](crate::FfmpegCarrier::from_bytes) does
/// not.
///
/// So sharing is offered only where the slack is **provable**, and
/// proving it takes two facts, not one:
///
/// * **provenance** — the carrier must be a payload captured out of an
///   `AVPacket`'s own buffer, which is the only place libavformat's
///   padding contract applies. Trailing *capacity* is not padding: a
///   video plane has more pixels after it and a resampler's output
///   frame has more samples, and a bitstream reader running past the
///   payload would eat either as though it were bitstream. Provenance
///   is recorded at capture, where it is known, rather than inferred
///   here from a size comparison that cannot tell padding from a
///   neighbour;
/// * **extent** — the view must still leave at least the padding
///   between its end and the end of the buffer, because a payload can
///   be narrowed after capture.
///
/// Where either fails, the packet is copied, which is what
/// `try_packet_copy` allocates the padding for. Handing a decoder an
/// unpadded buffer would be an out-of-bounds read inside somebody
/// else's bitstream reader, found by nobody.
pub(crate) fn share_or_copy(
  body: &crate::FfmpegBuffer,
) -> std::result::Result<Packet, ffmpeg_next::Error> {
  use ffmpeg_next::packet::Mut;

  const PADDING: usize = ffmpeg_next::ffi::AV_INPUT_BUFFER_PADDING_SIZE as usize;

  // **Empty first, before anything is dereferenced.** The empty carrier
  // is deliberately backed by no buffer at all — that is what makes a
  // placeholder plane slot free — so `as_av_buffer_ref` answers null
  // for it, and reading `size` through that would be undefined on the
  // most ordinary packet there is: a side-data-only one.
  if body.is_empty() {
    return try_packet_copy(&[]);
  }

  // Provenance before extent: the cheaper question, and the one that
  // decides whether the other is worth asking.
  if body.origin() != crate::view::Origin::PacketPayload {
    return try_packet_copy(body.as_ref());
  }

  let buf = body.as_av_buffer_ref();
  if buf.is_null() {
    return try_packet_copy(body.as_ref());
  }
  // SAFETY: the carrier holds a live reference, just checked non-null;
  // `size` is a public field on the `AVBufferRef` it names.
  let capacity = unsafe { (*buf).size };
  let end = body.offset().saturating_add(body.len());
  let padded = capacity
    .checked_sub(end)
    .is_some_and(|slack| slack >= PADDING);

  if !padded {
    // No provable slack behind the view — copy, which allocates the
    // padding libavcodec expects.
    return try_packet_copy(body.as_ref());
  }

  let mut out = Packet::empty();
  // SAFETY: `out` owns a live, zeroed `AVPacket`. `av_buffer_ref`
  // returns a new reference to the same allocation — the packet owns
  // that one and releases it on drop, while `body` keeps its own — and
  // `data`/`size` are set to the view, which was proved to lie inside
  // the buffer when the carrier was constructed.
  unsafe {
    let raw = out.as_mut_ptr();
    let shared = ffmpeg_next::ffi::av_buffer_ref(buf.cast_mut());
    if shared.is_null() {
      return Err(ffmpeg_next::Error::Other {
        errno: libc::ENOMEM,
      });
    }
    (*raw).buf = shared;
    (*raw).data = (*shared).data.add(body.offset());
    (*raw).size = c_int::try_from(body.len()).map_err(|_| ffmpeg_next::Error::Other {
      errno: libc::EINVAL,
    })?;
  }
  Ok(out)
}

/// Builds an `ffmpeg::Packet` from a [`mediadecode::VideoPacket`]
/// parameterized by [`crate::extras::VideoPacketExtra`] and
/// `FfmpegBytes`.
///
/// The compressed bytes are **copied** into a new packet allocation.
/// The return leg copies for the same reason the outbound one does:
/// the carrier is Rust-owned memory with no `AVBufferRef` behind it to
/// hand back.
/// PTS / DTS / duration / flags / stream_index are propagated.
///
/// Side data on the extras is reattached to the rebuilt packet — see
/// [`attach_side_data`] for why that is not optional.
///
/// Returns [`PacketBuildError`] on:
/// * payload larger than `c_int::MAX` (would overflow `AVPacket.size`);
/// * `av_new_packet` allocation failure (OOM);
/// * a side-data entry this build of FFmpeg cannot name, or one whose
///   allocation failed.
pub fn ffmpeg_packet_from_video_packet(
  packet: &mediadecode::packet::VideoPacket<VideoPacketExtra, crate::FfmpegBuffer>,
  limits: PacketLimits,
) -> std::result::Result<Packet, PacketBuildError> {
  build_video_packet::<crate::View>(packet, limits, BodyRoute::Copy)
}

/// [`ffmpeg_packet_from_video_packet`] on the owned lane.
pub fn ffmpeg_packet_from_owned_video_packet(
  packet: &mediadecode::packet::VideoPacket<VideoPacketExtra, FfmpegBytes>,
  limits: PacketLimits,
) -> std::result::Result<Packet, PacketBuildError> {
  build_video_packet::<crate::Owned>(packet, limits, BodyRoute::Copy)
}

/// [`ffmpeg_packet_from_video_packet`] built for **submission**, and
/// scoped so the packet cannot outlive the call.
///
/// This is the only road on which the view lane may hand a decoder
/// its own buffer rather than a copy, and the scoping is why it may.
/// An `ffmpeg_next::Packet` lends `&mut [u8]` through `data_mut`,
/// while the carrier it was built from still lends `&[u8]` — so a
/// shared body reachable from a value a caller *holds* is two live
/// references to one allocation with one of them mutable, out of
/// entirely safe code, and `!Sync` would not save it either. Here the
/// packet is built, lent to `submit` as `&Packet`, and dropped before
/// this function returns: no value that can produce a `&mut` into the
/// shared bytes ever exists.
///
/// **Which is why the route is the caller's to choose.** "Dropped
/// before this returns" is a claim about this function, and a decoder
/// that *records* what it is sent makes it false: the video decoder's
/// hardware probe `av_packet_ref`s every accepted packet into a rescue
/// history, and `FallbackFailed::unconsumed_packets` hands those
/// recordings to the caller as owned, **mutable** `Packet`s. A
/// submission that could be recorded must therefore carry
/// [`BodyRoute::Copy`], so that what enters the history is storage
/// nobody else reads. Callers with no history — every software road,
/// and the hardware road after commit — pass
/// [`BodyRoute::Submission`] and keep the zero-copy send.
pub(crate) fn with_ffmpeg_video_packet<C: crate::FfmpegCarrier + crate::CarrierOps, T>(
  packet: &mediadecode::packet::VideoPacket<VideoPacketExtra, C::Buffer>,
  limits: PacketLimits,
  route: BodyRoute,
  submit: impl FnOnce(&Packet) -> T,
) -> std::result::Result<T, PacketBuildError> {
  let av_packet = build_video_packet::<C>(packet, limits, route)?;
  let out = submit(&av_packet);
  drop(av_packet);
  Ok(out)
}

fn build_video_packet<C: crate::FfmpegCarrier + crate::CarrierOps>(
  packet: &mediadecode::packet::VideoPacket<VideoPacketExtra, C::Buffer>,
  limits: PacketLimits,
  route: BodyRoute,
) -> std::result::Result<Packet, PacketBuildError> {
  let body = packet.data().as_ref();
  // Before the budget and before the allocation: an uncarriable
  // payload is not made carriable by fitting.
  refuse_trusted(packet.flags(), body.len())?;
  // And the annotations, judged whole before the body is allocated —
  // a list that cannot be carried should not cost a packet first.
  check_side_data_budget(packet.extra().side_data())?;
  // **The into-FFmpeg budget, before the allocation.** `try_packet_copy`
  // duplicates these bytes into a fresh `AVPacket` and checks only that
  // they fit `c_int`. Without this the configured ceiling was dead on
  // the road a caller feeds a decoder directly: bytes the demux
  // boundary would have refused went straight into libavcodec.
  if body.len() > limits.max_packet_bytes() {
    return Err(PacketBuildError::SendPayloadTooLarge(
      SendPayloadTooLarge::new(body.len(), limits.max_packet_bytes()),
    ));
  }
  let mut out = C::packet_body(packet.data(), route)?;
  attach_side_data(&mut out, packet.extra().side_data())?;
  if let Some(ts) = packet.pts() {
    out.set_pts(Some(ts.pts()));
  }
  if let Some(ts) = packet.dts() {
    out.set_dts(Some(ts.pts()));
  }
  if let Some(d) = packet.duration() {
    out.set_duration(d.pts());
  }
  // SAFETY: `out` owns the `AVPacket` `try_packet_copy` just built.
  unsafe { write_md_flags(&mut out, packet.flags()) };
  out.set_stream(packet.extra().stream_index() as usize);
  Ok(out)
}

/// Builds an `ffmpeg::Packet` from a [`mediadecode::AudioPacket`].
/// Same shape as [`ffmpeg_packet_from_video_packet`] — bytes are
/// copied; pts/dts/duration/flags/stream_index and side data are
/// forwarded. Same failure modes.
pub fn ffmpeg_packet_from_audio_packet(
  packet: &mediadecode::packet::AudioPacket<AudioPacketExtra, crate::FfmpegBuffer>,
  limits: PacketLimits,
) -> std::result::Result<Packet, PacketBuildError> {
  build_audio_packet::<crate::View>(packet, limits, BodyRoute::Copy)
}

/// [`ffmpeg_packet_from_audio_packet`] on the owned lane.
pub fn ffmpeg_packet_from_owned_audio_packet(
  packet: &mediadecode::packet::AudioPacket<AudioPacketExtra, FfmpegBytes>,
  limits: PacketLimits,
) -> std::result::Result<Packet, PacketBuildError> {
  build_audio_packet::<crate::Owned>(packet, limits, BodyRoute::Copy)
}

/// [`ffmpeg_packet_from_audio_packet`] built for **submission**, and
/// scoped so the packet cannot outlive the call.
///
/// This is the only road on which the view lane may hand a decoder
/// its own buffer rather than a copy, and the scoping is why it may.
/// An `ffmpeg_next::Packet` lends `&mut [u8]` through `data_mut`,
/// while the carrier it was built from still lends `&[u8]` — so a
/// shared body reachable from a value a caller *holds* is two live
/// references to one allocation with one of them mutable, out of
/// entirely safe code, and `!Sync` would not save it either. Here the
/// packet is built, lent to `submit` as `&Packet`, and dropped before
/// this function returns: no value that can produce a `&mut` into the
/// shared bytes ever exists.
pub(crate) fn with_ffmpeg_audio_packet<C: crate::FfmpegCarrier + crate::CarrierOps, T>(
  packet: &mediadecode::packet::AudioPacket<AudioPacketExtra, C::Buffer>,
  limits: PacketLimits,
  route: BodyRoute,
  submit: impl FnOnce(&Packet) -> T,
) -> std::result::Result<T, PacketBuildError> {
  let av_packet = build_audio_packet::<C>(packet, limits, route)?;
  let out = submit(&av_packet);
  drop(av_packet);
  Ok(out)
}

fn build_audio_packet<C: crate::FfmpegCarrier + crate::CarrierOps>(
  packet: &mediadecode::packet::AudioPacket<AudioPacketExtra, C::Buffer>,
  limits: PacketLimits,
  route: BodyRoute,
) -> std::result::Result<Packet, PacketBuildError> {
  let body = packet.data().as_ref();
  // Before the budget and before the allocation: an uncarriable
  // payload is not made carriable by fitting.
  refuse_trusted(packet.flags(), body.len())?;
  // And the annotations, judged whole before the body is allocated —
  // a list that cannot be carried should not cost a packet first.
  check_side_data_budget(packet.extra().side_data())?;
  // **The into-FFmpeg budget, before the allocation.** `try_packet_copy`
  // duplicates these bytes into a fresh `AVPacket` and checks only that
  // they fit `c_int`. Without this the configured ceiling was dead on
  // the road a caller feeds a decoder directly: bytes the demux
  // boundary would have refused went straight into libavcodec.
  if body.len() > limits.max_packet_bytes() {
    return Err(PacketBuildError::SendPayloadTooLarge(
      SendPayloadTooLarge::new(body.len(), limits.max_packet_bytes()),
    ));
  }
  let mut out = C::packet_body(packet.data(), route)?;
  attach_side_data(&mut out, packet.extra().side_data())?;
  if let Some(ts) = packet.pts() {
    out.set_pts(Some(ts.pts()));
  }
  if let Some(ts) = packet.dts() {
    out.set_dts(Some(ts.pts()));
  }
  if let Some(d) = packet.duration() {
    out.set_duration(d.pts());
  }
  // SAFETY: `out` owns the `AVPacket` `try_packet_copy` just built.
  unsafe { write_md_flags(&mut out, packet.flags()) };
  out.set_stream(packet.extra().stream_index() as usize);
  Ok(out)
}

/// Builds an `ffmpeg::Packet` from a [`mediadecode::SubtitlePacket`].
/// Bytes copied; pts/duration/flags/stream_index and side data
/// forwarded. Subtitle packets have no `dts` in the mediadecode model.
/// Same failure modes as [`ffmpeg_packet_from_video_packet`].
pub fn ffmpeg_packet_from_subtitle_packet(
  packet: &mediadecode::packet::SubtitlePacket<SubtitlePacketExtra, crate::FfmpegBuffer>,
  limits: PacketLimits,
) -> std::result::Result<Packet, PacketBuildError> {
  build_subtitle_packet::<crate::View>(packet, limits, BodyRoute::Copy)
}

/// [`ffmpeg_packet_from_subtitle_packet`] on the owned lane.
pub fn ffmpeg_packet_from_owned_subtitle_packet(
  packet: &mediadecode::packet::SubtitlePacket<SubtitlePacketExtra, FfmpegBytes>,
  limits: PacketLimits,
) -> std::result::Result<Packet, PacketBuildError> {
  build_subtitle_packet::<crate::Owned>(packet, limits, BodyRoute::Copy)
}

/// [`ffmpeg_packet_from_subtitle_packet`] built for **submission**, and
/// scoped so the packet cannot outlive the call.
///
/// This is the only road on which the view lane may hand a decoder
/// its own buffer rather than a copy, and the scoping is why it may.
/// An `ffmpeg_next::Packet` lends `&mut [u8]` through `data_mut`,
/// while the carrier it was built from still lends `&[u8]` — so a
/// shared body reachable from a value a caller *holds* is two live
/// references to one allocation with one of them mutable, out of
/// entirely safe code, and `!Sync` would not save it either. Here the
/// packet is built, lent to `submit` as `&Packet`, and dropped before
/// this function returns: no value that can produce a `&mut` into the
/// shared bytes ever exists.
pub(crate) fn with_ffmpeg_subtitle_packet<C: crate::FfmpegCarrier + crate::CarrierOps, T>(
  packet: &mediadecode::packet::SubtitlePacket<SubtitlePacketExtra, C::Buffer>,
  limits: PacketLimits,
  route: BodyRoute,
  submit: impl FnOnce(&Packet) -> T,
) -> std::result::Result<T, PacketBuildError> {
  let av_packet = build_subtitle_packet::<C>(packet, limits, route)?;
  let out = submit(&av_packet);
  drop(av_packet);
  Ok(out)
}

fn build_subtitle_packet<C: crate::FfmpegCarrier + crate::CarrierOps>(
  packet: &mediadecode::packet::SubtitlePacket<SubtitlePacketExtra, C::Buffer>,
  limits: PacketLimits,
  route: BodyRoute,
) -> std::result::Result<Packet, PacketBuildError> {
  let body = packet.data().as_ref();
  // Before the budget and before the allocation: an uncarriable
  // payload is not made carriable by fitting.
  refuse_trusted(packet.flags(), body.len())?;
  // And the annotations, judged whole before the body is allocated —
  // a list that cannot be carried should not cost a packet first.
  check_side_data_budget(packet.extra().side_data())?;
  // **The into-FFmpeg budget, before the allocation.** `try_packet_copy`
  // duplicates these bytes into a fresh `AVPacket` and checks only that
  // they fit `c_int`. Without this the configured ceiling was dead on
  // the road a caller feeds a decoder directly: bytes the demux
  // boundary would have refused went straight into libavcodec.
  if body.len() > limits.max_packet_bytes() {
    return Err(PacketBuildError::SendPayloadTooLarge(
      SendPayloadTooLarge::new(body.len(), limits.max_packet_bytes()),
    ));
  }
  let mut out = C::packet_body(packet.data(), route)?;
  attach_side_data(&mut out, packet.extra().side_data())?;
  if let Some(ts) = packet.pts() {
    out.set_pts(Some(ts.pts()));
  }
  if let Some(d) = packet.duration() {
    out.set_duration(d.pts());
  }
  // SAFETY: `out` owns the `AVPacket` `try_packet_copy` just built.
  unsafe { write_md_flags(&mut out, packet.flags()) };
  out.set_stream(packet.extra().stream_index() as usize);
  Ok(out)
}

// ---------------------------------------------------------------------------
//  Safe wrappers — `&ffmpeg::Packet` → `mediadecode::*Packet`.
// ---------------------------------------------------------------------------

/// Carries a borrowed [`ffmpeg::Packet`] out as a
/// [`mediadecode::packet::VideoPacket`] on the **view** lane: the
/// payload is a refcounted window onto the source `AVPacket`'s own
/// buffer, which is why the delivered packet's lifetime is answerable
/// to libavformat's — see [the two carrier lanes][lanes]. For a packet
/// that must outlive the demuxer, ask for the owned lane by name:
/// `video_packet_from_ffmpeg_as::<Owned>`, whose payload is copied and
/// which the [D-seat amputation contract][law] governs.
///
/// Timestamps, duration, key/corrupt flags, and the source stream index
/// are forwarded to the produced packet.
///
/// Uses the default [`PacketLimits`]; the `_in` sibling takes them
/// explicitly, alongside the stream's timebase.
///
/// [lanes]: mediadecode::adapter#the-two-carrier-lanes
///
/// Returns `Ok(None)` when the source packet has no payload at all
/// (an empty packet — typical after EOF), and [`PacketBufferError`]
/// when a payload that *is* there could not be carried — over budget,
/// or claiming bytes outside its own buffer. Those are never the same
/// answer. Caller can also fill in [`VideoPacketExtra::byte_pos`] /
/// `side_data` post-construction if they need those.
///
/// [law]: mediadecode::adapter#the-d-seat-amputation-contract
pub fn video_packet_from_ffmpeg(
  packet: &Packet,
) -> Result<Option<VideoPacket<VideoPacketExtra, FfmpegBytes>>, PacketBufferError> {
  video_packet_from_borrowed::<crate::Owned>(
    packet,
    mediadecode::Timebase::default(),
    PacketLimits::default(),
    crate::buffer::PayloadProvenance::CallerSupplied,
  )
}

/// Carries a borrowed [`ffmpeg::Packet`] out as a
/// [`mediadecode::packet::AudioPacket`]. Same shape as
/// [`video_packet_from_ffmpeg`] — shared payload, forwarded metadata,
/// default budgets.
pub fn audio_packet_from_ffmpeg(
  packet: &Packet,
) -> Result<Option<AudioPacket<AudioPacketExtra, FfmpegBytes>>, PacketBufferError> {
  audio_packet_from_borrowed::<crate::Owned>(
    packet,
    mediadecode::Timebase::default(),
    PacketLimits::default(),
    crate::buffer::PayloadProvenance::CallerSupplied,
  )
}

/// Carries a borrowed [`ffmpeg::Packet`] out as a
/// [`mediadecode::packet::SubtitlePacket`]. Subtitle packets have no
/// `dts` in the mediadecode model; everything else mirrors
/// [`video_packet_from_ffmpeg`], shared payload included.
pub fn subtitle_packet_from_ffmpeg(
  packet: &Packet,
) -> Result<Option<SubtitlePacket<SubtitlePacketExtra, FfmpegBytes>>, PacketBufferError> {
  subtitle_packet_from_borrowed::<crate::Owned>(
    packet,
    mediadecode::Timebase::default(),
    PacketLimits::default(),
    crate::buffer::PayloadProvenance::CallerSupplied,
  )
}

/// The most side-data entries this crate will walk on one packet.
///
/// The floor is [`SIDE_DATA_MAX_ENTRIES`], the same bound the
/// frame-side collector uses; it rises with `AV_PKT_DATA_NB` so a
/// future FFmpeg that names more side-data types than the floor cannot
/// turn a legitimate packet into a refusal. Measured: FFmpeg's own
/// packet API cannot exceed one entry per named type — both
/// `av_packet_new_side_data` and `av_packet_add_side_data` replace an
/// existing entry of the same type — so a packet over this cap is one
/// no FFmpeg call produced.
fn side_data_entry_cap() -> usize {
  SIDE_DATA_MAX_ENTRIES.max(crate::ffi::side_data_type_count().max(0) as usize)
}

/// The side-data entries an `AVPacket` carries, copied into owned
/// values — **all of them, or none and an error**.
///
/// The packet twin of `convert::collect_side_data`, and bounded the
/// same way: at most [`side_data_entry_cap`] entries and
/// [`SIDE_DATA_MAX_TOTAL_BYTES`] bytes per packet, allocated through
/// `try_reserve_exact`. What is *not* the same is what happens when a
/// bound is reached. The frame collector truncates and warns, which it
/// can afford to — frame side data is descriptive, the frame is
/// delivered either way, and nothing downstream acts on it. Packet side
/// data is the opposite: `NEW_EXTRADATA` replaces a decoder's
/// parameters, `PARAM_CHANGE` moves its rate, `SKIP_SAMPLES` trims the
/// stream, and the codec acts on every one. A truncated copy is a
/// decoder quietly running on stale parameters, and a truncated copy of
/// a side-data-only packet is `Ok(None)` — the packet vanishing
/// entirely, which is the very defect this seam was built to close. So
/// every bound here is an error, and a caller either gets a packet with
/// all of its side data or a `DemuxError` naming what stopped it.
///
/// `AVPacket.side_data` is a flat array of `AVPacketSideData` (not the
/// array of pointers an `AVFrame` keeps), and its `type_` is read as
/// the integer it is on the wire — a discriminant this build has no
/// name for would be undefined behaviour the moment it existed as an
/// `AVPacketSideDataType`.
fn packet_side_data(packet: &Packet) -> Result<Vec<SideDataEntry>, PacketBufferError> {
  use ffmpeg_next::packet::Ref;
  // SAFETY: `packet` keeps the `AVPacket` live; `side_data` and
  // `side_data_elems` are public fields.
  let count_raw = unsafe { (*packet.as_ptr()).side_data_elems };
  let entries = unsafe { (*packet.as_ptr()).side_data };
  // Zero entries is the only shape that means "no side data". Every
  // other reading of that answer — a malformed count, a missing array —
  // is judged, and judged *before* the pointer: a count this crate
  // cannot walk stays an error whether or not the array happens to be
  // null, and a null array with entries to read is malformed rather
  // than empty. Both used to leave here as `Ok(vec![])`, which is the
  // silent loss the caps taught us to name, reached through the
  // pointer instead of the budget.
  if count_raw == 0 {
    return Ok(Vec::new());
  }
  let cap = side_data_entry_cap();
  if count_raw < 0 || count_raw as usize > cap {
    return Err(PacketBufferError::SideDataEntries(SideDataEntries::new(
      count_raw, cap,
    )));
  }
  if entries.is_null() {
    return Err(PacketBufferError::SideDataArray(SideDataArray::new(
      count_raw,
    )));
  }
  let count = count_raw as usize;
  let mut out: Vec<SideDataEntry> = Vec::new();
  if out.try_reserve_exact(count).is_err() {
    return Err(PacketBufferError::SideDataAlloc(BufferSideDataAlloc::new(
      count * core::mem::size_of::<SideDataEntry>(),
    )));
  }
  let mut total_bytes: usize = 0;
  for index in 0..count {
    // SAFETY: `entries` is valid for `count_raw` contiguous
    // `AVPacketSideData` values per FFmpeg's contract, and `index` is
    // below that count.
    let entry = unsafe { entries.add(index) };
    let kind = unsafe { read_unaligned(addr_of!((*entry).type_).cast::<i32>()) };
    let size = unsafe { (*entry).size };
    let data_ptr = unsafe { (*entry).data };
    let data = if size == 0 {
      // A marker entry: a type and no bytes. FFmpeg emits these, and
      // there is nothing to carry or to charge the budget for.
      FfmpegBytes::empty()
    } else if data_ptr.is_null() {
      // Bytes declared and not carried. Reading this as an empty entry
      // delivered a packet whose side data was a lie, and charged the
      // budget nothing for it.
      return Err(PacketBufferError::SideDataPayload(SideDataPayload::new(
        index, size,
      )));
    } else {
      total_bytes = total_bytes.saturating_add(size);
      if total_bytes > SIDE_DATA_MAX_TOTAL_BYTES {
        return Err(PacketBufferError::SideDataBytes(SideDataBytes::new(
          total_bytes,
          SIDE_DATA_MAX_TOTAL_BYTES,
        )));
      }
      let mut buf: Vec<u8> = Vec::new();
      if buf.try_reserve_exact(size).is_err() {
        return Err(PacketBufferError::SideDataAlloc(BufferSideDataAlloc::new(
          size,
        )));
      }
      // SAFETY: `data` is valid for `size` bytes per FFmpeg's
      // `AVPacketSideData` contract.
      buf.extend_from_slice(unsafe { core::slice::from_raw_parts(data_ptr, size) });
      // Staged through the `Vec` so `try_reserve_exact` keeps *one* of
      // the two payload-sized allocations a named refusal rather than
      // an abort. The carrier copy that follows is a second full
      // allocation of the same size — not a header — and it is
      // infallible, so what the staging really buys is that the first
      // and larger risk is reportable and the second is asked for a
      // size the allocator has just proved it has. The doubling is
      // affordable only because side data is capped at
      // [`SIDE_DATA_MAX_TOTAL_BYTES`]; the plane paths, which are not
      // small, use the one-allocation road instead.
      FfmpegBytes::copy_from_slice(&buf)
    };
    out.push(SideDataEntry::new(kind, data));
  }
  Ok(out)
}

/// The buffer a timed packet is delivered with, or `None` when there is
/// no packet to deliver at all.
///
/// **`size == 0` is not the same as "nothing".** A packet with no body
/// and one or more side-data entries is a real packet: FFmpeg uses that
/// shape for `AV_PKT_DATA_NEW_EXTRADATA` and for a parameter change,
/// and a decoder that never sees it keeps decoding on parameters the
/// container has already replaced. Such a packet is delivered with an
/// owned empty buffer — zero bytes, but a buffer, so the packet exists
/// and its side data rides the extras.
///
/// `Ok(None)` therefore means the packet carried neither a payload nor
/// side data: the empty marker, and the only thing a pull loop may skip.
fn delivered_payload<C: crate::FfmpegCarrier + crate::CarrierOps>(
  packet: &Packet,
  side_data: &[SideDataEntry],
  limits: PacketLimits,
  provenance: crate::buffer::PayloadProvenance,
) -> Result<Option<C::Buffer>, PacketBufferError> {
  use ffmpeg_next::packet::Ref;
  // SAFETY: `packet` keeps the AVPacket live for the duration of this
  // call, which is all `payload_of` requires.
  if let Some(bytes) = unsafe {
    crate::buffer::payload_of::<C>(packet.as_ptr(), limits.max_packet_bytes(), provenance)
  }? {
    return Ok(Some(bytes));
  }
  if side_data.is_empty() {
    return Ok(None);
  }
  Ok(Some(C::empty()))
}

// ---------------------------------------------------------------------------
//  Timebase-carrying variants.
//
//  An `AVPacket`'s timestamps are integers in its *stream's* timebase,
//  which the packet does not carry — the four functions above therefore
//  stamp `Timebase::default()` (1/1), leaving the caller to know what
//  the ticks meant. A demuxer knows: it holds the track table. These
//  variants take that timebase, so the produced `Timestamp` is a
//  complete, self-describing value.
// ---------------------------------------------------------------------------

/// [`video_packet_from_ffmpeg`], with the stream's timebase stamped
/// onto every timestamp instead of the 1/1 placeholder.
pub(crate) fn video_packet_from_ffmpeg_as<C: crate::FfmpegCarrier + crate::CarrierOps>(
  source: Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
  provenance: crate::buffer::PayloadProvenance,
) -> Result<Option<VideoPacket<VideoPacketExtra, C::Buffer>>, PacketBufferError> {
  // **The source is consumed.** A borrowed `AVPacket` cannot be
  // safely viewed: `ffmpeg_next::Packet` lends `&mut [u8]` through
  // `data_mut` and shares its buffer by refcount with no
  // copy-on-write, so a caller who kept the packet would hold a
  // mutable alias of every byte the returned carrier reads — and
  // both sides are `Send`. Taking it by value is what makes that
  // unconstructible; the packet is released when this returns, and
  // the view lane's carrier keeps the buffer alive by its own
  // reference. See the borrowed siblings for the owned lane, which
  // copies and so may borrow.
  video_packet_from_borrowed::<C>(&source, time_base, limits, provenance)
}

/// The shared implementation of the video road.
///
/// Crate-private, and borrowed: the two public doors differ only in
/// what they owe the borrow checker. The consuming one may be asked
/// for either lane; the borrowing one is the owned lane, where the
/// bytes are copied and the source is nobody's concern afterwards.
pub(crate) fn video_packet_from_borrowed<C: crate::FfmpegCarrier + crate::CarrierOps>(
  packet: &Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
  provenance: crate::buffer::PayloadProvenance,
) -> Result<Option<VideoPacket<VideoPacketExtra, C::Buffer>>, PacketBufferError> {
  let side_data = packet_side_data(packet)?;
  let Some(buf) = delivered_payload::<C>(packet, &side_data, limits, provenance)? else {
    return Ok(None);
  };
  let extra = VideoPacketExtra::new(packet.stream() as i32).with_side_data(side_data);
  let mut out = VideoPacket::new(buf, extra)
    .with_flags(md_flags_from_packet(packet)?)
    .with_pts(packet.pts().map(|p| Timestamp::new(p, time_base)))
    .with_dts(packet.dts().map(|d| Timestamp::new(d, time_base)));
  let dur = packet.duration();
  if dur > 0 {
    out = out.with_duration(Some(Timestamp::new(dur, time_base)));
  }
  Ok(Some(out))
}

/// [`audio_packet_from_ffmpeg`], with the stream's timebase stamped
/// onto every timestamp instead of the 1/1 placeholder.
pub(crate) fn audio_packet_from_ffmpeg_as<C: crate::FfmpegCarrier + crate::CarrierOps>(
  source: Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
  provenance: crate::buffer::PayloadProvenance,
) -> Result<Option<AudioPacket<AudioPacketExtra, C::Buffer>>, PacketBufferError> {
  // **The source is consumed.** A borrowed `AVPacket` cannot be
  // safely viewed: `ffmpeg_next::Packet` lends `&mut [u8]` through
  // `data_mut` and shares its buffer by refcount with no
  // copy-on-write, so a caller who kept the packet would hold a
  // mutable alias of every byte the returned carrier reads — and
  // both sides are `Send`. Taking it by value is what makes that
  // unconstructible; the packet is released when this returns, and
  // the view lane's carrier keeps the buffer alive by its own
  // reference. See the borrowed siblings for the owned lane, which
  // copies and so may borrow.
  audio_packet_from_borrowed::<C>(&source, time_base, limits, provenance)
}

/// The shared implementation of the audio road.
///
/// Crate-private, and borrowed: the two public doors differ only in
/// what they owe the borrow checker. The consuming one may be asked
/// for either lane; the borrowing one is the owned lane, where the
/// bytes are copied and the source is nobody's concern afterwards.
pub(crate) fn audio_packet_from_borrowed<C: crate::FfmpegCarrier + crate::CarrierOps>(
  packet: &Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
  provenance: crate::buffer::PayloadProvenance,
) -> Result<Option<AudioPacket<AudioPacketExtra, C::Buffer>>, PacketBufferError> {
  let side_data = packet_side_data(packet)?;
  let Some(buf) = delivered_payload::<C>(packet, &side_data, limits, provenance)? else {
    return Ok(None);
  };
  let extra = AudioPacketExtra::new(packet.stream() as i32).with_side_data(side_data);
  let mut out = AudioPacket::new(buf, extra)
    .with_flags(md_flags_from_packet(packet)?)
    .with_pts(packet.pts().map(|p| Timestamp::new(p, time_base)))
    .with_dts(packet.dts().map(|d| Timestamp::new(d, time_base)));
  let dur = packet.duration();
  if dur > 0 {
    out = out.with_duration(Some(Timestamp::new(dur, time_base)));
  }
  Ok(Some(out))
}

/// [`subtitle_packet_from_ffmpeg`], with the stream's timebase stamped
/// onto every timestamp instead of the 1/1 placeholder.
pub(crate) fn subtitle_packet_from_ffmpeg_as<C: crate::FfmpegCarrier + crate::CarrierOps>(
  source: Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
  provenance: crate::buffer::PayloadProvenance,
) -> Result<Option<SubtitlePacket<SubtitlePacketExtra, C::Buffer>>, PacketBufferError> {
  // **The source is consumed.** A borrowed `AVPacket` cannot be
  // safely viewed: `ffmpeg_next::Packet` lends `&mut [u8]` through
  // `data_mut` and shares its buffer by refcount with no
  // copy-on-write, so a caller who kept the packet would hold a
  // mutable alias of every byte the returned carrier reads — and
  // both sides are `Send`. Taking it by value is what makes that
  // unconstructible; the packet is released when this returns, and
  // the view lane's carrier keeps the buffer alive by its own
  // reference. See the borrowed siblings for the owned lane, which
  // copies and so may borrow.
  subtitle_packet_from_borrowed::<C>(&source, time_base, limits, provenance)
}

/// The shared implementation of the subtitle road.
///
/// Crate-private, and borrowed: the two public doors differ only in
/// what they owe the borrow checker. The consuming one may be asked
/// for either lane; the borrowing one is the owned lane, where the
/// bytes are copied and the source is nobody's concern afterwards.
pub(crate) fn subtitle_packet_from_borrowed<C: crate::FfmpegCarrier + crate::CarrierOps>(
  packet: &Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
  provenance: crate::buffer::PayloadProvenance,
) -> Result<Option<SubtitlePacket<SubtitlePacketExtra, C::Buffer>>, PacketBufferError> {
  let side_data = packet_side_data(packet)?;
  let Some(buf) = delivered_payload::<C>(packet, &side_data, limits, provenance)? else {
    return Ok(None);
  };
  let extra = SubtitlePacketExtra::new(packet.stream() as i32).with_side_data(side_data);
  let mut out = SubtitlePacket::new(buf, extra)
    .with_flags(md_flags_from_packet(packet)?)
    .with_pts(packet.pts().map(|p| Timestamp::new(p, time_base)));
  let dur = packet.duration();
  if dur > 0 {
    out = out.with_duration(Some(Timestamp::new(dur, time_base)));
  }
  Ok(Some(out))
}

/// Wraps a borrowed [`ffmpeg::Packet`] from a **data** track — timecode,
/// KLV, timed ID3 — as a [`mediadecode::demuxer::DataPacket`], with the
/// stream's timebase stamped onto every timestamp.
///
/// Data packets are never reordered, so the mediadecode model gives
/// them no `dts` seat; everything else mirrors
/// [`video_packet_from_ffmpeg_in`]. `byte_pos` is forwarded from
/// `AVPacket.pos`, which data consumers use to correlate a payload with
/// its position in the file.
pub(crate) fn data_packet_from_ffmpeg_as<C: crate::FfmpegCarrier + crate::CarrierOps>(
  source: Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
  provenance: crate::buffer::PayloadProvenance,
) -> Result<Option<DataPacket<DataPacketExtra, C::Buffer>>, PacketBufferError> {
  // **The source is consumed.** A borrowed `AVPacket` cannot be
  // safely viewed: `ffmpeg_next::Packet` lends `&mut [u8]` through
  // `data_mut` and shares its buffer by refcount with no
  // copy-on-write, so a caller who kept the packet would hold a
  // mutable alias of every byte the returned carrier reads — and
  // both sides are `Send`. Taking it by value is what makes that
  // unconstructible; the packet is released when this returns, and
  // the view lane's carrier keeps the buffer alive by its own
  // reference. See the borrowed siblings for the owned lane, which
  // copies and so may borrow.
  data_packet_from_borrowed::<C>(&source, time_base, limits, provenance)
}

/// The shared implementation of the data road.
///
/// Crate-private, and borrowed: the two public doors differ only in
/// what they owe the borrow checker. The consuming one may be asked
/// for either lane; the borrowing one is the owned lane, where the
/// bytes are copied and the source is nobody's concern afterwards.
pub(crate) fn data_packet_from_borrowed<C: crate::FfmpegCarrier + crate::CarrierOps>(
  packet: &Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
  provenance: crate::buffer::PayloadProvenance,
) -> Result<Option<DataPacket<DataPacketExtra, C::Buffer>>, PacketBufferError> {
  let side_data = packet_side_data(packet)?;
  let Some(buf) = delivered_payload::<C>(packet, &side_data, limits, provenance)? else {
    return Ok(None);
  };
  let pos = packet.position();
  let extra = DataPacketExtra::new(packet.stream() as i32)
    .with_byte_pos((pos >= 0).then_some(pos as i64))
    .with_side_data(side_data);
  let mut out = DataPacket::new(buf, extra)
    .with_flags(md_flags_from_packet(packet)?)
    .with_pts(packet.pts().map(|p| Timestamp::new(p, time_base)));
  let dur = packet.duration();
  if dur > 0 {
    out = out.with_duration(Some(Timestamp::new(dur, time_base)));
  }
  Ok(Some(out))
}

/// Wraps a borrowed [`ffmpeg::Packet`] from an **attachment** track —
/// cover art that the container really does store as a packet — as a
/// [`mediadecode::demuxer::AttachmentPacket`].
///
/// No timestamps are forwarded, and there is nowhere to put them: an
/// attachment is not on the timeline. `synthesized` is `false`, because
/// this payload came from a real packet; the demuxer sets it `true` for
/// the packets it builds out of codec extradata.
///
/// **The one arm where a payload-less packet really is nothing.** The
/// four timed conversions deliver a side-data-only packet, because
/// codec-control side data is content a decoder must see. An attachment
/// is not decoded and is not on a timeline: it *is* its bytes, so a
/// packet carrying none carries no attachment, and this answers
/// `Ok(None)`. `AttachmentPacketExtra` accordingly has no side-data
/// seat — a deliberate absence, not an oversight.
pub(crate) fn attachment_packet_from_ffmpeg_as<C: crate::FfmpegCarrier + crate::CarrierOps>(
  source: Packet,
  limits: PacketLimits,
  provenance: crate::buffer::PayloadProvenance,
) -> Result<Option<AttachmentPacket<AttachmentPacketExtra, C::Buffer>>, PacketBufferError> {
  // **The source is consumed.** A borrowed `AVPacket` cannot be
  // safely viewed: `ffmpeg_next::Packet` lends `&mut [u8]` through
  // `data_mut` and shares its buffer by refcount with no
  // copy-on-write, so a caller who kept the packet would hold a
  // mutable alias of every byte the returned carrier reads — and
  // both sides are `Send`. Taking it by value is what makes that
  // unconstructible; the packet is released when this returns, and
  // the view lane's carrier keeps the buffer alive by its own
  // reference. See the borrowed siblings for the owned lane, which
  // copies and so may borrow.
  attachment_packet_from_borrowed::<C>(&source, limits, provenance)
}

/// The shared implementation of the attachment road.
///
/// Crate-private, and borrowed: the two public doors differ only in
/// what they owe the borrow checker. The consuming one may be asked
/// for either lane; the borrowing one is the owned lane, where the
/// bytes are copied and the source is nobody's concern afterwards.
pub(crate) fn attachment_packet_from_borrowed<C: crate::FfmpegCarrier + crate::CarrierOps>(
  packet: &Packet,
  limits: PacketLimits,
  provenance: crate::buffer::PayloadProvenance,
) -> Result<Option<AttachmentPacket<AttachmentPacketExtra, C::Buffer>>, PacketBufferError> {
  use ffmpeg_next::packet::Ref;
  // SAFETY: `packet` keeps the AVPacket live for the duration of this
  // call, which is all `payload_of` requires.
  let Some(buf) = (unsafe {
    crate::buffer::payload_of::<C>(packet.as_ptr(), limits.max_packet_bytes(), provenance)
  })?
  else {
    return Ok(None);
  };
  Ok(Some(
    AttachmentPacket::new(buf, AttachmentPacketExtra::new(packet.stream() as i32))
      .with_flags(md_flags_from_packet(packet)?),
  ))
}

/// Every flag the packet really carries.
///
/// Read from `AVPacket.flags` as the raw integer rather than through
/// `ffmpeg_next`'s `Packet::flags()`, whose `Flags` bit set names only
/// `KEY` and `CORRUPT` and drops the rest in `from_bits_truncate`.
/// `AV_PKT_FLAG_DISCARD` is among the dropped: it tells a consumer that
/// a packet must be fed to the decoder and its output thrown away, and
/// losing it makes preroll output look like something to keep.
///
/// `PacketFlags` is a bit set whose documented lossless door is
/// `from_bits_retain`, and every packet flag FFmpeg names lives inside
/// the byte it carries — including the three bits nothing names yet.
/// A bit outside that byte cannot be carried at all, and is refused
/// rather than dropped; the assertion below states the fact that keeps
/// the refusal unreachable against this build.
fn md_flags_from_packet(packet: &Packet) -> Result<MdPacketFlags, PacketBufferError> {
  use ffmpeg_next::packet::Ref;
  // SAFETY: `packet` keeps the `AVPacket` live for the call, which is
  // all the raw reader asks.
  unsafe { md_flags_from_av_packet(packet.as_ptr()) }
}

/// [`md_flags_from_packet`] for an `AVPacket` that has no safe wrapper
/// — the one libavformat embeds in an `AVStream` for cover art.
///
/// The demuxer hoists that packet by hand, and hoisting it *without*
/// its flags was how an attached picture arrived with none: FFmpeg
/// marks it `AV_PKT_FLAG_KEY`, which is the one thing a still image is
/// certain to be. One reader, so a second construction site cannot
/// quietly disagree with the five that go through the boundary.
///
/// # Safety
///
/// `pkt` must be a live `*const AVPacket` for the duration of this
/// call.
pub(crate) unsafe fn md_flags_from_av_packet(
  pkt: *const ffmpeg_next::ffi::AVPacket,
) -> Result<MdPacketFlags, PacketBufferError> {
  // SAFETY: `pkt` is live per the contract above; `flags` is a public
  // field and a plain `c_int`.
  let raw = unsafe { (*pkt).flags };
  let carried = i32::from(u8::MAX);
  if raw & !carried != 0 {
    return Err(PacketBufferError::UnrepresentableFlags(
      UnrepresentableFlags::new(raw),
    ));
  }
  Ok(MdPacketFlags::from_bits_retain(raw as u8))
}

/// What a track is, folded from `AVCodecParameters.codec_type` read as
/// the integer it is on the wire.
///
/// The dependency-API half of this crate's open-C-enum discipline.
/// `ffmpeg_next::codec::Parameters::medium()` materialises an
/// `AVMediaType` out of FFmpeg memory to answer the same question, and
/// a value outside this build's discriminant set is undefined behaviour
/// the moment it exists — before any `match` on it can run. That the
/// set is small and has been stable for years is a reason it has not
/// bitten, not a reason it cannot.
///
/// So the read is raw and the fold is total: anything this build does
/// not name becomes [`Unknown`](Self::Unknown), which the callers
/// already had to handle.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, IsVariant)]
pub enum MediaKind {
  /// `AVMEDIA_TYPE_VIDEO`.
  Video,
  /// `AVMEDIA_TYPE_AUDIO`.
  Audio,
  /// `AVMEDIA_TYPE_SUBTITLE`.
  Subtitle,
  /// `AVMEDIA_TYPE_DATA`.
  Data,
  /// `AVMEDIA_TYPE_ATTACHMENT`.
  Attachment,
  /// Anything this build of FFmpeg does not name, `AVMEDIA_TYPE_UNKNOWN`
  /// included.
  Unknown,
}

/// Folds a raw `AVMediaType` integer into [`MediaKind`].
pub(crate) fn media_kind_from_raw(raw: i32) -> MediaKind {
  use ffmpeg_next::ffi::AVMediaType::*;
  match raw {
    x if x == AVMEDIA_TYPE_VIDEO as i32 => MediaKind::Video,
    x if x == AVMEDIA_TYPE_AUDIO as i32 => MediaKind::Audio,
    x if x == AVMEDIA_TYPE_SUBTITLE as i32 => MediaKind::Subtitle,
    x if x == AVMEDIA_TYPE_DATA as i32 => MediaKind::Data,
    x if x == AVMEDIA_TYPE_ATTACHMENT as i32 => MediaKind::Attachment,
    _ => MediaKind::Unknown,
  }
}

/// The medium a set of codec parameters declares, without forming an
/// `AVMediaType`. Replaces every `Parameters::medium()` call in this
/// crate — see [`MediaKind`].
pub(crate) fn media_kind_of(parameters: &ffmpeg_next::codec::Parameters) -> MediaKind {
  // SAFETY: `as_ptr` is `unsafe` only because the pointer must not
  // outlive `parameters`; it is used and discarded inside this call.
  let ptr = unsafe { parameters.as_ptr() };
  if ptr.is_null() {
    return MediaKind::Unknown;
  }
  // SAFETY: `ptr` is a live `*const AVCodecParameters`; `addr_of!`
  // computes the field address without forming a reference, and reading
  // as `i32` matches the bindgen enum's `c_int` storage.
  let raw = unsafe { read_unaligned(addr_of!((*ptr).codec_type) as *const i32) };
  media_kind_from_raw(raw)
}

/// Every packet flag this build of FFmpeg names fits the byte
/// `PacketFlags` carries. If that stops being true, this fails the
/// build rather than letting a flag go missing at run time.
const _: () = {
  assert!(
    (ffmpeg_next::ffi::AV_PKT_FLAG_KEY
      | ffmpeg_next::ffi::AV_PKT_FLAG_CORRUPT
      | ffmpeg_next::ffi::AV_PKT_FLAG_DISCARD
      | ffmpeg_next::ffi::AV_PKT_FLAG_TRUSTED
      | ffmpeg_next::ffi::AV_PKT_FLAG_DISPOSABLE)
      <= u8::MAX as c_int,
    "FFmpeg names a packet flag outside the byte `PacketFlags` carries",
  );
};

// ---------------------------------------------------------------------------
//  Empty-frame placeholders for `receive_frame` destinations.
// ---------------------------------------------------------------------------

/// Constructs an empty [`mediadecode::frame::VideoFrame`] suitable as
/// the destination argument to
/// [`mediadecode::decoder::VideoStreamDecoder::receive_frame`]. The
/// decoder overwrites the frame on success; this just provides a
/// well-formed slot.
///
/// All four plane slots hold the shared empty carrier (the array shape
/// requires a buffer in every slot, but `plane_count = 0` reports them
/// as inactive).
///
/// **Infallible on both lanes, and the `try_` sibling is gone.**
/// Through 0.8 each slot was its own one-byte `AVBufferRef`, so
/// building a placeholder was four FFmpeg allocations that could fail —
/// hence a `try_empty_video_frame` returning `Option` and an
/// `empty_video_frame` that panicked on `None`. Neither lane allocates
/// here now: the owned lane's empty carrier is made once for the
/// process and cloned by refcount, and the view lane's is a null-backed
/// zero-length view, which is what lets the *view* lane keep the same
/// infallible constructor rather than reintroducing 0.8's failure mode
/// under a new name. A constructor with no failure mode does not get to
/// keep a `Result`-shaped door.
pub(crate) fn empty_video_frame_as<C: crate::FfmpegCarrier + crate::CarrierOps>()
-> VideoFrame<PixelFormat, VideoFrameExtra, C::Buffer> {
  VideoFrame::new(
    Dimensions::new(0, 0),
    // mediaframe 0.3's named "no format yet" member, and its
    // `Default` — the state a descriptor is in before a decoder has
    // said what it produces, which is exactly this placeholder.
    PixelFormat::None,
    core::array::from_fn(|_| Plane::new(C::empty(), 0)),
    0,
    VideoFrameExtra::default(),
  )
}

/// Constructs an empty [`mediadecode::frame::AudioFrame`] suitable as
/// the destination argument to
/// [`mediadecode::decoder::AudioStreamDecoder::receive_frame`]. Same
/// behaviour as [`empty_video_frame`] — eight shared empty plane
/// carriers, `plane_count = 0`, and no way to fail.
pub(crate) fn empty_audio_frame_as<C: crate::FfmpegCarrier + crate::CarrierOps>()
-> AudioFrame<SampleFormat, ChannelLayoutDescription, AudioFrameExtra, C::Buffer> {
  AudioFrame::new(
    0,
    0,
    0,
    SampleFormat::NONE,
    ChannelLayoutDescription::default(),
    core::array::from_fn(|_| Plane::new(C::empty(), 0)),
    0,
    AudioFrameExtra::default(),
  )
}

/// Constructs an empty [`mediadecode::frame::SubtitleFrame`] suitable
/// as the destination argument to
/// [`mediadecode::decoder::SubtitleDecoder::receive_frame`]. The
/// payload is an empty `Text` placeholder; the decoder overwrites it
/// on success. Infallible, as its two siblings are.
pub(crate) fn empty_subtitle_frame_as<C: crate::FfmpegCarrier + crate::CarrierOps>()
-> SubtitleFrame<SubtitleFrameExtra, C::Buffer> {
  SubtitleFrame::new(
    SubtitlePayload::Text(SubtitleText::new(C::empty(), None)),
    SubtitleFrameExtra::default(),
  )
}

// --- The bare names, on the view lane --------------------------------
//
// **The signature says the lane, and the compiler enforces it.** A
// conversion that *borrows* its `AVPacket` can only copy: the packet
// type lends `&mut [u8]` and shares its buffer by refcount with no
// copy-on-write, so a caller who still holds the packet holds a mutable
// alias of anything a view would read. A conversion that *consumes* its
// packet can share, because after it returns there is no other handle.
//
// So: **borrow in, owned lane out; move in, either lane out.** The
// `_in` names below take the packet by value and answer with the view
// lane — the ordinary road, where a direct consumer reads a packet in
// place and drops it — while the lane-generic `_as` workers, also
// by value, let a caller ask for either. The borrowing doors are the
// bare `*_packet_from_ffmpeg` names, and they are the owned lane.
//
// A generic function has no default parameter to fall back on (defaults
// are used when a type is *written*, not inferred from a call), so
// these are monomorphic wrappers rather than a default that would never
// apply.

/// [`video_packet_from_ffmpeg_as`] on the view lane, with the
/// stream's timebase stamped onto every timestamp.
///
/// **Takes the packet by value**, and that is the safety property, not
/// a style choice. The program below is the alias this signature
/// forbids — it was accepted when the parameter was a reference, and
/// `data_mut` handed out a `&mut [u8]` over bytes the returned carrier
/// was still lending as `&[u8]`:
///
/// ```compile_fail,E0382
/// use ffmpeg_next::packet::Mut;
/// use mediadecode::Timebase;
/// use mediadecode_ffmpeg::{PacketLimits, video_packet_from_ffmpeg_in};
///
/// let mut packet = ffmpeg_next::Packet::copy(&[0u8; 64]);
/// let viewed = video_packet_from_ffmpeg_in(packet, Timebase::default(), PacketLimits::default());
/// // The source is gone, so this cannot be written.
/// let _aliased = packet.data_mut();
/// ```
///
/// A caller who wants to keep the packet wants the owned lane, which
/// copies and may therefore borrow:
/// [`owned_video_packet_from_ffmpeg_in`].
pub fn video_packet_from_ffmpeg_in(
  packet: Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
) -> Result<Option<VideoPacket<VideoPacketExtra, crate::FfmpegBuffer>>, PacketBufferError> {
  video_packet_from_ffmpeg_as::<crate::View>(
    packet,
    time_base,
    limits,
    crate::buffer::PayloadProvenance::CallerSupplied,
  )
}

/// [`audio_packet_from_ffmpeg_as`] on the view lane.
pub fn audio_packet_from_ffmpeg_in(
  packet: Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
) -> Result<Option<AudioPacket<AudioPacketExtra, crate::FfmpegBuffer>>, PacketBufferError> {
  audio_packet_from_ffmpeg_as::<crate::View>(
    packet,
    time_base,
    limits,
    crate::buffer::PayloadProvenance::CallerSupplied,
  )
}

/// [`subtitle_packet_from_ffmpeg_as`] on the view lane.
pub fn subtitle_packet_from_ffmpeg_in(
  packet: Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
) -> Result<Option<SubtitlePacket<SubtitlePacketExtra, crate::FfmpegBuffer>>, PacketBufferError> {
  subtitle_packet_from_ffmpeg_as::<crate::View>(
    packet,
    time_base,
    limits,
    crate::buffer::PayloadProvenance::CallerSupplied,
  )
}

/// [`data_packet_from_ffmpeg_as`] on the view lane.
pub fn data_packet_from_ffmpeg_in(
  packet: Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
) -> Result<Option<DataPacket<DataPacketExtra, crate::FfmpegBuffer>>, PacketBufferError> {
  data_packet_from_ffmpeg_as::<crate::View>(
    packet,
    time_base,
    limits,
    crate::buffer::PayloadProvenance::CallerSupplied,
  )
}

// --- The borrowing doors, on the owned lane --------------------------
//
// The owned lane may borrow because it copies: nothing it returns
// points at the source, so the source's fate is its own business.
// These are the shapes the view lane cannot offer, and the reason it
// cannot is the whole of the split — see the note above the `_in`
// family.

/// [`video_packet_from_ffmpeg`] with the stream's timebase and explicit
/// budgets.
pub fn owned_video_packet_from_ffmpeg_in(
  packet: &Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
) -> Result<Option<VideoPacket<VideoPacketExtra, FfmpegBytes>>, PacketBufferError> {
  video_packet_from_borrowed::<crate::Owned>(
    packet,
    time_base,
    limits,
    crate::buffer::PayloadProvenance::CallerSupplied,
  )
}

/// [`audio_packet_from_ffmpeg`] with the stream's timebase and explicit
/// budgets.
pub fn owned_audio_packet_from_ffmpeg_in(
  packet: &Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
) -> Result<Option<AudioPacket<AudioPacketExtra, FfmpegBytes>>, PacketBufferError> {
  audio_packet_from_borrowed::<crate::Owned>(
    packet,
    time_base,
    limits,
    crate::buffer::PayloadProvenance::CallerSupplied,
  )
}

/// [`subtitle_packet_from_ffmpeg`] with the stream's timebase and
/// explicit budgets.
pub fn owned_subtitle_packet_from_ffmpeg_in(
  packet: &Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
) -> Result<Option<SubtitlePacket<SubtitlePacketExtra, FfmpegBytes>>, PacketBufferError> {
  subtitle_packet_from_borrowed::<crate::Owned>(
    packet,
    time_base,
    limits,
    crate::buffer::PayloadProvenance::CallerSupplied,
  )
}

/// [`data_packet_from_ffmpeg_in`] on the owned lane, borrowing its
/// source.
pub fn owned_data_packet_from_ffmpeg_in(
  packet: &Packet,
  time_base: mediadecode::Timebase,
  limits: PacketLimits,
) -> Result<Option<DataPacket<DataPacketExtra, FfmpegBytes>>, PacketBufferError> {
  data_packet_from_borrowed::<crate::Owned>(
    packet,
    time_base,
    limits,
    crate::buffer::PayloadProvenance::CallerSupplied,
  )
}

/// [`attachment_packet_from_ffmpeg`] on the owned lane, borrowing its
/// source.
pub fn owned_attachment_packet_from_ffmpeg(
  packet: &Packet,
  limits: PacketLimits,
) -> Result<Option<AttachmentPacket<AttachmentPacketExtra, FfmpegBytes>>, PacketBufferError> {
  attachment_packet_from_borrowed::<crate::Owned>(
    packet,
    limits,
    crate::buffer::PayloadProvenance::CallerSupplied,
  )
}

/// [`empty_video_frame_as`] on the view lane — the destination a
/// [`FfmpegVideoStreamDecoder`](crate::FfmpegVideoStreamDecoder) fills.
#[must_use]
pub fn empty_video_frame() -> VideoFrame<PixelFormat, VideoFrameExtra, crate::FfmpegBuffer> {
  empty_video_frame_as::<crate::View>()
}

/// [`empty_video_frame_as`] on the owned lane.
#[must_use]
pub fn empty_owned_video_frame() -> VideoFrame<PixelFormat, VideoFrameExtra, FfmpegBytes> {
  empty_video_frame_as::<crate::Owned>()
}

/// [`empty_audio_frame_as`] on the view lane.
#[must_use]
pub fn empty_audio_frame()
-> AudioFrame<SampleFormat, ChannelLayoutDescription, AudioFrameExtra, crate::FfmpegBuffer> {
  empty_audio_frame_as::<crate::View>()
}

/// [`empty_audio_frame_as`] on the owned lane.
#[must_use]
pub fn empty_owned_audio_frame()
-> AudioFrame<SampleFormat, ChannelLayoutDescription, AudioFrameExtra, FfmpegBytes> {
  empty_audio_frame_as::<crate::Owned>()
}

/// [`empty_subtitle_frame_as`] on the view lane.
#[must_use]
pub fn empty_subtitle_frame() -> SubtitleFrame<SubtitleFrameExtra, crate::FfmpegBuffer> {
  empty_subtitle_frame_as::<crate::View>()
}

/// [`empty_subtitle_frame_as`] on the owned lane.
#[must_use]
pub fn empty_owned_subtitle_frame() -> SubtitleFrame<SubtitleFrameExtra, FfmpegBytes> {
  empty_subtitle_frame_as::<crate::Owned>()
}

/// [`attachment_packet_from_ffmpeg_as`] on the view lane.
pub fn attachment_packet_from_ffmpeg(
  packet: Packet,
  limits: PacketLimits,
) -> Result<Option<AttachmentPacket<AttachmentPacketExtra, crate::FfmpegBuffer>>, PacketBufferError>
{
  attachment_packet_from_ffmpeg_as::<crate::View>(
    packet,
    limits,
    crate::buffer::PayloadProvenance::CallerSupplied,
  )
}

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

  /// A refcounted packet whose header claims a payload larger than the
  /// buffer behind it — the shape a malformed container, or an
  /// `av_packet_split_side_data` gone wrong, produces. Its payload is
  /// *there* and cannot be wrapped, which is exactly the case that must
  /// not read as "empty".
  fn out_of_bounds_packet() -> Packet {
    use ffmpeg_next::packet::Mut;
    let mut packet = Packet::copy(&[1u8, 2, 3, 4]);
    // SAFETY: `packet` owns a live `AVPacket` with a refcounted buffer;
    // `size` is a public field.
    unsafe {
      (*packet.as_mut_ptr()).size = 1 << 20;
    }
    packet
  }

  #[test]
  fn a_payload_that_cannot_be_wrapped_is_an_error_on_every_arm() {
    // All five delivery arms plus the attachment path go through the
    // same wrapper. If one of them still folded the failure into
    // `None`, the demuxer would drop that kind of packet in silence.
    let forged = out_of_bounds_packet();
    let tb = mediadecode::Timebase::default();
    assert!(matches!(
      video_packet_from_borrowed::<crate::Owned>(
        &forged,
        tb,
        PacketLimits::default(),
        crate::buffer::PayloadProvenance::CallerSupplied
      ),
      Err(PacketBufferError::Bounds(_)),
    ));
    assert!(matches!(
      audio_packet_from_borrowed::<crate::Owned>(
        &forged,
        tb,
        PacketLimits::default(),
        crate::buffer::PayloadProvenance::CallerSupplied
      ),
      Err(PacketBufferError::Bounds(_)),
    ));
    assert!(matches!(
      subtitle_packet_from_borrowed::<crate::Owned>(
        &forged,
        tb,
        PacketLimits::default(),
        crate::buffer::PayloadProvenance::CallerSupplied
      ),
      Err(PacketBufferError::Bounds(_)),
    ));
    assert!(matches!(
      data_packet_from_borrowed::<crate::Owned>(
        &forged,
        tb,
        PacketLimits::default(),
        crate::buffer::PayloadProvenance::CallerSupplied
      ),
      Err(PacketBufferError::Bounds(_)),
    ));
    assert!(matches!(
      attachment_packet_from_borrowed::<crate::Owned>(
        &forged,
        PacketLimits::default(),
        crate::buffer::PayloadProvenance::CallerSupplied
      ),
      Err(PacketBufferError::Bounds(_)),
    ));
  }

  /// A packet with **no body** and one side-data entry — the shape
  /// FFmpeg uses to hand a decoder new extradata or a parameter change.
  /// `size` is 0 and `buf` is null, which is exactly what used to read
  /// as "empty, skip it".
  fn side_data_only_packet() -> Packet {
    use ffmpeg_next::{
      ffi::{AVPacketSideDataType, av_packet_new_side_data},
      packet::Mut,
    };
    let mut packet = Packet::empty();
    // SAFETY: `packet` owns a live `AVPacket`; the side-data type is a
    // compile-time constant of this build, so no invalid discriminant
    // is formed. The returned pointer is valid for the four bytes just
    // allocated.
    unsafe {
      let ptr = av_packet_new_side_data(
        packet.as_mut_ptr(),
        AVPacketSideDataType::AV_PKT_DATA_NEW_EXTRADATA,
        4,
      );
      assert!(!ptr.is_null(), "av_packet_new_side_data");
      core::ptr::copy_nonoverlapping([1u8, 2, 3, 4].as_ptr(), ptr, 4);
    }
    packet
  }

  const NEW_EXTRADATA: i32 =
    ffmpeg_next::ffi::AVPacketSideDataType::AV_PKT_DATA_NEW_EXTRADATA as i32;

  use ffmpeg_next::ffi::{AVPacketSideData, AVPacketSideDataType, av_malloc};

  /// A packet carrying one side-data entry of exactly `size` bytes,
  /// with a body when `body` is set.
  fn packet_with_side_data(size: usize, body: bool) -> Packet {
    use ffmpeg_next::{
      ffi::{AVPacketSideDataType, av_packet_new_side_data},
      packet::Mut,
    };
    let mut packet = if body {
      Packet::copy(&[1u8, 2, 3])
    } else {
      Packet::empty()
    };
    // SAFETY: `packet` owns a live `AVPacket` and the type is a
    // compile-time constant of this build.
    let ptr = unsafe {
      av_packet_new_side_data(
        packet.as_mut_ptr(),
        AVPacketSideDataType::AV_PKT_DATA_NEW_EXTRADATA,
        size,
      )
    };
    assert!(!ptr.is_null(), "av_packet_new_side_data({size})");
    packet
  }

  /// A packet whose side-data array has been forged into a shape
  /// FFmpeg's own API cannot produce, and cannot free either: `Drop`
  /// puts the header back into a state `av_packet_free_side_data` can
  /// walk, so a fixture for a malformed packet cannot take the test
  /// process down with it.
  ///
  /// Both `av_packet_new_side_data` and `av_packet_add_side_data`
  /// replace an existing entry of the same type (measured: seventy
  /// calls leave one entry), so every shape below — an over-cap count, a
  /// negative count, a missing array, an entry that declares bytes it
  /// does not carry — is one no FFmpeg call produces. That is exactly
  /// what the validation is for.
  struct Forged {
    packet: Packet,
    freeable: i32,
  }

  impl Drop for Forged {
    fn drop(&mut self) {
      use ffmpeg_next::packet::Mut;
      // SAFETY: `packet` owns a live `AVPacket`; putting the count back
      // to what the array really holds is what makes the free sound.
      unsafe { (*self.packet.as_mut_ptr()).side_data_elems = self.freeable };
    }
  }

  impl Forged {
    /// `count` real entries of four bytes each.
    fn entries(count: usize, body: bool) -> Self {
      let mut packet = Self::carrier(body);
      // SAFETY: the array and every entry payload come from FFmpeg's
      // own allocator and are handed to the packet, which frees them in
      // `av_packet_free_side_data`. The carrier has no side data of its
      // own, so the overwrite leaks nothing.
      unsafe {
        let array = Self::array(count);
        for index in 0..count {
          let data = av_malloc(4) as *mut u8;
          assert!(!data.is_null(), "av_malloc");
          core::ptr::write_bytes(data, 7, 4);
          (*array.add(index)).data = data;
          (*array.add(index)).size = 4;
          (*array.add(index)).type_ = AVPacketSideDataType::AV_PKT_DATA_NEW_EXTRADATA;
        }
        Self::attach(&mut packet, array, count as i32);
      }
      Self {
        packet,
        freeable: count as i32,
      }
    }

    /// A count with no array behind it at all.
    fn null_array(count: i32, body: bool) -> Self {
      let mut packet = Self::carrier(body);
      use ffmpeg_next::packet::Mut;
      // SAFETY: the packet's side data is null already; only the count
      // is forged, and `Drop` puts it back to zero before the free.
      unsafe { (*packet.as_mut_ptr()).side_data_elems = count };
      Self {
        packet,
        freeable: 0,
      }
    }

    /// One entry declaring `size` bytes it does not carry.
    fn null_entry_data(size: usize, body: bool) -> Self {
      let mut packet = Self::carrier(body);
      // SAFETY: as in `entries`, except the payload pointer is left
      // null — `av_freep(&NULL)` is a no-op, so the free stays sound.
      unsafe {
        let array = Self::array(1);
        (*array).data = core::ptr::null_mut();
        (*array).size = size;
        (*array).type_ = AVPacketSideDataType::AV_PKT_DATA_NEW_EXTRADATA;
        Self::attach(&mut packet, array, 1);
      }
      Self {
        packet,
        freeable: 1,
      }
    }

    /// Overrides the declared count, keeping the array intact.
    fn with_declared_count(mut self, count: i32) -> Self {
      use ffmpeg_next::packet::Mut;
      // SAFETY: `Drop` restores `freeable`, which still describes the
      // array really attached.
      unsafe { (*self.packet.as_mut_ptr()).side_data_elems = count };
      self
    }

    fn carrier(body: bool) -> Packet {
      if body {
        Packet::copy(&[1u8, 2, 3])
      } else {
        Packet::empty()
      }
    }

    /// # Safety
    /// The returned array is FFmpeg-allocated and uninitialised.
    unsafe fn array(count: usize) -> *mut AVPacketSideData {
      let array = unsafe { av_malloc(count * core::mem::size_of::<AVPacketSideData>()) }
        as *mut AVPacketSideData;
      assert!(!array.is_null(), "av_malloc");
      array
    }

    /// # Safety
    /// `array` must hold `count` initialised entries owned by FFmpeg.
    unsafe fn attach(packet: &mut Packet, array: *mut AVPacketSideData, count: i32) {
      use ffmpeg_next::packet::Mut;
      unsafe {
        (*packet.as_mut_ptr()).side_data = array;
        (*packet.as_mut_ptr()).side_data_elems = count;
      }
    }
  }

  /// Runs one packet through all four timed conversions, returning what
  /// each answered — the arms share a collector, and a fix that misses
  /// one of them is a fix that misses.
  fn every_timed_arm(packet: &Packet) -> [(&'static str, Result<bool, PacketBufferError>); 4] {
    let tb = mediadecode::Timebase::default();
    [
      (
        "video",
        video_packet_from_borrowed::<crate::Owned>(
          packet,
          tb,
          PacketLimits::default(),
          crate::buffer::PayloadProvenance::CallerSupplied,
        )
        .map(|p| p.is_some()),
      ),
      (
        "audio",
        audio_packet_from_borrowed::<crate::Owned>(
          packet,
          tb,
          PacketLimits::default(),
          crate::buffer::PayloadProvenance::CallerSupplied,
        )
        .map(|p| p.is_some()),
      ),
      (
        "subtitle",
        subtitle_packet_from_borrowed::<crate::Owned>(
          packet,
          tb,
          PacketLimits::default(),
          crate::buffer::PayloadProvenance::CallerSupplied,
        )
        .map(|p| p.is_some()),
      ),
      (
        "data",
        data_packet_from_borrowed::<crate::Owned>(
          packet,
          tb,
          PacketLimits::default(),
          crate::buffer::PayloadProvenance::CallerSupplied,
        )
        .map(|p| p.is_some()),
      ),
    ]
  }

  #[test]
  fn side_data_a_packet_declares_and_does_not_carry_refuses_it() {
    // Two pointer paths walked straight past the all-or-error rule: a
    // count with no array behind it returned "no side data", and an
    // entry declaring bytes it did not carry became an empty entry —
    // charged nothing, delivered as though the packet had said so. Both
    // are how a body-bearing packet reached a decoder stripped of its
    // control data, and how a side-data-only packet became `None` and
    // was skipped.
    for body in [true, false] {
      let forged = Forged::null_array(3, body);
      for (arm, result) in every_timed_arm(&forged.packet) {
        match result {
          Err(PacketBufferError::SideDataArray(p)) => assert_eq!(p.count(), 3, "{arm}"),
          other => panic!("{arm} (body={body}): expected SideDataArray, got {other:?}"),
        }
      }

      let forged = Forged::null_entry_data(64, body);
      for (arm, result) in every_timed_arm(&forged.packet) {
        match result {
          Err(PacketBufferError::SideDataPayload(p)) => {
            assert_eq!((p.index(), p.size()), (0, 64), "{arm}");
          }
          other => panic!("{arm} (body={body}): expected SideDataPayload, got {other:?}"),
        }
      }

      // And the malformed count keeps being refused when the array is
      // missing too — the count is judged on its own, before the
      // pointer, so one cannot excuse the other.
      let forged = Forged::null_array(-3, body);
      for (arm, result) in every_timed_arm(&forged.packet) {
        assert!(
          matches!(
            result,
            Err(PacketBufferError::SideDataEntries(p)) if p.count() == -3
          ),
          "{arm} (body={body}): {result:?}",
        );
      }
      let forged = Forged::null_array(i32::MAX, body);
      for (arm, result) in every_timed_arm(&forged.packet) {
        assert!(
          matches!(result, Err(PacketBufferError::SideDataEntries(_))),
          "{arm} (body={body}): {result:?}",
        );
      }
    }

    // A zero-size entry is a marker, not a lie: a type with no bytes is
    // exactly what FFmpeg emits for some side data, and it stays
    // welcome.
    let marker = Forged::null_entry_data(0, true);
    let packet = video_packet_from_borrowed::<crate::Owned>(
      &marker.packet,
      mediadecode::Timebase::default(),
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("a marker entry is carriable")
    .expect("present");
    assert_eq!(packet.extra().side_data().len(), 1);
    assert!(packet.extra().side_data()[0].data().is_empty());
  }

  #[test]
  fn side_data_that_cannot_be_carried_whole_refuses_the_packet() {
    let tb = mediadecode::Timebase::default();

    // A body-bearing packet whose side data is over the byte cap. The
    // caps used to truncate and warn, which handed the codec a packet
    // that looked complete and was not: `NEW_EXTRADATA` gone, a decoder
    // left on parameters the container had already replaced.
    let oversized = packet_with_side_data(SIDE_DATA_MAX_TOTAL_BYTES + 1, true);
    for (arm, result) in [
      (
        "video",
        video_packet_from_borrowed::<crate::Owned>(
          &oversized,
          tb,
          PacketLimits::default(),
          crate::buffer::PayloadProvenance::CallerSupplied,
        )
        .map(|p| p.is_some()),
      ),
      (
        "audio",
        audio_packet_from_borrowed::<crate::Owned>(
          &oversized,
          tb,
          PacketLimits::default(),
          crate::buffer::PayloadProvenance::CallerSupplied,
        )
        .map(|p| p.is_some()),
      ),
      (
        "subtitle",
        subtitle_packet_from_borrowed::<crate::Owned>(
          &oversized,
          tb,
          PacketLimits::default(),
          crate::buffer::PayloadProvenance::CallerSupplied,
        )
        .map(|p| p.is_some()),
      ),
      (
        "data",
        data_packet_from_borrowed::<crate::Owned>(
          &oversized,
          tb,
          PacketLimits::default(),
          crate::buffer::PayloadProvenance::CallerSupplied,
        )
        .map(|p| p.is_some()),
      ),
    ] {
      match result {
        Err(PacketBufferError::SideDataBytes(p)) => {
          assert_eq!(p.cap(), SIDE_DATA_MAX_TOTAL_BYTES);
          assert!(p.bytes() > p.cap(), "{arm}");
        }
        other => panic!("{arm}: expected SideDataBytes, got {other:?}"),
      }
    }

    // And the same packet with no body at all. This one used to
    // collapse twice over: the side data was dropped, the packet
    // therefore looked empty, and the demuxer skipped it in silence.
    let oversized = packet_with_side_data(SIDE_DATA_MAX_TOTAL_BYTES + 1, false);
    assert!(matches!(
      video_packet_from_borrowed::<crate::Owned>(
        &oversized,
        tb,
        PacketLimits::default(),
        crate::buffer::PayloadProvenance::CallerSupplied
      )
      .map(|p| p.is_some()),
      Err(PacketBufferError::SideDataBytes(_)),
    ));
  }

  #[test]
  fn the_side_data_caps_are_refusals_at_the_boundary_not_before_it() {
    let tb = mediadecode::Timebase::default();

    // Exactly at the byte cap: carried, whole.
    let at_cap = packet_with_side_data(SIDE_DATA_MAX_TOTAL_BYTES, true);
    let packet = video_packet_from_borrowed::<crate::Owned>(
      &at_cap,
      tb,
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("exactly at the cap is not over it")
    .expect("present");
    assert_eq!(packet.extra().side_data().len(), 1);
    assert_eq!(
      packet.extra().side_data()[0].data().len(),
      SIDE_DATA_MAX_TOTAL_BYTES,
    );

    // One byte past: refused.
    let past = packet_with_side_data(SIDE_DATA_MAX_TOTAL_BYTES + 1, true);
    assert!(matches!(
      video_packet_from_borrowed::<crate::Owned>(
        &past,
        tb,
        PacketLimits::default(),
        crate::buffer::PayloadProvenance::CallerSupplied
      )
      .map(|p| p.is_some()),
      Err(PacketBufferError::SideDataBytes(_)),
    ));

    // The entry cap, both sides of it. FFmpeg names fewer types than
    // the floor today, so the effective cap is the floor; if a future
    // build names more, this assertion moves the boundary rather than
    // letting the lane quietly test nothing.
    let cap = side_data_entry_cap();
    assert_eq!(cap, SIDE_DATA_MAX_ENTRIES, "the cap this lane straddles");

    let at_cap = Forged::entries(cap, true);
    let packet = video_packet_from_borrowed::<crate::Owned>(
      &at_cap.packet,
      tb,
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("exactly at the cap is not over it")
    .expect("present");
    assert_eq!(packet.extra().side_data().len(), cap);

    let past = Forged::entries(cap + 1, true);
    match video_packet_from_borrowed::<crate::Owned>(
      &past.packet,
      tb,
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .map(|p| p.is_some())
    {
      Err(PacketBufferError::SideDataEntries(p)) => {
        assert_eq!(p.count() as usize, cap + 1);
        assert_eq!(p.cap(), cap);
      }
      other => panic!("expected SideDataEntries, got {other:?}"),
    }

    // A negative count is malformed, not empty — reading it as "no side
    // data" would be the same silent loss by another route.
    let corrupt = Forged::entries(1, true).with_declared_count(-3);
    assert!(matches!(
      video_packet_from_borrowed::<crate::Owned>(&corrupt.packet, tb, PacketLimits::default(), crate::buffer::PayloadProvenance::CallerSupplied).map(|p| p.is_some()),
      Err(PacketBufferError::SideDataEntries(p)) if p.count() == -3,
    ));
  }

  #[test]
  fn a_trusted_packet_is_refused_on_both_legs() {
    use ffmpeg_next::packet::Mut;
    let tb = mediadecode::Timebase::default();

    // Copy-out: the leg such a packet would enter the graph on.
    let mut packet = Packet::copy(&[1u8, 2, 3]);
    // SAFETY: `packet` owns a live `AVPacket`; `flags` is a public field
    // and this bit has no `ffmpeg_next::Flags` spelling.
    unsafe {
      (*packet.as_mut_ptr()).flags =
        ffmpeg_next::ffi::AV_PKT_FLAG_KEY | ffmpeg_next::ffi::AV_PKT_FLAG_TRUSTED;
    };
    for (arm, taken) in [
      (
        "video",
        video_packet_from_borrowed::<crate::Owned>(
          &packet,
          tb,
          PacketLimits::default(),
          crate::buffer::PayloadProvenance::CallerSupplied,
        )
        .map(|p| p.is_some()),
      ),
      (
        "audio",
        audio_packet_from_borrowed::<crate::Owned>(
          &packet,
          tb,
          PacketLimits::default(),
          crate::buffer::PayloadProvenance::CallerSupplied,
        )
        .map(|p| p.is_some()),
      ),
      (
        "subtitle",
        subtitle_packet_from_borrowed::<crate::Owned>(
          &packet,
          tb,
          PacketLimits::default(),
          crate::buffer::PayloadProvenance::CallerSupplied,
        )
        .map(|p| p.is_some()),
      ),
      (
        "data",
        data_packet_from_borrowed::<crate::Owned>(
          &packet,
          tb,
          PacketLimits::default(),
          crate::buffer::PayloadProvenance::CallerSupplied,
        )
        .map(|p| p.is_some()),
      ),
      (
        "attachment",
        attachment_packet_from_borrowed::<crate::Owned>(
          &packet,
          PacketLimits::default(),
          crate::buffer::PayloadProvenance::CallerSupplied,
        )
        .map(|p| p.is_some()),
      ),
    ] {
      match taken {
        Err(PacketBufferError::TrustedPayload(p)) => assert_eq!(p.len(), 3, "{arm}"),
        other => panic!("{arm}: a TRUSTED payload must not be copied out, got {other:?}"),
      }
    }

    // Rebuild: the leg a flag that arrived by some other route would be
    // handed back to a decoder on. Built by hand, because the copy-out
    // leg above will no longer produce one.
    let clean = Packet::copy(&[1u8, 2, 3]);
    let video = video_packet_from_borrowed::<crate::Owned>(
      &clean,
      tb,
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("a clean packet is carriable")
    .expect("present");
    let trusted = video
      .clone()
      .with_flags(MdPacketFlags::from_bits_retain(crate::buffer::TRUSTED_BIT));
    assert!(
      matches!(
        ffmpeg_packet_from_owned_video_packet(&trusted, PacketLimits::default()),
        Err(PacketBuildError::TrustedPayload(_)),
      ),
      "a TRUSTED flag must not be written back onto an AVPacket",
    );
  }

  #[test]
  fn every_flag_bit_survives_both_directions() {
    // `PacketFlags` is a bit set whose documented lossless door is
    // `from_bits_retain`, and both directions used to squeeze it
    // through `ffmpeg_next`'s `Flags`, which names `KEY` and `CORRUPT`
    // and truncates the rest away. `AV_PKT_FLAG_DISCARD` is the one
    // that matters most: it tells a consumer to decode a packet and
    // throw its output away, and without it preroll output looks like
    // something to keep.
    use ffmpeg_next::packet::{Mut, Ref};
    const DISCARD: i32 = ffmpeg_next::ffi::AV_PKT_FLAG_DISCARD;
    const TRUSTED: i32 = ffmpeg_next::ffi::AV_PKT_FLAG_TRUSTED;
    const DISPOSABLE: i32 = ffmpeg_next::ffi::AV_PKT_FLAG_DISPOSABLE;
    const UNNAMED: i32 = 0b0010_0000; // nothing names this bit yet
    // `TRUSTED` is deliberately **not** in this set, and the constant is
    // kept only to say so. It used to be — this lane asserted it made
    // the round trip — and that assertion was the bug: a `TRUSTED`
    // payload may be a structure of pointers into other live objects,
    // so copying it mints an owned-looking carrier that dangles when
    // its source drops. It is now refused on both legs, which
    // `a_trusted_packet_is_refused_on_both_legs` pins.
    let _ = TRUSTED;
    let raw = ffmpeg_next::ffi::AV_PKT_FLAG_KEY | DISCARD | DISPOSABLE | UNNAMED;

    let mut packet = Packet::copy(&[1u8, 2, 3]);
    // SAFETY: `packet` owns a live `AVPacket`; `flags` is a public
    // field and this is the only way to set a bit `ffmpeg_next`'s
    // `Flags` cannot spell.
    unsafe { (*packet.as_mut_ptr()).flags = raw };
    assert_ne!(
      packet.flags().bits(),
      raw,
      "the wrapper's own accessor is what loses them",
    );

    let tb = mediadecode::Timebase::default();
    let expected = MdPacketFlags::from_bits_retain(raw as u8);

    let video = video_packet_from_borrowed::<crate::Owned>(
      &packet,
      tb,
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("wrappable")
    .expect("present");
    assert_eq!(video.flags(), expected);
    assert!(video.flags().contains(MdPacketFlags::DISCARD));
    let audio = audio_packet_from_borrowed::<crate::Owned>(
      &packet,
      tb,
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("wrappable")
    .expect("present");
    assert_eq!(audio.flags(), expected);
    let subtitle = subtitle_packet_from_borrowed::<crate::Owned>(
      &packet,
      tb,
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("wrappable")
    .expect("present");
    assert_eq!(subtitle.flags(), expected);
    let data = data_packet_from_borrowed::<crate::Owned>(
      &packet,
      tb,
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("wrappable")
    .expect("present");
    assert_eq!(data.flags(), expected);
    let attachment = attachment_packet_from_borrowed::<crate::Owned>(
      &packet,
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("wrappable")
    .expect("present");
    assert_eq!(attachment.flags(), expected);

    // And back out again, on the three paths a decoder is fed from.
    for (arm, rebuilt) in [
      (
        "video",
        ffmpeg_packet_from_owned_video_packet(&video, PacketLimits::default()).expect("rebuilt"),
      ),
      (
        "audio",
        ffmpeg_packet_from_owned_audio_packet(&audio, PacketLimits::default()).expect("rebuilt"),
      ),
      (
        "subtitle",
        ffmpeg_packet_from_owned_subtitle_packet(&subtitle, PacketLimits::default())
          .expect("rebuilt"),
      ),
    ] {
      // SAFETY: `rebuilt` owns a live `AVPacket`.
      let carried = unsafe { (*rebuilt.as_ptr()).flags };
      assert_eq!(carried, raw, "{arm} rebuilt {carried:#x} from {raw:#x}");
    }
  }

  #[test]
  fn a_flag_bit_the_vocabulary_cannot_hold_refuses_the_packet() {
    // Unreachable against this build — every flag FFmpeg names lives in
    // the byte `PacketFlags` carries, and a compile-time assertion says
    // so. It is here because the day that stops being true, a packet
    // must be refused rather than delivered with a bit missing.
    use ffmpeg_next::packet::Mut;
    let mut packet = Packet::copy(&[1u8]);
    // SAFETY: `packet` owns a live `AVPacket`.
    unsafe { (*packet.as_mut_ptr()).flags = 0x1_00 };
    let tb = mediadecode::Timebase::default();
    for (arm, result) in every_timed_arm(&packet) {
      assert!(
        matches!(
          result,
          Err(PacketBufferError::UnrepresentableFlags(p)) if p.raw() == 0x1_00
        ),
        "{arm}: {result:?}",
      );
    }
    assert!(matches!(
      attachment_packet_from_borrowed::<crate::Owned>(
        &packet,
        PacketLimits::default(),
        crate::buffer::PayloadProvenance::CallerSupplied
      )
      .map(|p| p.is_some()),
      Err(PacketBufferError::UnrepresentableFlags(_)),
    ));
    let _ = tb;
  }

  /// A live `AVBufferRef` of `len` bytes, released when the guard drops.
  struct TestBuffer(*mut ffmpeg_next::ffi::AVBufferRef);

  impl TestBuffer {
    fn new(len: usize) -> Self {
      // SAFETY: a plain allocation, checked for null, written through
      // its own `data` pointer.
      unsafe {
        let raw = ffmpeg_next::ffi::av_buffer_alloc(len as _);
        assert!(!raw.is_null(), "av_buffer_alloc");
        core::ptr::write_bytes((*raw).data, 0xAB, len);
        Self(raw)
      }
    }
  }

  impl Drop for TestBuffer {
    fn drop(&mut self) {
      // SAFETY: the guard owns exactly one reference, released once.
      unsafe { ffmpeg_next::ffi::av_buffer_unref(&mut self.0) };
    }
  }

  /// The payload pointer a packet's buffer currently holds.
  fn payload_address(packet: &Packet) -> usize {
    use ffmpeg_next::packet::Ref;
    // SAFETY: the packet is live; `data` is a public field.
    unsafe { (*packet.as_ptr()).data as usize }
  }

  /// The refcount of a packet's payload buffer.
  fn payload_references(packet: &Packet) -> i32 {
    use ffmpeg_next::packet::Ref;
    // SAFETY: the packet is live and refcounted; `buf` is a public
    // field and `av_buffer_get_ref_count` only reads the atomic.
    unsafe {
      let buf = (*packet.as_ptr()).buf;
      assert!(!buf.is_null(), "the fixture must be refcounted");
      ffmpeg_next::ffi::av_buffer_get_ref_count(buf)
    }
  }

  #[test]
  fn a_shared_packet_buffer_is_refused_without_being_read() {
    // **The consumption argument has a hole a dependency can open, and
    // the obvious patch had a worse one.** Taking the packet by value
    // proves no *other* handle survives only if the packet's buffer was
    // the caller's alone to give. An earlier round answered a shared
    // buffer with a silent copy — but a copy needs a read, and a
    // refcount above one is exactly the state in which somebody else
    // may be writing those bytes, from safe code, on another thread.
    // The read *is* the race. So the answer is a refusal that touches
    // nothing.
    let mut original = Packet::copy(&[7u8; 4096]);
    let mut shared = Packet::empty();
    // SAFETY: both packets are live; `av_packet_ref` takes a reference
    // to `original`'s buffer without copying it.
    unsafe {
      use ffmpeg_next::packet::{Mut, Ref};
      assert_eq!(
        ffmpeg_next::ffi::av_packet_ref(shared.as_mut_ptr(), original.as_ptr()),
        0,
      );
    }
    assert_eq!(
      payload_address(&shared),
      payload_address(&original),
      "the premise: the two packets share one allocation",
    );
    assert_eq!(payload_references(&original), 2);

    // The refusal is lane-independent, because the hazard is: both
    // lanes read the payload, one to view it and one to copy it.
    match video_packet_from_ffmpeg_as::<crate::View>(
      shared,
      mediadecode::Timebase::default(),
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    ) {
      Err(PacketBufferError::SharedPayload(refused)) => {
        assert_eq!(refused.references(), 2);
      }
      other => panic!("a shared payload must be refused by name, got {other:?}"),
    }

    // Nothing was read and nothing was carried: the packet the caller
    // kept is still theirs, still writable, and now sole owner again.
    {
      let slot = original.data_mut().expect("a writable payload");
      slot[0] = 0xEE;
    }
    assert_eq!(
      payload_references(&original),
      1,
      "the refusal must not have taken a reference of its own",
    );

    // And the owned lane answers the same way, for the same reason: its
    // copy is a read too.
    let mut shared_again = Packet::empty();
    // SAFETY: as above.
    unsafe {
      use ffmpeg_next::packet::{Mut, Ref};
      assert_eq!(
        ffmpeg_next::ffi::av_packet_ref(shared_again.as_mut_ptr(), original.as_ptr()),
        0,
      );
    }
    assert!(matches!(
      owned_video_packet_from_ffmpeg_in(
        &shared_again,
        mediadecode::Timebase::default(),
        PacketLimits::default(),
      ),
      Err(PacketBufferError::SharedPayload(_)),
    ));
  }

  #[test]
  fn a_clone_that_silently_shared_is_refused_by_name() {
    // The exact path: `ffmpeg_next::Packet::clone` calls
    // `av_packet_ref` then `av_packet_make_writable` and **ignores both
    // return codes**. Under allocation failure the second one leaves
    // the "clone" sharing the source's buffer, and nothing says so.
    //
    // The cap is process-global, so the manufacture runs alone. It is
    // lifted before the conversion, because what is being tested is the
    // conversion's answer to a shared buffer — not its behaviour under
    // memory pressure, which the other lanes cover.
    crate::fault_subprocess::in_subprocess(
      "boundary::tests::a_clone_that_silently_shared_is_refused_by_name",
      || {
        let mut original = Packet::copy(&[3u8; 8192]);

        // Small allocations still succeed, so `av_packet_ref` gets its
        // `AVBufferRef`; a payload-sized one does not, so
        // `av_packet_make_writable` fails and is ignored.
        crate::fault_subprocess::cap_ffmpeg_allocations(512);
        let clone = original.clone();
        crate::fault_subprocess::uncap_ffmpeg_allocations();

        assert_eq!(
          payload_address(&clone),
          payload_address(&original),
          "the premise this test exists for: a failed `make_writable` \
           leaves the clone sharing, silently",
        );
        assert_eq!(payload_references(&original), 2);

        match video_packet_from_ffmpeg_as::<crate::View>(
          clone,
          mediadecode::Timebase::default(),
          PacketLimits::default(),
          crate::buffer::PayloadProvenance::CallerSupplied,
        ) {
          Err(PacketBufferError::SharedPayload(refused)) => {
            assert_eq!(refused.references(), 2);
          }
          other => panic!("expected a named refusal, got {other:?}"),
        }

        // The handle the caller kept is untouched and unaliased.
        {
          let slot = original.data_mut().expect("a writable payload");
          slot[0] = 0x5A;
        }
        assert_eq!(payload_references(&original), 1);
      },
    );
  }

  #[test]
  fn only_a_packet_payload_with_provable_padding_is_shared_into_a_decoder() {
    use crate::view::Origin;
    use ffmpeg_next::packet::Ref;

    const PADDING: usize = ffmpeg_next::ffi::AV_INPUT_BUFFER_PADDING_SIZE as usize;
    let src = TestBuffer::new(512);
    let body_len = 512 - PADDING;

    // The one shape that may share: a payload captured out of an
    // `AVPacket`'s own buffer, with libavformat's padding behind it.
    // SAFETY: `src` is live for the test and the extent is inside it.
    let payload =
      unsafe { crate::FfmpegBuffer::view_of(src.0, 0, body_len, Origin::PacketPayload) }
        .expect("a view");
    let shared = share_or_copy(&payload).expect("built");
    // SAFETY: both are live; `data` is a public field.
    assert_eq!(
      unsafe { (*shared.as_ptr()).data as usize },
      payload.as_ref().as_ptr() as usize,
      "a packet payload with provable padding must be shared",
    );

    // **The same bytes, the same slack, no provenance.** This is the
    // frame-origin case: a decoded plane or a resampler's output reused
    // as a packet body. What follows it is more pixels or more samples,
    // and a bitstream reader running past the payload would eat them.
    // SAFETY: as above.
    let plane =
      unsafe { crate::FfmpegBuffer::view_of(src.0, 0, body_len, Origin::Foreign) }.expect("a view");
    let copied = share_or_copy(&plane).expect("built");
    // SAFETY: both are live.
    assert_ne!(
      unsafe { (*copied.as_ptr()).data as usize },
      plane.as_ref().as_ptr() as usize,
      "trailing capacity is not padding provenance — this must be copied",
    );
    assert_eq!(copied.data().unwrap_or(&[]), plane.as_ref());

    // And a payload whose slack is short of the padding is copied too,
    // provenance or not: the extent has to hold as well.
    // SAFETY: as above.
    let tight = unsafe { crate::FfmpegBuffer::view_of(src.0, 0, 511, Origin::PacketPayload) }
      .expect("a view");
    let copied = share_or_copy(&tight).expect("built");
    // SAFETY: both are live.
    assert_ne!(
      unsafe { (*copied.as_ptr()).data as usize },
      tight.as_ref().as_ptr() as usize,
      "a payload with less than the padding behind it must be copied",
    );

    // A narrowed payload has lost its claim, and is copied.
    let mut narrowed = payload.clone();
    narrowed.shrink_to(16);
    let copied = share_or_copy(&narrowed).expect("built");
    // SAFETY: both are live.
    assert_ne!(
      unsafe { (*copied.as_ptr()).data as usize },
      narrowed.as_ref().as_ptr() as usize,
    );
  }

  #[test]
  fn an_empty_view_carrier_builds_a_payload_less_packet() {
    // The empty carrier is backed by **no buffer at all** — that is
    // what makes a placeholder plane slot free — so anything that
    // reads its `AVBufferRef` before asking whether it is empty
    // dereferences null. A side-data-only packet is the ordinary way
    // to get here, not an exotic one.
    let empty = crate::FfmpegBuffer::empty();
    assert!(empty.as_av_buffer_ref().is_null(), "the premise");
    let built = share_or_copy(&empty).expect("a payload-less packet");
    assert_eq!(built.size(), 0);
    // And it is a packet side data can be attached to.
    let mut built = built;
    attach_side_data(
      &mut built,
      &[SideDataEntry::new(
        NEW_EXTRADATA,
        FfmpegBytes::copy_from_slice(&[1u8, 2, 3, 4]),
      )],
    )
    .expect("side data attaches to a payload-less packet");
  }

  #[test]
  fn a_side_data_only_packet_round_trips_on_every_view_decoder_family() {
    // Every family, both roads: the public builder a caller can call,
    // and the scoped submission a decoder goes through. Neither may
    // touch the empty carrier's absent buffer.
    let tb = mediadecode::Timebase::default();
    let source = side_data_only_packet();
    let limits = PacketLimits::default();

    let video = video_packet_from_ffmpeg_as::<crate::View>(
      source.clone(),
      tb,
      limits,
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("carried")
    .expect("a side-data-only packet is a packet");
    assert!(video.data().as_ref().is_empty(), "the premise: no payload");
    assert_eq!(
      ffmpeg_packet_from_video_packet(&video, limits)
        .expect("built")
        .size(),
      0,
    );
    with_ffmpeg_video_packet::<crate::View, _>(&video, limits, BodyRoute::Submission, |av| {
      assert_eq!(av.size(), 0);
    })
    .expect("submitted");

    let audio = audio_packet_from_ffmpeg_as::<crate::View>(
      source.clone(),
      tb,
      limits,
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("carried")
    .expect("a side-data-only packet is a packet");
    assert!(audio.data().as_ref().is_empty());
    assert_eq!(
      ffmpeg_packet_from_audio_packet(&audio, limits)
        .expect("built")
        .size(),
      0,
    );
    with_ffmpeg_audio_packet::<crate::View, _>(&audio, limits, BodyRoute::Submission, |av| {
      assert_eq!(av.size(), 0);
    })
    .expect("submitted");

    let subtitle = subtitle_packet_from_ffmpeg_as::<crate::View>(
      source.clone(),
      tb,
      limits,
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("carried")
    .expect("a side-data-only packet is a packet");
    assert!(subtitle.data().as_ref().is_empty());
    assert_eq!(
      ffmpeg_packet_from_subtitle_packet(&subtitle, limits)
        .expect("built")
        .size(),
      0,
    );
    with_ffmpeg_subtitle_packet::<crate::View, _>(&subtitle, limits, BodyRoute::Submission, |av| {
      assert_eq!(av.size(), 0);
    })
    .expect("submitted");
  }

  #[test]
  fn side_data_survives_the_round_trip_on_every_decoder_bound_arm() {
    // The forward direction captures side data; the reverse direction
    // is what a decoder is actually handed. Capturing without
    // reattaching is theatre: `NEW_EXTRADATA` never reaches the codec,
    // `SKIP_SAMPLES` never trims, and nothing says so.
    let tb = mediadecode::Timebase::default();
    let mut with_body = Packet::copy(&[4u8, 5, 6]);
    {
      use ffmpeg_next::{
        ffi::{AVPacketSideDataType, av_packet_new_side_data},
        packet::Mut,
      };
      unsafe {
        let ptr = av_packet_new_side_data(
          with_body.as_mut_ptr(),
          AVPacketSideDataType::AV_PKT_DATA_SKIP_SAMPLES,
          3,
        );
        assert!(!ptr.is_null());
        core::ptr::copy_nonoverlapping([1u8, 2, 3].as_ptr(), ptr, 3);
      }
    }
    const SKIP_SAMPLES: i32 =
      ffmpeg_next::ffi::AVPacketSideDataType::AV_PKT_DATA_SKIP_SAMPLES as i32;

    for (name, source) in [
      ("body plus side data", &with_body),
      ("side data only", &side_data_only_packet()),
    ] {
      let expected_kind = if name == "side data only" {
        NEW_EXTRADATA
      } else {
        SKIP_SAMPLES
      };

      let video = video_packet_from_borrowed::<crate::Owned>(
        source,
        tb,
        PacketLimits::default(),
        crate::buffer::PayloadProvenance::CallerSupplied,
      )
      .expect("wrappable")
      .expect("present");
      let rebuilt =
        ffmpeg_packet_from_owned_video_packet(&video, PacketLimits::default()).expect("rebuilt");
      let carried = packet_side_data(&rebuilt).expect("readable");
      assert_eq!(carried.len(), 1, "{name}: video lost its side data");
      assert_eq!(carried[0].kind(), expected_kind, "{name}: video");
      assert_eq!(carried[0].data(), video.extra().side_data()[0].data());

      let audio = audio_packet_from_borrowed::<crate::Owned>(
        source,
        tb,
        PacketLimits::default(),
        crate::buffer::PayloadProvenance::CallerSupplied,
      )
      .expect("wrappable")
      .expect("present");
      let rebuilt =
        ffmpeg_packet_from_owned_audio_packet(&audio, PacketLimits::default()).expect("rebuilt");
      let carried = packet_side_data(&rebuilt).expect("readable");
      assert_eq!(carried.len(), 1, "{name}: audio lost its side data");
      assert_eq!(carried[0].data(), audio.extra().side_data()[0].data());

      let subtitle = subtitle_packet_from_borrowed::<crate::Owned>(
        source,
        tb,
        PacketLimits::default(),
        crate::buffer::PayloadProvenance::CallerSupplied,
      )
      .expect("wrappable")
      .expect("present");
      let rebuilt = ffmpeg_packet_from_owned_subtitle_packet(&subtitle, PacketLimits::default())
        .expect("rebuilt");
      let carried = packet_side_data(&rebuilt).expect("readable");
      assert_eq!(carried.len(), 1, "{name}: subtitle lost its side data");
      assert_eq!(carried[0].data(), subtitle.extra().side_data()[0].data());
    }
  }

  #[test]
  fn a_side_data_type_this_build_cannot_name_is_refused_not_dropped() {
    // This crate carries side-data types as the raw integers they are
    // on the wire, so a hand-built entry can name anything. Handing an
    // unknown one to C would either form an invalid discriminant or
    // attach a type nothing downstream reads — and dropping it quietly
    // is the very defect this whole seam exists to close.
    let limit = crate::ffi::side_data_type_count();
    let packet = mediadecode::packet::VideoPacket::new(
      FfmpegBytes::copy_from_slice(&[1u8]),
      VideoPacketExtra::new(0).with_side_data(vec![SideDataEntry::new(
        limit,
        FfmpegBytes::copy_from_slice(&[9u8]),
      )]),
    );
    match ffmpeg_packet_from_owned_video_packet(&packet, PacketLimits::default()).map(|_| ()) {
      Err(PacketBuildError::UnknownSideData(p)) => {
        assert_eq!(p.kind(), limit);
        assert_eq!(p.limit(), limit);
      }
      other => panic!("expected UnknownSideData, got {other:?}"),
    }
    assert!(matches!(
      ffmpeg_packet_from_owned_video_packet(&mediadecode::packet::VideoPacket::new(
        FfmpegBytes::copy_from_slice(&[1u8]),
        VideoPacketExtra::new(0).with_side_data(vec![SideDataEntry::new(-1, FfmpegBytes::copy_from_slice(&[9u8]))]),
      ), PacketLimits::default())
      .map(|_| ()),
      Err(PacketBuildError::UnknownSideData(p)) if p.kind() == -1,
    ));
  }

  #[test]
  fn a_side_data_only_packet_is_delivered_on_every_timed_arm() {
    // Codec-control data with no body is still a packet. Dropped as an
    // "empty marker", it leaves a decoder running on parameters the
    // container has already replaced — and says nothing about it.
    let packet = side_data_only_packet();
    let tb = mediadecode::Timebase::default();

    let video = video_packet_from_borrowed::<crate::Owned>(
      &packet,
      tb,
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("wrappable")
    .expect("a side-data-only packet is a packet");
    assert!(video.data().as_ref().is_empty(), "no body, but a buffer");
    assert_eq!(video.extra().side_data().len(), 1);
    assert_eq!(video.extra().side_data()[0].kind(), NEW_EXTRADATA);
    assert_eq!(video.extra().side_data()[0].data(), &[1, 2, 3, 4]);

    let audio = audio_packet_from_borrowed::<crate::Owned>(
      &packet,
      tb,
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("wrappable")
    .expect("present");
    assert!(audio.data().as_ref().is_empty());
    assert_eq!(audio.extra().side_data()[0].data(), &[1, 2, 3, 4]);

    let subtitle = subtitle_packet_from_borrowed::<crate::Owned>(
      &packet,
      tb,
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("wrappable")
    .expect("present");
    assert!(subtitle.data().as_ref().is_empty());
    assert_eq!(subtitle.extra().side_data()[0].data(), &[1, 2, 3, 4]);

    let data = data_packet_from_borrowed::<crate::Owned>(
      &packet,
      tb,
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("wrappable")
    .expect("present");
    assert!(data.data().as_ref().is_empty());
    assert_eq!(data.extra().side_data()[0].data(), &[1, 2, 3, 4]);

    // The one arm where a payload-less packet really is nothing: an
    // attachment is its bytes, and there are none.
    assert!(
      attachment_packet_from_borrowed::<crate::Owned>(
        &packet,
        PacketLimits::default(),
        crate::buffer::PayloadProvenance::CallerSupplied
      )
      .expect("wrappable")
      .is_none(),
      "an attachment with no bytes is no attachment",
    );
  }

  #[test]
  fn side_data_rides_a_packet_that_has_a_body_too() {
    // The seat's documented promise — "the raw side-data entries from
    // `AVPacket.side_data`" — was never kept by the conversion before.
    use ffmpeg_next::{
      ffi::{AVPacketSideDataType, av_packet_new_side_data},
      packet::Mut,
    };
    let mut packet = Packet::copy(&[7u8, 7, 7]);
    unsafe {
      let ptr = av_packet_new_side_data(
        packet.as_mut_ptr(),
        AVPacketSideDataType::AV_PKT_DATA_NEW_EXTRADATA,
        2,
      );
      assert!(!ptr.is_null());
      core::ptr::copy_nonoverlapping([9u8, 9].as_ptr(), ptr, 2);
    }
    let video = video_packet_from_borrowed::<crate::Owned>(
      &packet,
      mediadecode::Timebase::default(),
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("wrappable")
    .expect("present");
    assert_eq!(video.data().as_ref(), &[7, 7, 7]);
    assert_eq!(video.extra().side_data()[0].data(), &[9, 9]);
  }

  #[test]
  fn an_empty_packet_is_absent_and_a_real_one_is_present() {
    let tb = mediadecode::Timebase::default();
    // No payload at all: the marker some demuxers emit. Absent, not an
    // error — this is the only thing a pull loop may skip.
    let empty = Packet::empty();
    assert!(
      video_packet_from_borrowed::<crate::Owned>(
        &empty,
        tb,
        PacketLimits::default(),
        crate::buffer::PayloadProvenance::CallerSupplied
      )
      .expect("not a failure")
      .is_none()
    );
    assert!(
      attachment_packet_from_borrowed::<crate::Owned>(
        &empty,
        PacketLimits::default(),
        crate::buffer::PayloadProvenance::CallerSupplied
      )
      .expect("not a failure")
      .is_none()
    );

    let real = Packet::copy(&[9u8, 8, 7]);
    let wrapped = video_packet_from_borrowed::<crate::Owned>(
      &real,
      tb,
      PacketLimits::default(),
      crate::buffer::PayloadProvenance::CallerSupplied,
    )
    .expect("wrappable")
    .expect("present");
    assert_eq!(wrapped.data().as_ref(), &[9, 8, 7]);
  }

  #[test]
  fn nv12_round_trips() {
    assert_eq!(
      from_av_pixel_format(AVPixelFormat::AV_PIX_FMT_NV12 as i32),
      PixelFormat::Nv12,
    );
  }

  #[test]
  fn p010be_maps_to_p010be() {
    // BE must map to the BE variant — the previous "fold to LE"
    // mapping silently corrupted P010BE pixel data via the safe
    // export path. The unsupported-format gate in `convert::av_frame_to_video_frame`
    // is the right place to reject BE today.
    assert_eq!(
      from_av_pixel_format(AVPixelFormat::AV_PIX_FMT_P010BE as i32),
      PixelFormat::P010Be,
    );
  }

  #[test]
  fn unnamed_raw_maps_to_none() {
    assert_eq!(from_av_pixel_format(-99_999), PixelFormat::None);
  }

  #[test]
  fn av_pix_fmt_none_maps_to_none() {
    assert_eq!(
      from_av_pixel_format(AVPixelFormat::AV_PIX_FMT_NONE as i32),
      PixelFormat::None,
    );
  }

  #[test]
  fn hw_formats_detected() {
    assert!(is_hardware_pix_fmt(
      AVPixelFormat::AV_PIX_FMT_VIDEOTOOLBOX as i32
    ));
    assert!(is_hardware_pix_fmt(AVPixelFormat::AV_PIX_FMT_VAAPI as i32));
    assert!(is_hardware_pix_fmt(AVPixelFormat::AV_PIX_FMT_CUDA as i32));
    assert!(is_hardware_pix_fmt(AVPixelFormat::AV_PIX_FMT_D3D11 as i32));
  }

  #[test]
  fn cpu_formats_not_detected_as_hw() {
    assert!(!is_hardware_pix_fmt(AVPixelFormat::AV_PIX_FMT_NV12 as i32));
    assert!(!is_hardware_pix_fmt(
      AVPixelFormat::AV_PIX_FMT_YUV420P as i32
    ));
    assert!(!is_hardware_pix_fmt(AVPixelFormat::AV_PIX_FMT_NONE as i32));
  }

  #[test]
  fn hw_formats_map_to_none_in_pixel_format() {
    // HW sentinels intentionally don't have a mediadecode::PixelFormat
    // representation — they're not CPU pixel data.
    assert_eq!(
      from_av_pixel_format(AVPixelFormat::AV_PIX_FMT_VIDEOTOOLBOX as i32),
      PixelFormat::None,
    );
    assert_eq!(
      from_av_pixel_format(AVPixelFormat::AV_PIX_FMT_VAAPI as i32),
      PixelFormat::None,
    );
  }
}