ff-filter 0.14.3

Video and audio filter graph operations - the Rust way
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
//! Graph-building helpers: hardware acceleration and filter linking.

use super::{
    AUDIO_TIME_BASE_NUM, BuildResult, FilterCtxVec, VIDEO_TIME_BASE_DEN, VIDEO_TIME_BASE_NUM,
    VideoGraphResult, ffmpeg_err,
};
use std::ptr::NonNull;
use std::time::Duration;

use crate::blend::BlendMode;
use crate::error::FilterError;
use crate::graph::filter_step::FilterStep;
use crate::graph::types::{EqBand, HwAccel};

// ── Hardware acceleration helpers ─────────────────────────────────────────────

/// Map a [`HwAccel`] variant to the corresponding `AVHWDeviceType` constant.
pub(super) fn hw_accel_to_device_type(hw: HwAccel) -> ff_sys::AVHWDeviceType {
    match hw {
        HwAccel::Cuda => ff_sys::AVHWDeviceType_AV_HWDEVICE_TYPE_CUDA,
        HwAccel::VideoToolbox => ff_sys::AVHWDeviceType_AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
        HwAccel::Vaapi => ff_sys::AVHWDeviceType_AV_HWDEVICE_TYPE_VAAPI,
    }
}

/// Create and link a named hardware filter (e.g., `hwupload_cuda`, `hwdownload`)
/// with no arguments.  Sets the filter context's `hw_device_ctx` to a new
/// reference obtained via `av_buffer_ref(hw_ctx)` so the filter owns its own ref.
///
/// # Safety
///
/// `graph`, `prev_ctx`, and `hw_ctx` must be valid non-null pointers.
/// `hw_ctx` must be a valid `AVBufferRef` wrapping an `AVHWDeviceContext`.
pub(super) unsafe fn create_hw_filter(
    graph: *mut ff_sys::AVFilterGraph,
    prev_ctx: *mut ff_sys::AVFilterContext,
    filter_name: &std::ffi::CStr,
    instance_name: &std::ffi::CStr,
    hw_ctx: *mut ff_sys::AVBufferRef,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    // SAFETY: `avfilter_get_by_name` reads a valid null-terminated C string and
    // returns a borrowed, process-lifetime pointer (or null if not found).
    let filter = ff_sys::avfilter_get_by_name(filter_name.as_ptr());
    if filter.is_null() {
        log::warn!(
            "hw filter not found name={}",
            filter_name.to_str().unwrap_or("?")
        );
        return Err(FilterError::BuildFailed);
    }

    let mut ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut ctx,
        filter,
        instance_name.as_ptr(),
        std::ptr::null(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!(
            "hw filter creation failed name={} code={ret}",
            filter_name.to_str().unwrap_or("?")
        );
        return Err(FilterError::BuildFailed);
    }
    log::debug!(
        "hw filter added name={}",
        filter_name.to_str().unwrap_or("?")
    );

    // Give this filter context its own reference to the hardware device context.
    // SAFETY: `hw_ctx` is a valid `AVBufferRef`; `av_buffer_ref` returns a new
    // reference counted ref, or null on allocation failure.
    let filter_hw_ref = ff_sys::av_buffer_ref(hw_ctx);
    if filter_hw_ref.is_null() {
        log::warn!("av_buffer_ref failed for hw device context");
        return Err(FilterError::BuildFailed);
    }
    (*ctx).hw_device_ctx = filter_hw_ref;

    // SAFETY: `prev_ctx` and `ctx` belong to the same graph; pad indices are valid.
    let ret = ff_sys::avfilter_link(prev_ctx, 0, ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    Ok(ctx)
}

// ── Shared graph-building helper ──────────────────────────────────────────────

/// Create an `AVFilterContext` for `step`, link it after `prev_ctx`, and return
/// the new context pointer.
///
/// # Safety
///
/// `graph` and `prev_ctx` must be valid pointers owned by the same
/// `AVFilterGraph`.
pub(crate) unsafe fn add_and_link_step(
    graph: *mut ff_sys::AVFilterGraph,
    prev_ctx: *mut ff_sys::AVFilterContext,
    step: &FilterStep,
    index: usize,
    prefix: &str,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    let filter_name =
        std::ffi::CString::new(step.filter_name()).map_err(|_| FilterError::BuildFailed)?;
    let filter = ff_sys::avfilter_get_by_name(filter_name.as_ptr());
    if filter.is_null() {
        log::warn!("filter not found name={}", step.filter_name());
        return Err(FilterError::BuildFailed);
    }

    let step_name =
        std::ffi::CString::new(format!("{prefix}{index}")).map_err(|_| FilterError::BuildFailed)?;
    let step_args_str = step.args();
    let step_args =
        std::ffi::CString::new(step_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;

    let mut step_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut step_ctx,
        filter,
        step_name.as_ptr(),
        step_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!(
            "filter creation failed name={} args={}",
            step.filter_name(),
            step.args()
        );
        return Err(FilterError::BuildFailed);
    }
    log::debug!(
        "filter added name={} args={}",
        step.filter_name(),
        step.args()
    );

    let ret = ff_sys::avfilter_link(prev_ctx, 0, step_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }
    Ok(step_ctx)
}

/// Create and link a filter by explicit name and args strings, without a
/// `FilterStep`.  Used when the filter name cannot be determined statically
/// (e.g., `AudioDelay` dispatches to `adelay` or `atrim` depending on sign).
///
/// # Safety
///
/// `graph` and `prev_ctx` must be valid pointers owned by the same
/// `AVFilterGraph`.
pub(super) unsafe fn add_raw_filter_step(
    graph: *mut ff_sys::AVFilterGraph,
    prev_ctx: *mut ff_sys::AVFilterContext,
    filter_name: &str,
    args: &str,
    index: usize,
    prefix: &str,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    let filter_cname = std::ffi::CString::new(filter_name).map_err(|_| FilterError::BuildFailed)?;
    // SAFETY: `avfilter_get_by_name` reads a valid null-terminated C string.
    let filter = ff_sys::avfilter_get_by_name(filter_cname.as_ptr());
    if filter.is_null() {
        log::warn!("filter not found name={filter_name}");
        return Err(FilterError::BuildFailed);
    }

    let step_name =
        std::ffi::CString::new(format!("{prefix}{index}")).map_err(|_| FilterError::BuildFailed)?;
    let step_args = std::ffi::CString::new(args).map_err(|_| FilterError::BuildFailed)?;

    let mut step_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut step_ctx,
        filter,
        step_name.as_ptr(),
        step_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name={filter_name} args={args}");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name={filter_name} args={args}");

    // SAFETY: `prev_ctx` and `step_ctx` belong to the same graph; pad indices are valid.
    let ret = ff_sys::avfilter_link(prev_ctx, 0, step_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }
    Ok(step_ctx)
}

/// Insert a `setpts=PTS-STARTPTS` filter immediately after a `trim` step so
/// that the output stream's timestamps start at zero.
///
/// # Safety
///
/// `graph` and `prev_ctx` must be valid pointers owned by the same
/// `AVFilterGraph`.
pub(super) unsafe fn add_setpts_after_trim(
    graph: *mut ff_sys::AVFilterGraph,
    prev_ctx: *mut ff_sys::AVFilterContext,
    trim_index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    let setpts = ff_sys::avfilter_get_by_name(c"setpts".as_ptr());
    if setpts.is_null() {
        log::warn!("filter not found name=setpts");
        return Err(FilterError::BuildFailed);
    }

    let name = std::ffi::CString::new(format!("setpts{trim_index}"))
        .map_err(|_| FilterError::BuildFailed)?;

    let mut ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut ctx,
        setpts,
        name.as_ptr(),
        c"PTS-STARTPTS".as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=setpts args=PTS-STARTPTS");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=setpts args=PTS-STARTPTS");

    let ret = ff_sys::avfilter_link(prev_ctx, 0, ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }
    Ok(ctx)
}

/// Add a `pad` filter that centres the scaled frame on the target `width × height`
/// canvas, completing the scale-then-pad compound step for [`FilterStep::FitToAspect`].
///
/// # Safety
///
/// `graph` and `prev_ctx` must be valid pointers owned by the same
/// `AVFilterGraph`.
pub(super) unsafe fn add_fit_to_aspect_pad(
    graph: *mut ff_sys::AVFilterGraph,
    prev_ctx: *mut ff_sys::AVFilterContext,
    width: u32,
    height: u32,
    color: &str,
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    let pad_filter = ff_sys::avfilter_get_by_name(c"pad".as_ptr());
    if pad_filter.is_null() {
        log::warn!("filter not found name=pad (fit_to_aspect)");
        return Err(FilterError::BuildFailed);
    }

    let name =
        std::ffi::CString::new(format!("fitpad{index}")).map_err(|_| FilterError::BuildFailed)?;
    let args_str = format!("width={width}:height={height}:x=(ow-iw)/2:y=(oh-ih)/2:color={color}");
    let args = std::ffi::CString::new(args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;

    let mut ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut ctx,
        pad_filter,
        name.as_ptr(),
        args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=pad args={args_str}");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=pad args={args_str} index={index}");

    let ret = ff_sys::avfilter_link(prev_ctx, 0, ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }
    Ok(ctx)
}

// ── Speed (atempo chain) ──────────────────────────────────────────────────────

/// Decompose a speed `factor` into a chain of `atempo` values, each in [0.5, 100.0].
///
/// `FFmpeg` 4.0+ (`YAE_ATEMPO_MAX = 100.0`) accepts up to 100.0 per instance.
/// Chaining multiple instances multiplies the effective speed factor:
///
/// - `factor = 4.0`   → `[4.0]`         (single instance)
/// - `factor = 0.25`  → `[0.5, 0.5]`   (0.5 × 0.5 = 0.25)
/// - `factor = 150.0` → `[100.0, 1.5]` (2 instances instead of 8 at 2.0-max)
/// - `factor = 1.5`   → `[1.5]`        (within range directly)
pub(super) fn decompose_atempo(factor: f64) -> Vec<f64> {
    let mut remaining = factor;
    let mut chain = Vec::new();
    while remaining > 100.0 {
        chain.push(100.0);
        remaining /= 100.0;
    }
    while remaining < 0.5 {
        chain.push(0.5);
        remaining /= 0.5;
    }
    chain.push(remaining);
    chain
}

/// Insert a chain of `atempo` filters for the audio path of a `Speed` step.
///
/// Creates as many `atempo` instances as needed (see [`decompose_atempo`]) and
/// links them in series after `prev_ctx`.
///
/// # Safety
///
/// `graph` and `prev_ctx` must be valid pointers owned by the same
/// `AVFilterGraph`.
pub(crate) unsafe fn add_atempo_chain(
    graph: *mut ff_sys::AVFilterGraph,
    prev_ctx: *mut ff_sys::AVFilterContext,
    factor: f64,
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    let atempo_filter = ff_sys::avfilter_get_by_name(c"atempo".as_ptr());
    if atempo_filter.is_null() {
        log::warn!("filter not found name=atempo (speed)");
        return Err(FilterError::BuildFailed);
    }

    let chain = decompose_atempo(factor);
    let mut ctx = prev_ctx;
    for (j, &val) in chain.iter().enumerate() {
        let name = std::ffi::CString::new(format!("atempo{index}_{j}"))
            .map_err(|_| FilterError::BuildFailed)?;
        let args_str = format!("{val}");
        let args =
            std::ffi::CString::new(args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;
        let mut atempo_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
        let ret = ff_sys::avfilter_graph_create_filter(
            &raw mut atempo_ctx,
            atempo_filter,
            name.as_ptr(),
            args.as_ptr(),
            std::ptr::null_mut(),
            graph,
        );
        if ret < 0 {
            log::warn!("filter creation failed name=atempo args={val}");
            return Err(FilterError::BuildFailed);
        }
        log::debug!("filter added name=atempo args={val} index={index}_{j}");

        let ret = ff_sys::avfilter_link(ctx, 0, atempo_ctx, 0);
        if ret < 0 {
            return Err(FilterError::BuildFailed);
        }
        ctx = atempo_ctx;
    }
    Ok(ctx)
}

/// Insert a speed-change filter chain for `factor > 2.0`:
///
/// ```text
/// apad=pad_dur=1  →  asetrate=r={new_sr}  →  aresample={output_sr}  →  aeval (NaN clamp)
/// ```
///
/// * `apad` appends 1 s of silence so SWR's polyphase FIR resampler has enough
///   input at EOF to flush its filter window without reading uninitialised memory
///   (which produces NaN → AAC encoder EINVAL at high downsampling ratios).
/// * `aeval` replaces any residual NaN/Inf samples with 0 as a last-resort guard.
///   The expression is generated for `nb_channels` channels so it matches the
///   actual stream layout (the per-track aformat has already normalised the layout
///   before this chain is appended).
/// * Both `apad` and `aeval` are optional: if their filter is unavailable the
///   chain proceeds with just `asetrate → aresample`.
///
/// Audio pitch changes proportionally ("vinyl effect"); no WSOLA processing.
///
/// # Safety
///
/// `graph` and `prev_ctx` must be valid pointers owned by the same
/// `AVFilterGraph`.
pub(crate) unsafe fn add_asetrate_resample_chain(
    graph: *mut ff_sys::AVFilterGraph,
    prev_ctx: *mut ff_sys::AVFilterContext,
    new_sr: u32,
    output_sr: u32,
    nb_channels: u32,
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    let mut ctx = prev_ctx;

    // ── 1. apad=pad_dur=1 (optional) ────────────────────────────────────────
    // Prevents SWR polyphase resampler from reading uninitialised memory at EOF
    // when the last output frame cannot be filled from remaining input samples.
    // At speed=150 the 1 s of appended silence shrinks to ~7 ms — imperceptible.
    let apad_filter = ff_sys::avfilter_get_by_name(c"apad".as_ptr());
    if apad_filter.is_null() {
        log::warn!("apad filter unavailable — SWR tail-flush NaN guard skipped index={index}");
    } else {
        let name = std::ffi::CString::new(format!("apad_spd{index}"))
            .map_err(|_| FilterError::BuildFailed)?;
        let args = c"pad_dur=1";
        let mut apad_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
        let ret = ff_sys::avfilter_graph_create_filter(
            &raw mut apad_ctx,
            apad_filter,
            name.as_ptr(),
            args.as_ptr(),
            std::ptr::null_mut(),
            graph,
        );
        if ret < 0 || apad_ctx.is_null() {
            log::warn!(
                "apad creation FAILED ret={ret} index={index} — continuing without tail padding"
            );
        } else {
            let ret = ff_sys::avfilter_link(ctx, 0, apad_ctx, 0);
            if ret < 0 {
                log::warn!("apad link FAILED ret={ret} index={index}");
            } else {
                ctx = apad_ctx;
            }
        }
    }

    // ── 2. asetrate=r={new_sr} (required) ───────────────────────────────────
    let asetrate_filter = ff_sys::avfilter_get_by_name(c"asetrate".as_ptr());
    if asetrate_filter.is_null() {
        log::warn!("filter not found name=asetrate (speed fallback)");
        return Err(FilterError::BuildFailed);
    }
    let name = std::ffi::CString::new(format!("asetrate_spd{index}"))
        .map_err(|_| FilterError::BuildFailed)?;
    let args =
        std::ffi::CString::new(format!("r={new_sr}")).map_err(|_| FilterError::BuildFailed)?;
    let mut asetrate_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut asetrate_ctx,
        asetrate_filter,
        name.as_ptr(),
        args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=asetrate args=r={new_sr}");
        return Err(FilterError::BuildFailed);
    }
    let ret = ff_sys::avfilter_link(ctx, 0, asetrate_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }
    ctx = asetrate_ctx;

    // ── 3. aresample={output_sr} (required) ────────────────────────────────
    // aresample accepts the rate as a positional arg (just the number).
    // "r=" is not a valid option name in this FFmpeg version.
    let aresample_filter = ff_sys::avfilter_get_by_name(c"aresample".as_ptr());
    if aresample_filter.is_null() {
        log::warn!("filter not found name=aresample (speed fallback)");
        return Err(FilterError::BuildFailed);
    }
    let name2 = std::ffi::CString::new(format!("aresample_spd{index}"))
        .map_err(|_| FilterError::BuildFailed)?;
    let args2 =
        std::ffi::CString::new(format!("{output_sr}")).map_err(|_| FilterError::BuildFailed)?;
    let mut aresample_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut aresample_ctx,
        aresample_filter,
        name2.as_ptr(),
        args2.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=aresample args={output_sr} code={ret}");
        return Err(FilterError::BuildFailed);
    }
    let ret = ff_sys::avfilter_link(ctx, 0, aresample_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }
    ctx = aresample_ctx;

    // ── 4. aeval NaN/Inf sanitizer (optional) ───────────────────────────────
    // Replaces any NaN or Inf samples that survive the resampler with 0.
    // One expression per channel, joined by `|`, so the output layout matches
    // the input layout exactly regardless of mono/stereo/surround.
    let aeval_filter = ff_sys::avfilter_get_by_name(c"aeval".as_ptr());
    if aeval_filter.is_null() {
        log::warn!("aeval filter unavailable — NaN sanitizer skipped index={index}");
    } else {
        let name3 = std::ffi::CString::new(format!("aeval_spd{index}"))
            .map_err(|_| FilterError::BuildFailed)?;
        let per_ch = |ch: u32| format!("if(isnan(val({ch}))+isinf(val({ch})),0,val({ch}))");
        let exprs_str = (0..nb_channels.max(1))
            .map(per_ch)
            .collect::<Vec<_>>()
            .join("|");
        let args3 = std::ffi::CString::new(format!("exprs={exprs_str}"))
            .map_err(|_| FilterError::BuildFailed)?;
        let mut aeval_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
        let ret = ff_sys::avfilter_graph_create_filter(
            &raw mut aeval_ctx,
            aeval_filter,
            name3.as_ptr(),
            args3.as_ptr(),
            std::ptr::null_mut(),
            graph,
        );
        if ret < 0 || aeval_ctx.is_null() {
            log::warn!("aeval creation FAILED ret={ret} index={index} — NaN may reach encoder");
        } else {
            let ret = ff_sys::avfilter_link(ctx, 0, aeval_ctx, 0);
            if ret < 0 {
                log::warn!("aeval link FAILED ret={ret} index={index}");
            } else {
                ctx = aeval_ctx;
            }
        }
    }

    log::debug!("speed fallback asetrate={new_sr} aresample={output_sr} index={index}");
    Ok(ctx)
}

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

    #[test]
    fn decompose_atempo_should_return_single_value_for_factor_within_range() {
        assert_eq!(decompose_atempo(1.5), vec![1.5]);
    }

    #[test]
    fn decompose_atempo_should_return_single_value_for_factor_1() {
        assert_eq!(decompose_atempo(1.0), vec![1.0]);
    }

    #[test]
    fn decompose_atempo_should_use_single_instance_for_factor_4() {
        assert_eq!(decompose_atempo(4.0), vec![4.0]);
    }

    #[test]
    fn decompose_atempo_should_use_single_instance_for_factor_8() {
        assert_eq!(decompose_atempo(8.0), vec![8.0]);
    }

    #[test]
    fn decompose_atempo_should_chain_two_instances_for_factor_150() {
        let chain = decompose_atempo(150.0);
        assert_eq!(chain.len(), 2);
        let product: f64 = chain.iter().product();
        assert!(
            (product - 150.0).abs() < 1e-6,
            "product should be ~150, got {product}, chain={chain:?}"
        );
    }

    #[test]
    fn decompose_atempo_should_chain_two_instances_for_factor_0_25() {
        let chain = decompose_atempo(0.25);
        let product: f64 = chain.iter().product();
        assert!(
            (product - 0.25).abs() < 1e-9,
            "product should be ~0.25, got {product}, chain={chain:?}"
        );
        for &v in &chain {
            assert!(
                (0.5..=100.0).contains(&v),
                "each value must be in [0.5, 100.0], got {v}"
            );
        }
    }

    #[test]
    fn decompose_atempo_should_produce_values_all_within_valid_range() {
        for factor in [0.1, 0.25, 0.5, 1.0, 1.5, 2.0, 4.0, 8.0, 16.0, 100.0, 150.0] {
            let chain = decompose_atempo(factor);
            assert!(
                !chain.is_empty(),
                "chain must not be empty for factor={factor}"
            );
            let product: f64 = chain.iter().product();
            assert!(
                (product - factor).abs() < 1e-6,
                "product {product} must equal factor {factor}, chain={chain:?}"
            );
            for &v in &chain {
                assert!(
                    (0.5..=100.0).contains(&v),
                    "each value must be in [0.5, 100.0], got {v} for factor={factor}"
                );
            }
        }
    }
}

// ── Overlay image compound step ───────────────────────────────────────────────

/// Insert the compound `movie → lut → overlay` filter chain for an
/// [`FilterStep::OverlayImage`] step.
///
/// Unlike standard steps (which go through [`add_and_link_step`]), this step
/// creates three filter contexts internally:
///
/// 1. `movie` — loads the PNG from `path` as a self-contained video source
///    (no buffersrc input slot is consumed).
/// 2. `lut` — scales the alpha channel by `opacity` (`a = val * opacity`).
/// 3. `overlay` — composites the main stream (pad 0) with the image (pad 1)
///    at position `(x, y)`.
///
/// # Safety
///
/// `graph` and `prev_ctx` must be valid pointers owned by the same
/// `AVFilterGraph`.
pub(super) unsafe fn add_overlay_image_step(
    graph: *mut ff_sys::AVFilterGraph,
    prev_ctx: *mut ff_sys::AVFilterContext,
    path: &str,
    x: &str,
    y: &str,
    opacity: f32,
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    use std::ffi::CString;

    // 1. movie filter — self-contained PNG source, no buffersrc slot needed.
    let movie_filter = ff_sys::avfilter_get_by_name(c"movie".as_ptr());
    if movie_filter.is_null() {
        log::warn!("filter not found name=movie (overlay_image)");
        return Err(FilterError::BuildFailed);
    }
    let movie_name = CString::new(format!("movie{index}")).map_err(|_| FilterError::BuildFailed)?;
    let movie_args =
        CString::new(format!("filename={path}")).map_err(|_| FilterError::BuildFailed)?;
    let mut movie_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut movie_ctx,
        movie_filter,
        movie_name.as_ptr(),
        movie_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=movie args=filename={path}");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=movie args=filename={path} index={index}");

    // 2. lut filter — scale the alpha channel: a = val * opacity.
    //    For 8-bit RGBA the `lut` filter operates per-channel; `val` is the
    //    current pixel value (0–255) and the expression is evaluated per sample.
    let lut_filter = ff_sys::avfilter_get_by_name(c"lut".as_ptr());
    if lut_filter.is_null() {
        log::warn!("filter not found name=lut (overlay_image)");
        return Err(FilterError::BuildFailed);
    }
    let lut_name = CString::new(format!("lut{index}")).map_err(|_| FilterError::BuildFailed)?;
    let lut_args_str = format!("a=val*{opacity}");
    let lut_args = CString::new(lut_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;
    let mut lut_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut lut_ctx,
        lut_filter,
        lut_name.as_ptr(),
        lut_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=lut args={lut_args_str}");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=lut args={lut_args_str} index={index}");

    // Link: movie → lut (alpha scaling).
    let ret = ff_sys::avfilter_link(movie_ctx, 0, lut_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    // 3. overlay filter — composite main stream (pad 0) with image (pad 1).
    let overlay_filter = ff_sys::avfilter_get_by_name(c"overlay".as_ptr());
    if overlay_filter.is_null() {
        log::warn!("filter not found name=overlay (overlay_image)");
        return Err(FilterError::BuildFailed);
    }
    let overlay_name =
        CString::new(format!("step{index}")).map_err(|_| FilterError::BuildFailed)?;
    let overlay_args_str = format!("{x}:{y}");
    let overlay_args =
        CString::new(overlay_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;
    let mut overlay_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut overlay_ctx,
        overlay_filter,
        overlay_name.as_ptr(),
        overlay_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=overlay args={overlay_args_str}");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=overlay args={overlay_args_str} index={index}");

    // Link: prev_ctx → overlay pad 0 (main video stream).
    let ret = ff_sys::avfilter_link(prev_ctx, 0, overlay_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    // Link: lut → overlay pad 1 (image stream).
    let ret = ff_sys::avfilter_link(lut_ctx, 0, overlay_ctx, 1);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    Ok(overlay_ctx)
}

// ── Blend Normal mode compound step ──────────────────────────────────────────

/// Insert a Normal-mode blend compound step.
///
/// The top layer's `top_steps` are first applied to `top_src_ctx` (the `in1`
/// buffersrc), then optionally followed by `colorchannelmixer=aa=<opacity>` when
/// `opacity < 1.0`.  The result is composited onto `bottom_ctx` using
/// `overlay=format=auto:shortest=1`.
///
/// ```text
/// [in1]top_steps...[top_processed]
/// [top_processed]colorchannelmixer=aa=<opacity>[top_faded]   ← when opacity < 1.0
/// [bottom_ctx][top_faded]overlay=format=auto:shortest=1[out]
/// ```
///
/// # Safety
///
/// `graph`, `bottom_ctx`, and `top_src_ctx` must be valid pointers owned by the
/// same `AVFilterGraph`.
pub(super) unsafe fn add_blend_normal_step(
    graph: *mut ff_sys::AVFilterGraph,
    bottom_ctx: *mut ff_sys::AVFilterContext,
    top_src_ctx: *mut ff_sys::AVFilterContext,
    top_steps: &[FilterStep],
    opacity: f32,
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    use std::ffi::CString;

    // 1. Chain the top builder's steps starting from the in1 buffersrc.
    let mut top_ctx = top_src_ctx;
    for (j, step) in top_steps.iter().enumerate() {
        // Skip audio-only steps; they have no place in the video blend chain.
        if matches!(
            step,
            FilterStep::AReverse
                | FilterStep::AFadeIn { .. }
                | FilterStep::AFadeOut { .. }
                | FilterStep::ParametricEq { .. }
                | FilterStep::ANoiseGate { .. }
                | FilterStep::ACompressor { .. }
                | FilterStep::StereoToMono
                | FilterStep::ChannelMap { .. }
                | FilterStep::AudioDelay { .. }
                | FilterStep::ConcatAudio { .. }
                | FilterStep::LoudnessNormalize { .. }
                | FilterStep::NormalizePeak { .. }
        ) {
            continue;
        }
        top_ctx = add_and_link_step(graph, top_ctx, step, index * 1000 + j, "blend_top")?;
    }

    // 2. When opacity < 1.0, attenuate the top layer's alpha channel.
    if opacity < 1.0 {
        let ccm_name =
            CString::new(format!("blend_ccm{index}")).map_err(|_| FilterError::BuildFailed)?;
        let ccm_args_str = format!("aa={opacity}");
        let ccm_args = CString::new(ccm_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;

        let ccm_filter = ff_sys::avfilter_get_by_name(c"colorchannelmixer".as_ptr());
        if ccm_filter.is_null() {
            log::warn!("filter not found name=colorchannelmixer (blend_normal)");
            return Err(FilterError::BuildFailed);
        }

        let mut ccm_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
        let ret = ff_sys::avfilter_graph_create_filter(
            &raw mut ccm_ctx,
            ccm_filter,
            ccm_name.as_ptr(),
            ccm_args.as_ptr(),
            std::ptr::null_mut(),
            graph,
        );
        if ret < 0 {
            log::warn!("filter creation failed name=colorchannelmixer args={ccm_args_str}");
            return Err(FilterError::BuildFailed);
        }
        log::debug!("filter added name=colorchannelmixer args={ccm_args_str} index={index}");

        // SAFETY: top_ctx and ccm_ctx belong to the same graph; pad indices valid.
        let ret = ff_sys::avfilter_link(top_ctx, 0, ccm_ctx, 0);
        if ret < 0 {
            return Err(FilterError::BuildFailed);
        }
        top_ctx = ccm_ctx;
    }

    // 3. Create the overlay filter.
    let overlay_filter = ff_sys::avfilter_get_by_name(c"overlay".as_ptr());
    if overlay_filter.is_null() {
        log::warn!("filter not found name=overlay (blend_normal)");
        return Err(FilterError::BuildFailed);
    }
    let overlay_name =
        CString::new(format!("blend_overlay{index}")).map_err(|_| FilterError::BuildFailed)?;
    let overlay_args = c"format=auto:shortest=1";
    let mut overlay_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut overlay_ctx,
        overlay_filter,
        overlay_name.as_ptr(),
        overlay_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!(
            "filter creation failed name=overlay args=format=auto:shortest=1 (blend_normal)"
        );
        return Err(FilterError::BuildFailed);
    }
    log::debug!(
        "filter added name=overlay args=format=auto:shortest=1 index={index} (blend_normal)"
    );

    // 4. Link: bottom → overlay[0], top → overlay[1].
    // SAFETY: bottom_ctx, top_ctx, overlay_ctx are all in the same graph.
    let ret = ff_sys::avfilter_link(bottom_ctx, 0, overlay_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }
    let ret = ff_sys::avfilter_link(top_ctx, 0, overlay_ctx, 1);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    log::debug!("filter blend_normal expanded opacity={opacity} index={index}");
    Ok(overlay_ctx)
}

// ── Blend photographic-mode compound step ────────────────────────────────────

/// Insert a photographic-mode blend compound step (Multiply, Screen, etc.).
///
/// Chains `top_steps` on `top_src_ctx`, then creates
/// `blend=all_mode=<mode_name>[:all_opacity=<opacity>]` and links both inputs.
///
/// ```text
/// [in1]top_steps...[top_processed]
/// [bottom_ctx][top_processed]blend=all_mode=<mode>[out]
/// ```
///
/// # Safety
///
/// `graph`, `bottom_ctx`, and `top_src_ctx` must be valid pointers owned by the
/// same `AVFilterGraph`.
pub(super) unsafe fn add_blend_photographic_step(
    graph: *mut ff_sys::AVFilterGraph,
    bottom_ctx: *mut ff_sys::AVFilterContext,
    top_src_ctx: *mut ff_sys::AVFilterContext,
    top_steps: &[FilterStep],
    mode_name: &str,
    opacity: f32,
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    use std::ffi::CString;

    // 1. Chain the top builder's steps starting from the in1 buffersrc.
    let mut top_ctx = top_src_ctx;
    for (j, step) in top_steps.iter().enumerate() {
        // Skip audio-only steps; they have no place in the video blend chain.
        if matches!(
            step,
            FilterStep::AReverse
                | FilterStep::AFadeIn { .. }
                | FilterStep::AFadeOut { .. }
                | FilterStep::ParametricEq { .. }
                | FilterStep::ANoiseGate { .. }
                | FilterStep::ACompressor { .. }
                | FilterStep::StereoToMono
                | FilterStep::ChannelMap { .. }
                | FilterStep::AudioDelay { .. }
                | FilterStep::ConcatAudio { .. }
                | FilterStep::LoudnessNormalize { .. }
                | FilterStep::NormalizePeak { .. }
        ) {
            continue;
        }
        top_ctx = add_and_link_step(graph, top_ctx, step, index * 1000 + j, "blend_top")?;
    }

    // 2. Create the blend filter.
    let blend_filter = ff_sys::avfilter_get_by_name(c"blend".as_ptr());
    if blend_filter.is_null() {
        log::warn!("filter not found name=blend (blend_photographic)");
        return Err(FilterError::BuildFailed);
    }
    let blend_name =
        CString::new(format!("blend_phot{index}")).map_err(|_| FilterError::BuildFailed)?;
    let blend_args_str = if (opacity - 1.0).abs() < f32::EPSILON {
        format!("all_mode={mode_name}")
    } else {
        format!("all_mode={mode_name}:all_opacity={opacity}")
    };
    let blend_args = CString::new(blend_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;
    let mut blend_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut blend_ctx,
        blend_filter,
        blend_name.as_ptr(),
        blend_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=blend args={blend_args_str} (blend_photographic)");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=blend args={blend_args_str} index={index} (blend_photographic)");

    // 3. Link: bottom → blend[0], top → blend[1].
    // SAFETY: bottom_ctx, top_ctx, blend_ctx are all in the same graph.
    let ret = ff_sys::avfilter_link(bottom_ctx, 0, blend_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }
    let ret = ff_sys::avfilter_link(top_ctx, 0, blend_ctx, 1);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    log::debug!(
        "filter blend_photographic expanded mode={mode_name} opacity={opacity} index={index}"
    );
    Ok(blend_ctx)
}

// ── Blend Porter-Duff Under compound step ────────────────────────────────────

/// Insert a Porter-Duff Under blend step.
///
/// Equivalent to Over with inputs swapped: the top layer becomes the
/// background and the bottom layer is composited on top of it.
///
/// ```text
/// [in1]top_steps...[top_processed]
/// [top_processed][bottom_ctx]overlay=format=auto:shortest=1[out]
/// ```
///
/// # Safety
///
/// `graph`, `bottom_ctx`, and `top_src_ctx` must be valid pointers owned by the
/// same `AVFilterGraph`.
pub(super) unsafe fn add_blend_under_step(
    graph: *mut ff_sys::AVFilterGraph,
    bottom_ctx: *mut ff_sys::AVFilterContext,
    top_src_ctx: *mut ff_sys::AVFilterContext,
    top_steps: &[FilterStep],
    opacity: f32,
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    use std::ffi::CString;

    // 1. Chain the top builder's steps starting from the in1 buffersrc.
    let mut top_ctx = top_src_ctx;
    for (j, step) in top_steps.iter().enumerate() {
        if matches!(
            step,
            FilterStep::AReverse
                | FilterStep::AFadeIn { .. }
                | FilterStep::AFadeOut { .. }
                | FilterStep::ParametricEq { .. }
                | FilterStep::ANoiseGate { .. }
                | FilterStep::ACompressor { .. }
                | FilterStep::StereoToMono
                | FilterStep::ChannelMap { .. }
                | FilterStep::AudioDelay { .. }
                | FilterStep::ConcatAudio { .. }
                | FilterStep::LoudnessNormalize { .. }
                | FilterStep::NormalizePeak { .. }
        ) {
            continue;
        }
        top_ctx = add_and_link_step(graph, top_ctx, step, index * 1000 + j, "blend_top")?;
    }

    // 2. When opacity < 1.0, attenuate the bottom (background) layer's alpha channel.
    if opacity < 1.0 {
        let ccm_name = CString::new(format!("blend_under_ccm{index}"))
            .map_err(|_| FilterError::BuildFailed)?;
        let ccm_args_str = format!("aa={opacity}");
        let ccm_args = CString::new(ccm_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;
        let ccm_filter = ff_sys::avfilter_get_by_name(c"colorchannelmixer".as_ptr());
        if ccm_filter.is_null() {
            log::warn!("filter not found name=colorchannelmixer (blend_under)");
            return Err(FilterError::BuildFailed);
        }
        let mut ccm_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
        let ret = ff_sys::avfilter_graph_create_filter(
            &raw mut ccm_ctx,
            ccm_filter,
            ccm_name.as_ptr(),
            ccm_args.as_ptr(),
            std::ptr::null_mut(),
            graph,
        );
        if ret < 0 {
            log::warn!("filter creation failed name=colorchannelmixer args={ccm_args_str}");
            return Err(FilterError::BuildFailed);
        }
        log::debug!("filter added name=colorchannelmixer args={ccm_args_str} index={index}");
        // SAFETY: top_ctx and ccm_ctx belong to the same graph; pad indices valid.
        let ret = ff_sys::avfilter_link(top_ctx, 0, ccm_ctx, 0);
        if ret < 0 {
            return Err(FilterError::BuildFailed);
        }
        top_ctx = ccm_ctx;
    }

    // 3. Create the overlay filter.
    let overlay_filter = ff_sys::avfilter_get_by_name(c"overlay".as_ptr());
    if overlay_filter.is_null() {
        log::warn!("filter not found name=overlay (blend_under)");
        return Err(FilterError::BuildFailed);
    }
    let overlay_name = CString::new(format!("blend_under_overlay{index}"))
        .map_err(|_| FilterError::BuildFailed)?;
    let overlay_args = c"format=auto:shortest=1";
    let mut overlay_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut overlay_ctx,
        overlay_filter,
        overlay_name.as_ptr(),
        overlay_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=overlay args=format=auto:shortest=1 (blend_under)");
        return Err(FilterError::BuildFailed);
    }
    log::debug!(
        "filter added name=overlay args=format=auto:shortest=1 index={index} (blend_under)"
    );

    // 4. Link (swapped vs. Normal): top → overlay[0], bottom → overlay[1].
    // SAFETY: bottom_ctx, top_ctx, overlay_ctx are all in the same graph.
    let ret = ff_sys::avfilter_link(top_ctx, 0, overlay_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }
    let ret = ff_sys::avfilter_link(bottom_ctx, 0, overlay_ctx, 1);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    log::debug!("filter blend_under expanded opacity={opacity} index={index}");
    Ok(overlay_ctx)
}

// ── Blend expression-based compound step ────────────────────────────────────

/// Insert a Porter-Duff expression-based blend step (In, Out).
///
/// Chains `top_steps` on `top_src_ctx`, then creates
/// `blend=all_expr=<expr>` and links both inputs.
///
/// ```text
/// [in1]top_steps...[top_processed]
/// [bottom_ctx][top_processed]blend=all_expr=<expr>[out]
/// ```
///
/// # Safety
///
/// `graph`, `bottom_ctx`, and `top_src_ctx` must be valid pointers owned by the
/// same `AVFilterGraph`.
pub(super) unsafe fn add_blend_expr_step(
    graph: *mut ff_sys::AVFilterGraph,
    bottom_ctx: *mut ff_sys::AVFilterContext,
    top_src_ctx: *mut ff_sys::AVFilterContext,
    top_steps: &[FilterStep],
    expr: &str,
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    use std::ffi::CString;

    // 1. Chain the top builder's steps starting from the in1 buffersrc.
    let mut top_ctx = top_src_ctx;
    for (j, step) in top_steps.iter().enumerate() {
        if matches!(
            step,
            FilterStep::AReverse
                | FilterStep::AFadeIn { .. }
                | FilterStep::AFadeOut { .. }
                | FilterStep::ParametricEq { .. }
                | FilterStep::ANoiseGate { .. }
                | FilterStep::ACompressor { .. }
                | FilterStep::StereoToMono
                | FilterStep::ChannelMap { .. }
                | FilterStep::AudioDelay { .. }
                | FilterStep::ConcatAudio { .. }
                | FilterStep::LoudnessNormalize { .. }
                | FilterStep::NormalizePeak { .. }
        ) {
            continue;
        }
        top_ctx = add_and_link_step(graph, top_ctx, step, index * 1000 + j, "blend_top")?;
    }

    // 2. Create the blend filter with the expression.
    let blend_filter = ff_sys::avfilter_get_by_name(c"blend".as_ptr());
    if blend_filter.is_null() {
        log::warn!("filter not found name=blend (blend_expr)");
        return Err(FilterError::BuildFailed);
    }
    let blend_name =
        CString::new(format!("blend_expr{index}")).map_err(|_| FilterError::BuildFailed)?;
    let blend_args_str = format!("all_expr={expr}");
    let blend_args = CString::new(blend_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;
    let mut blend_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut blend_ctx,
        blend_filter,
        blend_name.as_ptr(),
        blend_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=blend args={blend_args_str} (blend_expr)");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=blend args={blend_args_str} index={index} (blend_expr)");

    // 3. Link: bottom → blend[0], top → blend[1].
    // SAFETY: bottom_ctx, top_ctx, blend_ctx are all in the same graph.
    let ret = ff_sys::avfilter_link(bottom_ctx, 0, blend_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }
    let ret = ff_sys::avfilter_link(top_ctx, 0, blend_ctx, 1);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    log::debug!("filter blend_expr expanded expr={expr} index={index}");
    Ok(blend_ctx)
}

// ── FeatherMask compound step ─────────────────────────────────────────────────

/// Insert the feather-mask compound step.
///
/// Splits the input into a color copy and an alpha copy, blurs the alpha
/// using `gblur=sigma=<radius>`, then re-merges:
///
/// ```text
/// [in]split=2[color][with_alpha];
/// [with_alpha]alphaextract[alpha_only];
/// [alpha_only]gblur=sigma=<radius>[alpha_blurred];
/// [color][alpha_blurred]alphamerge[out]
/// ```
///
/// # Safety
///
/// `graph` and `prev_ctx` must be valid pointers owned by the same
/// `AVFilterGraph`.
// ── Glow compound step ────────────────────────────────────────────────────────
/// Insert the glow / bloom compound step.
///
/// ```text
/// prev_ctx → split=2 → pad0 ──────────────────────────────→ blend[0]
///                    → pad1 → curves(hi_lo) → gblur(r) ──→ blend[1]
///                                                            blend(addition, opacity=iv) → out
/// ```
///
/// # Safety
///
/// `graph` and `prev_ctx` must be valid pointers owned by the same
/// `AVFilterGraph`.
pub(super) unsafe fn add_glow_step(
    graph: *mut ff_sys::AVFilterGraph,
    prev_ctx: *mut ff_sys::AVFilterContext,
    threshold: f32,
    radius: f32,
    intensity: f32,
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    use std::ffi::CString;

    let t = threshold.clamp(0.0, 1.0);
    let r = radius.clamp(0.5, 50.0);
    let iv = intensity.clamp(0.0, 2.0);

    // 1. split=2 — duplicate the stream into a base path and a highlights path.
    let split_filter = ff_sys::avfilter_get_by_name(c"split".as_ptr());
    if split_filter.is_null() {
        log::warn!("filter not found name=split (glow)");
        return Err(FilterError::BuildFailed);
    }
    let split_name =
        CString::new(format!("glow_split{index}")).map_err(|_| FilterError::BuildFailed)?;
    let mut split_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    // SAFETY: split_filter and graph are non-null; "2" is valid args.
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut split_ctx,
        split_filter,
        split_name.as_ptr(),
        c"2".as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=split (glow) code={ret}");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=split args=2 index={index} (glow)");

    // Link: prev_ctx → split[0].
    // SAFETY: prev_ctx and split_ctx belong to the same graph; pad indices valid.
    let ret = ff_sys::avfilter_link(prev_ctx, 0, split_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    // 2. curves — extract highlights: below threshold → black; above → full.
    let curves_filter = ff_sys::avfilter_get_by_name(c"curves".as_ptr());
    if curves_filter.is_null() {
        log::warn!("filter not found name=curves (glow)");
        return Err(FilterError::BuildFailed);
    }
    let curves_name =
        CString::new(format!("glow_curves{index}")).map_err(|_| FilterError::BuildFailed)?;
    let hi_lo = format!("0/0 {t}/0 1/1");
    let curves_args_str = format!("all='{hi_lo}'");
    let curves_args =
        CString::new(curves_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;
    let mut curves_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut curves_ctx,
        curves_filter,
        curves_name.as_ptr(),
        curves_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=curves args={curves_args_str} (glow) code={ret}");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=curves args={curves_args_str} index={index} (glow)");

    // Link: split[1] → curves[0] (highlights path).
    // SAFETY: split_ctx and curves_ctx belong to the same graph; pad 1 of split valid.
    let ret = ff_sys::avfilter_link(split_ctx, 1, curves_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    // 3. gblur — blur the extracted highlights.
    let gblur_filter = ff_sys::avfilter_get_by_name(c"gblur".as_ptr());
    if gblur_filter.is_null() {
        log::warn!("filter not found name=gblur (glow)");
        return Err(FilterError::BuildFailed);
    }
    let gblur_name =
        CString::new(format!("glow_gblur{index}")).map_err(|_| FilterError::BuildFailed)?;
    let gblur_args_str = format!("sigma={r}");
    let gblur_args = CString::new(gblur_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;
    let mut gblur_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut gblur_ctx,
        gblur_filter,
        gblur_name.as_ptr(),
        gblur_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=gblur args={gblur_args_str} (glow) code={ret}");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=gblur args={gblur_args_str} index={index} (glow)");

    // Link: curves → gblur.
    let ret = ff_sys::avfilter_link(curves_ctx, 0, gblur_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    // 4. blend — additively blend blurred highlights back over the base image.
    let blend_filter = ff_sys::avfilter_get_by_name(c"blend".as_ptr());
    if blend_filter.is_null() {
        log::warn!("filter not found name=blend (glow)");
        return Err(FilterError::BuildFailed);
    }
    let blend_name =
        CString::new(format!("glow_blend{index}")).map_err(|_| FilterError::BuildFailed)?;
    let blend_args_str = format!("all_mode=addition:all_opacity={iv}");
    let blend_args = CString::new(blend_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;
    let mut blend_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut blend_ctx,
        blend_filter,
        blend_name.as_ptr(),
        blend_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=blend args={blend_args_str} (glow) code={ret}");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=blend args={blend_args_str} index={index} (glow)");

    // Link: split[0] → blend[0] (base path — the bottom stream).
    // SAFETY: split_ctx and blend_ctx belong to the same graph; pad indices valid.
    let ret = ff_sys::avfilter_link(split_ctx, 0, blend_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    // Link: gblur → blend[1] (blurred highlights — the top stream).
    let ret = ff_sys::avfilter_link(gblur_ctx, 0, blend_ctx, 1);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    log::debug!(
        "filter glow expanded threshold={threshold} radius={radius} intensity={intensity} index={index}"
    );
    Ok(blend_ctx)
}

pub(super) unsafe fn add_feather_mask_step(
    graph: *mut ff_sys::AVFilterGraph,
    prev_ctx: *mut ff_sys::AVFilterContext,
    radius: u32,
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    use std::ffi::CString;

    // 1. split=2 — duplicate the stream into a color copy and an alpha copy.
    let split_filter = ff_sys::avfilter_get_by_name(c"split".as_ptr());
    if split_filter.is_null() {
        log::warn!("filter not found name=split (feather_mask)");
        return Err(FilterError::BuildFailed);
    }
    let split_name =
        CString::new(format!("feather_split{index}")).map_err(|_| FilterError::BuildFailed)?;
    let mut split_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    // SAFETY: split_filter and graph are non-null; "2" is valid args.
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut split_ctx,
        split_filter,
        split_name.as_ptr(),
        c"2".as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=split (feather_mask) code={ret}");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=split args=2 index={index} (feather_mask)");

    // Link: prev_ctx → split[0].
    // SAFETY: prev_ctx and split_ctx belong to the same graph; pad indices valid.
    let ret = ff_sys::avfilter_link(prev_ctx, 0, split_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    // 2. alphaextract — convert the alpha plane from split output pad 1 into
    //    a grayscale stream so it can be processed independently.
    let alphaextract_filter = ff_sys::avfilter_get_by_name(c"alphaextract".as_ptr());
    if alphaextract_filter.is_null() {
        log::warn!("filter not found name=alphaextract (feather_mask)");
        return Err(FilterError::BuildFailed);
    }
    let alphaextract_name = CString::new(format!("feather_alphaextract{index}"))
        .map_err(|_| FilterError::BuildFailed)?;
    let mut alphaextract_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut alphaextract_ctx,
        alphaextract_filter,
        alphaextract_name.as_ptr(),
        std::ptr::null(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=alphaextract (feather_mask) code={ret}");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=alphaextract index={index} (feather_mask)");

    // Link: split[1] → alphaextract[0].
    // SAFETY: split_ctx and alphaextract_ctx belong to the same graph; pad 1 of split valid.
    let ret = ff_sys::avfilter_link(split_ctx, 1, alphaextract_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    // 3. gblur=sigma=<radius> — blur the extracted alpha plane.
    let gblur_filter = ff_sys::avfilter_get_by_name(c"gblur".as_ptr());
    if gblur_filter.is_null() {
        log::warn!("filter not found name=gblur (feather_mask)");
        return Err(FilterError::BuildFailed);
    }
    let gblur_name =
        CString::new(format!("feather_gblur{index}")).map_err(|_| FilterError::BuildFailed)?;
    let gblur_args_str = format!("sigma={radius}");
    let gblur_args = CString::new(gblur_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;
    let mut gblur_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut gblur_ctx,
        gblur_filter,
        gblur_name.as_ptr(),
        gblur_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!(
            "filter creation failed name=gblur args={gblur_args_str} (feather_mask) code={ret}"
        );
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=gblur args={gblur_args_str} index={index} (feather_mask)");

    // Link: alphaextract → gblur.
    let ret = ff_sys::avfilter_link(alphaextract_ctx, 0, gblur_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    // 4. alphamerge — re-merge color (split pad 0) with blurred alpha (gblur output).
    let alphamerge_filter = ff_sys::avfilter_get_by_name(c"alphamerge".as_ptr());
    if alphamerge_filter.is_null() {
        log::warn!("filter not found name=alphamerge (feather_mask)");
        return Err(FilterError::BuildFailed);
    }
    let alphamerge_name =
        CString::new(format!("feather_alphamerge{index}")).map_err(|_| FilterError::BuildFailed)?;
    let mut alphamerge_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    // SAFETY: alphamerge_filter and graph are non-null; null args are accepted.
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut alphamerge_ctx,
        alphamerge_filter,
        alphamerge_name.as_ptr(),
        std::ptr::null(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=alphamerge (feather_mask) code={ret}");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=alphamerge index={index} (feather_mask)");

    // Link: split[0] → alphamerge[0] (color path).
    // SAFETY: split_ctx and alphamerge_ctx belong to the same graph; pad indices valid.
    let ret = ff_sys::avfilter_link(split_ctx, 0, alphamerge_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    // Link: gblur → alphamerge[1] (blurred alpha path).
    let ret = ff_sys::avfilter_link(gblur_ctx, 0, alphamerge_ctx, 1);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    log::debug!("filter feather_mask expanded radius={radius} index={index}");
    Ok(alphamerge_ctx)
}

// ── AlphaMatte compound step ─────────────────────────────────────────────────

/// Insert the alphamerge compound step.
///
/// Chains `matte_steps` on `matte_src_ctx` (the `in1` buffersrc), then creates
/// the `alphamerge` filter and links:
///
/// ```text
/// [in1]matte_steps...[matte_processed]
/// [bottom_ctx][matte_processed]alphamerge[out]
/// ```
///
/// # Safety
///
/// `graph`, `bottom_ctx`, and `matte_src_ctx` must be valid pointers owned by
/// the same `AVFilterGraph`.
pub(super) unsafe fn add_alphamerge_step(
    graph: *mut ff_sys::AVFilterGraph,
    bottom_ctx: *mut ff_sys::AVFilterContext,
    matte_src_ctx: *mut ff_sys::AVFilterContext,
    matte_steps: &[FilterStep],
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    use std::ffi::CString;

    // 1. Chain the matte builder's steps starting from the in1 buffersrc.
    let mut matte_ctx = matte_src_ctx;
    for (j, step) in matte_steps.iter().enumerate() {
        // Skip audio-only steps; they have no place in the matte chain.
        if matches!(
            step,
            FilterStep::AReverse
                | FilterStep::AFadeIn { .. }
                | FilterStep::AFadeOut { .. }
                | FilterStep::ParametricEq { .. }
                | FilterStep::ANoiseGate { .. }
                | FilterStep::ACompressor { .. }
                | FilterStep::StereoToMono
                | FilterStep::ChannelMap { .. }
                | FilterStep::AudioDelay { .. }
                | FilterStep::ConcatAudio { .. }
                | FilterStep::LoudnessNormalize { .. }
                | FilterStep::NormalizePeak { .. }
        ) {
            continue;
        }
        matte_ctx = add_and_link_step(graph, matte_ctx, step, index * 1000 + j, "matte")?;
    }

    // 2. Create the alphamerge filter.
    let alphamerge_filter = ff_sys::avfilter_get_by_name(c"alphamerge".as_ptr());
    if alphamerge_filter.is_null() {
        log::warn!("filter not found name=alphamerge");
        return Err(FilterError::BuildFailed);
    }
    let alphamerge_name =
        CString::new(format!("alphamerge{index}")).map_err(|_| FilterError::BuildFailed)?;
    let mut alphamerge_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    // SAFETY: alphamerge_filter and graph are non-null; null opaque and args are accepted.
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut alphamerge_ctx,
        alphamerge_filter,
        alphamerge_name.as_ptr(),
        std::ptr::null(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=alphamerge code={ret}");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=alphamerge index={index}");

    // 3. Link: main video → alphamerge[0], matte → alphamerge[1].
    // SAFETY: bottom_ctx, matte_ctx, alphamerge_ctx are all in the same graph.
    let ret = ff_sys::avfilter_link(bottom_ctx, 0, alphamerge_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }
    let ret = ff_sys::avfilter_link(matte_ctx, 0, alphamerge_ctx, 1);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    log::debug!("filter alphamerge linked index={index}");
    Ok(alphamerge_ctx)
}

// ── buffersrc / buffersink arg-string helpers ──────────────────────────────────

/// Build the `args` string passed to `avfilter_graph_create_filter` when
/// creating a video `buffer` (buffersrc) context.
///
/// The format follows libavfilter's `buffer` filter parameter syntax:
/// `video_size=WxH:pix_fmt=N:time_base=NUM/DEN:pixel_aspect=1/1`.
pub(super) fn video_buffersrc_args(
    width: u32,
    height: u32,
    pix_fmt: std::os::raw::c_int,
) -> String {
    format!(
        "video_size={}x{}:pix_fmt={}:time_base={}/{}:pixel_aspect=1/1",
        width, height, pix_fmt, VIDEO_TIME_BASE_NUM, VIDEO_TIME_BASE_DEN,
    )
}

/// Build the `args` string passed to `avfilter_graph_create_filter` when
/// creating an audio `abuffer` (buffersrc) context.
///
/// The format follows libavfilter's `abuffer` filter parameter syntax:
/// `sample_rate=R:sample_fmt=FMT:channels=C:time_base=1/R`.
pub(super) fn audio_buffersrc_args(
    sample_rate: u32,
    sample_fmt_name: &str,
    channels: u32,
) -> String {
    format!(
        "sample_rate={}:sample_fmt={}:channels={}:time_base={}/{}",
        sample_rate, sample_fmt_name, channels, AUDIO_TIME_BASE_NUM, sample_rate,
    )
}

/// Parse the `sample_rate` field from a `buffersrc_args` string.
///
/// The expected format is `sample_rate=R:sample_fmt=FMT:channels=C:...`.
/// Returns `44100` as a safe fallback if the field is absent or unparseable.
pub(super) fn parse_sample_rate_from_buffersrc(buffersrc_args: &str) -> u32 {
    buffersrc_args
        .split(':')
        .find_map(|kv| {
            let (k, v) = kv.split_once('=')?;
            if k == "sample_rate" {
                v.parse().ok()
            } else {
                None
            }
        })
        .unwrap_or(44100)
}

// ── Parametric EQ (multi-band chain) ─────────────────────────────────────────

/// Insert a chain of filter nodes for a [`FilterStep::ParametricEq`] step.
///
/// One node per band is created using the band's own filter name (e.g.,
/// `lowshelf`, `highshelf`, `equalizer`) and args, and each is linked in
/// series after `prev_ctx`.
///
/// # Safety
///
/// `graph` and `prev_ctx` must be valid pointers owned by the same
/// `AVFilterGraph`.
pub(super) unsafe fn add_parametric_eq_chain(
    graph: *mut ff_sys::AVFilterGraph,
    prev_ctx: *mut ff_sys::AVFilterContext,
    bands: &[EqBand],
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    let mut ctx = prev_ctx;
    for (j, band) in bands.iter().enumerate() {
        let filter_name =
            std::ffi::CString::new(band.filter_name()).map_err(|_| FilterError::BuildFailed)?;
        let filter = ff_sys::avfilter_get_by_name(filter_name.as_ptr());
        if filter.is_null() {
            log::warn!("filter not found name={}", band.filter_name());
            return Err(FilterError::BuildFailed);
        }

        let name = std::ffi::CString::new(format!("eq{index}_{j}"))
            .map_err(|_| FilterError::BuildFailed)?;
        let args_str = band.args();
        let args =
            std::ffi::CString::new(args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;

        let mut band_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
        let ret = ff_sys::avfilter_graph_create_filter(
            &raw mut band_ctx,
            filter,
            name.as_ptr(),
            args.as_ptr(),
            std::ptr::null_mut(),
            graph,
        );
        if ret < 0 {
            log::warn!(
                "filter creation failed name={} args={}",
                band.filter_name(),
                args_str
            );
            return Err(FilterError::BuildFailed);
        }
        log::debug!(
            "filter added name={} args={} index={index} band={j}",
            band.filter_name(),
            args_str
        );

        let ret = ff_sys::avfilter_link(ctx, 0, band_ctx, 0);
        if ret < 0 {
            return Err(FilterError::BuildFailed);
        }
        ctx = band_ctx;
    }
    Ok(ctx)
}

// ── JoinWithDissolve compound step ────────────────────────────────────────────

/// Expand a `JoinWithDissolve` step into the following compound filter graph:
///
/// ```text
/// clip_a_src → trim(end=clip_a_end+dissolve_dur) → setpts → xfade[0]
/// clip_b_src → trim(start=max(0, clip_b_start−dissolve_dur)) → setpts → xfade[1]
/// ```
///
/// Returns the `xfade` filter context, which becomes the new `prev_ctx`.
///
/// # Safety
///
/// `graph`, `clip_a_src`, and `clip_b_src` must be valid non-null pointers
/// belonging to the same `AVFilterGraph`.
pub(super) unsafe fn add_join_with_dissolve_step(
    graph: *mut ff_sys::AVFilterGraph,
    clip_a_src: *mut ff_sys::AVFilterContext,
    clip_b_src: *mut ff_sys::AVFilterContext,
    clip_a_end: f64,
    clip_b_start: f64,
    dissolve_dur: f64,
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    // 1. trim_a: keep clip A frames up to clip_a_end + dissolve_dur
    let a_trim_end = clip_a_end + dissolve_dur;
    let trim_a = add_raw_filter_step(
        graph,
        clip_a_src,
        "trim",
        &format!("end={a_trim_end}"),
        index,
        "jwd_trima",
    )?;

    // 2. setpts_a: reset clip A timestamps to zero after trim
    let setpts_a = add_raw_filter_step(
        graph,
        trim_a,
        "setpts",
        "PTS-STARTPTS",
        index,
        "jwd_setptsa",
    )?;

    // 3. Create xfade filter; pads are wired manually below.
    let xfade_filter = ff_sys::avfilter_get_by_name(c"xfade".as_ptr());
    if xfade_filter.is_null() {
        log::warn!("filter not found name=xfade (join_with_dissolve)");
        return Err(FilterError::BuildFailed);
    }
    let xfade_name = std::ffi::CString::new(format!("jwd_xfade{index}"))
        .map_err(|_| FilterError::BuildFailed)?;
    let xfade_args_str = format!("transition=dissolve:duration={dissolve_dur}:offset={clip_a_end}");
    let xfade_args =
        std::ffi::CString::new(xfade_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;
    let mut xfade_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    // SAFETY: all pointers are valid; xfade_filter, xfade_name, and xfade_args
    // are non-null and null-terminated.
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut xfade_ctx,
        xfade_filter,
        xfade_name.as_ptr(),
        xfade_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=xfade args={xfade_args_str} (join_with_dissolve)");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=xfade args={xfade_args_str} (join_with_dissolve)");

    // Link setpts_a (clip A chain) → xfade[0]
    // SAFETY: setpts_a and xfade_ctx belong to the same graph; pad indices valid.
    let ret = ff_sys::avfilter_link(setpts_a, 0, xfade_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    // 4. trim_b: keep clip B frames from max(0, clip_b_start - dissolve_dur)
    let b_trim_start = (clip_b_start - dissolve_dur).max(0.0);
    let trim_b = add_raw_filter_step(
        graph,
        clip_b_src,
        "trim",
        &format!("start={b_trim_start}"),
        index,
        "jwd_trimb",
    )?;

    // 5. setpts_b: reset clip B timestamps to zero after trim
    let setpts_b = add_raw_filter_step(
        graph,
        trim_b,
        "setpts",
        "PTS-STARTPTS",
        index,
        "jwd_setptsb",
    )?;

    // Link setpts_b (clip B chain) → xfade[1]
    // SAFETY: setpts_b and xfade_ctx belong to the same graph; pad index 1 is valid for xfade.
    let ret = ff_sys::avfilter_link(setpts_b, 0, xfade_ctx, 1);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    log::debug!(
        "filter join_with_dissolve expanded dissolve_dur={dissolve_dur} offset={clip_a_end}"
    );
    Ok(xfade_ctx)
}

// ── Graph orchestrators ───────────────────────────────────────────────────────

use super::FilterGraphInner;

impl FilterGraphInner {
    /// Build the `AVFilterGraph` for video, returning `(src_ctxs, vsink_ctx)`.
    ///
    /// `num_inputs` buffersrc contexts are created (`in0`..`inN-1`).  For
    /// multi-input filters like `overlay`, the extra sources are linked to the
    /// appropriate input pads after the main chain link is established.
    ///
    /// # Safety
    ///
    /// `graph_nn` must be a valid, freshly-allocated `AVFilterGraph`.
    pub(super) unsafe fn build_video_graph(
        graph_nn: NonNull<ff_sys::AVFilterGraph>,
        buffersrc_args: &str,
        num_inputs: usize,
        steps: &[FilterStep],
        hw: Option<&HwAccel>,
    ) -> VideoGraphResult {
        let graph = graph_nn.as_ptr();

        // 0. When hardware acceleration is requested, create a device context
        //    and disable automatic pixel-format conversion so FFmpeg does not
        //    insert implicit hwupload/scale filters that would conflict with the
        //    explicit ones we add below.
        let hw_device_ctx: Option<*mut ff_sys::AVBufferRef> = if let Some(hw) = hw {
            let device_type = hw_accel_to_device_type(*hw);
            let mut raw_hw_ctx: *mut ff_sys::AVBufferRef = std::ptr::null_mut();
            let ret = ff_sys::av_hwdevice_ctx_create(
                &raw mut raw_hw_ctx,
                device_type,
                std::ptr::null(),     // device: null = system default
                std::ptr::null_mut(), // opts: null = defaults
                0,
            );
            if ret < 0 {
                log::warn!("av_hwdevice_ctx_create failed hw={hw:?} code={ret}");
                return Err(FilterError::BuildFailed);
            }
            // AVFILTER_AUTO_CONVERT_NONE = 0: hardware filters must receive
            // frames in exactly the format they expect.
            ff_sys::avfilter_graph_set_auto_convert(graph, 0u32);
            log::debug!("hw device context created hw={hw:?}");
            Some(raw_hw_ctx)
        } else {
            None
        };

        // Helper closure: free hw_device_ctx and return an error.  Used at
        // every early-return failure point that occurs *after* the device
        // context has been allocated so it is not leaked.
        macro_rules! bail {
            ($err:expr) => {{
                if let Some(mut hw_ctx) = hw_device_ctx {
                    ff_sys::av_buffer_unref(std::ptr::addr_of_mut!(hw_ctx));
                }
                return Err($err);
            }};
        }

        // SAFETY: `avfilter_get_by_name` returns a borrowed pointer valid for
        // the process lifetime; we never free it.
        let buffersrc = ff_sys::avfilter_get_by_name(c"buffer".as_ptr());
        if buffersrc.is_null() {
            bail!(FilterError::BuildFailed);
        }

        let Ok(src_args) = std::ffi::CString::new(buffersrc_args) else {
            bail!(FilterError::BuildFailed)
        };
        let mut src_ctxs: FilterCtxVec = Vec::with_capacity(num_inputs);

        // 1. Create in0 (always present).
        let mut raw_ctx0: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
        let ret = ff_sys::avfilter_graph_create_filter(
            &raw mut raw_ctx0,
            buffersrc,
            c"in0".as_ptr(),
            src_args.as_ptr(),
            std::ptr::null_mut(),
            graph,
        );
        if ret < 0 {
            bail!(FilterError::BuildFailed);
        }
        log::debug!("filter added name=buffersrc slot=0");
        // SAFETY: ret >= 0 guarantees non-null.
        src_ctxs.push(Some(NonNull::new_unchecked(raw_ctx0)));

        // Create in1..inN-1 (for overlay etc.)
        for slot in 1..num_inputs {
            let Ok(ctx_name) = std::ffi::CString::new(format!("in{slot}")) else {
                bail!(FilterError::BuildFailed)
            };
            let mut raw_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
            let ret = ff_sys::avfilter_graph_create_filter(
                &raw mut raw_ctx,
                buffersrc,
                ctx_name.as_ptr(),
                src_args.as_ptr(),
                std::ptr::null_mut(),
                graph,
            );
            if ret < 0 {
                bail!(FilterError::BuildFailed);
            }
            log::debug!("filter added name=buffersrc slot={slot}");
            // SAFETY: ret >= 0 guarantees non-null.
            src_ctxs.push(Some(NonNull::new_unchecked(raw_ctx)));
        }

        // 2. Create buffersink ("buffersink").
        let buffersink = ff_sys::avfilter_get_by_name(c"buffersink".as_ptr());
        if buffersink.is_null() {
            bail!(FilterError::BuildFailed);
        }

        let mut sink_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
        let ret = ff_sys::avfilter_graph_create_filter(
            &raw mut sink_ctx,
            buffersink,
            c"out".as_ptr(),
            std::ptr::null_mut(),
            std::ptr::null_mut(),
            graph,
        );
        if ret < 0 {
            bail!(FilterError::BuildFailed);
        }

        // 3. Insert hwupload/hwupload_cuda BEFORE the filter steps so that
        //    subsequent filters receive hardware (CUDA/VAAPI/VTB) frames.
        let mut prev_ctx: *mut ff_sys::AVFilterContext = raw_ctx0;
        if let (Some(hw_ctx), Some(hw_backend)) = (hw_device_ctx, hw) {
            let upload_name = match hw_backend {
                HwAccel::Cuda => c"hwupload_cuda",
                HwAccel::VideoToolbox | HwAccel::Vaapi => c"hwupload",
            };
            prev_ctx = match create_hw_filter(graph, prev_ctx, upload_name, c"hwupload0", hw_ctx) {
                Ok(ctx) => ctx,
                Err(e) => bail!(e),
            };
        }

        // 4-5. Add each `FilterStep`, link the main chain (in0 → step[0] → …),
        // and wire extra input pads for multi-input filters.
        for (i, step) in steps.iter().enumerate() {
            // Audio-only steps; skip them in the video graph.
            if matches!(
                step,
                FilterStep::AReverse
                    | FilterStep::AFadeIn { .. }
                    | FilterStep::AFadeOut { .. }
                    | FilterStep::ParametricEq { .. }
                    | FilterStep::ANoiseGate { .. }
                    | FilterStep::ACompressor { .. }
                    | FilterStep::StereoToMono
                    | FilterStep::ChannelMap { .. }
                    | FilterStep::AudioDelay { .. }
                    | FilterStep::ConcatAudio { .. }
            ) {
                continue;
            }

            // LoudnessNormalize is audio-only and handled via two-pass in
            // push_audio / pull_audio rather than through the filter graph.
            if matches!(step, FilterStep::LoudnessNormalize { .. }) {
                continue;
            }

            // NormalizePeak is audio-only and handled via two-pass buffering in
            // push_audio / pull_audio.
            if matches!(step, FilterStep::NormalizePeak { .. }) {
                continue;
            }

            // OverlayImage is a compound step (movie → lut → overlay).  It
            // creates its own internal source node via the `movie` filter and
            // does not consume a buffersrc slot, so it must bypass the standard
            // `add_and_link_step` path which assumes a single filter per step.
            if let FilterStep::OverlayImage {
                path,
                x,
                y,
                opacity,
            } = step
            {
                prev_ctx = match add_overlay_image_step(graph, prev_ctx, path, x, y, *opacity, i) {
                    Ok(ctx) => ctx,
                    Err(e) => bail!(e),
                };
                continue;
            }

            // JoinWithDissolve is a compound step that expands to:
            //   prev → trim_a → setpts_a → xfade[0]
            //   in1  → trim_b → setpts_b → xfade[1]
            // It bypasses the standard add_and_link_step path entirely.
            if let FilterStep::JoinWithDissolve {
                clip_a_end,
                clip_b_start,
                dissolve_dur,
            } = step
            {
                let Some(b_src) = src_ctxs.get(1).and_then(|o| *o) else {
                    bail!(FilterError::BuildFailed)
                };
                prev_ctx = match add_join_with_dissolve_step(
                    graph,
                    prev_ctx,
                    b_src.as_ptr(),
                    *clip_a_end,
                    *clip_b_start,
                    *dissolve_dur,
                    i,
                ) {
                    Ok(ctx) => ctx,
                    Err(e) => bail!(e),
                };
                continue;
            }

            // Glow — compound step: split → [base | curves → gblur] → blend(addition).
            // All filters operate on the single main stream; no extra buffersrc needed.
            if let FilterStep::Glow {
                threshold,
                radius,
                intensity,
            } = step
            {
                prev_ctx = match add_glow_step(graph, prev_ctx, *threshold, *radius, *intensity, i)
                {
                    Ok(ctx) => ctx,
                    Err(e) => bail!(e),
                };
                continue;
            }

            // FeatherMask — compound step: split → alphaextract → gblur → alphamerge.
            // All filters operate on the single main stream; no extra buffersrc needed.
            if let FilterStep::FeatherMask { radius } = step {
                prev_ctx = match add_feather_mask_step(graph, prev_ctx, *radius, i) {
                    Ok(ctx) => ctx,
                    Err(e) => bail!(e),
                };
                continue;
            }

            // AlphaMatte — compound step: matte pipeline applied to in1, then
            // alphamerge links main video (pad 0) with the matte (pad 1).
            if let FilterStep::AlphaMatte { matte } = step {
                let Some(matte_src) = src_ctxs.get(1).and_then(|o| *o) else {
                    bail!(FilterError::BuildFailed)
                };
                prev_ctx = match add_alphamerge_step(
                    graph,
                    prev_ctx,
                    matte_src.as_ptr(),
                    matte.steps(),
                    i,
                ) {
                    Ok(ctx) => ctx,
                    Err(e) => bail!(e),
                };
                continue;
            }

            // Blend (Normal / PorterDuffOver) — both use overlay=format=auto:shortest=1.
            //   prev → [bottom]overlay=format=auto:shortest=1 ← [top][ccm]
            // where [top] is in1 with the top builder's steps applied.
            // Unimplemented modes are caught by build() before reaching here.
            if let FilterStep::Blend {
                top,
                mode: BlendMode::Normal | BlendMode::PorterDuffOver,
                opacity,
            } = step
            {
                let Some(top_src) = src_ctxs.get(1).and_then(|o| *o) else {
                    bail!(FilterError::BuildFailed)
                };
                prev_ctx = match add_blend_normal_step(
                    graph,
                    prev_ctx,
                    top_src.as_ptr(),
                    top.steps(),
                    *opacity,
                    i,
                ) {
                    Ok(ctx) => ctx,
                    Err(e) => bail!(e),
                };
                continue;
            }

            // Blend (photographic modes: Multiply, Screen, Overlay, SoftLight, HardLight)
            if let FilterStep::Blend {
                top,
                mode:
                    mode @ (BlendMode::Multiply
                    | BlendMode::Screen
                    | BlendMode::Overlay
                    | BlendMode::SoftLight
                    | BlendMode::HardLight
                    | BlendMode::ColorDodge
                    | BlendMode::ColorBurn
                    | BlendMode::Darken
                    | BlendMode::Lighten
                    | BlendMode::Difference
                    | BlendMode::Exclusion
                    | BlendMode::Add
                    | BlendMode::Subtract
                    | BlendMode::Hue
                    | BlendMode::Saturation
                    | BlendMode::Color
                    | BlendMode::Luminosity),
                opacity,
            } = step
            {
                let Some(top_src) = src_ctxs.get(1).and_then(|o| *o) else {
                    bail!(FilterError::BuildFailed)
                };
                let mode_name = match mode {
                    BlendMode::Multiply => "multiply",
                    BlendMode::Screen => "screen",
                    BlendMode::Overlay => "overlay",
                    BlendMode::SoftLight => "softlight",
                    BlendMode::HardLight => "hardlight",
                    BlendMode::ColorDodge => "dodge",
                    BlendMode::ColorBurn => "burn",
                    BlendMode::Darken => "darken",
                    BlendMode::Lighten => "lighten",
                    BlendMode::Difference => "difference",
                    BlendMode::Exclusion => "exclusion",
                    BlendMode::Add => "addition",
                    BlendMode::Subtract => "subtract",
                    BlendMode::Hue => "hue",
                    BlendMode::Saturation => "saturation",
                    BlendMode::Color => "color",
                    BlendMode::Luminosity => "luminosity",
                    _ => unreachable!(),
                };
                prev_ctx = match add_blend_photographic_step(
                    graph,
                    prev_ctx,
                    top_src.as_ptr(),
                    top.steps(),
                    mode_name,
                    *opacity,
                    i,
                ) {
                    Ok(ctx) => ctx,
                    Err(e) => bail!(e),
                };
                continue;
            }

            // Blend (PorterDuffUnder) — overlay with swapped input order.
            if let FilterStep::Blend {
                top,
                mode: BlendMode::PorterDuffUnder,
                opacity,
            } = step
            {
                let Some(top_src) = src_ctxs.get(1).and_then(|o| *o) else {
                    bail!(FilterError::BuildFailed)
                };
                prev_ctx = match add_blend_under_step(
                    graph,
                    prev_ctx,
                    top_src.as_ptr(),
                    top.steps(),
                    *opacity,
                    i,
                ) {
                    Ok(ctx) => ctx,
                    Err(e) => bail!(e),
                };
                continue;
            }

            // Blend (PorterDuffIn / PorterDuffOut / PorterDuffAtop / PorterDuffXor) — expression-based blend.
            if let FilterStep::Blend {
                top,
                mode:
                    mode @ (BlendMode::PorterDuffIn
                    | BlendMode::PorterDuffOut
                    | BlendMode::PorterDuffAtop
                    | BlendMode::PorterDuffXor),
                ..
            } = step
            {
                let Some(top_src) = src_ctxs.get(1).and_then(|o| *o) else {
                    bail!(FilterError::BuildFailed)
                };
                let expr = match mode {
                    BlendMode::PorterDuffIn => "B*A/255",
                    BlendMode::PorterDuffOut => "B*(255-A)/255",
                    BlendMode::PorterDuffAtop => "B*A/255 + A*(255-B)/255",
                    BlendMode::PorterDuffXor => "B*(255-A)/255 + A*(255-B)/255",
                    _ => unreachable!(),
                };
                prev_ctx = match add_blend_expr_step(
                    graph,
                    prev_ctx,
                    top_src.as_ptr(),
                    top.steps(),
                    expr,
                    i,
                ) {
                    Ok(ctx) => ctx,
                    Err(e) => bail!(e),
                };
                continue;
            }

            // Animated filter steps use type-specific node names ("crop_N",
            // "gblur_N") so that avfilter_graph_send_command targets the correct
            // context in #363.  The per-type index is the count of preceding steps
            // of the same animated type.
            let (step_prefix, step_index): (&str, usize) = match step {
                FilterStep::CropAnimated { .. } => {
                    let n = steps[..i]
                        .iter()
                        .filter(|s| matches!(s, FilterStep::CropAnimated { .. }))
                        .count();
                    ("crop_", n)
                }
                FilterStep::GBlurAnimated { .. } => {
                    let n = steps[..i]
                        .iter()
                        .filter(|s| matches!(s, FilterStep::GBlurAnimated { .. }))
                        .count();
                    ("gblur_", n)
                }
                FilterStep::EqAnimated { .. } => {
                    let n = steps[..i]
                        .iter()
                        .filter(|s| matches!(s, FilterStep::EqAnimated { .. }))
                        .count();
                    ("eq_", n)
                }
                FilterStep::ColorBalanceAnimated { .. } => {
                    let n = steps[..i]
                        .iter()
                        .filter(|s| matches!(s, FilterStep::ColorBalanceAnimated { .. }))
                        .count();
                    ("colorbalance_", n)
                }
                _ => ("step", i),
            };
            prev_ctx = match add_and_link_step(graph, prev_ctx, step, step_index, step_prefix) {
                Ok(ctx) => ctx,
                Err(e) => bail!(e),
            };

            // After trim, insert setpts=PTS-STARTPTS so the output timestamps
            // are reset to start at zero.
            if matches!(step, FilterStep::Trim { .. }) {
                prev_ctx = match add_setpts_after_trim(graph, prev_ctx, i) {
                    Ok(ctx) => ctx,
                    Err(e) => bail!(e),
                };
            }

            // FitToAspect is a compound step: the scale filter (added by
            // add_and_link_step above) preserves the source aspect ratio; the
            // pad filter added here centres the scaled frame on the target canvas.
            if let FilterStep::FitToAspect {
                width,
                height,
                color,
            } = step
            {
                prev_ctx = match add_fit_to_aspect_pad(graph, prev_ctx, *width, *height, color, i) {
                    Ok(ctx) => ctx,
                    Err(e) => bail!(e),
                };
            }

            // LumaKey with invert=true appends a geq filter that negates the
            // alpha channel, turning the "key out pixels matching threshold"
            // effect into "key out the complementary region".
            if let FilterStep::LumaKey { invert: true, .. } = step {
                prev_ctx = match add_raw_filter_step(
                    graph,
                    prev_ctx,
                    "geq",
                    "r='r(X,Y)':g='g(X,Y)':b='b(X,Y)':a='255-alpha(X,Y)'",
                    i,
                    "geqluma",
                ) {
                    Ok(ctx) => ctx,
                    Err(e) => bail!(e),
                };
            }

            // Overlay and xfade both consume a second input on pad 1.
            if matches!(step, FilterStep::Overlay { .. } | FilterStep::XFade { .. })
                && let Some(Some(extra_src)) = src_ctxs.get(1)
            {
                let ret = ff_sys::avfilter_link(extra_src.as_ptr(), 0, prev_ctx, 1);
                if ret < 0 {
                    bail!(FilterError::BuildFailed);
                }
                log::debug!("filter linked extra_input=in1 to two-input filter pad=1");
            }

            // ConcatVideo consumes n input pads; link src_ctxs[1..n-1] to pads 1..n-1.
            if let FilterStep::ConcatVideo { n } = step {
                for slot in 1..*n as usize {
                    if let Some(Some(extra_src)) = src_ctxs.get(slot) {
                        let ret =
                            ff_sys::avfilter_link(extra_src.as_ptr(), 0, prev_ctx, slot as u32);
                        if ret < 0 {
                            bail!(FilterError::BuildFailed);
                        }
                        log::debug!("filter linked extra_input=in{slot} to concat pad={slot}");
                    }
                }
            }
        }

        // 6. Insert hwdownload AFTER all filter steps so output frames are
        //    downloaded back to system memory before reaching the buffersink.
        if let Some(hw_ctx) = hw_device_ctx {
            prev_ctx =
                match create_hw_filter(graph, prev_ctx, c"hwdownload", c"hwdownload0", hw_ctx) {
                    Ok(ctx) => ctx,
                    Err(e) => bail!(e),
                };
        }

        // Link last filter to sink.
        let ret = ff_sys::avfilter_link(prev_ctx, 0, sink_ctx, 0);
        if ret < 0 {
            bail!(FilterError::BuildFailed);
        }

        // 7. Configure the graph.
        let ret = ff_sys::avfilter_graph_config(graph, std::ptr::null_mut());
        if ret < 0 {
            log::warn!("avfilter_graph_config failed code={ret}");
            // If there is a crop step the most likely cause is the rectangle
            // extending beyond the source frame dimensions.
            let crop_info = steps.iter().find_map(|s| match s {
                FilterStep::Crop {
                    x,
                    y,
                    width,
                    height,
                } => Some((
                    f64::from(*x),
                    f64::from(*y),
                    f64::from(*width),
                    f64::from(*height),
                )),
                FilterStep::CropAnimated {
                    x,
                    y,
                    width,
                    height,
                } => Some((
                    x.value_at(Duration::ZERO),
                    y.value_at(Duration::ZERO),
                    width.value_at(Duration::ZERO),
                    height.value_at(Duration::ZERO),
                )),
                _ => None,
            });
            if let Some((x, y, w, h)) = crop_info {
                bail!(FilterError::InvalidConfig {
                    reason: format!("crop rect {x},{y}+{w}x{h} exceeds source frame dimensions"),
                });
            }
            bail!(ffmpeg_err(ret));
        }

        // SAFETY: `avfilter_graph_create_filter` with ret >= 0 guarantees
        // non-null pointers.
        let sink_nn = NonNull::new_unchecked(sink_ctx);
        Ok((src_ctxs, sink_nn, hw_device_ctx))
    }

    /// Build the `AVFilterGraph` for audio, returning `(src_ctxs, asink_ctx)`.
    ///
    /// # Safety
    ///
    /// `graph_nn` must be a valid, freshly-allocated `AVFilterGraph`.
    pub(super) unsafe fn build_audio_graph(
        graph_nn: NonNull<ff_sys::AVFilterGraph>,
        buffersrc_args: &str,
        num_inputs: usize,
        steps: &[FilterStep],
        _hw: Option<&HwAccel>,
    ) -> BuildResult {
        let graph = graph_nn.as_ptr();
        let mut src_ctxs = Vec::with_capacity(num_inputs);

        // 1. Create abuffer sources, one per input slot.
        let abuffer = ff_sys::avfilter_get_by_name(c"abuffer".as_ptr());
        if abuffer.is_null() {
            return Err(FilterError::BuildFailed);
        }
        let src_args =
            std::ffi::CString::new(buffersrc_args).map_err(|_| FilterError::BuildFailed)?;

        // First input slot.
        let first_src_ctx = {
            let mut raw_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
            let ret = ff_sys::avfilter_graph_create_filter(
                &raw mut raw_ctx,
                abuffer,
                c"in0".as_ptr(),
                src_args.as_ptr(),
                std::ptr::null_mut(),
                graph,
            );
            if ret < 0 {
                return Err(FilterError::BuildFailed);
            }
            log::debug!("filter added name=abuffersrc slot=0");
            // SAFETY: ret >= 0 means raw_ctx is non-null.
            let nn = NonNull::new_unchecked(raw_ctx);
            src_ctxs.push(Some(nn));
            nn
        };

        // Additional input slots for amix.
        for slot in 1..num_inputs {
            let ctx_name = std::ffi::CString::new(format!("in{slot}"))
                .map_err(|_| FilterError::BuildFailed)?;
            let mut raw_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
            let ret = ff_sys::avfilter_graph_create_filter(
                &raw mut raw_ctx,
                abuffer,
                ctx_name.as_ptr(),
                src_args.as_ptr(),
                std::ptr::null_mut(),
                graph,
            );
            if ret < 0 {
                return Err(FilterError::BuildFailed);
            }
            log::debug!("filter added name=abuffersrc slot={slot}");
            // SAFETY: ret >= 0 means raw_ctx is non-null.
            src_ctxs.push(Some(NonNull::new_unchecked(raw_ctx)));
        }

        // 2. Create abuffersink.
        let abuffersink = ff_sys::avfilter_get_by_name(c"abuffersink".as_ptr());
        if abuffersink.is_null() {
            return Err(FilterError::BuildFailed);
        }

        let mut sink_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
        let ret = ff_sys::avfilter_graph_create_filter(
            &raw mut sink_ctx,
            abuffersink,
            c"aout".as_ptr(),
            std::ptr::null_mut(),
            std::ptr::null_mut(),
            graph,
        );
        if ret < 0 {
            return Err(FilterError::BuildFailed);
        }

        // 3-5. Add each `FilterStep` (audio-relevant steps) and link.
        let mut prev_ctx = first_src_ctx.as_ptr();
        for (i, step) in steps.iter().enumerate() {
            // Video-only steps; skip them in the audio graph.
            if matches!(
                step,
                FilterStep::Reverse
                    | FilterStep::ConcatVideo { .. }
                    | FilterStep::JoinWithDissolve { .. }
                    | FilterStep::Blend { .. }
            ) {
                continue;
            }

            // LoudnessNormalize is handled via two-pass buffering in
            // push_audio / pull_audio; the regular audio graph is never built
            // when this step is present, so this guard is defensive only.
            if matches!(step, FilterStep::LoudnessNormalize { .. }) {
                continue;
            }

            // NormalizePeak is handled via two-pass buffering in
            // push_audio / pull_audio; same reasoning as LoudnessNormalize.
            if matches!(step, FilterStep::NormalizePeak { .. }) {
                continue;
            }

            // Speed uses `setpts` for video but `atempo` for audio.  Bypass the
            // standard `add_and_link_step` path and insert the atempo chain here.
            if let FilterStep::Speed { factor } = step {
                prev_ctx = add_atempo_chain(graph, prev_ctx, *factor, i)?;
                continue;
            }

            // ParametricEq generates one filter node per band; bypass the
            // single-node `add_and_link_step` path.
            if let FilterStep::ParametricEq { bands } = step {
                prev_ctx = add_parametric_eq_chain(graph, prev_ctx, bands, i)?;
                continue;
            }

            // ReverbIr — compound step: amovie[+adelay] → afir.
            // amovie is a self-contained audio source; no buffersrc slot is consumed.
            if let FilterStep::ReverbIr {
                ir_path,
                wet,
                dry,
                pre_delay_ms,
            } = step
            {
                prev_ctx =
                    add_reverb_ir_step(graph, prev_ctx, ir_path, *wet, *dry, *pre_delay_ms, i)?;
                continue;
            }

            // TimeStretch — uses the same atempo chain as the audio path of Speed,
            // but without any video setpts change.
            if let FilterStep::TimeStretch { factor } = step {
                prev_ctx = add_atempo_chain(graph, prev_ctx, f64::from(*factor), i)?;
                continue;
            }

            // SpeedChange — asetrate only (speed and pitch change together).
            // The sample rate is resolved from buffersrc_args so the integer
            // value is substituted literally into the filter args.
            if let FilterStep::SpeedChange { factor } = step {
                let sr = parse_sample_rate_from_buffersrc(buffersrc_args);
                #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
                let new_sr = (f64::from(sr) * factor).round() as u64;
                // SAFETY: graph and prev_ctx are valid pointers in the same graph.
                prev_ctx = add_raw_filter_step(
                    graph,
                    prev_ctx,
                    "asetrate",
                    &format!("r={new_sr}"),
                    i,
                    "speed_asetrate",
                )?;
                continue;
            }

            // PitchShift — compound step: asetrate → atempo.
            // asetrate changes the declared sample rate (shifting pitch); atempo
            // restores the original duration.  The actual sample rate is resolved
            // from buffersrc_args so the integer value is substituted literally.
            if let FilterStep::PitchShift { semitones } = step {
                let rate = 2f64.powf(f64::from(*semitones) / 12.0);
                let sr = parse_sample_rate_from_buffersrc(buffersrc_args);
                #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
                let new_sr = (f64::from(sr) * rate).round() as u64;
                let atempo = 1.0 / rate;
                // SAFETY: graph and prev_ctx are valid pointers in the same graph.
                prev_ctx = add_raw_filter_step(
                    graph,
                    prev_ctx,
                    "asetrate",
                    &format!("r={new_sr}"),
                    i,
                    "pitch_asetrate",
                )?;
                prev_ctx = add_raw_filter_step(
                    graph,
                    prev_ctx,
                    "atempo",
                    &format!("{atempo:.6}"),
                    i,
                    "pitch_atempo",
                )?;
                continue;
            }

            // Duck — two-input compound step: sidechaincompress.
            // Slot 0 = background (main signal, gets ducked); slot 1 = foreground (sidechain).
            if let FilterStep::Duck {
                threshold_linear,
                ratio,
                attack_ms,
                release_ms,
            } = step
            {
                let side_ctx = src_ctxs
                    .get(1)
                    .and_then(|s| *s)
                    .ok_or(FilterError::BuildFailed)?
                    .as_ptr();
                // SAFETY: graph, prev_ctx, and side_ctx are valid pointers in the same graph.
                prev_ctx = add_sidechain_compress_step(
                    graph,
                    prev_ctx,
                    side_ctx,
                    &DuckArgs {
                        threshold_linear: *threshold_linear,
                        ratio: *ratio,
                        attack_ms: *attack_ms,
                        release_ms: *release_ms,
                    },
                    i,
                )?;
                continue;
            }

            // AudioDelay dispatches to adelay (positive/zero) or atrim (negative).
            if let FilterStep::AudioDelay { ms } = step {
                let (filter_name, args) = if *ms >= 0.0 {
                    ("adelay".to_string(), format!("delays={ms}:all=1"))
                } else {
                    ("atrim".to_string(), format!("start={}", -ms / 1000.0))
                };
                // SAFETY: graph and prev_ctx are valid pointers in the same graph.
                prev_ctx = add_raw_filter_step(graph, prev_ctx, &filter_name, &args, i, "adelay")?;
                continue;
            }

            prev_ctx = add_and_link_step(graph, prev_ctx, step, i, "astep")?;

            // ConcatAudio consumes n input pads; link src_ctxs[1..n-1] to pads 1..n-1.
            if let FilterStep::ConcatAudio { n } = step {
                for slot in 1..*n as usize {
                    if let Some(Some(extra_src)) = src_ctxs.get(slot) {
                        let ret =
                            ff_sys::avfilter_link(extra_src.as_ptr(), 0, prev_ctx, slot as u32);
                        if ret < 0 {
                            return Err(FilterError::BuildFailed);
                        }
                        log::debug!("filter linked extra_input=in{slot} to concat pad={slot}");
                    }
                }
            }
        }

        // Link last filter to sink.
        let ret = ff_sys::avfilter_link(prev_ctx, 0, sink_ctx, 0);
        if ret < 0 {
            return Err(FilterError::BuildFailed);
        }

        // 6. Configure the graph.
        let ret = ff_sys::avfilter_graph_config(graph, std::ptr::null_mut());
        if ret < 0 {
            log::warn!("avfilter_graph_config failed code={ret}");
            return Err(ffmpeg_err(ret));
        }

        // SAFETY: sink_ctx is non-null (ret >= 0 above).
        let sink_nn = NonNull::new_unchecked(sink_ctx);
        Ok((src_ctxs, sink_nn))
    }
}

// ── ReverbIr compound step ────────────────────────────────────────────────────
/// Insert the convolution reverb compound step.
///
/// ```text
/// prev_ctx ──────────────────────────────────────────→ afir[0]
/// amovie(ir_path) [→ adelay(pre_delay_ms)] ─────────→ afir[1]
///                                           afir(dry, wet) → out
/// ```
///
/// `amovie` is a self-contained audio source — no buffersrc input slot is consumed.
///
/// # Safety
///
/// `graph` and `prev_ctx` must be valid pointers owned by the same
/// `AVFilterGraph`.
pub(super) unsafe fn add_reverb_ir_step(
    graph: *mut ff_sys::AVFilterGraph,
    prev_ctx: *mut ff_sys::AVFilterContext,
    ir_path: &str,
    wet: f32,
    dry: f32,
    pre_delay_ms: u32,
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    use std::ffi::CString;

    let wet = wet.clamp(0.0, 1.0);
    let dry = dry.clamp(0.0, 1.0);
    let delay = pre_delay_ms.min(500);

    // 1. amovie=filename={ir_path} — self-contained IR source.
    let amovie_filter = ff_sys::avfilter_get_by_name(c"amovie".as_ptr());
    if amovie_filter.is_null() {
        log::warn!("filter not found name=amovie (reverb_ir)");
        return Err(FilterError::BuildFailed);
    }
    let amovie_name =
        CString::new(format!("reverb_amovie{index}")).map_err(|_| FilterError::BuildFailed)?;
    let amovie_args_str = format!("filename={ir_path}");
    let amovie_args =
        CString::new(amovie_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;
    let mut amovie_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    // SAFETY: amovie_filter and graph are non-null; args are valid null-terminated C strings.
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut amovie_ctx,
        amovie_filter,
        amovie_name.as_ptr(),
        amovie_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!(
            "filter creation failed name=amovie args={amovie_args_str} (reverb_ir) code={ret}"
        );
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=amovie args={amovie_args_str} index={index} (reverb_ir)");

    // 2. [optional] adelay={delay}:all=1 — pre-delay on the IR path.
    let ir_out_ctx = if delay > 0 {
        let adelay_filter = ff_sys::avfilter_get_by_name(c"adelay".as_ptr());
        if adelay_filter.is_null() {
            log::warn!("filter not found name=adelay (reverb_ir)");
            return Err(FilterError::BuildFailed);
        }
        let adelay_name =
            CString::new(format!("reverb_adelay{index}")).map_err(|_| FilterError::BuildFailed)?;
        let adelay_args_str = format!("delays={delay}:all=1");
        let adelay_args =
            CString::new(adelay_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;
        let mut adelay_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
        let ret = ff_sys::avfilter_graph_create_filter(
            &raw mut adelay_ctx,
            adelay_filter,
            adelay_name.as_ptr(),
            adelay_args.as_ptr(),
            std::ptr::null_mut(),
            graph,
        );
        if ret < 0 {
            log::warn!(
                "filter creation failed name=adelay args={adelay_args_str} (reverb_ir) code={ret}"
            );
            return Err(FilterError::BuildFailed);
        }
        log::debug!("filter added name=adelay args={adelay_args_str} index={index} (reverb_ir)");
        // SAFETY: amovie_ctx and adelay_ctx belong to the same graph; pad indices valid.
        let ret = ff_sys::avfilter_link(amovie_ctx, 0, adelay_ctx, 0);
        if ret < 0 {
            return Err(FilterError::BuildFailed);
        }
        adelay_ctx
    } else {
        amovie_ctx
    };

    // 3. afir=dry={dry}:wet={wet} — convolution reverb.
    let afir_filter = ff_sys::avfilter_get_by_name(c"afir".as_ptr());
    if afir_filter.is_null() {
        log::warn!("filter not found name=afir (reverb_ir)");
        return Err(FilterError::BuildFailed);
    }
    let afir_name =
        CString::new(format!("reverb_afir{index}")).map_err(|_| FilterError::BuildFailed)?;
    let afir_args_str = format!("dry={dry}:wet={wet}");
    let afir_args = CString::new(afir_args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;
    let mut afir_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut afir_ctx,
        afir_filter,
        afir_name.as_ptr(),
        afir_args.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!("filter creation failed name=afir args={afir_args_str} (reverb_ir) code={ret}");
        return Err(FilterError::BuildFailed);
    }
    log::debug!("filter added name=afir args={afir_args_str} index={index} (reverb_ir)");

    // Link: prev_ctx → afir[0] (dry audio input).
    // SAFETY: prev_ctx and afir_ctx belong to the same graph; pad indices valid.
    let ret = ff_sys::avfilter_link(prev_ctx, 0, afir_ctx, 0);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    // Link: ir_out_ctx → afir[1] (impulse response input).
    let ret = ff_sys::avfilter_link(ir_out_ctx, 0, afir_ctx, 1);
    if ret < 0 {
        return Err(FilterError::BuildFailed);
    }

    log::debug!(
        "filter reverb_ir expanded ir_path={ir_path} wet={wet} dry={dry} pre_delay_ms={pre_delay_ms} index={index}"
    );
    Ok(afir_ctx)
}

// ── SidechainCompress compound step ──────────────────────────────────────────

/// Parameters for `add_sidechain_compress_step`.
pub(super) struct DuckArgs {
    pub threshold_linear: f32,
    pub ratio: f32,
    pub attack_ms: f32,
    pub release_ms: f32,
}

/// Wire the two-input `sidechaincompress` filter for audio ducking.
///
/// ```text
/// main_ctx (background, slot 0) ──→ sidechaincompress[0]
/// side_ctx (foreground, slot 1) ──→ sidechaincompress[1]
///                                   sidechaincompress → out
/// ```
///
/// # Safety
///
/// `graph`, `main_ctx`, and `side_ctx` must be valid pointers owned by the
/// same `AVFilterGraph`.
pub(super) unsafe fn add_sidechain_compress_step(
    graph: *mut ff_sys::AVFilterGraph,
    main_ctx: *mut ff_sys::AVFilterContext,
    side_ctx: *mut ff_sys::AVFilterContext,
    args: &DuckArgs,
    index: usize,
) -> Result<*mut ff_sys::AVFilterContext, FilterError> {
    use std::ffi::CString;

    let DuckArgs {
        threshold_linear,
        ratio,
        attack_ms,
        release_ms,
    } = args;

    let filter = ff_sys::avfilter_get_by_name(c"sidechaincompress".as_ptr());
    if filter.is_null() {
        log::warn!("filter not found name=sidechaincompress (duck)");
        return Err(FilterError::BuildFailed);
    }

    let name = CString::new(format!("duck{index}")).map_err(|_| FilterError::BuildFailed)?;
    let args_str = format!(
        "threshold={threshold_linear}:ratio={ratio}:attack={attack_ms}:release={release_ms}"
    );
    let cargs = CString::new(args_str.as_str()).map_err(|_| FilterError::BuildFailed)?;

    let mut sc_ctx: *mut ff_sys::AVFilterContext = std::ptr::null_mut();
    // SAFETY: filter and graph are non-null; args are valid null-terminated C strings.
    let ret = ff_sys::avfilter_graph_create_filter(
        &raw mut sc_ctx,
        filter,
        name.as_ptr(),
        cargs.as_ptr(),
        std::ptr::null_mut(),
        graph,
    );
    if ret < 0 {
        log::warn!(
            "filter creation failed name=sidechaincompress args={args_str} code={ret} (duck)"
        );
        return Err(ffmpeg_err(ret));
    }
    log::debug!("filter added name=sidechaincompress args={args_str} index={index} (duck)");

    // Link main signal → input pad 0 (the stream to be compressed).
    // SAFETY: main_ctx and sc_ctx belong to the same graph; pad indices are valid.
    let ret = ff_sys::avfilter_link(main_ctx, 0, sc_ctx, 0);
    if ret < 0 {
        log::warn!("avfilter_link failed main→sidechaincompress[0] code={ret}");
        return Err(ffmpeg_err(ret));
    }

    // Link sidechain signal → input pad 1 (the trigger signal).
    // SAFETY: side_ctx and sc_ctx belong to the same graph; pad indices are valid.
    let ret = ff_sys::avfilter_link(side_ctx, 0, sc_ctx, 1);
    if ret < 0 {
        log::warn!("avfilter_link failed sidechain→sidechaincompress[1] code={ret}");
        return Err(ffmpeg_err(ret));
    }

    log::debug!(
        "filter duck expanded threshold_linear={threshold_linear} ratio={ratio} \
         attack_ms={attack_ms} release_ms={release_ms} index={index}"
    );
    Ok(sc_ctx)
}