aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 55ad7fa8acad8044a3436bb3322e07e278c98e1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
extern "C" {
#include "../include/capture/nvfbc.h"
#include "../include/capture/xcomposite.h"
#include "../include/capture/kms.h"
#ifdef GSR_PORTAL
#include "../include/capture/portal.h"
#include "../include/dbus.h"
#endif
#ifdef GSR_APP_AUDIO
#include "../include/pipewire_audio.h"
#endif
#include "../include/encoder/video/nvenc.h"
#include "../include/encoder/video/vaapi.h"
#include "../include/encoder/video/vulkan.h"
#include "../include/encoder/video/software.h"
#include "../include/codec_query/nvenc.h"
#include "../include/codec_query/vaapi.h"
#include "../include/codec_query/vulkan.h"
#include "../include/egl.h"
#include "../include/utils.h"
#include "../include/damage.h"
#include "../include/color_conversion.h"
}

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <unordered_map>
#include <thread>
#include <mutex>
#include <map>
#include <signal.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/wait.h>
#include <inttypes.h>
#include <libgen.h>

#include "../include/sound.hpp"

extern "C" {
#include <libavutil/pixfmt.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
#include <libswresample/swresample.h>
#include <libavutil/avutil.h>
#include <libavutil/time.h>
#include <libavutil/mastering_display_metadata.h>
#include <libavfilter/avfilter.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
}

#include <deque>
#include <future>

#ifndef GSR_VERSION
#define GSR_VERSION "unknown"
#endif

// TODO: If options are not supported then they are returned (allocated) in the options. This should be free'd.

// TODO: Remove LIBAVUTIL_VERSION_MAJOR checks in the future when ubuntu, pop os LTS etc update ffmpeg to >= 5.0

static const int AUDIO_SAMPLE_RATE = 48000;

static const int VIDEO_STREAM_INDEX = 0;

static thread_local char av_error_buffer[AV_ERROR_MAX_STRING_SIZE];

static void monitor_output_callback_print(const gsr_monitor *monitor, void *userdata) {
    (void)userdata;
    fprintf(stderr, "    \"%.*s\"    (%dx%d+%d+%d)\n", monitor->name_len, monitor->name, monitor->size.x, monitor->size.y, monitor->pos.x, monitor->pos.y);
}

typedef struct {
    const char *output_name;
} FirstOutputCallback;

static void get_first_output(const gsr_monitor *monitor, void *userdata) {
    FirstOutputCallback *first_output = (FirstOutputCallback*)userdata;
    if(!first_output->output_name)
        first_output->output_name = strndup(monitor->name, monitor->name_len + 1);
}

static char* av_error_to_string(int err) {
    if(av_strerror(err, av_error_buffer, sizeof(av_error_buffer)) < 0)
        strcpy(av_error_buffer, "Unknown error");
    return av_error_buffer;
}

enum class VideoQuality {
    MEDIUM,
    HIGH,
    VERY_HIGH,
    ULTRA
};

enum class VideoCodec {
    H264,
    HEVC,
    HEVC_HDR,
    HEVC_10BIT,
    AV1,
    AV1_HDR,
    AV1_10BIT,
    VP8,
    VP9,
    H264_VULKAN,
    HEVC_VULKAN
};

enum class AudioCodec {
    AAC,
    OPUS,
    FLAC
};

enum class PixelFormat {
    YUV420,
    YUV444
};

enum class FramerateMode {
    CONSTANT,
    VARIABLE,
    CONTENT
};

enum class BitrateMode {
    QP,
    VBR,
    CBR
};

static int x11_error_handler(Display*, XErrorEvent*) {
    return 0;
}

static int x11_io_error_handler(Display*) {
    return 0;
}

static bool video_codec_is_hdr(VideoCodec video_codec) {
    // TODO: Vulkan
    switch(video_codec) {
        case VideoCodec::HEVC_HDR:
        case VideoCodec::AV1_HDR:
            return true;
        default:
            return false;
    }
}

static VideoCodec hdr_video_codec_to_sdr_video_codec(VideoCodec video_codec) {
    // TODO: Vulkan
    switch(video_codec) {
        case VideoCodec::HEVC_HDR:
            return VideoCodec::HEVC;
        case VideoCodec::AV1_HDR:
            return VideoCodec::AV1;
        default:
            return video_codec;
    }
}

static gsr_color_depth video_codec_to_bit_depth(VideoCodec video_codec) {
    // TODO: Vulkan
    switch(video_codec) {
        case VideoCodec::HEVC_HDR:
        case VideoCodec::HEVC_10BIT:
        case VideoCodec::AV1_HDR:
        case VideoCodec::AV1_10BIT:
            return GSR_COLOR_DEPTH_10_BITS;
        default:
            return GSR_COLOR_DEPTH_8_BITS;
    }
}

// static bool video_codec_is_hevc(VideoCodec video_codec) {
// TODO: Vulkan
//     switch(video_codec) {
//         case VideoCodec::HEVC:
//         case VideoCodec::HEVC_HDR:
//         case VideoCodec::HEVC_10BIT:
//             return true;
//         default:
//             return false;
//     }
// }

static bool video_codec_is_av1(VideoCodec video_codec) {
    // TODO: Vulkan
    switch(video_codec) {
        case VideoCodec::AV1:
        case VideoCodec::AV1_HDR:
        case VideoCodec::AV1_10BIT:
            return true;
        default:
            return false;
    }
}

static bool video_codec_is_vulkan(VideoCodec video_codec) {
    switch(video_codec) {
        case VideoCodec::H264_VULKAN:
        case VideoCodec::HEVC_VULKAN:
            return true;
        default:
            return false;
    }
}

struct PacketData {
    PacketData() {}
    PacketData(const PacketData&) = delete;
    PacketData& operator=(const PacketData&) = delete;

    ~PacketData() {
        av_free(data.data);
    }

    AVPacket data;
};

// |stream| is only required for non-replay mode
static void receive_frames(AVCodecContext *av_codec_context, int stream_index, AVStream *stream, int64_t pts,
                           AVFormatContext *av_format_context,
                           double replay_start_time,
                           std::deque<std::shared_ptr<PacketData>> &frame_data_queue,
                           int replay_buffer_size_secs,
                           bool &frames_erased,
                           std::mutex &write_output_mutex,
                           double paused_time_offset) {
    for (;;) {
        AVPacket *av_packet = av_packet_alloc();
        if(!av_packet)
            break;

        av_packet->data = NULL;
        av_packet->size = 0;
        int res = avcodec_receive_packet(av_codec_context, av_packet);
        if (res == 0) { // we have a packet, send the packet to the muxer
            av_packet->stream_index = stream_index;
            av_packet->pts = pts;
            av_packet->dts = pts;

            std::lock_guard<std::mutex> lock(write_output_mutex);
            if(replay_buffer_size_secs != -1) {
                // TODO: Preallocate all frames data and use those instead.
                // Why are we doing this you ask? there is a new ffmpeg bug that causes cpu usage to increase over time when you have
                // packets that are not being free'd until later. So we copy the packet data, free the packet and then reconstruct
                // the packet later on when we need it, to keep packets alive only for a short period.
                auto new_packet = std::make_shared<PacketData>();
                new_packet->data = *av_packet;
                new_packet->data.data = (uint8_t*)av_malloc(av_packet->size);
                memcpy(new_packet->data.data, av_packet->data, av_packet->size);

                double time_now = clock_get_monotonic_seconds() - paused_time_offset;
                double replay_time_elapsed = time_now - replay_start_time;

                frame_data_queue.push_back(std::move(new_packet));
                if(replay_time_elapsed >= replay_buffer_size_secs) {
                    frame_data_queue.pop_front();
                    frames_erased = true;
                }
            } else {
                av_packet_rescale_ts(av_packet, av_codec_context->time_base, stream->time_base);
                av_packet->stream_index = stream->index;
                // TODO: Is av_interleaved_write_frame needed?. Answer: might be needed for mkv but dont use it! it causes frames to be inconsistent, skipping frames and duplicating frames
                int ret = av_write_frame(av_format_context, av_packet);
                if(ret < 0) {
                    fprintf(stderr, "Error: Failed to write frame index %d to muxer, reason: %s (%d)\n", av_packet->stream_index, av_error_to_string(ret), ret);
                }
            }
            av_packet_free(&av_packet);
        } else if (res == AVERROR(EAGAIN)) { // we have no packet
                                             // fprintf(stderr, "No packet!\n");
            av_packet_free(&av_packet);
            break;
        } else if (res == AVERROR_EOF) { // this is the end of the stream
            av_packet_free(&av_packet);
            fprintf(stderr, "End of stream!\n");
            break;
        } else {
            av_packet_free(&av_packet);
            fprintf(stderr, "Unexpected error: %d\n", res);
            break;
        }
    }
}

static const char* audio_codec_get_name(AudioCodec audio_codec) {
    switch(audio_codec) {
        case AudioCodec::AAC:  return "aac";
        case AudioCodec::OPUS: return "opus";
        case AudioCodec::FLAC: return "flac";
    }
    assert(false);
    return "";
}

static AVCodecID audio_codec_get_id(AudioCodec audio_codec) {
    switch(audio_codec) {
        case AudioCodec::AAC:  return AV_CODEC_ID_AAC;
        case AudioCodec::OPUS: return AV_CODEC_ID_OPUS;
        case AudioCodec::FLAC: return AV_CODEC_ID_FLAC;
    }
    assert(false);
    return AV_CODEC_ID_AAC;
}

static AVSampleFormat audio_codec_get_sample_format(AVCodecContext *audio_codec_context, AudioCodec audio_codec, const AVCodec *codec, bool mix_audio) {
    (void)audio_codec_context;
    switch(audio_codec) {
        case AudioCodec::AAC: {
            return AV_SAMPLE_FMT_FLTP;
        }
        case AudioCodec::OPUS: {
            bool supports_s16 = false;
            bool supports_flt = false;

            #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(61, 15, 0)
            for(size_t i = 0; codec->sample_fmts && codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; ++i) {
                if(codec->sample_fmts[i] == AV_SAMPLE_FMT_S16) {
                    supports_s16 = true;
                } else if(codec->sample_fmts[i] == AV_SAMPLE_FMT_FLT) {
                    supports_flt = true;
                }
            }
            #else
            const enum AVSampleFormat *sample_fmts = NULL;
            if(avcodec_get_supported_config(audio_codec_context, codec, AV_CODEC_CONFIG_SAMPLE_FORMAT, 0, (const void**)&sample_fmts, NULL) >= 0) {
                if(sample_fmts) {
                    for(size_t i = 0; sample_fmts[i] != AV_SAMPLE_FMT_NONE; ++i) {
                        if(sample_fmts[i] == AV_SAMPLE_FMT_S16) {
                            supports_s16 = true;
                        } else if(sample_fmts[i] == AV_SAMPLE_FMT_FLT) {
                            supports_flt = true;
                        }
                    }
                } else {
                    // What a dumb API. It returns NULL if all formats are supported
                    supports_s16 = true;
                    supports_flt = true;
                }
            }
            #endif

            // Amix only works with float audio
            if(mix_audio)
                supports_s16 = false;

            if(!supports_s16 && !supports_flt) {
                fprintf(stderr, "Warning: opus audio codec is chosen but your ffmpeg version does not support s16/flt sample format and performance might be slightly worse.\n");
                fprintf(stderr, "  You can either rebuild ffmpeg with libopus instead of the built-in opus, use the flatpak version of gpu screen recorder or record with aac audio codec instead (-ac aac).\n");
                fprintf(stderr, "  Falling back to fltp audio sample format instead.\n");
            }

            if(supports_s16)
                return AV_SAMPLE_FMT_S16;
            else if(supports_flt)
                return AV_SAMPLE_FMT_FLT;
            else
                return AV_SAMPLE_FMT_FLTP;
        }
        case AudioCodec::FLAC: {
            return AV_SAMPLE_FMT_S32;
        }
    }
    assert(false);
    return AV_SAMPLE_FMT_FLTP;
}

static int64_t audio_codec_get_get_bitrate(AudioCodec audio_codec) {
    switch(audio_codec) {
        case AudioCodec::AAC:  return 160000;
        case AudioCodec::OPUS: return 128000;
        case AudioCodec::FLAC: return 128000;
    }
    assert(false);
    return 128000;
}

static AudioFormat audio_codec_context_get_audio_format(const AVCodecContext *audio_codec_context) {
    switch(audio_codec_context->sample_fmt) {
        case AV_SAMPLE_FMT_FLT:   return F32;
        case AV_SAMPLE_FMT_FLTP:  return S32;
        case AV_SAMPLE_FMT_S16:   return S16;
        case AV_SAMPLE_FMT_S32:   return S32;
        default:                  return S16;
    }
}

static AVSampleFormat audio_format_to_sample_format(const AudioFormat audio_format) {
    switch(audio_format) {
        case S16:   return AV_SAMPLE_FMT_S16;
        case S32:   return AV_SAMPLE_FMT_S32;
        case F32:   return AV_SAMPLE_FMT_FLT;
    }
    assert(false);
    return AV_SAMPLE_FMT_S16;
}

static AVCodecContext* create_audio_codec_context(int fps, AudioCodec audio_codec, bool mix_audio, int64_t audio_bitrate) {
    (void)fps;
    const AVCodec *codec = avcodec_find_encoder(audio_codec_get_id(audio_codec));
    if (!codec) {
        fprintf(stderr, "Error: Could not find %s audio encoder\n", audio_codec_get_name(audio_codec));
        _exit(1);
    }

    AVCodecContext *codec_context = avcodec_alloc_context3(codec);

    assert(codec->type == AVMEDIA_TYPE_AUDIO);
    codec_context->codec_id = codec->id;
    codec_context->sample_fmt = audio_codec_get_sample_format(codec_context, audio_codec, codec, mix_audio);
    codec_context->bit_rate = audio_bitrate == 0 ? audio_codec_get_get_bitrate(audio_codec) : audio_bitrate;
    codec_context->sample_rate = AUDIO_SAMPLE_RATE;
    if(audio_codec == AudioCodec::AAC)
        codec_context->profile = FF_PROFILE_AAC_LOW;
#if LIBAVCODEC_VERSION_MAJOR < 60
    codec_context->channel_layout = AV_CH_LAYOUT_STEREO;
    codec_context->channels = 2;
#else
    av_channel_layout_default(&codec_context->ch_layout, 2);
#endif

    codec_context->time_base.num = 1;
    codec_context->time_base.den = codec_context->sample_rate;
    codec_context->thread_count = 1;
    codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

    return codec_context;
}

static int vbr_get_quality_parameter(AVCodecContext *codec_context, VideoQuality video_quality, bool hdr) {
    // 8 bit / 10 bit = 80%
    const float qp_multiply = hdr ? 8.0f/10.0f : 1.0f;
    if(codec_context->codec_id == AV_CODEC_ID_AV1) {
        switch(video_quality) {
            case VideoQuality::MEDIUM:
                return 160 * qp_multiply;
            case VideoQuality::HIGH:
                return 130 * qp_multiply;
            case VideoQuality::VERY_HIGH:
                return 110 * qp_multiply;
            case VideoQuality::ULTRA:
                return 90 * qp_multiply;
        }
    } else if(codec_context->codec_id == AV_CODEC_ID_H264) {
        switch(video_quality) {
            case VideoQuality::MEDIUM:
                return 35 * qp_multiply;
            case VideoQuality::HIGH:
                return 30 * qp_multiply;
            case VideoQuality::VERY_HIGH:
                return 25 * qp_multiply;
            case VideoQuality::ULTRA:
                return 22 * qp_multiply;
        }
    } else if(codec_context->codec_id == AV_CODEC_ID_HEVC) {
        switch(video_quality) {
            case VideoQuality::MEDIUM:
                return 35 * qp_multiply;
            case VideoQuality::HIGH:
                return 30 * qp_multiply;
            case VideoQuality::VERY_HIGH:
                return 25 * qp_multiply;
            case VideoQuality::ULTRA:
                return 22 * qp_multiply;
        }
    } else if(codec_context->codec_id == AV_CODEC_ID_VP8 || codec_context->codec_id == AV_CODEC_ID_VP9) {
        switch(video_quality) {
            case VideoQuality::MEDIUM:
                return 35 * qp_multiply;
            case VideoQuality::HIGH:
                return 30 * qp_multiply;
            case VideoQuality::VERY_HIGH:
                return 25 * qp_multiply;
            case VideoQuality::ULTRA:
                return 22 * qp_multiply;
        }
    }
    assert(false);
    return 22 * qp_multiply;
}

static AVCodecContext *create_video_codec_context(AVPixelFormat pix_fmt,
                            VideoQuality video_quality,
                            int fps, const AVCodec *codec, bool low_latency, gsr_gpu_vendor vendor, FramerateMode framerate_mode,
                            bool hdr, gsr_color_range color_range, float keyint, bool use_software_video_encoder, BitrateMode bitrate_mode, VideoCodec video_codec, int64_t bitrate) {

    AVCodecContext *codec_context = avcodec_alloc_context3(codec);

    //double fps_ratio = (double)fps / 30.0;

    assert(codec->type == AVMEDIA_TYPE_VIDEO);
    codec_context->codec_id = codec->id;
    // Timebase: This is the fundamental unit of time (in seconds) in terms
    // of which frame timestamps are represented. For fixed-fps content,
    // timebase should be 1/framerate and timestamp increments should be
    // identical to 1
    codec_context->time_base.num = 1;
    codec_context->time_base.den = framerate_mode == FramerateMode::CONSTANT ? fps : AV_TIME_BASE;
    codec_context->framerate.num = fps;
    codec_context->framerate.den = 1;
    codec_context->sample_aspect_ratio.num = 0;
    codec_context->sample_aspect_ratio.den = 0;
    if(low_latency) {
        codec_context->flags |= (AV_CODEC_FLAG_CLOSED_GOP | AV_CODEC_FLAG_LOW_DELAY);
        codec_context->flags2 |= AV_CODEC_FLAG2_FAST;
        //codec_context->gop_size = std::numeric_limits<int>::max();
        //codec_context->keyint_min = std::numeric_limits<int>::max();
        codec_context->gop_size = fps * keyint;
    } else {
        // High values reduce file size but increases time it takes to seek
        codec_context->gop_size = fps * keyint;
    }
    codec_context->max_b_frames = 0;
    codec_context->pix_fmt = pix_fmt;
    codec_context->color_range = color_range == GSR_COLOR_RANGE_LIMITED ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
    if(hdr) {
        codec_context->color_primaries = AVCOL_PRI_BT2020;
        codec_context->color_trc = AVCOL_TRC_SMPTE2084;
        codec_context->colorspace = AVCOL_SPC_BT2020_NCL;
    } else {
        codec_context->color_primaries = AVCOL_PRI_BT709;
        codec_context->color_trc = AVCOL_TRC_BT709;
        codec_context->colorspace = AVCOL_SPC_BT709;
    }
    //codec_context->chroma_sample_location = AVCHROMA_LOC_CENTER;
    if(codec->id == AV_CODEC_ID_HEVC)
        codec_context->codec_tag = MKTAG('h', 'v', 'c', '1'); // QuickTime on MacOS requires this or the video wont be playable

    if(bitrate_mode == BitrateMode::CBR) {
        codec_context->bit_rate = bitrate;
        codec_context->rc_max_rate = codec_context->bit_rate;
        //codec_context->rc_min_rate = codec_context->bit_rate;
        codec_context->rc_buffer_size = codec_context->bit_rate;//codec_context->bit_rate / 10;
        codec_context->rc_initial_buffer_occupancy = 0;//codec_context->bit_rate;//codec_context->bit_rate * 1000;
    } else if(bitrate_mode == BitrateMode::VBR) {
        const int quality = vbr_get_quality_parameter(codec_context, video_quality, hdr);
        switch(video_quality) {
            case VideoQuality::MEDIUM:
                codec_context->qmin = quality;
                codec_context->qmax = quality;
                codec_context->bit_rate = 100000;//4500000 + (codec_context->width * codec_context->height)*0.75;
                break;
            case VideoQuality::HIGH:
                codec_context->qmin = quality;
                codec_context->qmax = quality;
                codec_context->bit_rate = 100000;//10000000-9000000 + (codec_context->width * codec_context->height)*0.75;
                break;
            case VideoQuality::VERY_HIGH:
                codec_context->qmin = quality;
                codec_context->qmax = quality;
                codec_context->bit_rate = 100000;//10000000-9000000 + (codec_context->width * codec_context->height)*0.75;
                break;
            case VideoQuality::ULTRA:
                codec_context->qmin = quality;
                codec_context->qmax = quality;
                codec_context->bit_rate = 100000;//10000000-9000000 + (codec_context->width * codec_context->height)*0.75;
                break;
        }

        codec_context->rc_max_rate = codec_context->bit_rate;
        //codec_context->rc_min_rate = codec_context->bit_rate;
        codec_context->rc_buffer_size = codec_context->bit_rate;//codec_context->bit_rate / 10;
        codec_context->rc_initial_buffer_occupancy = codec_context->bit_rate;//codec_context->bit_rate * 1000;
    } else {
        //codec_context->rc_buffer_size = 50000 * 1000;
    }
    //codec_context->profile = FF_PROFILE_H264_MAIN;
    if (codec_context->codec_id == AV_CODEC_ID_MPEG1VIDEO)
        codec_context->mb_decision = 2;

    if(!use_software_video_encoder && vendor != GSR_GPU_VENDOR_NVIDIA && bitrate_mode != BitrateMode::CBR) {
        // 8 bit / 10 bit = 80%, and increase it even more
        const float quality_multiply = hdr ? (8.0f/10.0f * 0.7f) : 1.0f;
        if(codec_context->codec_id == AV_CODEC_ID_AV1 || codec_context->codec_id == AV_CODEC_ID_H264 || codec_context->codec_id == AV_CODEC_ID_HEVC) {
            switch(video_quality) {
                case VideoQuality::MEDIUM:
                    codec_context->global_quality = 150 * quality_multiply;
                    break;
                case VideoQuality::HIGH:
                    codec_context->global_quality = 120 * quality_multiply;
                    break;
                case VideoQuality::VERY_HIGH:
                    codec_context->global_quality = 115 * quality_multiply;
                    break;
                case VideoQuality::ULTRA:
                    codec_context->global_quality = 90 * quality_multiply;
                    break;
            }
        } else if(codec_context->codec_id == AV_CODEC_ID_VP8) {
            switch(video_quality) {
                case VideoQuality::MEDIUM:
                    codec_context->global_quality = 35 * quality_multiply;
                    break;
                case VideoQuality::HIGH:
                    codec_context->global_quality = 30 * quality_multiply;
                    break;
                case VideoQuality::VERY_HIGH:
                    codec_context->global_quality = 25 * quality_multiply;
                    break;
                case VideoQuality::ULTRA:
                    codec_context->global_quality = 10 * quality_multiply;
                    break;
            }
        } else if(codec_context->codec_id == AV_CODEC_ID_VP9) {
            switch(video_quality) {
                case VideoQuality::MEDIUM:
                    codec_context->global_quality = 35 * quality_multiply;
                    break;
                case VideoQuality::HIGH:
                    codec_context->global_quality = 30 * quality_multiply;
                    break;
                case VideoQuality::VERY_HIGH:
                    codec_context->global_quality = 25 * quality_multiply;
                    break;
                case VideoQuality::ULTRA:
                    codec_context->global_quality = 10 * quality_multiply;
                    break;
            }
        }
    }

    av_opt_set_int(codec_context->priv_data, "b_ref_mode", 0, 0);
    //av_opt_set_int(codec_context->priv_data, "cbr", true, 0);

    if(vendor != GSR_GPU_VENDOR_NVIDIA) {
        // TODO: More options, better options
        //codec_context->bit_rate = codec_context->width * codec_context->height;
        switch(bitrate_mode) {
            case BitrateMode::QP: {
                if(video_codec_is_vulkan(video_codec))
                    av_opt_set(codec_context->priv_data, "rc_mode", "cqp", 0);
                else if(vendor == GSR_GPU_VENDOR_NVIDIA)
                    av_opt_set(codec_context->priv_data, "rc", "constqp", 0);
                else
                    av_opt_set(codec_context->priv_data, "rc_mode", "CQP", 0);
                break;
            }
            case BitrateMode::VBR: {
                if(video_codec_is_vulkan(video_codec))
                    av_opt_set(codec_context->priv_data, "rc_mode", "vbr", 0);
                else if(vendor == GSR_GPU_VENDOR_NVIDIA)
                    av_opt_set(codec_context->priv_data, "rc", "vbr", 0);
                else
                    av_opt_set(codec_context->priv_data, "rc_mode", "VBR", 0);
                break;
            }
            case BitrateMode::CBR: {
                if(video_codec_is_vulkan(video_codec))
                    av_opt_set(codec_context->priv_data, "rc_mode", "cbr", 0);
                else if(vendor == GSR_GPU_VENDOR_NVIDIA)
                    av_opt_set(codec_context->priv_data, "rc", "cbr", 0);
                else
                    av_opt_set(codec_context->priv_data, "rc_mode", "CBR", 0);
                break;
            }
        }
        //codec_context->global_quality = 4;
        //codec_context->compression_level = 2;
    }

    //av_opt_set(codec_context->priv_data, "bsf", "hevc_metadata=colour_primaries=9:transfer_characteristics=16:matrix_coefficients=9", 0);

    codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

    return codec_context;
}

static void open_audio(AVCodecContext *audio_codec_context) {
    AVDictionary *options = nullptr;
    av_dict_set(&options, "strict", "experimental", 0);

    int ret;
    ret = avcodec_open2(audio_codec_context, audio_codec_context->codec, &options);
    if(ret < 0) {
        fprintf(stderr, "failed to open codec, reason: %s\n", av_error_to_string(ret));
        _exit(1);
    }
}

static AVFrame* create_audio_frame(AVCodecContext *audio_codec_context) {
    AVFrame *frame = av_frame_alloc();
    if(!frame) {
        fprintf(stderr, "failed to allocate audio frame\n");
        _exit(1);
    }

    frame->sample_rate = audio_codec_context->sample_rate;
    frame->nb_samples = audio_codec_context->frame_size;
    frame->format = audio_codec_context->sample_fmt;
#if LIBAVCODEC_VERSION_MAJOR < 60
    frame->channels = audio_codec_context->channels;
    frame->channel_layout = audio_codec_context->channel_layout;
#else
    av_channel_layout_copy(&frame->ch_layout, &audio_codec_context->ch_layout);
#endif

    int ret = av_frame_get_buffer(frame, 0);
    if(ret < 0) {
        fprintf(stderr, "failed to allocate audio data buffers, reason: %s\n", av_error_to_string(ret));
        _exit(1);
    }

    return frame;
}

static void dict_set_profile(AVCodecContext *codec_context, gsr_gpu_vendor vendor, gsr_color_depth color_depth, AVDictionary **options) {
    #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(61, 17, 100)
    if(codec_context->codec_id == AV_CODEC_ID_H264) {
        // TODO: Only for vaapi
        //if(color_depth == GSR_COLOR_DEPTH_10_BITS)
        //    av_dict_set(options, "profile", "high10", 0);
        //else
        av_dict_set(options, "profile", "high", 0);
    } else if(codec_context->codec_id == AV_CODEC_ID_AV1) {
        if(vendor == GSR_GPU_VENDOR_NVIDIA) {
            if(color_depth == GSR_COLOR_DEPTH_10_BITS)
                av_dict_set_int(options, "highbitdepth", 1, 0);
        } else {
            av_dict_set(options, "profile", "main", 0); // TODO: use professional instead?
        }
    } else if(codec_context->codec_id == AV_CODEC_ID_HEVC) {
        if(color_depth == GSR_COLOR_DEPTH_10_BITS)
            av_dict_set(options, "profile", "main10", 0);
        else
            av_dict_set(options, "profile", "main", 0);
    }
    #else
    if(codec_context->codec_id == AV_CODEC_ID_H264) {
        // TODO: Only for vaapi
        //if(color_depth == GSR_COLOR_DEPTH_10_BITS)
        //    av_dict_set_int(options, "profile", AV_PROFILE_H264_HIGH_10, 0);
        //else
        av_dict_set_int(options, "profile", AV_PROFILE_H264_HIGH, 0);
    } else if(codec_context->codec_id == AV_CODEC_ID_AV1) {
        if(vendor == GSR_GPU_VENDOR_NVIDIA) {
            if(color_depth == GSR_COLOR_DEPTH_10_BITS)
                av_dict_set_int(options, "highbitdepth", 1, 0);
        } else {
            av_dict_set_int(options, "profile", AV_PROFILE_AV1_MAIN, 0); // TODO: use professional instead?
        }
    } else if(codec_context->codec_id == AV_CODEC_ID_HEVC) {
        if(color_depth == GSR_COLOR_DEPTH_10_BITS)
            av_dict_set_int(options, "profile", AV_PROFILE_HEVC_MAIN_10, 0);
        else
            av_dict_set_int(options, "profile", AV_PROFILE_HEVC_MAIN, 0);
    }
    #endif
}

static void video_software_set_qp(AVCodecContext *codec_context, VideoQuality video_quality, bool hdr, AVDictionary **options) {
    // 8 bit / 10 bit = 80%
    const float qp_multiply = hdr ? 8.0f/10.0f : 1.0f;
    if(codec_context->codec_id == AV_CODEC_ID_AV1) {
        switch(video_quality) {
            case VideoQuality::MEDIUM:
                av_dict_set_int(options, "qp", 35 * qp_multiply, 0);
                break;
            case VideoQuality::HIGH:
                av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
                break;
            case VideoQuality::VERY_HIGH:
                av_dict_set_int(options, "qp", 25 * qp_multiply, 0);
                break;
            case VideoQuality::ULTRA:
                av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
                break;
        }
    } else if(codec_context->codec_id == AV_CODEC_ID_H264) {
        switch(video_quality) {
            case VideoQuality::MEDIUM:
                av_dict_set_int(options, "qp", 34 * qp_multiply, 0);
                break;
            case VideoQuality::HIGH:
                av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
                break;
            case VideoQuality::VERY_HIGH:
                av_dict_set_int(options, "qp", 25 * qp_multiply, 0);
                break;
            case VideoQuality::ULTRA:
                av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
                break;
        }
    } else {
        switch(video_quality) {
            case VideoQuality::MEDIUM:
                av_dict_set_int(options, "qp", 35 * qp_multiply, 0);
                break;
            case VideoQuality::HIGH:
                av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
                break;
            case VideoQuality::VERY_HIGH:
                av_dict_set_int(options, "qp", 25 * qp_multiply, 0);
                break;
            case VideoQuality::ULTRA:
                av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
                break;
        }
    }
}

static void open_video_software(AVCodecContext *codec_context, VideoQuality video_quality, PixelFormat pixel_format, bool hdr, gsr_color_depth color_depth, BitrateMode bitrate_mode) {
    (void)pixel_format; // TODO:
    AVDictionary *options = nullptr;

    if(bitrate_mode == BitrateMode::QP)
        video_software_set_qp(codec_context, video_quality, hdr, &options);

    av_dict_set(&options, "preset", "veryfast", 0);
    av_dict_set(&options, "tune", "film", 0);
    dict_set_profile(codec_context, GSR_GPU_VENDOR_INTEL, color_depth, &options);

    if(codec_context->codec_id == AV_CODEC_ID_H264) {
        av_dict_set(&options, "coder", "cabac", 0); // TODO: cavlc is faster than cabac but worse compression. Which to use?
    }

    av_dict_set(&options, "strict", "experimental", 0);

    int ret = avcodec_open2(codec_context, codec_context->codec, &options);
    if (ret < 0) {
        fprintf(stderr, "Error: Could not open video codec: %s\n", av_error_to_string(ret));
        _exit(1);
    }
}

static void video_set_rc(VideoCodec video_codec, gsr_gpu_vendor vendor, BitrateMode bitrate_mode, AVDictionary **options) {
    switch(bitrate_mode) {
        case BitrateMode::QP: {
            if(video_codec_is_vulkan(video_codec))
                av_dict_set(options, "rc_mode", "cqp", 0);
            else if(vendor == GSR_GPU_VENDOR_NVIDIA)
                av_dict_set(options, "rc", "constqp", 0);
            else
                av_dict_set(options, "rc_mode", "CQP", 0);
            break;
        }
        case BitrateMode::VBR: {
            if(video_codec_is_vulkan(video_codec))
                av_dict_set(options, "rc_mode", "vbr", 0);
            else if(vendor == GSR_GPU_VENDOR_NVIDIA)
                av_dict_set(options, "rc", "vbr", 0);
            else
                av_dict_set(options, "rc_mode", "VBR", 0);
            break;
        }
        case BitrateMode::CBR: {
            if(video_codec_is_vulkan(video_codec))
                av_dict_set(options, "rc_mode", "cbr", 0);
            else if(vendor == GSR_GPU_VENDOR_NVIDIA)
                av_dict_set(options, "rc", "cbr", 0);
            else
                av_dict_set(options, "rc_mode", "CBR", 0);
            break;
        }
    }
}

static void video_hardware_set_qp(AVCodecContext *codec_context, VideoQuality video_quality, gsr_gpu_vendor vendor, bool hdr, AVDictionary **options) {
    // 8 bit / 10 bit = 80%
    const float qp_multiply = hdr ? 8.0f/10.0f : 1.0f;
    if(vendor == GSR_GPU_VENDOR_NVIDIA) {
        // TODO: Test if these should be in the same range as vaapi
        if(codec_context->codec_id == AV_CODEC_ID_AV1) {
            switch(video_quality) {
                case VideoQuality::MEDIUM:
                    av_dict_set_int(options, "qp", 35 * qp_multiply, 0);
                    break;
                case VideoQuality::HIGH:
                    av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
                    break;
                case VideoQuality::VERY_HIGH:
                    av_dict_set_int(options, "qp", 27 * qp_multiply, 0);
                    break;
                case VideoQuality::ULTRA:
                    av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
                    break;
            }
        } else if(codec_context->codec_id == AV_CODEC_ID_H264) {
            switch(video_quality) {
                case VideoQuality::MEDIUM:
                    av_dict_set_int(options, "qp", 35 * qp_multiply, 0);
                    break;
                case VideoQuality::HIGH:
                    av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
                    break;
                case VideoQuality::VERY_HIGH:
                    av_dict_set_int(options, "qp", 27 * qp_multiply, 0);
                    break;
                case VideoQuality::ULTRA:
                    av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
                    break;
            }
        } else if(codec_context->codec_id == AV_CODEC_ID_HEVC) {
            switch(video_quality) {
                case VideoQuality::MEDIUM:
                    av_dict_set_int(options, "qp", 35 * qp_multiply, 0);
                    break;
                case VideoQuality::HIGH:
                    av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
                    break;
                case VideoQuality::VERY_HIGH:
                    av_dict_set_int(options, "qp", 27 * qp_multiply, 0);
                    break;
                case VideoQuality::ULTRA:
                    av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
                    break;
            }
        } else if(codec_context->codec_id == AV_CODEC_ID_VP8 || codec_context->codec_id == AV_CODEC_ID_VP9) {
            switch(video_quality) {
                case VideoQuality::MEDIUM:
                    av_dict_set_int(options, "qp", 35 * qp_multiply, 0);
                    break;
                case VideoQuality::HIGH:
                    av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
                    break;
                case VideoQuality::VERY_HIGH:
                    av_dict_set_int(options, "qp", 27 * qp_multiply, 0);
                    break;
                case VideoQuality::ULTRA:
                    av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
                    break;
            }
        }
    } else {
        if(codec_context->codec_id == AV_CODEC_ID_AV1) {
            // Using global_quality option
        } else if(codec_context->codec_id == AV_CODEC_ID_H264) {
            switch(video_quality) {
                case VideoQuality::MEDIUM:
                    av_dict_set_int(options, "qp", 35 * qp_multiply, 0);
                    break;
                case VideoQuality::HIGH:
                    av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
                    break;
                case VideoQuality::VERY_HIGH:
                    av_dict_set_int(options, "qp", 27 * qp_multiply, 0);
                    break;
                case VideoQuality::ULTRA:
                    av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
                    break;
            }
        } else if(codec_context->codec_id == AV_CODEC_ID_HEVC) {
            switch(video_quality) {
                case VideoQuality::MEDIUM:
                    av_dict_set_int(options, "qp", 35 * qp_multiply, 0);
                    break;
                case VideoQuality::HIGH:
                    av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
                    break;
                case VideoQuality::VERY_HIGH:
                    av_dict_set_int(options, "qp", 27 * qp_multiply, 0);
                    break;
                case VideoQuality::ULTRA:
                    av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
                    break;
            }
        } else if(codec_context->codec_id == AV_CODEC_ID_VP8 || codec_context->codec_id == AV_CODEC_ID_VP9) {
            switch(video_quality) {
                case VideoQuality::MEDIUM:
                    av_dict_set_int(options, "qp", 35 * qp_multiply, 0);
                    break;
                case VideoQuality::HIGH:
                    av_dict_set_int(options, "qp", 30 * qp_multiply, 0);
                    break;
                case VideoQuality::VERY_HIGH:
                    av_dict_set_int(options, "qp", 27 * qp_multiply, 0);
                    break;
                case VideoQuality::ULTRA:
                    av_dict_set_int(options, "qp", 22 * qp_multiply, 0);
                    break;
            }
        }
    }
}

static void open_video_hardware(AVCodecContext *codec_context, VideoQuality video_quality, bool very_old_gpu, gsr_gpu_vendor vendor, PixelFormat pixel_format, bool hdr, gsr_color_depth color_depth, BitrateMode bitrate_mode, VideoCodec video_codec, bool low_power) {
    (void)very_old_gpu;
    AVDictionary *options = nullptr;

    if(bitrate_mode == BitrateMode::QP)
        video_hardware_set_qp(codec_context, video_quality, vendor, hdr, &options);

    video_set_rc(video_codec, vendor, bitrate_mode, &options);

    // TODO: Enable multipass

    // TODO: Set "usage" option to "record"/"stream" and "content" option to "rendered" for vulkan encoding

    if(vendor == GSR_GPU_VENDOR_NVIDIA) {
        // TODO: These dont seem to be necessary
        // av_dict_set_int(&options, "zerolatency", 1, 0);
        // if(codec_context->codec_id == AV_CODEC_ID_AV1) {
        //     av_dict_set(&options, "tune", "ll", 0);
        // } else if(codec_context->codec_id == AV_CODEC_ID_H264 || codec_context->codec_id == AV_CODEC_ID_HEVC) {
        //     av_dict_set(&options, "preset", "llhq", 0);
        //     av_dict_set(&options, "tune", "ll", 0);
        // }
        av_dict_set(&options, "tune", "hq", 0);

        dict_set_profile(codec_context, vendor, color_depth, &options);

        if(codec_context->codec_id == AV_CODEC_ID_H264) {
            // TODO: h264 10bit?
            // TODO:
            // switch(pixel_format) {
            //     case PixelFormat::YUV420:
            //         av_dict_set_int(&options, "profile", AV_PROFILE_H264_HIGH, 0);
            //         break;
            //     case PixelFormat::YUV444:
            //         av_dict_set_int(&options, "profile", AV_PROFILE_H264_HIGH_444, 0);
            //         break;
            // }
        } else if(codec_context->codec_id == AV_CODEC_ID_AV1) {
            switch(pixel_format) {
                case PixelFormat::YUV420:
                    av_dict_set(&options, "rgb_mode", "yuv420", 0);
                    break;
                case PixelFormat::YUV444:
                    av_dict_set(&options, "rgb_mode", "yuv444", 0);
                    break;
            }
        } else if(codec_context->codec_id == AV_CODEC_ID_HEVC) {
            //av_dict_set(&options, "pix_fmt", "yuv420p16le", 0);
        }
    } else {
        // TODO: More quality options
        if(low_power)
            av_dict_set_int(&options, "low_power", 1, 0);
        // Improves performance but increases vram
        //av_dict_set_int(&options, "async_depth", 8, 0);

        if(codec_context->codec_id == AV_CODEC_ID_H264) {
            // Removed because it causes stutter in games for some people
            //av_dict_set_int(&options, "quality", 5, 0); // quality preset
        } else if(codec_context->codec_id == AV_CODEC_ID_AV1) {
            av_dict_set(&options, "tier", "main", 0);
        } else if(codec_context->codec_id == AV_CODEC_ID_HEVC) {
            if(hdr)
                av_dict_set(&options, "sei", "hdr", 0);
        }

        // TODO: vp8/vp9 10bit
    }

    if(codec_context->codec_id == AV_CODEC_ID_H264) {
        av_dict_set(&options, "coder", "cabac", 0); // TODO: cavlc is faster than cabac but worse compression. Which to use?
    }

    av_dict_set(&options, "strict", "experimental", 0);

    int ret = avcodec_open2(codec_context, codec_context->codec, &options);
    if (ret < 0) {
        fprintf(stderr, "Error: Could not open video codec: %s\n", av_error_to_string(ret));
        _exit(1);
    }
}

static void usage_header() {
    const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
    const char *program_name = inside_flatpak ? "flatpak run --command=gpu-screen-recorder com.dec05eba.gpu_screen_recorder" : "gpu-screen-recorder";
#ifdef GSR_APP_AUDIO
    const char *app_audio_options = " [-aa <application_name>] [-aai <application_name>] ";
#else
    const char *app_audio_options = "";
#endif
    fprintf(stderr, "usage: %s -w <window_id|monitor|focused|portal> [-c <container_format>] [-s WxH] -f <fps> [-a <audio_input>]%s[-q <quality>] [-r <replay_buffer_size_sec>] [-k h264|hevc|av1|vp8|vp9|hevc_hdr|av1_hdr|hevc_10bit|av1_10bit] [-ac aac|opus|flac] [-ab <bitrate>] [-oc yes|no] [-fm cfr|vfr|content] [-bm auto|qp|vbr|cbr] [-cr limited|full] [-df yes|no] [-sc <script_path>] [-cursor yes|no] [-keyint <value>] [-restore-portal-session yes|no] [-portal-session-token-filepath filepath] [-encoder gpu|cpu] [-o <output_file>] [-v yes|no] [--version] [-h|--help]\n", program_name, app_audio_options);
}

// TODO: Update with portal info
static void usage_full() {
    const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
    const char *program_name = inside_flatpak ? "flatpak run --command=gpu-screen-recorder com.dec05eba.gpu_screen_recorder" : "gpu-screen-recorder";
    usage_header();
    fprintf(stderr, "\n");
    fprintf(stderr, "OPTIONS:\n");
    fprintf(stderr, "  -w    Window id to record, a display (monitor name), \"screen\", \"screen-direct-force\", \"focused\" or \"portal\".\n");
    fprintf(stderr, "        If this is \"portal\" then xdg desktop screencast portal with PipeWire will be used. Portal option is only available on Wayland.\n");
    fprintf(stderr, "        If you select to save the session (token) in the desktop portal capture popup then the session will be saved for the next time you use \"portal\",\n");
    fprintf(stderr, "        but the session will be ignored unless you run GPU Screen Recorder with the '-restore-portal-session yes' option.\n");
    fprintf(stderr, "        If this is \"screen\" or \"screen-direct-force\" then all monitors are recorded on Nvidia X11.\n");
    fprintf(stderr, "        On AMD/Intel or wayland \"screen\" will record the first monitor found.\n");
    fprintf(stderr, "        \"screen-direct-force\" is not recommended unless you use a VRR (G-SYNC) monitor on Nvidia X11 and you are aware that using this option can cause\n");
    fprintf(stderr, "        games to freeze/crash or other issues because of Nvidia driver issues.\n");
    fprintf(stderr, "        \"screen-direct-force\" option is only available on Nvidia X11. VRR works without this option on other systems.\n");
    fprintf(stderr, "        Run GPU Screen Recorder with the --list-capture-options option to list valid values for this option.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -c    Container format for output file, for example mp4, or flv. Only required if no output file is specified or if recording in replay buffer mode.\n");
    fprintf(stderr, "        If an output file is specified and -c is not used then the container format is determined from the output filename extension.\n");
    fprintf(stderr, "        Only containers that support h264, hevc, av1, vp8 or vp9 are supported, which means that only mp4, mkv, flv, webm (and some others) are supported.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -s    The output resolution limit of the video in the format WxH, for example 1920x1080. If this is 0x0 then the original resolution is used. Optional, except when -w is \"focused\".\n");
    fprintf(stderr, "        Note: the captured content is scaled to this size. The output resolution might not be exactly as specified by this option. The original aspect ratio is respected so the resolution will match that.\n");
    fprintf(stderr, "        The video encoder might also need to add padding, which will result in black bars on the sides of the video. This is especially an issue on AMD.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -f    Frame rate to record at. Recording will only capture frames at this target frame rate.\n");
    fprintf(stderr, "        For constant frame rate mode this option is the frame rate every frame will be captured at and if the capture frame rate is below this target frame rate then the frames will be duplicated.\n");
    fprintf(stderr, "        For variable frame rate mode this option is the max frame rate and if the capture frame rate is below this target frame rate then frames will not be duplicated.\n");
    fprintf(stderr, "        Content frame rate is similar to variable frame rate mode, except the frame rate will match the frame rate of the captured content when possible, but not capturing above the frame rate set in this -f option.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -a    Audio device to record from (pulse audio device). Can be specified multiple times. Each time this is specified a new audio track is added for the specified audio device.\n");
    fprintf(stderr, "        A name can be given to the audio input device by prefixing the audio input with <name>/, for example \"dummy/alsa_output.pci-0000_00_1b.0.analog-stereo.monitor\".\n");
    fprintf(stderr, "        Multiple audio devices can be merged into one audio track by using \"|\" as a separator into one -a argument, for example: -a \"alsa_output1|alsa_output2\".\n");
    fprintf(stderr, "        The audio device can also be \"default_output\" in which case the default output device is used, or \"default_input\" in which case the default input device is used.\n");
    fprintf(stderr, "        If the audio device is an empty string then the argument is ignored.\n");
    fprintf(stderr, "        Optional, no audio track is added by default.\n");
    fprintf(stderr, "        Run GPU Screen Recorder with the --list-audio-devices option to list valid audio devices to use with this -a option.\n");
    fprintf(stderr, "\n");
#ifdef GSR_APP_AUDIO
    fprintf(stderr, "  -aa   Application to record audio from (case-insensitive). Can be specified multiple times. Each time this is specified a new audio track is added for the specified application audio.\n");
    fprintf(stderr, "        Multiple application audio can be merged into one audio track by using \"|\" as a separator into one -a argument, for example: -a \"firefox|csgo\".\n");
    fprintf(stderr, "        If the application name is an empty string then the argument is ignored.\n");
    fprintf(stderr, "        Optional, no application audio is added by default.\n");
    fprintf(stderr, "        Note: this option is only available when the sound server on the system is PipeWire.\n");
    fprintf(stderr, "        Run GPU Screen Recorder with the --list-application-audio option to list valid application names to use with this -aa option.\n");
    fprintf(stderr, "        It's possible to use an application name that is not listed in --list-application-audio, for example when trying to record audio from an application that hasn't started yet.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -aai  Record audio from all applications except the ones specified with this option (case-insensitive). Can be specified multiple times.\n");
    fprintf(stderr, "        Each time this is specified a new audio track is added that records audio from all applications except the ones specified.\n");
    fprintf(stderr, "        Multiple application audio can be merged into one audio track by using \"|\" as a separator into one -a argument, for example: -a \"firefox|csgo\".\n");
    fprintf(stderr, "        If the application name is an empty string then the argument is ignored.\n");
    fprintf(stderr, "        Optional, no application audio is added by default.\n");
    fprintf(stderr, "        Note: this option is only available when the sound server on the system is PipeWire.\n");
    fprintf(stderr, "        Run GPU Screen Recorder with the --list-application-audio option to list valid application names to use with this -aai option.\n");
    fprintf(stderr, "        It's possible to use an application name that is not listed in --list-application-audio, for example when trying to record audio and the target application hasn't started yet.\n");
    fprintf(stderr, "\n");
#endif
    fprintf(stderr, "  -q    Video quality. Should be either 'medium', 'high', 'very_high' or 'ultra' when using '-bm qp' or '-bm vbr' options, and '-bm qp' is the default option used.\n");
    fprintf(stderr, "        'high' is the recommended option when live streaming or when you have a slower harddrive.\n");
    fprintf(stderr, "        When using '-bm cbr' option then this is option is instead used to specify the video bitrate in kbps.\n");
    fprintf(stderr, "        Optional when using '-bm qp' or '-bm vbr' options, set to 'very_high' be default.\n");
    fprintf(stderr, "        Required when using '-bm cbr' option.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -r    Replay buffer size in seconds. If this is set, then only the last seconds as set by this option will be stored\n");
    fprintf(stderr, "        and the video will only be saved when the gpu-screen-recorder is closed. This feature is similar to Nvidia's instant replay feature.\n");
    fprintf(stderr, "        This option has be between 5 and 1200. Note that the replay buffer size will not always be precise, because of keyframes. Optional, disabled by default.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -k    Video codec to use. Should be either 'auto', 'h264', 'hevc', 'av1', 'vp8', 'vp9', 'hevc_hdr', 'av1_hdr', 'hevc_10bit' or 'av1_10bit'.\n");
    fprintf(stderr, "        Optional, set to 'auto' by default which defaults to 'h264'. Forcefully set to 'h264' if the file container type is 'flv'.\n");
    fprintf(stderr, "        'hevc_hdr' and 'av1_hdr' option is not available on X11 nor when using the portal capture option.\n");
    fprintf(stderr, "        'hevc_10bit' and 'av1_10bit' options allow you to select 10 bit color depth which can reduce banding and improve quality in darker areas, but not all video players support 10 bit color depth\n");
    fprintf(stderr, "        and if you upload the video to a website the website might reduce 10 bit to 8 bit.\n");
    fprintf(stderr, "        Note that when using 'hevc_hdr' or 'av1_hdr' the color depth is also 10 bits.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -ac   Audio codec to use. Should be either 'aac', 'opus' or 'flac'. Optional, set to 'opus' for .mp4/.mkv files, otherwise set to 'aac'.\n");
    fprintf(stderr, "        'opus' and 'flac' is only supported by .mp4/.mkv files. 'opus' is recommended for best performance and smallest audio size.\n");
    fprintf(stderr, "        Flac audio codec is option is disable at the moment because of a temporary issue.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -ab   Audio bitrate in kbps. If this is set to 0 then it's the same as if it's absent, in which case the bitrate is determined automatically depending on the audio codec.\n");
    fprintf(stderr, "        Optional, by default the bitrate is 128kbps for opus and flac and 160kbps for aac.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -oc   Overclock memory transfer rate to the maximum performance level. This only applies to NVIDIA on X11 and exists to overcome a bug in NVIDIA driver where performance level\n");
    fprintf(stderr, "        is dropped when you record a game. Only needed if you are recording a game that is bottlenecked by GPU. The same issue exists on Wayland but overclocking is not possible on Wayland.\n");
    fprintf(stderr, "        Works only if your have \"Coolbits\" set to \"12\" in NVIDIA X settings, see README for more information. Note! use at your own risk! Optional, disabled by default.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -fm   Framerate mode. Should be either 'cfr' (constant frame rate), 'vfr' (variable frame rate) or 'content'. Optional, set to 'vfr' by default.\n");
    fprintf(stderr, "        'vfr' is recommended for recording for less issue with very high system load but some applications such as video editors may not support it properly.\n");
    fprintf(stderr, "        'content' is currently only supported on X11 or when using portal capture option. The 'content' option matches the recording frame rate to the captured content.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -bm   Bitrate mode. Should be either 'auto', 'qp' (constant quality), 'vbr' (variable bitrate) or 'cbr' (constant bitrate). Optional, set to 'auto' by default which defaults to 'qp' on all devices\n");
    fprintf(stderr, "        except steam deck that has broken drivers and doesn't support qp.\n");
    fprintf(stderr, "        Note: 'vbr' option is not supported when using '-encoder cpu' option.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -cr   Color range. Should be either 'limited' (aka mpeg) or 'full' (aka jpeg). Optional, set to 'limited' by default.\n");
    fprintf(stderr, "        Limited color range means that colors are in range 16-235 (4112-60395 for hdr) while full color range means that colors are in range 0-255 (0-65535 for hdr).\n");
    fprintf(stderr, "        Note that some buggy video players (such as vlc) are unable to correctly display videos in full color range and when upload the video to websites the website\n");
    fprintf(stderr, "        might re-encoder the video to make the video limited color range.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -df   Organise replays in folders based on the current date.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -sc   Run a script on the saved video file (asynchronously). The first argument to the script is the filepath to the saved video file and the second argument is the recording type (either \"regular\" or \"replay\").\n");
    fprintf(stderr, "        Not applicable for live streams.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -cursor\n");
    fprintf(stderr, "        Record cursor. Optional, set to 'yes' by default.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -keyint\n");
    fprintf(stderr, "        Specifies the keyframe interval in seconds, the max amount of time to wait to generate a keyframe. Keyframes can be generated more often than this.\n");
    fprintf(stderr, "        This also affects seeking in the video and may affect how the replay video is cut. If this is set to 10 for example then you can only seek in 10-second chunks in the video.\n");
    fprintf(stderr, "        Setting this to a higher value reduces the video file size if you are ok with the previously described downside. This option is expected to be a floating point number.\n");
    fprintf(stderr, "        By default this value is set to 2.0.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -restore-portal-session\n");
    fprintf(stderr, "        If GPU Screen Recorder should use the same capture option as the last time. Using this option removes the popup asking what you want to record the next time you record with '-w portal' if you selected the option to save session (token) in the desktop portal screencast popup.\n");
    fprintf(stderr, "        This option may not have any effect on your Wayland compositor and your systems desktop portal needs to support ScreenCast version 5 or later. Optional, set to 'no' by default.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -portal-session-token-filepath\n");
    fprintf(stderr, "        This option is used together with -restore-portal-session option to specify the file path to save/restore the portal session token to/from.\n");
    fprintf(stderr, "        This can be used to remember different portal capture options depending on different recording option (such as recording/replay).\n");
    fprintf(stderr, "        Optional, set to \"$XDG_CONFIG_HOME/gpu-screen-recorder/restore_token\" by default ($XDG_CONFIG_HOME defaults to \"$HOME/.config\").\n");
    fprintf(stderr, "        Note: the directory to the portal session token file is created automatically if it doesn't exist.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -encoder\n");
    fprintf(stderr, "        Which device should be used for video encoding. Should either be 'gpu' or 'cpu'. 'cpu' option currently only work with h264 codec option (-k).\n");
    fprintf(stderr, "        Optional, set to 'gpu' by default.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  --info\n");
    fprintf(stderr, "        List info about the system. Lists the following information (prints them to stdout and exits):\n");
    fprintf(stderr, "        Supported video codecs (h264, h264_software, hevc, hevc_hdr, hevc_10bit, av1, av1_hdr, av1_10bit, vp8, vp9 (if supported)).\n");
    fprintf(stderr, "        Supported capture options (window, focused, screen, monitors and portal, if supported by the system).\n");
    fprintf(stderr, "        If opengl initialization fails then the program exits with 22, if no usable drm device is found then it exits with 23. On success it exits with 0.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  --list-capture-options\n");
    fprintf(stderr, "        List available capture options. Lists capture options in the following format (prints them to stdout and exits):\n");
    fprintf(stderr, "          <option>\n");
    fprintf(stderr, "          <monitor_name>|<resolution>\n");
    fprintf(stderr, "        For example:\n");
    fprintf(stderr, "          window\n");
    fprintf(stderr, "          DP-1|1920x1080\n");
    fprintf(stderr, "        The <option> and <monitor_name> is the name that can be passed to GPU Screen Recorder with the -w option.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  --list-audio-devices\n");
    fprintf(stderr, "        List audio devices. Lists audio devices in the following format (prints them to stdout and exits):\n");
    fprintf(stderr, "          <audio_device_name>|<audio_device_name_in_human_readable_format>\n");
    fprintf(stderr, "        For example:\n");
    fprintf(stderr, "          bluez_input.88:C9:E8:66:A2:27|WH-1000XM4\n");
    fprintf(stderr, "          alsa_output.pci-0000_0c_00.4.iec958-stereo|Monitor of Starship/Matisse HD Audio Controller Digital Stereo (IEC958)\n");
    fprintf(stderr, "        The <audio_device_name> is the name that can be passed to GPU Screen Recorder with the -a option.\n");
    fprintf(stderr, "\n");
#ifdef GSR_APP_AUDIO
    fprintf(stderr, "  --list-application-audio\n");
    fprintf(stderr, "        Lists application that you can record from (with the -aa or -aai option) (prints them to stdout and exits), for example:\n");
    fprintf(stderr, "          firefox\n");
    fprintf(stderr, "          csgo\n");
    fprintf(stderr, "        These names are the application audio names that can be passed to GPU Screen Recorder with the -aa option.\n");
    fprintf(stderr, "\n");
#endif
    fprintf(stderr, "  --version\n");
    fprintf(stderr, "        Print version (%s) and exit\n", GSR_VERSION);
    fprintf(stderr, "\n");
    //fprintf(stderr, "  -pixfmt  The pixel format to use for the output video. yuv420 is the most common format and is best supported, but the color is compressed, so colors can look washed out and certain colors of text can look bad. Use yuv444 for no color compression, but the video may not work everywhere and it may not work with hardware video decoding. Optional, set to 'yuv420' by default\n");
    fprintf(stderr, "  -o    The output file path. If omitted then the encoded data is sent to stdout. Required in replay mode (when using -r).\n");
    fprintf(stderr, "        In replay mode this has to be a directory instead of a file.\n");
    fprintf(stderr, "        Note: the directory to the file is created automatically if it doesn't already exist.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -v    Prints fps and damage info once per second. Optional, set to 'yes' by default.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "  -h, --help\n");
    fprintf(stderr, "        Show this help.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "NOTES:\n");
    fprintf(stderr, "  Send signal SIGINT to gpu-screen-recorder (Ctrl+C, or killall -SIGINT gpu-screen-recorder) to stop and save the recording. When in replay mode this stops recording without saving.\n");
    fprintf(stderr, "  Send signal SIGUSR1 to gpu-screen-recorder (killall -SIGUSR1 gpu-screen-recorder) to save a replay (when in replay mode).\n");
    fprintf(stderr, "  Send signal SIGUSR2 to gpu-screen-recorder (killall -SIGUSR2 gpu-screen-recorder) to pause/unpause recording. Only applicable and useful when recording (not streaming nor replay).\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "EXAMPLES:\n");
    fprintf(stderr, "  %s -w screen -f 60 -a default_output -o \"$HOME/Videos/video.mp4\"\n", program_name);
    fprintf(stderr, "  %s -w screen -f 60 -a default_output -a default_input -o \"$HOME/Videos/video.mp4\"\n", program_name);
    fprintf(stderr, "  %s -w screen -f 60 -a \"default_output|default_input\" -o \"$HOME/Videos/video.mp4\"\n", program_name);
    fprintf(stderr, "  %s -w screen -f 60 -a default_output -c mkv -r 60 -o \"$HOME/Videos\"\n", program_name);
    fprintf(stderr, "  %s -w screen -f 60 -a default_output -c mkv -sc script.sh -r 60 -o \"$HOME/Videos\"\n", program_name);
    fprintf(stderr, "  %s -w portal -f 60 -a default_output -restore-portal-session yes -o \"$HOME/Videos/video.mp4\"\n", program_name);
    fprintf(stderr, "  %s -w screen -f 60 -a default_output -bm cbr -q 15000 -o \"$HOME/Videos/video.mp4\"\n", program_name);
#ifdef GSR_APP_AUDIO
    fprintf(stderr, "  %s -w screen -f 60 -aa \"firefox|csgo\" -o \"$HOME/Videos/video.mp4\"\n", program_name);
    fprintf(stderr, "  %s -w screen -f 60 -aai \"firefox|csgo\" -o \"$HOME/Videos/video.mp4\"\n", program_name);
#endif
    //fprintf(stderr, "  gpu-screen-recorder -w screen -f 60 -q ultra -pixfmt yuv444 -o video.mp4\n");
    _exit(1);
}

static void usage() {
    usage_header();
    _exit(1);
}

static sig_atomic_t running = 1;
static sig_atomic_t save_replay = 0;
static sig_atomic_t toggle_pause = 0;

static void stop_handler(int) {
    running = 0;
}

static void save_replay_handler(int) {
    save_replay = 1;
}

static void toggle_pause_handler(int) {
    toggle_pause = 1;
}

static bool is_hex_num(char c) {
    return (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9');
}

static bool contains_non_hex_number(const char *str) {
    bool hex_start = false;
    size_t len = strlen(str);
    if(len >= 2 && memcmp(str, "0x", 2) == 0) {
        str += 2;
        len -= 2;
        hex_start = true;
    }

    bool is_hex = false;
    for(size_t i = 0; i < len; ++i) {
        char c = str[i];
        if(c == '\0')
            return false;
        if(!is_hex_num(c))
            return true;
        if((c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))
            is_hex = true;
    }

    return is_hex && !hex_start;
}

static std::string get_date_str() {
    char str[128];
    time_t now = time(NULL);
    struct tm *t = localtime(&now);
    strftime(str, sizeof(str)-1, "%Y-%m-%d_%H-%M-%S", t);
    return str; 
}

static std::string get_date_only_str() {
    char str[128];
    time_t now = time(NULL);
    struct tm *t = localtime(&now);
    strftime(str, sizeof(str)-1, "%Y-%m-%d", t);
    return str;
}

static std::string get_time_only_str() {
    char str[128];
    time_t now = time(NULL);
    struct tm *t = localtime(&now);
    strftime(str, sizeof(str)-1, "%H-%M-%S", t);
    return str;
}

static AVStream* create_stream(AVFormatContext *av_format_context, AVCodecContext *codec_context) {
    AVStream *stream = avformat_new_stream(av_format_context, nullptr);
    if (!stream) {
        fprintf(stderr, "Error: Could not allocate stream\n");
        _exit(1);
    }
    stream->id = av_format_context->nb_streams - 1;
    stream->time_base = codec_context->time_base;
    stream->avg_frame_rate = codec_context->framerate;
    return stream;
}

static void run_recording_saved_script_async(const char *script_file, const char *video_file, const char *type) {
    char script_file_full[PATH_MAX];
    script_file_full[0] = '\0';
    if(!realpath(script_file, script_file_full)) {
        fprintf(stderr, "Error: script file not found: %s\n", script_file);
        return;
    }

    const char *args[6];
    const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;

    if(inside_flatpak) {
        args[0] = "flatpak-spawn";
        args[1] = "--host";
        args[2] = script_file_full;
        args[3] = video_file;
        args[4] = type;
        args[5] = NULL;
    } else {
        args[0] = script_file_full;
        args[1] = video_file;
        args[2] = type;
        args[3] = NULL;
    }

    pid_t pid = fork();
    if(pid == -1) {
        perror(script_file_full);
        return;
    } else if(pid == 0) { // child
        setsid();
        signal(SIGHUP, SIG_IGN);

        pid_t second_child = fork();
        if(second_child == 0) { // child
            execvp(args[0], (char* const*)args);
            perror(script_file_full);
            _exit(127);
        } else if(second_child != -1) { // parent
            _exit(0);
        }
    } else { // parent
        waitpid(pid, NULL, 0);
    }
}

static double audio_codec_get_desired_delay(AudioCodec audio_codec, int fps) {
    const double fps_inv = 1.0 / (double)fps;
    const double base = 0.01 + 1.0/165.0;
    switch(audio_codec) {
        case AudioCodec::OPUS:
            return std::max(0.0, base - fps_inv);
        case AudioCodec::AAC:
            return std::max(0.0, (base + 0.008) * 2.0 - fps_inv);
        case AudioCodec::FLAC:
            // TODO: Test
            return std::max(0.0, base - fps_inv);
    }
    assert(false);
    return std::max(0.0, base - fps_inv);
}

struct AudioDevice {
    SoundDevice sound_device;
    AudioInput audio_input;
    AVFilterContext *src_filter_ctx = nullptr;
    AVFrame *frame = nullptr;
    std::thread thread; // TODO: Instead of having a thread for each track, have one thread for all threads and read the data with non-blocking read
    std::string combined_sink_name;
};

// TODO: Cleanup
struct AudioTrack {
    AVCodecContext *codec_context = nullptr;
    AVStream *stream = nullptr;

    std::vector<AudioDevice> audio_devices;
    AVFilterGraph *graph = nullptr;
    AVFilterContext *sink = nullptr;
    int stream_index = 0;
    int64_t pts = 0;
};

static bool add_hdr_metadata_to_video_stream(gsr_capture *cap, AVStream *video_stream) {
    size_t light_metadata_size = 0;
    size_t mastering_display_metadata_size = 0;
    AVContentLightMetadata *light_metadata = av_content_light_metadata_alloc(&light_metadata_size);
    #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(59, 37, 100)
    AVMasteringDisplayMetadata *mastering_display_metadata = av_mastering_display_metadata_alloc();
    mastering_display_metadata_size = sizeof(*mastering_display_metadata);
    #else
    AVMasteringDisplayMetadata *mastering_display_metadata = av_mastering_display_metadata_alloc_size(&mastering_display_metadata_size);
    #endif

    if(!light_metadata || !mastering_display_metadata) {
        if(light_metadata)
            av_freep(light_metadata);

        if(mastering_display_metadata)
            av_freep(mastering_display_metadata);

        return false;
    }

    if(!gsr_capture_set_hdr_metadata(cap, mastering_display_metadata, light_metadata)) {
        av_freep(light_metadata);
        av_freep(mastering_display_metadata);
        return false;
    }

    // TODO: More error checking

    #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(60, 31, 102)
    const bool content_light_level_added = av_stream_add_side_data(video_stream, AV_PKT_DATA_CONTENT_LIGHT_LEVEL, (uint8_t*)light_metadata, light_metadata_size) == 0;
    #else
    const bool content_light_level_added = av_packet_side_data_add(&video_stream->codecpar->coded_side_data, &video_stream->codecpar->nb_coded_side_data, AV_PKT_DATA_CONTENT_LIGHT_LEVEL, light_metadata, light_metadata_size, 0) != NULL;
    #endif

    #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(60, 31, 102)
    const bool mastering_display_metadata_added = av_stream_add_side_data(video_stream, AV_PKT_DATA_MASTERING_DISPLAY_METADATA, (uint8_t*)mastering_display_metadata, mastering_display_metadata_size) == 0;
    #else
    const bool mastering_display_metadata_added = av_packet_side_data_add(&video_stream->codecpar->coded_side_data, &video_stream->codecpar->nb_coded_side_data, AV_PKT_DATA_MASTERING_DISPLAY_METADATA, mastering_display_metadata, mastering_display_metadata_size, 0) != NULL;
    #endif

    if(!content_light_level_added)
        av_freep(light_metadata);

    if(!mastering_display_metadata_added)
        av_freep(mastering_display_metadata);

    // Return true even on failure because we dont want to retry adding hdr metadata on failure
    return true;
}

static std::future<void> save_replay_thread;
static std::vector<std::shared_ptr<PacketData>> save_replay_packets;
static std::string save_replay_output_filepath;

static void save_replay_async(AVCodecContext *video_codec_context, int video_stream_index, std::vector<AudioTrack> &audio_tracks, std::deque<std::shared_ptr<PacketData>> &frame_data_queue, bool frames_erased, std::string output_dir, const char *container_format, const std::string &file_extension, std::mutex &write_output_mutex, bool date_folders, bool hdr, gsr_capture *capture) {
    if(save_replay_thread.valid())
        return;
    
    size_t start_index = (size_t)-1;
    int64_t video_pts_offset = 0;
    int64_t audio_pts_offset = 0;

    {
        std::lock_guard<std::mutex> lock(write_output_mutex);
        start_index = (size_t)-1;
        for(size_t i = 0; i < frame_data_queue.size(); ++i) {
            const AVPacket &av_packet = frame_data_queue[i]->data;
            if((av_packet.flags & AV_PKT_FLAG_KEY) && av_packet.stream_index == video_stream_index) {
                start_index = i;
                break;
            }
        }

        if(start_index == (size_t)-1)
            return;

        if(frames_erased) {
            video_pts_offset = frame_data_queue[start_index]->data.pts;
            
            // Find the next audio packet to use as audio pts offset
            for(size_t i = start_index; i < frame_data_queue.size(); ++i) {
                const AVPacket &av_packet = frame_data_queue[i]->data;
                if(av_packet.stream_index != video_stream_index) {
                    audio_pts_offset = av_packet.pts;
                    break;
                }
            }
        } else {
            start_index = 0;
        }

        save_replay_packets.resize(frame_data_queue.size());
        for(size_t i = 0; i < frame_data_queue.size(); ++i) {
            save_replay_packets[i] = frame_data_queue[i];
        }
    }

    if (date_folders) {
        std::string output_folder = output_dir + '/' + get_date_only_str();
        create_directory_recursive(&output_folder[0]);
        save_replay_output_filepath = output_folder + "/Replay_" + get_time_only_str() + "." + file_extension;
    } else {
        create_directory_recursive(&output_dir[0]);
        save_replay_output_filepath = output_dir + "/Replay_" + get_date_str() + "." + file_extension;
    }

    AVFormatContext *av_format_context;
    avformat_alloc_output_context2(&av_format_context, nullptr, container_format, nullptr);

    AVStream *video_stream = create_stream(av_format_context, video_codec_context);
    avcodec_parameters_from_context(video_stream->codecpar, video_codec_context);

    std::unordered_map<int, AudioTrack*> stream_index_to_audio_track_map;
    for(AudioTrack &audio_track : audio_tracks) {
        stream_index_to_audio_track_map[audio_track.stream_index] = &audio_track;
        AVStream *audio_stream = create_stream(av_format_context, audio_track.codec_context);
        avcodec_parameters_from_context(audio_stream->codecpar, audio_track.codec_context);
        audio_track.stream = audio_stream;
    }

    const int open_ret = avio_open(&av_format_context->pb, save_replay_output_filepath.c_str(), AVIO_FLAG_WRITE);
    if (open_ret < 0) {
        fprintf(stderr, "Error: Could not open '%s': %s. Make sure %s is an existing directory with write access\n", save_replay_output_filepath.c_str(), av_error_to_string(open_ret), save_replay_output_filepath.c_str());
        return;
    }

    AVDictionary *options = nullptr;
    av_dict_set(&options, "strict", "experimental", 0);

    const int header_write_ret = avformat_write_header(av_format_context, &options);
    if (header_write_ret < 0) {
        fprintf(stderr, "Error occurred when writing header to output file: %s\n", av_error_to_string(header_write_ret));
        avio_close(av_format_context->pb);
        avformat_free_context(av_format_context);
        av_dict_free(&options);
        return;
    }

    if(hdr)
        add_hdr_metadata_to_video_stream(capture, video_stream);

    save_replay_thread = std::async(std::launch::async, [video_stream_index, video_stream, start_index, video_pts_offset, audio_pts_offset, video_codec_context, &audio_tracks, stream_index_to_audio_track_map, av_format_context, options]() mutable {
        for(size_t i = start_index; i < save_replay_packets.size(); ++i) {
            // TODO: Check if successful
            AVPacket av_packet;
            memset(&av_packet, 0, sizeof(av_packet));
            //av_packet_from_data(av_packet, save_replay_packets[i]->data.data, save_replay_packets[i]->data.size);
            av_packet.data = save_replay_packets[i]->data.data;
            av_packet.size = save_replay_packets[i]->data.size;
            av_packet.stream_index = save_replay_packets[i]->data.stream_index;
            av_packet.pts = save_replay_packets[i]->data.pts;
            av_packet.dts = save_replay_packets[i]->data.pts;
            av_packet.flags = save_replay_packets[i]->data.flags;
            //av_packet.duration = save_replay_packets[i]->data.duration;

            AVStream *stream = video_stream;
            AVCodecContext *codec_context = video_codec_context;

            if(av_packet.stream_index == video_stream_index) {
                av_packet.pts -= video_pts_offset;
                av_packet.dts -= video_pts_offset;
            } else {
                AudioTrack *audio_track = stream_index_to_audio_track_map[av_packet.stream_index];
                stream = audio_track->stream;
                codec_context = audio_track->codec_context;

                av_packet.pts -= audio_pts_offset;
                av_packet.dts -= audio_pts_offset;
            }

            av_packet.stream_index = stream->index;
            av_packet_rescale_ts(&av_packet, codec_context->time_base, stream->time_base);

            const int ret = av_write_frame(av_format_context, &av_packet);
            if(ret < 0)
                fprintf(stderr, "Error: Failed to write frame index %d to muxer, reason: %s (%d)\n", stream->index, av_error_to_string(ret), ret);

            //av_packet_free(&av_packet);
        }

        if (av_write_trailer(av_format_context) != 0)
            fprintf(stderr, "Failed to write trailer\n");

        avio_close(av_format_context->pb);
        avformat_free_context(av_format_context);
        av_dict_free(&options);

        for(AudioTrack &audio_track : audio_tracks) {
            audio_track.stream = nullptr;
        }
    });
}

static void split_string(const std::string &str, char delimiter, std::function<bool(const char*,size_t)> callback) {
    size_t index = 0;
    while(index < str.size()) {
        size_t end_index = str.find(delimiter, index);
        if(end_index == std::string::npos)
            end_index = str.size();

        if(!callback(&str[index], end_index - index))
            break;

        index = end_index + 1;
    }
}

static const AudioInput* get_audio_device_by_name(const std::vector<AudioInput> &audio_inputs, const std::string &name) {
    for(const auto &audio_input : audio_inputs) {
        if(audio_input.name == name)
            return &audio_input;
    }
    return nullptr;
}

static std::vector<AudioInput> parse_audio_input_arg(const char *str, const AudioDevices &audio_devices) {
    std::vector<AudioInput> audio_inputs;
    split_string(str, '|', [&](const char *sub, size_t size) {
        AudioInput audio_input;
        audio_input.name.assign(sub, size);

        const bool name_is_existing_audio_device = get_audio_device_by_name(audio_devices.audio_inputs, audio_input.name);
        const size_t index = audio_input.name.find('/');
        if(!name_is_existing_audio_device && index != std::string::npos) {
            audio_input.description = audio_input.name.substr(0, index);
            audio_input.name.erase(audio_input.name.begin(), audio_input.name.begin() + index + 1);
        }

        audio_inputs.push_back(std::move(audio_input));
        return true;
    });
    return audio_inputs;
}

static std::vector<AudioInput> parse_app_audio_input_arg(const char *str) {
    std::vector<AudioInput> audio_inputs;
    split_string(str, '|', [&](const char *sub, size_t size) {
        AudioInput audio_input;
        audio_input.name.assign(sub, size);
        audio_input.description = audio_input.name;
        audio_inputs.push_back(std::move(audio_input));
        return true;
    });
    return audio_inputs;
}

// TODO: Does this match all livestreaming cases?
static bool is_livestream_path(const char *str) {
    const int len = strlen(str);
    if((len >= 7 && memcmp(str, "http://", 7) == 0) || (len >= 8 && memcmp(str, "https://", 8) == 0))
        return true;
    else if((len >= 7 && memcmp(str, "rtmp://", 7) == 0) || (len >= 8 && memcmp(str, "rtmps://", 8) == 0))
        return true;
    else if((len >= 7 && memcmp(str, "rtsp://", 7) == 0))
        return true;
    else if((len >= 6 && memcmp(str, "srt://", 6) == 0))
        return true;
    else if((len >= 6 && memcmp(str, "tcp://", 6) == 0))
        return true;
    else if((len >= 6 && memcmp(str, "udp://", 6) == 0))
        return true;
    else
        return false;
}

// TODO: Proper cleanup
static int init_filter_graph(AVCodecContext *audio_codec_context, AVFilterGraph **graph, AVFilterContext **sink, std::vector<AVFilterContext*> &src_filter_ctx, size_t num_sources) {
    char ch_layout[64];
    int err = 0;
    ch_layout[0] = '\0';
 
    AVFilterGraph *filter_graph = avfilter_graph_alloc();
    if (!filter_graph) {
        fprintf(stderr, "Unable to create filter graph.\n");
        return AVERROR(ENOMEM);
    }
 
    for(size_t i = 0; i < num_sources; ++i) {
        const AVFilter *abuffer = avfilter_get_by_name("abuffer");
        if (!abuffer) {
            fprintf(stderr, "Could not find the abuffer filter.\n");
            return AVERROR_FILTER_NOT_FOUND;
        }
    
        AVFilterContext *abuffer_ctx = avfilter_graph_alloc_filter(filter_graph, abuffer, NULL);
        if (!abuffer_ctx) {
            fprintf(stderr, "Could not allocate the abuffer instance.\n");
            return AVERROR(ENOMEM);
        }
    
        #if LIBAVCODEC_VERSION_MAJOR < 60
        av_get_channel_layout_string(ch_layout, sizeof(ch_layout), 0, AV_CH_LAYOUT_STEREO);
        #else
        av_channel_layout_describe(&audio_codec_context->ch_layout, ch_layout, sizeof(ch_layout));
        #endif
        av_opt_set    (abuffer_ctx, "channel_layout", ch_layout,                                               AV_OPT_SEARCH_CHILDREN);
        av_opt_set    (abuffer_ctx, "sample_fmt",     av_get_sample_fmt_name(audio_codec_context->sample_fmt), AV_OPT_SEARCH_CHILDREN);
        av_opt_set_q  (abuffer_ctx, "time_base",      audio_codec_context->time_base,                          AV_OPT_SEARCH_CHILDREN);
        av_opt_set_int(abuffer_ctx, "sample_rate",    audio_codec_context->sample_rate,                        AV_OPT_SEARCH_CHILDREN);
        av_opt_set_int(abuffer_ctx, "bit_rate",       audio_codec_context->bit_rate,                           AV_OPT_SEARCH_CHILDREN);
    
        err = avfilter_init_str(abuffer_ctx, NULL);
        if (err < 0) {
            fprintf(stderr, "Could not initialize the abuffer filter.\n");
            return err;
        }

        src_filter_ctx.push_back(abuffer_ctx);
    }

    const AVFilter *mix_filter = avfilter_get_by_name("amix");
    if (!mix_filter) {
        av_log(NULL, AV_LOG_ERROR, "Could not find the mix filter.\n");
        return AVERROR_FILTER_NOT_FOUND;
    }
    
    char args[512];
    snprintf(args, sizeof(args), "inputs=%d", (int)num_sources);
    
    AVFilterContext *mix_ctx;
    err = avfilter_graph_create_filter(&mix_ctx, mix_filter, "amix", args, NULL, filter_graph);
    if (err < 0) {
        av_log(NULL, AV_LOG_ERROR, "Cannot create audio amix filter\n");
        return err;
    }
 
    const AVFilter *abuffersink = avfilter_get_by_name("abuffersink");
    if (!abuffersink) {
        fprintf(stderr, "Could not find the abuffersink filter.\n");
        return AVERROR_FILTER_NOT_FOUND;
    }
 
    AVFilterContext *abuffersink_ctx = avfilter_graph_alloc_filter(filter_graph, abuffersink, "sink");
    if (!abuffersink_ctx) {
        fprintf(stderr, "Could not allocate the abuffersink instance.\n");
        return AVERROR(ENOMEM);
    }
 
    err = avfilter_init_str(abuffersink_ctx, NULL);
    if (err < 0) {
        fprintf(stderr, "Could not initialize the abuffersink instance.\n");
        return err;
    }
 
    err = 0;
    for(size_t i = 0; i < src_filter_ctx.size(); ++i) {
        AVFilterContext *src_ctx = src_filter_ctx[i];
        if (err >= 0)
            err = avfilter_link(src_ctx, 0, mix_ctx, i);
    }
    if (err >= 0)
        err = avfilter_link(mix_ctx, 0, abuffersink_ctx, 0);
    if (err < 0) {
        av_log(NULL, AV_LOG_ERROR, "Error connecting filters\n");
        return err;
    }
 
    err = avfilter_graph_config(filter_graph, NULL);
    if (err < 0) {
        av_log(NULL, AV_LOG_ERROR, "Error configuring the filter graph\n");
        return err;
    }
 
    *graph = filter_graph;
    *sink  = abuffersink_ctx;
 
    return 0;
}

static gsr_video_encoder* create_video_encoder(gsr_egl *egl, bool overclock, gsr_color_depth color_depth, bool use_software_video_encoder, VideoCodec video_codec) {
    gsr_video_encoder *video_encoder = nullptr;

    if(use_software_video_encoder) {
        gsr_video_encoder_software_params params;
        params.egl = egl;
        params.color_depth = color_depth;
        video_encoder = gsr_video_encoder_software_create(&params);
        return video_encoder;
    }

    if(video_codec_is_vulkan(video_codec)) {
        gsr_video_encoder_vulkan_params params;
        params.egl = egl;
        params.color_depth = color_depth;
        video_encoder = gsr_video_encoder_vulkan_create(&params);
        return video_encoder;
    }

    switch(egl->gpu_info.vendor) {
        case GSR_GPU_VENDOR_AMD:
        case GSR_GPU_VENDOR_INTEL: {
            gsr_video_encoder_vaapi_params params;
            params.egl = egl;
            params.color_depth = color_depth;
            video_encoder = gsr_video_encoder_vaapi_create(&params);
            break;
        }
        case GSR_GPU_VENDOR_NVIDIA: {
            gsr_video_encoder_nvenc_params params;
            params.egl = egl;
            params.overclock = overclock;
            params.color_depth = color_depth;
            video_encoder = gsr_video_encoder_nvenc_create(&params);
            break;
        }
    }

    return video_encoder;
}

static bool get_supported_video_codecs(gsr_egl *egl, VideoCodec video_codec, bool use_software_video_encoder, bool cleanup, gsr_supported_video_codecs *video_codecs) {
    memset(video_codecs, 0, sizeof(*video_codecs));

    if(use_software_video_encoder) {
        video_codecs->h264.supported = true;
        return true;
    }

    if(video_codec_is_vulkan(video_codec))
        return gsr_get_supported_video_codecs_vulkan(video_codecs, egl->card_path, cleanup);

    switch(egl->gpu_info.vendor) {
        case GSR_GPU_VENDOR_AMD:
        case GSR_GPU_VENDOR_INTEL:
            return gsr_get_supported_video_codecs_vaapi(video_codecs, egl->card_path, cleanup);
        case GSR_GPU_VENDOR_NVIDIA:
            return gsr_get_supported_video_codecs_nvenc(video_codecs, cleanup);
    }

    return false;
}

static void xwayland_check_callback(const gsr_monitor *monitor, void *userdata) {
    bool *xwayland_found = (bool*)userdata;
    if(monitor->name_len >= 8 && strncmp(monitor->name, "XWAYLAND", 8) == 0)
        *xwayland_found = true;
    else if(memmem(monitor->name, monitor->name_len, "X11", 3))
        *xwayland_found = true;
}

static bool is_xwayland(Display *display) {
    int opcode, event, error;
    if(XQueryExtension(display, "XWAYLAND", &opcode, &event, &error))
        return true;

    bool xwayland_found = false;
    for_each_active_monitor_output_x11_not_cached(display, xwayland_check_callback, &xwayland_found);
    return xwayland_found;
}

static bool is_using_prime_run() {
    const char *prime_render_offload = getenv("__NV_PRIME_RENDER_OFFLOAD");
    return prime_render_offload && strcmp(prime_render_offload, "1") == 0;
}

static void disable_prime_run() {
    unsetenv("__NV_PRIME_RENDER_OFFLOAD");
    unsetenv("__NV_PRIME_RENDER_OFFLOAD_PROVIDER");
    unsetenv("__GLX_VENDOR_LIBRARY_NAME");
    unsetenv("__VK_LAYER_NV_optimus");
}

static void list_system_info(bool wayland) {
    printf("display_server|%s\n", wayland ? "wayland" : "x11");
    bool supports_app_audio = false;
#ifdef GSR_APP_AUDIO
    supports_app_audio = pulseaudio_server_is_pipewire();
    gsr_pipewire_audio audio;
    if(gsr_pipewire_audio_init(&audio))
        gsr_pipewire_audio_deinit(&audio);
    else
        supports_app_audio = false;
#endif
    printf("supports_app_audio|%s\n", supports_app_audio ? "yes" : "no");
}

static void list_gpu_info(gsr_egl *egl) {
    switch(egl->gpu_info.vendor) {
        case GSR_GPU_VENDOR_AMD:
            printf("vendor|amd\n");
            break;
        case GSR_GPU_VENDOR_INTEL:
            printf("vendor|intel\n");
            break;
        case GSR_GPU_VENDOR_NVIDIA:
            printf("vendor|nvidia\n");
            break;
    }
}

static const AVCodec* get_ffmpeg_video_codec(VideoCodec video_codec, gsr_gpu_vendor vendor) {
    switch(video_codec) {
        case VideoCodec::H264:
            return avcodec_find_encoder_by_name(vendor == GSR_GPU_VENDOR_NVIDIA ? "h264_nvenc" : "h264_vaapi");
        case VideoCodec::HEVC:
        case VideoCodec::HEVC_HDR:
        case VideoCodec::HEVC_10BIT:
            return avcodec_find_encoder_by_name(vendor == GSR_GPU_VENDOR_NVIDIA ? "hevc_nvenc" : "hevc_vaapi");
        case VideoCodec::AV1:
        case VideoCodec::AV1_HDR:
        case VideoCodec::AV1_10BIT:
            return avcodec_find_encoder_by_name(vendor == GSR_GPU_VENDOR_NVIDIA ? "av1_nvenc" : "av1_vaapi");
        case VideoCodec::VP8:
            return avcodec_find_encoder_by_name(vendor == GSR_GPU_VENDOR_NVIDIA ? "vp8_nvenc" : "vp8_vaapi");
        case VideoCodec::VP9:
            return avcodec_find_encoder_by_name(vendor == GSR_GPU_VENDOR_NVIDIA ? "vp9_nvenc" : "vp9_vaapi");
        case VideoCodec::H264_VULKAN:
            return avcodec_find_encoder_by_name("h264_vulkan");
        case VideoCodec::HEVC_VULKAN:
            return avcodec_find_encoder_by_name("hevc_vulkan");
    }
    return nullptr;
}

static void set_supported_video_codecs_ffmpeg(gsr_supported_video_codecs *supported_video_codecs, gsr_supported_video_codecs *supported_video_codecs_vulkan, gsr_gpu_vendor vendor) {
    if(!get_ffmpeg_video_codec(VideoCodec::H264, vendor)) {
        supported_video_codecs->h264.supported = false;
    }

    if(!get_ffmpeg_video_codec(VideoCodec::HEVC, vendor)) {
        supported_video_codecs->hevc.supported = false;
        supported_video_codecs->hevc_hdr.supported = false;
        supported_video_codecs->hevc_10bit.supported = false;
    }

    if(!get_ffmpeg_video_codec(VideoCodec::AV1, vendor)) {
        supported_video_codecs->av1.supported = false;
        supported_video_codecs->av1_hdr.supported = false;
        supported_video_codecs->av1_10bit.supported = false;
    }

    if(!get_ffmpeg_video_codec(VideoCodec::VP8, vendor)) {
        supported_video_codecs->vp8.supported = false;
    }

    if(!get_ffmpeg_video_codec(VideoCodec::VP9, vendor)) {
        supported_video_codecs->vp9.supported = false;
    }

    if(!get_ffmpeg_video_codec(VideoCodec::H264_VULKAN, vendor)) {
        supported_video_codecs_vulkan->h264.supported = false;
    }

    if(!get_ffmpeg_video_codec(VideoCodec::HEVC_VULKAN, vendor)) {
        supported_video_codecs_vulkan->hevc.supported = false;
        supported_video_codecs_vulkan->hevc_hdr.supported = false;
        supported_video_codecs_vulkan->hevc_10bit.supported = false;
    }
}

static void list_supported_video_codecs(gsr_egl *egl, bool wayland) {
    // Dont clean it up on purpose to increase shutdown speed
    gsr_supported_video_codecs supported_video_codecs;
    get_supported_video_codecs(egl, VideoCodec::H264, false, false, &supported_video_codecs);

    gsr_supported_video_codecs supported_video_codecs_vulkan;
    get_supported_video_codecs(egl, VideoCodec::H264_VULKAN, false, false, &supported_video_codecs_vulkan);

    set_supported_video_codecs_ffmpeg(&supported_video_codecs, &supported_video_codecs_vulkan, egl->gpu_info.vendor);

    if(supported_video_codecs.h264.supported)
        puts("h264");
    if(avcodec_find_encoder_by_name("libx264"))
        puts("h264_software");
    if(supported_video_codecs.hevc.supported)
        puts("hevc");
    if(supported_video_codecs.hevc_hdr.supported && wayland)
        puts("hevc_hdr");
    if(supported_video_codecs.hevc_10bit.supported)
        puts("hevc_10bit");
    if(supported_video_codecs.av1.supported)
        puts("av1");
    if(supported_video_codecs.av1_hdr.supported && wayland)
        puts("av1_hdr");
    if(supported_video_codecs.av1_10bit.supported)
        puts("av1_10bit");
    if(supported_video_codecs.vp8.supported)
        puts("vp8");
    if(supported_video_codecs.vp9.supported)
        puts("vp9");
    //if(supported_video_codecs_vulkan.h264.supported)
    //    puts("h264_vulkan");
    //if(supported_video_codecs_vulkan.hevc.supported)
    //    puts("hevc_vulkan"); // TODO: hdr, 10 bit
}

static bool monitor_capture_use_drm(gsr_egl *egl, bool wayland) {
    return wayland || egl->gpu_info.vendor != GSR_GPU_VENDOR_NVIDIA;
}

typedef struct {
    bool wayland;
    gsr_egl *egl;
} capture_options_callback;

static void output_monitor_info(const gsr_monitor *monitor, void *userdata) {
    const capture_options_callback *options = (capture_options_callback*)userdata;
    if(options->wayland && monitor_capture_use_drm(options->egl, options->wayland)) {
        vec2i monitor_size = monitor->size;
        const gsr_monitor_rotation rot = drm_monitor_get_display_server_rotation(options->egl, monitor);
        if(rot == GSR_MONITOR_ROT_90 || rot == GSR_MONITOR_ROT_270)
            std::swap(monitor_size.x, monitor_size.y);
        printf("%.*s|%dx%d\n", monitor->name_len, monitor->name, monitor_size.x, monitor_size.y);
    } else {
        printf("%.*s|%dx%d\n", monitor->name_len, monitor->name, monitor->size.x, monitor->size.y);
    }
}

static void list_supported_capture_options(gsr_egl *egl, bool wayland) {
    if(!wayland) {
        puts("window");
        puts("focused");
    }

    capture_options_callback options;
    options.wayland = wayland;
    options.egl = egl;
    if(monitor_capture_use_drm(egl, wayland)) {
        const bool is_x11 = gsr_egl_get_display_server(egl) == GSR_DISPLAY_SERVER_X11;
        const gsr_connection_type connection_type = is_x11 ? GSR_CONNECTION_X11 : GSR_CONNECTION_DRM;
        for_each_active_monitor_output(egl, connection_type, output_monitor_info, &options);
    } else {
        puts("screen"); // All monitors in one, only available on Nvidia X11
        for_each_active_monitor_output(egl, GSR_CONNECTION_X11, output_monitor_info, &options);
    }

#ifdef GSR_PORTAL
    // Desktop portal capture on x11 doesn't seem to be hardware accelerated
    if(!wayland)
        return;

    gsr_dbus dbus;
    if(!gsr_dbus_init(&dbus, NULL))
        return;

    char *session_handle = NULL;
    if(gsr_dbus_screencast_create_session(&dbus, &session_handle) == 0) {
        free(session_handle);
        puts("portal");
    }
    gsr_dbus_deinit(&dbus);
#endif
}

static void info_command() {
    bool wayland = false;
    Display *dpy = XOpenDisplay(nullptr);
    if (!dpy) {
        wayland = true;
        fprintf(stderr, "Warning: failed to connect to the X server. Assuming wayland is running without Xwayland\n");
    }

    XSetErrorHandler(x11_error_handler);
    XSetIOErrorHandler(x11_io_error_handler);

    if(!wayland)
        wayland = is_xwayland(dpy);

    if(!wayland && is_using_prime_run()) {
        // Disable prime-run and similar options as it doesn't work, the monitor to capture has to be run on the same device.
        // This is fine on wayland since nvidia uses drm interface there and the monitor query checks the monitors connected
        // to the drm device.
        fprintf(stderr, "Warning: use of prime-run on X11 is not supported. Disabling prime-run\n");
        disable_prime_run();
    }

    gsr_egl egl;
    if(!gsr_egl_load(&egl, dpy, wayland, false)) {
        fprintf(stderr, "gsr error: failed to load opengl\n");
        _exit(22);
    }

    egl.card_path[0] = '\0';
    if(monitor_capture_use_drm(&egl, wayland)) {
        // TODO: Allow specifying another card, and in other places
        if(!gsr_get_valid_card_path(&egl, egl.card_path, false)) {
            fprintf(stderr, "Error: no /dev/dri/cardX device found. Make sure that you have at least one monitor connected\n");
            _exit(23);
        }
    }

    av_log_set_level(AV_LOG_FATAL);

    puts("section=system_info");
    list_system_info(wayland);
    if(egl.gpu_info.is_steam_deck)
        puts("is_steam_deck|yes");
    else
        puts("is_steam_deck|no");
    puts("section=gpu_info");
    list_gpu_info(&egl);
    puts("section=video_codecs");
    list_supported_video_codecs(&egl, wayland);
    puts("section=capture_options");
    list_supported_capture_options(&egl, wayland);

    fflush(stdout);

    // Not needed as this will just slow down shutdown
    //gsr_egl_unload(&egl);
    //if(dpy)
    //    XCloseDisplay(dpy);

    _exit(0);
}

static void list_audio_devices_command() {
    const AudioDevices audio_devices = get_pulseaudio_inputs();

    if(!audio_devices.default_output.empty())
        puts("default_output|Default output");

    if(!audio_devices.default_input.empty())
        puts("default_input|Default input");

    for(const auto &audio_input : audio_devices.audio_inputs) {
        printf("%s|%s\n", audio_input.name.c_str(), audio_input.description.c_str());
    }

    fflush(stdout);
    _exit(0);
}

static bool app_audio_query_callback(const char *app_name, void*) {
    puts(app_name);
    return true;
}

static void list_application_audio_command() {
#ifdef GSR_APP_AUDIO
    gsr_pipewire_audio audio;
    if(gsr_pipewire_audio_init(&audio)) {
        gsr_pipewire_audio_for_each_app(&audio, app_audio_query_callback, NULL);
        gsr_pipewire_audio_deinit(&audio);
    }
#endif

    fflush(stdout);
    _exit(0);
}

static void list_capture_options_command() {
    bool wayland = false;
    Display *dpy = XOpenDisplay(nullptr);
    if (!dpy) {
        wayland = true;
        fprintf(stderr, "Warning: failed to connect to the X server. Assuming wayland is running without Xwayland\n");
    }

    XSetErrorHandler(x11_error_handler);
    XSetIOErrorHandler(x11_io_error_handler);

    if(!wayland)
        wayland = is_xwayland(dpy);

    if(!wayland && is_using_prime_run()) {
        // Disable prime-run and similar options as it doesn't work, the monitor to capture has to be run on the same device.
        // This is fine on wayland since nvidia uses drm interface there and the monitor query checks the monitors connected
        // to the drm device.
        fprintf(stderr, "Warning: use of prime-run on X11 is not supported. Disabling prime-run\n");
        disable_prime_run();
    }

    gsr_egl egl;
    if(!gsr_egl_load(&egl, dpy, wayland, false)) {
        fprintf(stderr, "gsr error: failed to load opengl\n");
        _exit(1);
    }

    egl.card_path[0] = '\0';
    if(monitor_capture_use_drm(&egl, wayland)) {
        // TODO: Allow specifying another card, and in other places
        if(!gsr_get_valid_card_path(&egl, egl.card_path, false)) {
            fprintf(stderr, "Error: no /dev/dri/cardX device found. Make sure that you have at least one monitor connected\n");
            _exit(23);
        }
    }

    av_log_set_level(AV_LOG_FATAL);
    list_supported_capture_options(&egl, wayland);

    fflush(stdout);

    // Not needed as this will just slow down shutdown
    //gsr_egl_unload(&egl);
    //if(dpy)
    //    XCloseDisplay(dpy);

    _exit(0);
}

static gsr_capture* create_capture_impl(std::string &window_str, vec2i output_resolution, bool wayland, gsr_egl *egl, int fps, VideoCodec video_codec, gsr_color_range color_range,
    bool record_cursor, bool use_software_video_encoder, bool restore_portal_session, const char *portal_session_token_filepath,
    gsr_color_depth color_depth)
{
    Window src_window_id = None;
    bool follow_focused = false;

    gsr_capture *capture = nullptr;
    if(strcmp(window_str.c_str(), "focused") == 0) {
        if(wayland) {
            fprintf(stderr, "Error: GPU Screen Recorder window capture only works in a pure X11 session. Xwayland is not supported. You can record a monitor instead on wayland\n");
            _exit(2);
        }

        if(output_resolution.x <= 0 || output_resolution.y <= 0) {
            fprintf(stderr, "Error: invalid value for option -s '%dx%d' when using -w focused option. expected width and height to be greater than 0\n", output_resolution.x, output_resolution.y);
            usage();
        }

        follow_focused = true;
    } else if(strcmp(window_str.c_str(), "portal") == 0) {
#ifdef GSR_PORTAL
        // Desktop portal capture on x11 doesn't seem to be hardware accelerated
        if(!wayland) {
            fprintf(stderr, "Error: desktop portal capture is not supported on X11\n");
            _exit(1);
        }

        gsr_capture_portal_params portal_params;
        portal_params.egl = egl;
        portal_params.color_depth = color_depth;
        portal_params.color_range = color_range;
        portal_params.record_cursor = record_cursor;
        portal_params.restore_portal_session = restore_portal_session;
        portal_params.portal_session_token_filepath = portal_session_token_filepath;
        portal_params.output_resolution = output_resolution;
        capture = gsr_capture_portal_create(&portal_params);
        if(!capture)
            _exit(1);
#else
        fprintf(stderr, "Error: option '-w portal' used but GPU Screen Recorder was compiled without desktop portal support. Please recompile GPU Screen recorder with the -Dportal=true option\n");
        _exit(2);
#endif
    } else if(contains_non_hex_number(window_str.c_str())) {
        if(monitor_capture_use_drm(egl, wayland)) {
            const bool is_x11 = gsr_egl_get_display_server(egl) == GSR_DISPLAY_SERVER_X11;
            const gsr_connection_type connection_type = is_x11 ? GSR_CONNECTION_X11 : GSR_CONNECTION_DRM;

            if(strcmp(window_str.c_str(), "screen") == 0) {
                FirstOutputCallback first_output;
                first_output.output_name = NULL;
                for_each_active_monitor_output(egl, connection_type, get_first_output, &first_output);

                if(first_output.output_name) {
                    window_str = first_output.output_name;
                } else {
                    fprintf(stderr, "Error: no usable output found\n");
                    _exit(1);
                }
            } else {
                gsr_monitor gmon;
                if(!get_monitor_by_name(egl, connection_type, window_str.c_str(), &gmon)) {
                    fprintf(stderr, "gsr error: display \"%s\" not found, expected one of:\n", window_str.c_str());
                    fprintf(stderr, "    \"screen\"\n");
                    for_each_active_monitor_output(egl, connection_type, monitor_output_callback_print, NULL);
                    _exit(1);
                }
            }
        } else {
            if(strcmp(window_str.c_str(), "screen") != 0 && strcmp(window_str.c_str(), "screen-direct") != 0 && strcmp(window_str.c_str(), "screen-direct-force") != 0) {
                gsr_monitor gmon;
                if(!get_monitor_by_name(egl, GSR_CONNECTION_X11, window_str.c_str(), &gmon)) {
                    const int screens_width = XWidthOfScreen(DefaultScreenOfDisplay(egl->x11.dpy));
                    const int screens_height = XWidthOfScreen(DefaultScreenOfDisplay(egl->x11.dpy));
                    fprintf(stderr, "gsr error: display \"%s\" not found, expected one of:\n", window_str.c_str());
                    fprintf(stderr, "    \"screen\"    (%dx%d+%d+%d)\n", screens_width, screens_height, 0, 0);
                    fprintf(stderr, "    \"screen-direct\"    (%dx%d+%d+%d)\n", screens_width, screens_height, 0, 0);
                    fprintf(stderr, "    \"screen-direct-force\"    (%dx%d+%d+%d)\n", screens_width, screens_height, 0, 0);
                    for_each_active_monitor_output(egl, GSR_CONNECTION_X11, monitor_output_callback_print, NULL);
                    _exit(1);
                }
            }
        }

        if(egl->gpu_info.vendor == GSR_GPU_VENDOR_NVIDIA && !wayland) {
            const char *capture_target = window_str.c_str();
            bool direct_capture = strcmp(window_str.c_str(), "screen-direct") == 0;
            if(direct_capture) {
                capture_target = "screen";
                // TODO: Temporary disable direct capture because push model causes stuttering when it's direct capturing. This might be a nvfbc bug. This does not happen when using a compositor.
                direct_capture = false;
                fprintf(stderr, "Warning: screen-direct has temporary been disabled as it causes stuttering. This is likely a NvFBC bug. Falling back to \"screen\".\n");
            }

            if(strcmp(window_str.c_str(), "screen-direct-force") == 0) {
                direct_capture = true;
                capture_target = "screen";
            }

            gsr_capture_nvfbc_params nvfbc_params;
            nvfbc_params.egl = egl;
            nvfbc_params.display_to_capture = capture_target;
            nvfbc_params.fps = fps;
            nvfbc_params.pos = { 0, 0 };
            nvfbc_params.size = { 0, 0 };
            nvfbc_params.direct_capture = direct_capture;
            nvfbc_params.color_depth = color_depth;
            nvfbc_params.color_range = color_range;
            nvfbc_params.record_cursor = record_cursor;
            nvfbc_params.use_software_video_encoder = use_software_video_encoder;
            nvfbc_params.output_resolution = output_resolution;
            capture = gsr_capture_nvfbc_create(&nvfbc_params);
            if(!capture)
                _exit(1);
        } else {
            gsr_capture_kms_params kms_params;
            kms_params.egl = egl;
            kms_params.display_to_capture = window_str.c_str();
            kms_params.color_depth = color_depth;
            kms_params.color_range = color_range;
            kms_params.record_cursor = record_cursor;
            kms_params.hdr = video_codec_is_hdr(video_codec);
            kms_params.fps = fps;
            kms_params.output_resolution = output_resolution;
            capture = gsr_capture_kms_create(&kms_params);
            if(!capture)
                _exit(1);
        }
    } else {
        if(wayland) {
            fprintf(stderr, "Error: GPU Screen Recorder window capture only works in a pure X11 session. Xwayland is not supported. You can record a monitor instead on wayland\n");
            _exit(2);
        }

        errno = 0;
        src_window_id = strtol(window_str.c_str(), nullptr, 0);
        if(src_window_id == None || errno == EINVAL) {
            fprintf(stderr, "Invalid window number %s\n", window_str.c_str());
            usage();
        }
    }

    if(!capture) {
        gsr_capture_xcomposite_params xcomposite_params;
        xcomposite_params.egl = egl;
        xcomposite_params.window = src_window_id;
        xcomposite_params.follow_focused = follow_focused;
        xcomposite_params.color_range = color_range;
        xcomposite_params.record_cursor = record_cursor;
        xcomposite_params.color_depth = color_depth;
        xcomposite_params.output_resolution = output_resolution;
        capture = gsr_capture_xcomposite_create(&xcomposite_params);
        if(!capture)
            _exit(1);
    }

    return capture;
}

static AVPixelFormat get_pixel_format(VideoCodec video_codec, gsr_gpu_vendor vendor, bool use_software_video_encoder) {
    if(use_software_video_encoder) {
        return AV_PIX_FMT_NV12;
    } else {
        if(video_codec_is_vulkan(video_codec))
            return AV_PIX_FMT_VULKAN;
        else
            return vendor == GSR_GPU_VENDOR_NVIDIA ? AV_PIX_FMT_CUDA : AV_PIX_FMT_VAAPI;
    }
}

struct Arg {
    std::vector<const char*> values;
    bool optional = false;
    bool list = false;

    const char* value() const {
        if(values.empty())
            return nullptr;
        return values.front();
    }
};

static void match_app_audio_input_to_available_apps(const std::vector<AudioInput> &requested_audio_inputs, const std::vector<std::string> &app_audio_names) {
    for(const AudioInput &request_audio_input : requested_audio_inputs) {
        bool match = false;
        for(const std::string &app_name : app_audio_names) {
            if(strcasecmp(app_name.c_str(), request_audio_input.name.c_str()) == 0) {
                match = true;
                break;
            }
        }

        if(!match) {
            fprintf(stderr, "gsr warning: no audio application with the name \"%s\" was found, expected one of the following:\n", request_audio_input.name.c_str());
            for(const std::string &app_name : app_audio_names) {
                fprintf(stderr, "  * %s\n", app_name.c_str());
            }
            fprintf(stderr, "  assuming this is intentional (if you are trying to record audio for applications that haven't started yet).\n");
        }
    }
}

// Manually check if the audio inputs we give exist. This is only needed for pipewire, not pulseaudio.
// Pipewire instead DEFAULTS TO THE DEFAULT AUDIO INPUT. THAT'S RETARDED.
// OH, YOU MISSPELLED THE AUDIO INPUT? FUCK YOU
static std::vector<MergedAudioInputs> parse_audio_inputs(const AudioDevices &audio_devices, const Arg &audio_input_arg, const Arg &app_audio_input_arg, const Arg &app_audio_input_inverted_arg, bool &uses_amix, const std::vector<std::string> &app_audio_names) {
    std::vector<MergedAudioInputs> requested_audio_inputs;
    uses_amix = false;

    for(const char *audio_input : audio_input_arg.values) {
        if(!audio_input || audio_input[0] == '\0')
            continue;

        requested_audio_inputs.push_back({parse_audio_input_arg(audio_input, audio_devices), AudioInputType::DEVICE, false});
        if(requested_audio_inputs.back().audio_inputs.size() > 1)
            uses_amix = true;

        for(AudioInput &request_audio_input : requested_audio_inputs.back().audio_inputs) {
            bool match = false;

            if(!audio_devices.default_output.empty() && request_audio_input.name == "default_output") {
                request_audio_input.name = audio_devices.default_output;
                if(request_audio_input.description.empty())
                    request_audio_input.description = "gsr-Default output";
                match = true;
            }

            if(!audio_devices.default_input.empty() && request_audio_input.name == "default_input") {
                request_audio_input.name = audio_devices.default_input;
                if(request_audio_input.description.empty())
                    request_audio_input.description = "gsr-Default input";
                match = true;
            }

            const AudioInput* existing_audio_input = get_audio_device_by_name(audio_devices.audio_inputs, request_audio_input.name);
            if(existing_audio_input) {
                if(request_audio_input.description.empty())
                    request_audio_input.description = "gsr-" + existing_audio_input->description;
                match = true;
            }

            if(!match) {
                fprintf(stderr, "Error: Audio input device '%s' is not a valid audio device, expected one of:\n", request_audio_input.name.c_str());
                if(!audio_devices.default_output.empty())
                    fprintf(stderr, "    default_output (Default output)\n");
                if(!audio_devices.default_input.empty())
                    fprintf(stderr, "    default_input (Default input)\n");
                for(const auto &audio_device_input : audio_devices.audio_inputs) {
                    fprintf(stderr, "    %s (%s)\n", audio_device_input.name.c_str(), audio_device_input.description.c_str());
                }
                _exit(2);
            }
        }
    }

    for(const char *app_audio_input : app_audio_input_arg.values) {
        if(!app_audio_input || app_audio_input[0] == '\0')
            continue;

        requested_audio_inputs.push_back({parse_app_audio_input_arg(app_audio_input), AudioInputType::APPLICATION, false});
        match_app_audio_input_to_available_apps(requested_audio_inputs.back().audio_inputs, app_audio_names);
    }

    for(const char *app_audio_input : app_audio_input_inverted_arg.values) {
        if(!app_audio_input || app_audio_input[0] == '\0')
            continue;

        requested_audio_inputs.push_back({parse_app_audio_input_arg(app_audio_input), AudioInputType::APPLICATION, true});
        match_app_audio_input_to_available_apps(requested_audio_inputs.back().audio_inputs, app_audio_names);
    }

    return requested_audio_inputs;
}

static AudioCodec select_audio_codec_with_fallback(AudioCodec audio_codec, const std::string &file_extension,bool uses_amix) {
    switch(audio_codec) {
        case AudioCodec::AAC: {
            if(file_extension == "webm") {
                //audio_codec_to_use = "opus";
                audio_codec = AudioCodec::OPUS;
                fprintf(stderr, "Warning: .webm files only support opus audio codec, changing audio codec from aac to opus\n");
            }
            break;
        }
        case AudioCodec::OPUS: {
            // TODO: Also check mpegts?
            if(file_extension != "mp4" && file_extension != "mkv" && file_extension != "webm") {
                //audio_codec_to_use = "aac";
                audio_codec = AudioCodec::AAC;
                fprintf(stderr, "Warning: opus audio codec is only supported by .mp4, .mkv and .webm files, falling back to aac instead\n");
            }
            break;
        }
        case AudioCodec::FLAC: {
            // TODO: Also check mpegts?
            if(file_extension == "webm") {
                //audio_codec_to_use = "opus";
                audio_codec = AudioCodec::OPUS;
                fprintf(stderr, "Warning: .webm files only support opus audio codec, changing audio codec from flac to opus\n");
            } else if(file_extension != "mp4" && file_extension != "mkv") {
                //audio_codec_to_use = "aac";
                audio_codec = AudioCodec::AAC;
                fprintf(stderr, "Warning: flac audio codec is only supported by .mp4 and .mkv files, falling back to aac instead\n");
            } else if(uses_amix) {
                // TODO: remove this? is it true anymore?
                //audio_codec_to_use = "opus";
                audio_codec = AudioCodec::OPUS;
                fprintf(stderr, "Warning: flac audio codec is not supported when mixing audio sources, falling back to opus instead\n");
            }
            break;
        }
    }
    return audio_codec;
}

static const char* video_codec_to_string(VideoCodec video_codec) {
    switch(video_codec) { 
        case VideoCodec::H264:        return "h264";
        case VideoCodec::HEVC:        return "hevc";
        case VideoCodec::HEVC_HDR:    return "hevc_hdr";
        case VideoCodec::HEVC_10BIT:  return "hevc_10bit";
        case VideoCodec::AV1:         return "av1";
        case VideoCodec::AV1_HDR:     return "av1_hdr";
        case VideoCodec::AV1_10BIT:   return "av1_10bit";
        case VideoCodec::VP8:         return "vp8";
        case VideoCodec::VP9:         return "vp9";
        case VideoCodec::H264_VULKAN: return "h264_vulkan";
        case VideoCodec::HEVC_VULKAN: return "hevc_vulkan";
    }
    return "";
}

static bool video_codec_only_supports_low_power_mode(const gsr_supported_video_codecs &supported_video_codecs, VideoCodec video_codec) {
    switch(video_codec) { 
        case VideoCodec::H264:        return supported_video_codecs.h264.low_power;
        case VideoCodec::HEVC:        return supported_video_codecs.hevc.low_power;
        case VideoCodec::HEVC_HDR:    return supported_video_codecs.hevc_hdr.low_power;
        case VideoCodec::HEVC_10BIT:  return supported_video_codecs.hevc_10bit.low_power;
        case VideoCodec::AV1:         return supported_video_codecs.av1.low_power;
        case VideoCodec::AV1_HDR:     return supported_video_codecs.av1_hdr.low_power;
        case VideoCodec::AV1_10BIT:   return supported_video_codecs.av1_10bit.low_power;
        case VideoCodec::VP8:         return supported_video_codecs.vp8.low_power;
        case VideoCodec::VP9:         return supported_video_codecs.vp9.low_power;
        case VideoCodec::H264_VULKAN: return supported_video_codecs.h264.low_power;
        case VideoCodec::HEVC_VULKAN: return supported_video_codecs.hevc.low_power; // TODO: hdr, 10 bit
    }
    return false;
}

static const AVCodec* pick_video_codec(VideoCodec *video_codec, gsr_egl *egl, bool use_software_video_encoder, bool video_codec_auto, const char *video_codec_to_use, bool is_flv, bool *low_power) {
    // TODO: software encoder for hevc, av1, vp8 and vp9
    *low_power = false;

    gsr_supported_video_codecs supported_video_codecs;
    if(!get_supported_video_codecs(egl, *video_codec, use_software_video_encoder, true, &supported_video_codecs)) {
        fprintf(stderr, "Error: failed to query for supported video codecs\n");
        _exit(11);
    }

    const AVCodec *video_codec_f = nullptr;

    switch(*video_codec) {
        case VideoCodec::H264: {
            if(use_software_video_encoder)
                video_codec_f = avcodec_find_encoder_by_name("libx264");
            else if(supported_video_codecs.h264.supported)
                video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
            break;
        }
        case VideoCodec::HEVC: {
            if(supported_video_codecs.hevc.supported)
                video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
            break;
        }
        case VideoCodec::HEVC_HDR: {
            if(supported_video_codecs.hevc_hdr.supported)
                video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
            break;
        }
        case VideoCodec::HEVC_10BIT: {
            if(supported_video_codecs.hevc_10bit.supported)
                video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
            break;
        }
        case VideoCodec::AV1: {
            if(supported_video_codecs.av1.supported)
                video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
            break;
        }
        case VideoCodec::AV1_HDR: {
            if(supported_video_codecs.av1_hdr.supported)
                video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
            break;
        }
        case VideoCodec::AV1_10BIT: {
            if(supported_video_codecs.av1_10bit.supported)
                video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
            break;
        }
        case VideoCodec::VP8: {
            if(supported_video_codecs.vp8.supported)
                video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
            break;
        }
        case VideoCodec::VP9: {
            if(supported_video_codecs.vp9.supported)
                video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
            break;
        }
        case VideoCodec::H264_VULKAN: {
            if(supported_video_codecs.h264.supported)
                video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
            break;
        }
        case VideoCodec::HEVC_VULKAN: {
            // TODO: hdr, 10 bit
            if(supported_video_codecs.hevc.supported)
                video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
            break;
        }
    }

    if(!video_codec_auto && !video_codec_f && !is_flv) {
        switch(*video_codec) {
            case VideoCodec::H264: {
                fprintf(stderr, "Warning: selected video codec h264 is not supported, trying hevc instead\n");
                video_codec_to_use = "hevc";
                if(supported_video_codecs.hevc.supported)
                    video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
                break;
            }
            case VideoCodec::HEVC:
            case VideoCodec::HEVC_HDR:
            case VideoCodec::HEVC_10BIT: {
                fprintf(stderr, "Warning: selected video codec hevc is not supported, trying h264 instead\n");
                video_codec_to_use = "h264";
                *video_codec = VideoCodec::H264;
                if(supported_video_codecs.h264.supported)
                    video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
                break;
            }
            case VideoCodec::AV1:
            case VideoCodec::AV1_HDR:
            case VideoCodec::AV1_10BIT: {
                fprintf(stderr, "Warning: selected video codec av1 is not supported, trying h264 instead\n");
                video_codec_to_use = "h264";
                *video_codec = VideoCodec::H264;
                if(supported_video_codecs.h264.supported)
                    video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
                break;
            }
            case VideoCodec::VP8:
            case VideoCodec::VP9:
                // TODO: Cant fallback to other codec because webm only supports vp8/vp9
                break;
            case VideoCodec::H264_VULKAN: {
                fprintf(stderr, "Warning: selected video codec h264_vulkan is not supported, trying h264 instead\n");
                video_codec_to_use = "h264";
                *video_codec = VideoCodec::H264;
                // Need to do a query again because this time it's without vulkan
                if(!get_supported_video_codecs(egl, *video_codec, use_software_video_encoder, true, &supported_video_codecs)) {
                    fprintf(stderr, "Error: failed to query for supported video codecs\n");
                    _exit(11);
                }
                if(supported_video_codecs.h264.supported)
                    video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
                break;
            }
            case VideoCodec::HEVC_VULKAN: {
                fprintf(stderr, "Warning: selected video codec hevc_vulkan is not supported, trying hevc instead\n");
                video_codec_to_use = "hevc";
                *video_codec = VideoCodec::HEVC;
                // Need to do a query again because this time it's without vulkan
                if(!get_supported_video_codecs(egl, *video_codec, use_software_video_encoder, true, &supported_video_codecs)) {
                    fprintf(stderr, "Error: failed to query for supported video codecs\n");
                    _exit(11);
                }
                if(supported_video_codecs.hevc.supported)
                    video_codec_f = get_ffmpeg_video_codec(*video_codec, egl->gpu_info.vendor);
                break;
            }
        }
    }

    (void)video_codec_to_use;

    if(!video_codec_f) {
        const char *video_codec_name = video_codec_to_string(*video_codec);
        fprintf(stderr, "Error: your gpu does not support '%s' video codec. If you are sure that your gpu does support '%s' video encoding and you are using an AMD/Intel GPU,\n"
            "  then make sure you have installed the GPU specific vaapi packages (intel-media-driver, libva-intel-driver, libva-mesa-driver and linux-firmware).\n"
            "  It's also possible that your distro has disabled hardware accelerated video encoding for '%s' video codec.\n"
            "  This may be the case on corporate distros such as Manjaro, Fedora or OpenSUSE.\n"
            "  You can test this by running 'vainfo | grep VAEntrypointEncSlice' to see if it matches any H264/HEVC/AV1/VP8/VP9 profile.\n"
            "  On such distros, you need to manually install mesa from source to enable H264/HEVC hardware acceleration, or use a more user friendly distro. Alternatively record with AV1 if supported by your GPU.\n"
            "  You can alternatively use the flatpak version of GPU Screen Recorder (https://flathub.org/apps/com.dec05eba.gpu_screen_recorder) which bypasses system issues with patented H264/HEVC codecs.\n"
            "  Make sure you have mesa-extra freedesktop runtime installed when using the flatpak (this should be the default), which can be installed with this command:\n"
            "  flatpak install --system org.freedesktop.Platform.GL.default//23.08-extra\n"
            "  If your GPU doesn't support hardware accelerated video encoding then you can use '-encoder cpu' option to encode with your cpu instead.\n", video_codec_name, video_codec_name, video_codec_name);
        _exit(2);
    }

    *low_power = video_codec_only_supports_low_power_mode(supported_video_codecs, *video_codec);

    return video_codec_f;
}

static const AVCodec* select_video_codec_with_fallback(VideoCodec *video_codec, const char *video_codec_to_use, const char *file_extension, bool use_software_video_encoder, gsr_egl *egl, bool *low_power) {
    const bool video_codec_auto = strcmp(video_codec_to_use, "auto") == 0;
    if(video_codec_auto) {
        if(strcmp(file_extension, "webm") == 0) {
            fprintf(stderr, "Info: using vp8 encoder because a codec was not specified and the file extension is .webm\n");
            video_codec_to_use = "vp8";
            *video_codec = VideoCodec::VP8;
        } else {
            fprintf(stderr, "Info: using h264 encoder because a codec was not specified\n");
            video_codec_to_use = "h264";
            *video_codec = VideoCodec::H264;
        }
    }

    // TODO: Allow hevc, vp9 and av1 in (enhanced) flv (supported since ffmpeg 6.1)
    const bool is_flv = strcmp(file_extension, "flv") == 0;
    if(is_flv) {
        if(*video_codec != VideoCodec::H264) {
            video_codec_to_use = "h264";
            *video_codec = VideoCodec::H264;
            fprintf(stderr, "Warning: hevc/av1 is not compatible with flv, falling back to h264 instead.\n");
        }

        // if(audio_codec != AudioCodec::AAC) {
        //     audio_codec_to_use = "aac";
        //     audio_codec = AudioCodec::AAC;
        //     fprintf(stderr, "Warning: flv only supports aac, falling back to aac instead.\n");
        // }
    }

    const bool is_hls = strcmp(file_extension, "m3u8") == 0;
    if(is_hls) {
        if(video_codec_is_av1(*video_codec)) {
            video_codec_to_use = "hevc";
            *video_codec = VideoCodec::HEVC;
            fprintf(stderr, "Warning: av1 is not compatible with hls (m3u8), falling back to hevc instead.\n");
        }

        // if(audio_codec != AudioCodec::AAC) {
        //     audio_codec_to_use = "aac";
        //     audio_codec = AudioCodec::AAC;
        //     fprintf(stderr, "Warning: hls (m3u8) only supports aac, falling back to aac instead.\n");
        // }
    }

    if(use_software_video_encoder && *video_codec != VideoCodec::H264) {
        fprintf(stderr, "Error: \"-encoder cpu\" option is currently only available when using h264 codec option (-k)\n");
        usage();
    }

    return pick_video_codec(video_codec, egl, use_software_video_encoder, video_codec_auto, video_codec_to_use, is_flv, low_power);
}

static std::vector<AudioDevice> create_device_audio_inputs(const std::vector<AudioInput> &audio_inputs, AVCodecContext *audio_codec_context, int num_channels, double num_audio_frames_shift, std::vector<AVFilterContext*> &src_filter_ctx, bool use_amix) {
    std::vector<AudioDevice> audio_track_audio_devices;
    for(size_t i = 0; i < audio_inputs.size(); ++i) {
        const auto &audio_input = audio_inputs[i];
        AVFilterContext *src_ctx = nullptr;
        if(use_amix)
            src_ctx = src_filter_ctx[i];

        AudioDevice audio_device;
        audio_device.audio_input = audio_input;
        audio_device.src_filter_ctx = src_ctx;

        if(audio_input.name.empty()) {
            audio_device.sound_device.handle = NULL;
            audio_device.sound_device.frames = 0;
        } else {
            if(sound_device_get_by_name(&audio_device.sound_device, audio_input.name.c_str(), audio_input.description.c_str(), num_channels, audio_codec_context->frame_size, audio_codec_context_get_audio_format(audio_codec_context)) != 0) {
                fprintf(stderr, "Error: failed to get \"%s\" audio device\n", audio_input.name.c_str());
                _exit(1);
            }
        }

        audio_device.frame = create_audio_frame(audio_codec_context);
        audio_device.frame->pts = -audio_codec_context->frame_size * num_audio_frames_shift;

        audio_track_audio_devices.push_back(std::move(audio_device));
    }
    return audio_track_audio_devices;
}

#ifdef GSR_APP_AUDIO
static AudioDevice create_application_audio_audio_input(const MergedAudioInputs &merged_audio_inputs, AVCodecContext *audio_codec_context, int num_channels, double num_audio_frames_shift, gsr_pipewire_audio *pipewire_audio) {
    AudioDevice audio_device;
    audio_device.frame = create_audio_frame(audio_codec_context);
    audio_device.frame->pts = -audio_codec_context->frame_size * num_audio_frames_shift;

    char random_str[8];
    if(!generate_random_characters_standard_alphabet(random_str, sizeof(random_str))) {
        fprintf(stderr, "gsr error: ailed to generate random string\n");
        _exit(1);
    }
    audio_device.combined_sink_name = "gsr-combined-";
    audio_device.combined_sink_name.append(random_str, sizeof(random_str));

    if(sound_device_create_combined_sink_connect(&audio_device.sound_device, audio_device.combined_sink_name.c_str(), num_channels, audio_codec_context->frame_size, audio_codec_context_get_audio_format(audio_codec_context)) != 0) {
        fprintf(stderr, "Error: failed to setup audio recording to combined sink\n");
        _exit(1);
    }

    std::vector<const char*> app_names;
    app_names.reserve(merged_audio_inputs.audio_inputs.size());
    for(const auto &audio_input : merged_audio_inputs.audio_inputs) {
        app_names.push_back(audio_input.name.c_str());
    }

    if(merged_audio_inputs.inverted) {
        if(!gsr_pipewire_audio_add_link_from_apps_to_sink_inverted(pipewire_audio, app_names.data(), app_names.size(), audio_device.combined_sink_name.c_str())) {
            fprintf(stderr, "gsr error: failed to add application audio link\n");
            _exit(1);
        }
    } else {
        if(!gsr_pipewire_audio_add_link_from_apps_to_sink(pipewire_audio, app_names.data(), app_names.size(), audio_device.combined_sink_name.c_str())) {
            fprintf(stderr, "gsr error: failed to add application audio link\n");
            _exit(1);
        }
    }

    return audio_device;
}
#endif

int main(int argc, char **argv) {
    setlocale(LC_ALL, "C"); // Sigh... stupid C

    signal(SIGINT, stop_handler);
    signal(SIGUSR1, save_replay_handler);
    signal(SIGUSR2, toggle_pause_handler);

    // Stop nvidia driver from buffering frames
    setenv("__GL_MaxFramesAllowed", "1", true);
    // If this is set to 1 then cuGraphicsGLRegisterImage will fail for egl context with error: invalid OpenGL or DirectX context,
    // so we overwrite it
    setenv("__GL_THREADED_OPTIMIZATIONS", "0", true);
    // Forces low latency encoding mode. Use this environment variable until vaapi supports setting this as a parameter.
    // The downside of this is that it always uses maximum power, which is not ideal for replay mode that runs on system startup.
    // This option was added in mesa 24.1.4, released in july 17, 2024.
    // TODO: Add an option to enable/disable this?
    // Seems like the performance issue is not in encoding, but rendering the frame.
    // Some frames end up taking 10 times longer. Seems to be an issue with amd gpu power management when letting the application sleep on the cpu side?
    setenv("AMD_DEBUG", "lowlatencyenc", true);
    // Some people set this to nvidia (for nvdec) or vdpau (for nvidia vdpau), which breaks gpu screen recorder since
    // nvidia doesn't support vaapi and nvidia-vaapi-driver doesn't support encoding yet.
    // Let vaapi find the match vaapi driver instead of forcing a specific one.
    unsetenv("LIBVA_DRIVER_NAME");
    // Some people set this to force all applications to vsync on nvidia, but this makes eglSwapBuffers never return.
    unsetenv("__GL_SYNC_TO_VBLANK");
    // Same as above, but for amd/intel
    unsetenv("vblank_mode");

    if(argc <= 1)
        usage_full();

    if(argc == 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0))
        usage_full();

    if(argc == 2 && strcmp(argv[1], "--info") == 0) {
        info_command();
        _exit(0);
    }

    if(argc == 2 && strcmp(argv[1], "--list-audio-devices") == 0) {
        list_audio_devices_command();
        _exit(0);
    }

    if(argc == 2 && strcmp(argv[1], "--list-application-audio") == 0) {
        list_application_audio_command();
        _exit(0);
    }

    if(argc == 2 && strcmp(argv[1], "--list-capture-options") == 0) {
        list_capture_options_command();
        _exit(0);
    }

    if(argc == 2 && strcmp(argv[1], "--version") == 0) {
        puts(GSR_VERSION);
        _exit(0);
    }

    //av_log_set_level(AV_LOG_TRACE);

    std::map<std::string, Arg> args = {
        { "-w", Arg { {}, false, false } },
        { "-c", Arg { {}, true, false } },
        { "-f", Arg { {}, false, false } },
        { "-s", Arg { {}, true, false } },
        { "-a", Arg { {}, true, true } },
#ifdef GSR_APP_AUDIO
        { "-aa", Arg { {}, true, true } },
        { "-aai", Arg { {}, true, true } },
#endif
        { "-q", Arg { {}, true, false } },
        { "-o", Arg { {}, true, false } },
        { "-r", Arg { {}, true, false } },
        { "-k", Arg { {}, true, false } },
        { "-ac", Arg { {}, true, false } },
        { "-ab", Arg { {}, true, false } },
        { "-oc", Arg { {}, true, false } },
        { "-fm", Arg { {}, true, false } },
        { "-bm", Arg { {}, true, false } },
        { "-pixfmt", Arg { {}, true, false } },
        { "-v", Arg { {}, true, false } },
        { "-df", Arg { {}, true, false } },
        { "-sc", Arg { {}, true, false } },
        { "-cr", Arg { {}, true, false } },
        { "-cursor", Arg { {}, true, false } },
        { "-keyint", Arg { {}, true, false } },
        { "-restore-portal-session", Arg { {}, true, false } },
        { "-portal-session-token-filepath", Arg { {}, true, false } },
        { "-encoder", Arg { {}, true, false } },
    };

    for(int i = 1; i < argc; i += 2) {
        auto it = args.find(argv[i]);
        if(it == args.end()) {
            fprintf(stderr, "Invalid argument '%s'\n", argv[i]);
            usage();
        }

        if(!it->second.values.empty() && !it->second.list) {
            fprintf(stderr, "Expected argument '%s' to only be specified once\n", argv[i]);
            usage();
        }

        if(i + 1 >= argc) {
            fprintf(stderr, "Missing value for argument '%s'\n", argv[i]);
            usage();
        }

        it->second.values.push_back(argv[i + 1]);
    }

    for(auto &it : args) {
        if(!it.second.optional && !it.second.value()) {
            fprintf(stderr, "Missing argument '%s'\n", it.first.c_str());
            usage();
        }
    }

    VideoCodec video_codec = VideoCodec::H264;
    const char *video_codec_to_use = args["-k"].value();
    if(!video_codec_to_use)
        video_codec_to_use = "auto";

    if(strcmp(video_codec_to_use, "h264") == 0) {
        video_codec = VideoCodec::H264;
    } else if(strcmp(video_codec_to_use, "h265") == 0 || strcmp(video_codec_to_use, "hevc") == 0) {
        video_codec = VideoCodec::HEVC;
    } else if(strcmp(video_codec_to_use, "hevc_hdr") == 0) {
        video_codec = VideoCodec::HEVC_HDR;
    } else if(strcmp(video_codec_to_use, "hevc_10bit") == 0) {
        video_codec = VideoCodec::HEVC_10BIT;
    } else if(strcmp(video_codec_to_use, "av1") == 0) {
        video_codec = VideoCodec::AV1;
    } else if(strcmp(video_codec_to_use, "av1_hdr") == 0) {
        video_codec = VideoCodec::AV1_HDR;
    } else if(strcmp(video_codec_to_use, "av1_10bit") == 0) {
        video_codec = VideoCodec::AV1_10BIT;
    } else if(strcmp(video_codec_to_use, "vp8") == 0) {
        video_codec = VideoCodec::VP8;
    } else if(strcmp(video_codec_to_use, "vp9") == 0) {
        video_codec = VideoCodec::VP9;
    //} else if(strcmp(video_codec_to_use, "h264_vulkan") == 0) {
    //    video_codec = VideoCodec::H264_VULKAN;
    //} else if(strcmp(video_codec_to_use, "hevc_vulkan") == 0) {
    //    video_codec = VideoCodec::HEVC_VULKAN;
    } else if(strcmp(video_codec_to_use, "auto") != 0) {
        fprintf(stderr, "Error: -k should either be either 'auto', 'h264', 'hevc', 'av1', 'vp8', 'vp9', 'hevc_hdr', 'av1_hdr', 'hevc_10bit' or 'av1_10bit', got: '%s'\n", video_codec_to_use);
        usage();
    }

    AudioCodec audio_codec = AudioCodec::OPUS;
    const char *audio_codec_to_use = args["-ac"].value();
    if(!audio_codec_to_use)
        audio_codec_to_use = "opus";

    if(strcmp(audio_codec_to_use, "aac") == 0) {
        audio_codec = AudioCodec::AAC;
    } else if(strcmp(audio_codec_to_use, "opus") == 0) {
        audio_codec = AudioCodec::OPUS;
    } else if(strcmp(audio_codec_to_use, "flac") == 0) {
        audio_codec = AudioCodec::FLAC;
    } else {
        fprintf(stderr, "Error: -ac should either be either 'aac', 'opus' or 'flac', got: '%s'\n", audio_codec_to_use);
        usage();
    }

    if(audio_codec == AudioCodec::FLAC) {
        fprintf(stderr, "Warning: flac audio codec is temporary disabled, using opus audio codec instead\n");
        audio_codec_to_use = "opus";
        audio_codec = AudioCodec::OPUS;
    }

    int64_t audio_bitrate = 0;
    const char *audio_bitrate_str = args["-ab"].value();
    if(audio_bitrate_str) {
        if(sscanf(audio_bitrate_str, "%" PRIi64, &audio_bitrate) != 1) {
            fprintf(stderr, "Error: -ab argument \"%s\" is not an integer\n", audio_bitrate_str);
            usage();
        }

        if(audio_bitrate < 0) {
            fprintf(stderr, "Error: -ab is expected to be 0 or larger, got %" PRIi64 "\n", audio_bitrate);
            usage();
        }

        if(audio_bitrate > 50000) {
            fprintf(stderr, "Error: audio bitrate %" PRIi64 "is too high. It's expected to be in kbps, normally in the range 54-300\n", audio_bitrate);
            usage();
        }

        audio_bitrate *= 1000LL;
    }

    float keyint = 2.0;
    const char *keyint_str = args["-keyint"].value();
    if(keyint_str) {
        if(sscanf(keyint_str, "%f", &keyint) != 1) {
            fprintf(stderr, "Error: -keyint argument \"%s\" is not a floating point number\n", keyint_str);
            usage();
        }

        if(keyint < 0) {
            fprintf(stderr, "Error: -keyint is expected to be 0 or larger, got %f\n", keyint);
            usage();
        }
    }

    bool use_software_video_encoder = false;
    const char *encoder_str = args["-encoder"].value();
    if(encoder_str) {
        if(strcmp(encoder_str, "gpu") == 0) {
            use_software_video_encoder = false;
        } else if(strcmp(encoder_str, "cpu") == 0) {
            use_software_video_encoder = true;
        } else {
            fprintf(stderr, "Error: -encoder is expected to be 'gpu' or 'cpu', was '%s'\n", encoder_str);
            usage();
        }
    }

    bool overclock = false;
    const char *overclock_str = args["-oc"].value();
    if(!overclock_str)
        overclock_str = "no";

    if(strcmp(overclock_str, "yes") == 0) {
        overclock = true;
    } else if(strcmp(overclock_str, "no") == 0) {
        overclock = false;
    } else {
        fprintf(stderr, "Error: -oc should either be either 'yes' or 'no', got: '%s'\n", overclock_str);
        usage();
    }

    bool verbose = true;
    const char *verbose_str = args["-v"].value();
    if(!verbose_str)
        verbose_str = "yes";

    if(strcmp(verbose_str, "yes") == 0) {
        verbose = true;
    } else if(strcmp(verbose_str, "no") == 0) {
        verbose = false;
    } else {
        fprintf(stderr, "Error: -v should either be either 'yes' or 'no', got: '%s'\n", verbose_str);
        usage();
    }

    bool record_cursor = true;
    const char *record_cursor_str = args["-cursor"].value();
    if(!record_cursor_str)
        record_cursor_str = "yes";

    if(strcmp(record_cursor_str, "yes") == 0) {
        record_cursor = true;
    } else if(strcmp(record_cursor_str, "no") == 0) {
        record_cursor = false;
    } else {
        fprintf(stderr, "Error: -cursor should either be either 'yes' or 'no', got: '%s'\n", record_cursor_str);
        usage();
    }

    bool date_folders = false;
    const char *date_folders_str = args["-df"].value();
    if(!date_folders_str)
        date_folders_str = "no";

    if(strcmp(date_folders_str, "yes") == 0) {
        date_folders = true;
    } else if(strcmp(date_folders_str, "no") == 0) {
        date_folders = false;
    } else {
        fprintf(stderr, "Error: -df should either be either 'yes' or 'no', got: '%s'\n", date_folders_str);
        usage();
    }

    bool restore_portal_session = false;
    const char *restore_portal_session_str = args["-restore-portal-session"].value();
    if(!restore_portal_session_str)
        restore_portal_session_str = "no";

    if(strcmp(restore_portal_session_str, "yes") == 0) {
        restore_portal_session = true;
    } else if(strcmp(restore_portal_session_str, "no") == 0) {
        restore_portal_session = false;
    } else {
        fprintf(stderr, "Error: -restore-portal-session should either be either 'yes' or 'no', got: '%s'\n", restore_portal_session_str);
        usage();
    }

    const char *portal_session_token_filepath = args["-portal-session-token-filepath"].value();
    if(portal_session_token_filepath) {
        int len = strlen(portal_session_token_filepath);
        if(len > 0 && portal_session_token_filepath[len - 1] == '/') {
            fprintf(stderr, "Error: -portal-session-token-filepath should be a path to a file but it ends with a /: %s\n", portal_session_token_filepath);
            _exit(1);
        }
    }

    const char *recording_saved_script = args["-sc"].value();
    if(recording_saved_script) {
        struct stat buf;
        if(stat(recording_saved_script, &buf) == -1 || !S_ISREG(buf.st_mode)) {
            fprintf(stderr, "Error: Script \"%s\" either doesn't exist or it's not a file\n", recording_saved_script);
            usage();
        }

        if(!(buf.st_mode & S_IXUSR)) {
            fprintf(stderr, "Error: Script \"%s\" is not executable\n", recording_saved_script);
            usage();
        }
    }

    PixelFormat pixel_format = PixelFormat::YUV420;
    const char *pixfmt = args["-pixfmt"].value();
    if(!pixfmt)
        pixfmt = "yuv420";

    if(strcmp(pixfmt, "yuv420") == 0) {
        pixel_format = PixelFormat::YUV420;
    } else if(strcmp(pixfmt, "yuv444") == 0) {
        pixel_format = PixelFormat::YUV444;
    } else {
        fprintf(stderr, "Error: -pixfmt should either be either 'yuv420', or 'yuv444', got: '%s'\n", pixfmt);
        usage();
    }

    const Arg &audio_input_arg = args["-a"];
    const Arg &app_audio_input_arg = args["-aa"];
    const Arg &app_audio_input_inverted_arg = args["-aai"];

    AudioDevices audio_devices;
    if(!audio_input_arg.values.empty())
        audio_devices = get_pulseaudio_inputs();

    bool uses_app_audio = false;
    if(!app_audio_input_arg.values.empty() || !app_audio_input_inverted_arg.values.empty())
        uses_app_audio = true;

    std::vector<std::string> app_audio_names;
#ifdef GSR_APP_AUDIO
    gsr_pipewire_audio pipewire_audio;
    memset(&pipewire_audio, 0, sizeof(pipewire_audio));
    if(uses_app_audio) {
        if(!gsr_pipewire_audio_init(&pipewire_audio)) {
            fprintf(stderr, "gsr error: failed to setup PipeWire audio for application audio capture. The likely reason for this failure is that your sound server is not PipeWire. The options -aa and -aai are only available when running PipeWire audio server.\n");
            _exit(2);
        }

        if(!pulseaudio_server_is_pipewire()) {
            fprintf(stderr, "gsr error: your sound server is not PipeWire. The options -aa and -aai are only available when running PipeWire audio server.\n");
            _exit(2);
        }

        gsr_pipewire_audio_for_each_app(&pipewire_audio, [](const char *app_name, void *userdata) {
            std::vector<std::string> *app_audio_names = (std::vector<std::string>*)userdata;
            app_audio_names->push_back(app_name);
            return true;
        }, &app_audio_names);
    }
#endif

    bool uses_amix = false;
    std::vector<MergedAudioInputs> requested_audio_inputs = parse_audio_inputs(audio_devices, audio_input_arg, app_audio_input_arg, app_audio_input_inverted_arg, uses_amix, app_audio_names);

    const char *container_format = args["-c"].value();
    if(container_format && strcmp(container_format, "mkv") == 0)
        container_format = "matroska";

    int fps = atoi(args["-f"].value());
    if(fps == 0) {
        fprintf(stderr, "Invalid fps argument: %s\n", args["-f"].value());
        _exit(1);
    }
    if(fps < 1)
        fps = 1;

    int replay_buffer_size_secs = -1;
    const char *replay_buffer_size_secs_str = args["-r"].value();
    if(replay_buffer_size_secs_str) {
        replay_buffer_size_secs = atoi(replay_buffer_size_secs_str);
        if(replay_buffer_size_secs < 5 || replay_buffer_size_secs > 1200) {
            fprintf(stderr, "Error: option -r has to be between 5 and 1200, was: %s\n", replay_buffer_size_secs_str);
            _exit(1);
        }
        replay_buffer_size_secs += std::ceil(keyint); // Add a few seconds to account of lost packets because of non-keyframe packets skipped
    }

    std::string window_str = args["-w"].value();
    const bool is_portal_capture = strcmp(window_str.c_str(), "portal") == 0;

    if(!restore_portal_session && is_portal_capture) {
        fprintf(stderr, "gsr info: option '-w portal' was used without '-restore-portal-session yes'. The previous screencast session will be ignored\n");
    }

    bool wayland = false;
    Display *dpy = XOpenDisplay(nullptr);
    if (!dpy) {
        wayland = true;
        fprintf(stderr, "Warning: failed to connect to the X server. Assuming wayland is running without Xwayland\n");
    }

    XSetErrorHandler(x11_error_handler);
    XSetIOErrorHandler(x11_io_error_handler);

    if(!wayland)
        wayland = is_xwayland(dpy);

    if(!wayland && is_using_prime_run()) {
        // Disable prime-run and similar options as it doesn't work, the monitor to capture has to be run on the same device.
        // This is fine on wayland since nvidia uses drm interface there and the monitor query checks the monitors connected
        // to the drm device.
        fprintf(stderr, "Warning: use of prime-run on X11 is not supported. Disabling prime-run\n");
        disable_prime_run();
    }

    if(is_portal_capture && is_using_prime_run()) {
        fprintf(stderr, "Warning: use of prime-run with -w portal option is currently not supported. Disabling prime-run\n");
        disable_prime_run();
    }

    if(video_codec_is_hdr(video_codec) && !wayland) {
        fprintf(stderr, "Error: hdr video codec option %s is not available on X11\n", video_codec_to_use);
        _exit(1);
    }

    const bool is_monitor_capture = strcmp(window_str.c_str(), "focused") != 0 && !is_portal_capture && contains_non_hex_number(window_str.c_str());
    gsr_egl egl;
    if(!gsr_egl_load(&egl, dpy, wayland, is_monitor_capture)) {
        fprintf(stderr, "gsr error: failed to load opengl\n");
        _exit(1);
    }

    if(egl.gpu_info.is_steam_deck) {
        fprintf(stderr, "gsr warning: steam deck has multiple driver issues. One of them has been reported here: https://github.com/ValveSoftware/SteamOS/issues/1609\n"
            "If you have issues with GPU Screen Recorder on steam deck that you don't have on a desktop computer then report the issue to Valve and/or AMD.\n");
    }

    bool very_old_gpu = false;

    if(egl.gpu_info.vendor == GSR_GPU_VENDOR_NVIDIA && egl.gpu_info.gpu_version != 0 && egl.gpu_info.gpu_version < 900) {
        fprintf(stderr, "Info: your gpu appears to be very old (older than maxwell architecture). Switching to lower preset\n");
        very_old_gpu = true;
    }

    if(egl.gpu_info.vendor != GSR_GPU_VENDOR_NVIDIA && overclock) {
        fprintf(stderr, "Info: overclock option has no effect on amd/intel, ignoring option\n");
        overclock = false;
    }

    if(egl.gpu_info.vendor == GSR_GPU_VENDOR_NVIDIA && overclock && wayland) {
        fprintf(stderr, "Info: overclocking is not possible on nvidia on wayland, ignoring option\n");
        overclock = false;
    }

    egl.card_path[0] = '\0';
    if(monitor_capture_use_drm(&egl, wayland)) {
        // TODO: Allow specifying another card, and in other places
        if(!gsr_get_valid_card_path(&egl, egl.card_path, is_monitor_capture)) {
            fprintf(stderr, "Error: no /dev/dri/cardX device found. Make sure that you have at least one monitor connected or record a single window instead on X11 or record with the -w portal option\n");
            _exit(2);
        }
    }

    // if(wayland && is_monitor_capture) {
    //     fprintf(stderr, "gsr warning: it's not possible to sync video to recorded monitor exactly on wayland when recording a monitor."
    //         " If you experience stutter in the video then record with portal capture option instead (-w portal) or use X11 instead\n");
    // }

    // TODO: Fix constant framerate not working properly on amd/intel because capture framerate gets locked to the same framerate as
    // game framerate, which doesn't work well when you need to encode multiple duplicate frames (AMD/Intel is slow at encoding!).
    // It also appears to skip audio frames on nvidia wayland? why? that should be fine, but it causes video stuttering because of audio/video sync.
    FramerateMode framerate_mode = FramerateMode::VARIABLE;
    const char *framerate_mode_str = args["-fm"].value();
    if(!framerate_mode_str)
        framerate_mode_str = "vfr";

    if(strcmp(framerate_mode_str, "cfr") == 0) {
        framerate_mode = FramerateMode::CONSTANT;
    } else if(strcmp(framerate_mode_str, "vfr") == 0) {
        framerate_mode = FramerateMode::VARIABLE;
    } else if(strcmp(framerate_mode_str, "content") == 0) {
        framerate_mode = FramerateMode::CONTENT;
    } else {
        fprintf(stderr, "Error: -fm should either be either 'cfr', 'vfr' or 'content', got: '%s'\n", framerate_mode_str);
        usage();
    }

    if(framerate_mode == FramerateMode::CONTENT && wayland && !is_portal_capture) {
        fprintf(stderr, "Error: -fm 'content' is currently only supported on X11 or when using portal capture option\n");
        usage();
    }

    BitrateMode bitrate_mode = BitrateMode::QP;
    const char *bitrate_mode_str = args["-bm"].value();
    if(!bitrate_mode_str)
        bitrate_mode_str = "auto";

    if(strcmp(bitrate_mode_str, "qp") == 0) {
        bitrate_mode = BitrateMode::QP;
    } else if(strcmp(bitrate_mode_str, "vbr") == 0) {
        bitrate_mode = BitrateMode::VBR;
    } else if(strcmp(bitrate_mode_str, "cbr") == 0) {
        bitrate_mode = BitrateMode::CBR;
    } else if(strcmp(bitrate_mode_str, "auto") != 0) {
        fprintf(stderr, "Error: -bm should either be either 'auto', 'qp', 'vbr' or 'cbr', got: '%s'\n", bitrate_mode_str);
        usage();
    }

    if(strcmp(bitrate_mode_str, "auto") == 0) {
        // QP is broken on steam deck, see https://github.com/ValveSoftware/SteamOS/issues/1609
        bitrate_mode = egl.gpu_info.is_steam_deck ? BitrateMode::VBR : BitrateMode::QP;
    }

    if(egl.gpu_info.is_steam_deck && bitrate_mode == BitrateMode::QP) {
        fprintf(stderr, "Warning: qp bitrate mode is not supported on Steam Deck because of Steam Deck driver bugs. Using vbr instead\n");
        bitrate_mode = BitrateMode::VBR;
    }

    if(use_software_video_encoder && bitrate_mode == BitrateMode::VBR) {
        fprintf(stderr, "Warning: bitrate mode has been forcefully set to qp because software encoding option doesn't support vbr option\n");
        bitrate_mode = BitrateMode::QP;
    }

    const char *quality_str = args["-q"].value();
    VideoQuality quality = VideoQuality::VERY_HIGH;
    int64_t video_bitrate = 0;

    if(bitrate_mode == BitrateMode::CBR) {
        if(!quality_str) {
            fprintf(stderr, "Error: option '-q' is required when using '-bm cbr' option\n");
            usage();
        }

        if(sscanf(quality_str, "%" PRIi64, &video_bitrate) != 1) {
            fprintf(stderr, "Error: -q argument \"%s\" is not an integer value. When using '-bm cbr' option '-q' is expected to be an integer value\n", quality_str);
            usage();
        }

        if(video_bitrate < 0) {
            fprintf(stderr, "Error: -q is expected to be 0 or larger, got %" PRIi64 "\n", video_bitrate);
            usage();
        }

        video_bitrate *= 1000LL;
    } else {
        if(!quality_str)
            quality_str = "very_high";

        if(strcmp(quality_str, "medium") == 0) {
            quality = VideoQuality::MEDIUM;
        } else if(strcmp(quality_str, "high") == 0) {
            quality = VideoQuality::HIGH;
        } else if(strcmp(quality_str, "very_high") == 0) {
            quality = VideoQuality::VERY_HIGH;
        } else if(strcmp(quality_str, "ultra") == 0) {
            quality = VideoQuality::ULTRA;
        } else {
            fprintf(stderr, "Error: -q should either be either 'medium', 'high', 'very_high' or 'ultra', got: '%s'\n", quality_str);
            usage();
        }
    }

    gsr_color_range color_range = GSR_COLOR_RANGE_LIMITED;
    const char *color_range_str = args["-cr"].value();
    if(!color_range_str)
        color_range_str = "limited";

    if(strcmp(color_range_str, "limited") == 0) {
        color_range = GSR_COLOR_RANGE_LIMITED;
    } else if(strcmp(color_range_str, "full") == 0) {
        color_range = GSR_COLOR_RANGE_FULL;
    } else {
        fprintf(stderr, "Error: -cr should either be either 'limited' or 'full', got: '%s'\n", color_range_str);
        usage();
    }

    const char *output_resolution_str = args["-s"].value();
    if(!output_resolution_str && strcmp(window_str.c_str(), "focused") == 0) {
        fprintf(stderr, "Error: option -s is required when using -w focused option\n");
        usage();
    }

    vec2i output_resolution = {0, 0};
    if(output_resolution_str) {
        if(sscanf(output_resolution_str, "%dx%d", &output_resolution.x, &output_resolution.y) != 2) {
            fprintf(stderr, "Error: invalid value for option -s '%s', expected a value in format WxH\n", output_resolution_str);
            usage();
        }

        if(output_resolution.x < 0 || output_resolution.y < 0) {
            fprintf(stderr, "Error: invalud value for option -s '%s', expected width and height to be greater or equal to 0\n", output_resolution_str);
            usage();
        }
    }

    bool is_livestream = false;
    const char *filename = args["-o"].value();
    if(filename) {
        is_livestream = is_livestream_path(filename);
        if(is_livestream) {
            if(replay_buffer_size_secs != -1) {
                fprintf(stderr, "Error: replay mode is not applicable to live streaming\n");
                _exit(1);
            }
        } else {
            if(replay_buffer_size_secs == -1) {
                char directory_buf[PATH_MAX];
                snprintf(directory_buf, sizeof(directory_buf), "%s", filename);
                char *directory = dirname(directory_buf);
                if(strcmp(directory, ".") != 0 && strcmp(directory, "/") != 0) {
                    if(create_directory_recursive(directory) != 0) {
                        fprintf(stderr, "Error: failed to create directory for output file: %s\n", filename);
                        _exit(1);
                    }
                }
            } else {
                if(!container_format) {
                    fprintf(stderr, "Error: option -c is required when using option -r\n");
                    usage();
                }

                struct stat buf;
                if(stat(filename, &buf) != -1 && !S_ISDIR(buf.st_mode)) {
                    fprintf(stderr, "Error: File \"%s\" exists but it's not a directory\n", filename);
                    usage();
                }
            }
        }
    } else {
        if(replay_buffer_size_secs == -1) {
            filename = "/dev/stdout";
        } else {
            fprintf(stderr, "Error: Option -o is required when using option -r\n");
            usage();
        }

        if(!container_format) {
            fprintf(stderr, "Error: option -c is required when not using option -o\n");
            usage();
        }
    }

    const bool is_output_piped = strcmp(filename, "/dev/stdout") == 0;

    AVFormatContext *av_format_context;
    // The output format is automatically guessed by the file extension
    avformat_alloc_output_context2(&av_format_context, nullptr, container_format, filename);
    if (!av_format_context) {
        if(container_format) {
            fprintf(stderr, "Error: Container format '%s' (argument -c) is not valid\n", container_format);
        } else {
            fprintf(stderr, "Error: Failed to deduce container format from file extension. Use the '-c' option to specify container format\n");
            usage();
        }
        _exit(1);
    }

    const AVOutputFormat *output_format = av_format_context->oformat;

    std::string file_extension = output_format->extensions;
    {
        size_t comma_index = file_extension.find(',');
        if(comma_index != std::string::npos)
            file_extension = file_extension.substr(0, comma_index);
    }

    const bool force_no_audio_offset = is_livestream || is_output_piped || (file_extension != "mp4" && file_extension != "mkv" && file_extension != "webm");
    const double target_fps = 1.0 / (double)fps;

    if(video_codec_is_hdr(video_codec) && is_portal_capture) {
        fprintf(stderr, "Warning: portal capture option doesn't support hdr yet (PipeWire doesn't support hdr), the video will be tonemapped from hdr to sdr\n");
        video_codec = hdr_video_codec_to_sdr_video_codec(video_codec);
    }

    audio_codec = select_audio_codec_with_fallback(audio_codec, file_extension, uses_amix);
    bool low_power = false;
    const AVCodec *video_codec_f = select_video_codec_with_fallback(&video_codec, video_codec_to_use, file_extension.c_str(), use_software_video_encoder, &egl, &low_power);

    const gsr_color_depth color_depth = video_codec_to_bit_depth(video_codec);
    gsr_capture *capture = create_capture_impl(window_str, output_resolution, wayland, &egl, fps, video_codec, color_range, record_cursor, use_software_video_encoder, restore_portal_session, portal_session_token_filepath, color_depth);

    // (Some?) livestreaming services require at least one audio track to work.
    // If not audio is provided then create one silent audio track.
    if(is_livestream && requested_audio_inputs.empty()) {
        fprintf(stderr, "Info: live streaming but no audio track was added. Adding a silent audio track\n");
        MergedAudioInputs mai;
        mai.audio_inputs.push_back({ "", "gsr-silent" });
        requested_audio_inputs.push_back(std::move(mai));
    }

    if(is_livestream && recording_saved_script) {
        fprintf(stderr, "Warning: live stream detected, -sc script is ignored\n");
        recording_saved_script = nullptr;
    }

    AVStream *video_stream = nullptr;
    std::vector<AudioTrack> audio_tracks;
    const bool hdr = video_codec_is_hdr(video_codec);
    const bool low_latency_recording = is_livestream || is_output_piped;

    const enum AVPixelFormat video_pix_fmt = get_pixel_format(video_codec, egl.gpu_info.vendor, use_software_video_encoder);
    AVCodecContext *video_codec_context = create_video_codec_context(video_pix_fmt, quality, fps, video_codec_f, low_latency_recording, egl.gpu_info.vendor, framerate_mode, hdr, color_range, keyint, use_software_video_encoder, bitrate_mode, video_codec, video_bitrate);
    if(replay_buffer_size_secs == -1)
        video_stream = create_stream(av_format_context, video_codec_context);

    AVFrame *video_frame = av_frame_alloc();
    if(!video_frame) {
        fprintf(stderr, "Error: Failed to allocate video frame\n");
        _exit(1);
    }
    video_frame->format = video_codec_context->pix_fmt;
    video_frame->width = video_codec_context->width;
    video_frame->height = video_codec_context->height;
    video_frame->color_range = video_codec_context->color_range;
    video_frame->color_primaries = video_codec_context->color_primaries;
    video_frame->color_trc = video_codec_context->color_trc;
    video_frame->colorspace = video_codec_context->colorspace;
    video_frame->chroma_location = video_codec_context->chroma_sample_location;

    int capture_result = gsr_capture_start(capture, video_codec_context, video_frame);
    if(capture_result != 0) {
        fprintf(stderr, "gsr error: gsr_capture_start failed\n");
        _exit(capture_result);
    }

    gsr_video_encoder *video_encoder = create_video_encoder(&egl, overclock, color_depth, use_software_video_encoder, video_codec);
    if(!video_encoder) {
        fprintf(stderr, "Error: failed to create video encoder\n");
        _exit(1);
    }

    if(!gsr_video_encoder_start(video_encoder, video_codec_context, video_frame)) {
        fprintf(stderr, "Error: failed to start video encoder\n");
        _exit(1);
    }

    gsr_color_conversion_params color_conversion_params;
    memset(&color_conversion_params, 0, sizeof(color_conversion_params));
    color_conversion_params.color_range = color_range;
    color_conversion_params.egl = &egl;
    color_conversion_params.load_external_image_shader = gsr_capture_uses_external_image(capture);
    gsr_video_encoder_get_textures(video_encoder, color_conversion_params.destination_textures, &color_conversion_params.num_destination_textures, &color_conversion_params.destination_color);

    gsr_color_conversion color_conversion;
    if(gsr_color_conversion_init(&color_conversion, &color_conversion_params) != 0) {
        fprintf(stderr, "gsr error: gsr_capture_kms_setup_vaapi_textures: failed to create color conversion\n");
        _exit(1);
    }

    gsr_color_conversion_clear(&color_conversion);

    if(use_software_video_encoder) {
        open_video_software(video_codec_context, quality, pixel_format, hdr, color_depth, bitrate_mode);
    } else {
        open_video_hardware(video_codec_context, quality, very_old_gpu, egl.gpu_info.vendor, pixel_format, hdr, color_depth, bitrate_mode, video_codec, low_power);
    }
    if(video_stream)
        avcodec_parameters_from_context(video_stream->codecpar, video_codec_context);

    int audio_max_frame_size = 1024;
    int audio_stream_index = VIDEO_STREAM_INDEX + 1;
    for(const MergedAudioInputs &merged_audio_inputs : requested_audio_inputs) {
        const bool use_amix = merged_audio_inputs.audio_inputs.size() > 1;
        AVCodecContext *audio_codec_context = create_audio_codec_context(fps, audio_codec, use_amix, audio_bitrate);

        AVStream *audio_stream = nullptr;
        if(replay_buffer_size_secs == -1)
            audio_stream = create_stream(av_format_context, audio_codec_context);

        open_audio(audio_codec_context);
        if(audio_stream)
            avcodec_parameters_from_context(audio_stream->codecpar, audio_codec_context);

        #if LIBAVCODEC_VERSION_MAJOR < 60
        const int num_channels = audio_codec_context->channels;
        #else
        const int num_channels = audio_codec_context->ch_layout.nb_channels;
        #endif

        //audio_frame->sample_rate = audio_codec_context->sample_rate;

        std::vector<AVFilterContext*> src_filter_ctx;
        AVFilterGraph *graph = nullptr;
        AVFilterContext *sink = nullptr;
        if(use_amix && merged_audio_inputs.type == AudioInputType::DEVICE) {
            int err = init_filter_graph(audio_codec_context, &graph, &sink, src_filter_ctx, merged_audio_inputs.audio_inputs.size());
            if(err < 0) {
                fprintf(stderr, "Error: failed to create audio filter\n");
                _exit(1);
            }
        }

        // TODO: Cleanup above

        const double audio_fps = (double)audio_codec_context->sample_rate / (double)audio_codec_context->frame_size;
        const double timeout_sec = 1000.0 / audio_fps / 1000.0;

        const double audio_startup_time_seconds = force_no_audio_offset ? 0 : audio_codec_get_desired_delay(audio_codec, fps);// * ((double)audio_codec_context->frame_size / 1024.0);
        const double num_audio_frames_shift = audio_startup_time_seconds / timeout_sec;

        std::vector<AudioDevice> audio_track_audio_devices;
        switch(merged_audio_inputs.type) {
            case AudioInputType::DEVICE:
                audio_track_audio_devices = create_device_audio_inputs(merged_audio_inputs.audio_inputs, audio_codec_context, num_channels, num_audio_frames_shift, src_filter_ctx, use_amix);
                break;
            case AudioInputType::APPLICATION:
#ifdef GSR_APP_AUDIO
                audio_track_audio_devices.push_back(create_application_audio_audio_input(merged_audio_inputs, audio_codec_context, num_channels, num_audio_frames_shift, &pipewire_audio));
#endif
                break;
        }

        AudioTrack audio_track;
        audio_track.codec_context = audio_codec_context;
        audio_track.stream = audio_stream;
        audio_track.audio_devices = std::move(audio_track_audio_devices);
        audio_track.graph = graph;
        audio_track.sink = sink;
        audio_track.stream_index = audio_stream_index;
        audio_track.pts = -audio_codec_context->frame_size * num_audio_frames_shift;
        audio_tracks.push_back(std::move(audio_track));
        ++audio_stream_index;

        audio_max_frame_size = std::max(audio_max_frame_size, audio_codec_context->frame_size);
    }

    //av_dump_format(av_format_context, 0, filename, 1);

    if (replay_buffer_size_secs == -1 && !(output_format->flags & AVFMT_NOFILE)) {
        int ret = avio_open(&av_format_context->pb, filename, AVIO_FLAG_WRITE);
        if (ret < 0) {
            fprintf(stderr, "Error: Could not open '%s': %s\n", filename, av_error_to_string(ret));
            _exit(1);
        }
    }

    if(replay_buffer_size_secs == -1) {
        AVDictionary *options = nullptr;
        av_dict_set(&options, "strict", "experimental", 0);
        //av_dict_set_int(&av_format_context->metadata, "video_full_range_flag", 1, 0);

        int ret = avformat_write_header(av_format_context, &options);
        if (ret < 0) {
            fprintf(stderr, "Error occurred when writing header to output file: %s\n", av_error_to_string(ret));
            _exit(1);
        }

        av_dict_free(&options);
    }

    double fps_start_time = clock_get_monotonic_seconds();
    //double frame_timer_start = fps_start_time;
    int fps_counter = 0;
    int damage_fps_counter = 0;

    bool paused = false;
    double paused_time_offset = 0.0;
    double paused_time_start = 0.0;

    std::mutex write_output_mutex;
    std::mutex audio_filter_mutex;

    const double record_start_time = clock_get_monotonic_seconds();
    std::deque<std::shared_ptr<PacketData>> frame_data_queue;
    bool frames_erased = false;

    const size_t audio_buffer_size = audio_max_frame_size * 4 * 2; // max 4 bytes/sample, 2 channels
    uint8_t *empty_audio = (uint8_t*)malloc(audio_buffer_size);
    if(!empty_audio) {
        fprintf(stderr, "Error: failed to create empty audio\n");
        _exit(1);
    }
    memset(empty_audio, 0, audio_buffer_size);

    for(AudioTrack &audio_track : audio_tracks) {
        for(AudioDevice &audio_device : audio_track.audio_devices) {
            audio_device.thread = std::thread([&]() mutable {
                const AVSampleFormat sound_device_sample_format = audio_format_to_sample_format(audio_codec_context_get_audio_format(audio_track.codec_context));
                // TODO: Always do conversion for now. This fixes issue with stuttering audio on pulseaudio with opus + multiple audio sources merged
                const bool needs_audio_conversion = true;//audio_track.codec_context->sample_fmt != sound_device_sample_format;
                SwrContext *swr = nullptr;
                if(needs_audio_conversion) {
                    swr = swr_alloc();
                    if(!swr) {
                        fprintf(stderr, "Failed to create SwrContext\n");
                        _exit(1);
                    }
                    #if LIBAVUTIL_VERSION_MAJOR <= 56
                    av_opt_set_channel_layout(swr, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0);
                    av_opt_set_channel_layout(swr, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
                    #elif LIBAVUTIL_VERSION_MAJOR >= 59
                    av_opt_set_chlayout(swr, "in_chlayout", &audio_track.codec_context->ch_layout, 0);
                    av_opt_set_chlayout(swr, "out_chlayout", &audio_track.codec_context->ch_layout, 0);
                    #else
                    av_opt_set_chlayout(swr, "in_channel_layout", &audio_track.codec_context->ch_layout, 0);
                    av_opt_set_chlayout(swr, "out_channel_layout", &audio_track.codec_context->ch_layout, 0);
                    #endif
                    av_opt_set_int(swr, "in_sample_rate", audio_track.codec_context->sample_rate, 0);
                    av_opt_set_int(swr, "out_sample_rate", audio_track.codec_context->sample_rate, 0);
                    av_opt_set_sample_fmt(swr, "in_sample_fmt", sound_device_sample_format, 0);
                    av_opt_set_sample_fmt(swr, "out_sample_fmt", audio_track.codec_context->sample_fmt, 0);
                    swr_init(swr);
                }

                const double audio_fps = (double)audio_track.codec_context->sample_rate / (double)audio_track.codec_context->frame_size;
                const int64_t timeout_ms = std::round(1000.0 / audio_fps);
                const double timeout_sec = 1000.0 / audio_fps / 1000.0;
                bool first_frame = true;
                int64_t num_received_frames = 0;

                while(running) {
                    void *sound_buffer;
                    int sound_buffer_size = -1;
                    //const double time_before_read_seconds = clock_get_monotonic_seconds();
                    if(audio_device.sound_device.handle) {
                        // TODO: use this instead of calculating time to read. But this can fluctuate and we dont want to go back in time,
                        // also it's 0.0 for some users???
                        double latency_seconds = 0.0;
                        sound_buffer_size = sound_device_read_next_chunk(&audio_device.sound_device, &sound_buffer, timeout_sec * 2.0, &latency_seconds);
                    }

                    const bool got_audio_data = sound_buffer_size >= 0;
                    //fprintf(stderr, "got audio data: %s\n", got_audio_data ? "yes" : "no");
                    //const double time_after_read_seconds = clock_get_monotonic_seconds();
                    //const double time_to_read_seconds = time_after_read_seconds - time_before_read_seconds;
                    //fprintf(stderr, "time to read: %f, %s, %f\n", time_to_read_seconds, got_audio_data ? "yes" : "no", timeout_sec);
                    const double this_audio_frame_time = clock_get_monotonic_seconds() - paused_time_offset;

                    if(paused) {
                        if(!audio_device.sound_device.handle)
                            av_usleep(timeout_ms * 1000);

                        continue;
                    }

                    int ret = av_frame_make_writable(audio_device.frame);
                    if (ret < 0) {
                        fprintf(stderr, "Failed to make audio frame writable\n");
                        break;
                    }

                    // TODO: Is this |received_audio_time| really correct?
                    const int64_t num_expected_frames = std::round((this_audio_frame_time - record_start_time) / timeout_sec);
                    int64_t num_missing_frames = std::max((int64_t)0LL, num_expected_frames - num_received_frames);

                    if(got_audio_data)
                        num_missing_frames = std::max((int64_t)0LL, num_missing_frames - 1);

                    if(!audio_device.sound_device.handle)
                        num_missing_frames = std::max((int64_t)1, num_missing_frames);

                    // Fucking hell is there a better way to do this? I JUST WANT TO KEEP VIDEO AND AUDIO SYNCED HOLY FUCK I WANT TO KILL MYSELF NOW.
                    // THIS PIECE OF SHIT WANTS EMPTY FRAMES OTHERWISE VIDEO PLAYS TOO FAST TO KEEP UP WITH AUDIO OR THE AUDIO PLAYS TOO EARLY.
                    // BUT WE CANT USE DELAYS TO GIVE DUMMY DATA BECAUSE PULSEAUDIO MIGHT GIVE AUDIO A BIG DELAYED!!!
                    // This garbage is needed because we want to produce constant frame rate videos instead of variable frame rate
                    // videos because bad software such as video editing software and VLC do not support variable frame rate software,
                    // despite nvidia shadowplay and xbox game bar producing variable frame rate videos.
                    // So we have to make sure we produce frames at the same relative rate as the video.
                    if((num_missing_frames >= 1 && got_audio_data) || num_missing_frames >= 5 || !audio_device.sound_device.handle) {
                        // TODO:
                        //audio_track.frame->data[0] = empty_audio;
                        if(first_frame || num_missing_frames >= 5) {
                            if(needs_audio_conversion)
                                swr_convert(swr, &audio_device.frame->data[0], audio_track.codec_context->frame_size, (const uint8_t**)&empty_audio, audio_track.codec_context->frame_size);
                            else
                                audio_device.frame->data[0] = empty_audio;
                        }
                        first_frame = false;

                        // TODO: Check if duplicate frame can be saved just by writing it with a different pts instead of sending it again
                        std::lock_guard<std::mutex> lock(audio_filter_mutex);
                        for(int i = 0; i < num_missing_frames; ++i) {
                            if(audio_track.graph) {
                                // TODO: av_buffersrc_add_frame
                                if(av_buffersrc_write_frame(audio_device.src_filter_ctx, audio_device.frame) < 0) {
                                    fprintf(stderr, "Error: failed to add audio frame to filter\n");
                                }
                            } else {
                                ret = avcodec_send_frame(audio_track.codec_context, audio_device.frame);
                                if(ret >= 0) {
                                    // TODO: Move to separate thread because this could write to network (for example when livestreaming)
                                    receive_frames(audio_track.codec_context, audio_track.stream_index, audio_track.stream, audio_device.frame->pts, av_format_context, record_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset);
                                } else {
                                    fprintf(stderr, "Failed to encode audio!\n");
                                }
                            }

                            audio_device.frame->pts += audio_track.codec_context->frame_size;
                            num_received_frames++;
                        }
                    }

                    if(!audio_device.sound_device.handle)
                        av_usleep(timeout_ms * 1000);

                    if(got_audio_data) {
                        // TODO: Instead of converting audio, get float audio from alsa. Or does alsa do conversion internally to get this format?
                        if(needs_audio_conversion)
                            swr_convert(swr, &audio_device.frame->data[0], audio_track.codec_context->frame_size, (const uint8_t**)&sound_buffer, audio_track.codec_context->frame_size);
                        else
                            audio_device.frame->data[0] = (uint8_t*)sound_buffer;
                        first_frame = false;

                        if(audio_track.graph) {
                            std::lock_guard<std::mutex> lock(audio_filter_mutex);
                            // TODO: av_buffersrc_add_frame
                            if(av_buffersrc_write_frame(audio_device.src_filter_ctx, audio_device.frame) < 0) {
                                fprintf(stderr, "Error: failed to add audio frame to filter\n");
                            }
                        } else {
                            ret = avcodec_send_frame(audio_track.codec_context, audio_device.frame);
                            if(ret >= 0) {
                                // TODO: Move to separate thread because this could write to network (for example when livestreaming)
                                receive_frames(audio_track.codec_context, audio_track.stream_index, audio_track.stream, audio_device.frame->pts, av_format_context, record_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset);
                            } else {
                                fprintf(stderr, "Failed to encode audio!\n");
                            }
                        }

                        audio_device.frame->pts += audio_track.codec_context->frame_size;
                        num_received_frames++;
                    }
                }

                if(swr)
                    swr_free(&swr);
            });
        }
    }

    std::thread amix_thread;
    if(uses_amix) {
        amix_thread = std::thread([&]() {
            AVFrame *aframe = av_frame_alloc();
            while(running) {
                {
                    std::lock_guard<std::mutex> lock(audio_filter_mutex);
                    for(AudioTrack &audio_track : audio_tracks) {
                        if(!audio_track.sink)
                            continue;

                        int err = 0;
                        while ((err = av_buffersink_get_frame(audio_track.sink, aframe)) >= 0) {
                            aframe->pts = audio_track.pts;
                            err = avcodec_send_frame(audio_track.codec_context, aframe);
                            if(err >= 0){
                                // TODO: Move to separate thread because this could write to network (for example when livestreaming)
                                receive_frames(audio_track.codec_context, audio_track.stream_index, audio_track.stream, aframe->pts, av_format_context, record_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset);
                            } else {
                                fprintf(stderr, "Failed to encode audio!\n");
                            }
                            av_frame_unref(aframe);
                            audio_track.pts += audio_track.codec_context->frame_size;
                        }
                    }
                }
                av_usleep(5 * 1000); // 5 milliseconds
            }
            av_frame_free(&aframe);
        });
    }

    // Set update_fps to 24 to test if duplicate/delayed frames cause video/audio desync or too fast/slow video.
    //const double update_fps = fps + 190;
    bool should_stop_error = false;

    int64_t video_pts_counter = 0;
    int64_t video_prev_pts = 0;

    bool hdr_metadata_set = false;

    double damage_timeout_seconds = framerate_mode == FramerateMode::CONTENT ? 0.5 : 0.1;
    damage_timeout_seconds = std::max(damage_timeout_seconds, target_fps);

    bool use_damage_tracking = false;
    gsr_damage damage;
    memset(&damage, 0, sizeof(damage));
    if(gsr_egl_get_display_server(&egl) == GSR_DISPLAY_SERVER_X11) {
        gsr_damage_init(&damage, &egl, record_cursor);
        use_damage_tracking = true;
    }

    if(is_monitor_capture)
        gsr_damage_set_target_monitor(&damage, window_str.c_str());

    double last_capture_seconds = record_start_time;
    bool wait_until_frame_time_elapsed = false;

    while(running) {
        const double frame_start = clock_get_monotonic_seconds();

        while(gsr_egl_process_event(&egl)) {
            gsr_damage_on_event(&damage, gsr_egl_get_event_data(&egl));
            gsr_capture_on_event(capture, &egl);
        }
        gsr_damage_tick(&damage);
        gsr_capture_tick(capture);

        if(!is_monitor_capture) {
            Window damage_target_window = 0;
            if(capture->get_window_id)
                damage_target_window = capture->get_window_id(capture);

            if(damage_target_window != 0)
                gsr_damage_set_target_window(&damage, damage_target_window);
        }

        should_stop_error = false;
        if(gsr_capture_should_stop(capture, &should_stop_error)) {
            running = 0;
            break;
        }

        bool damaged = false;
        if(use_damage_tracking)
            damaged = gsr_damage_is_damaged(&damage);
        else if(capture->is_damaged)
            damaged = capture->is_damaged(capture);
        else
            damaged = true;

        // TODO: Readd wayland sync warning when removing this
        if(framerate_mode != FramerateMode::CONTENT)
            damaged = true;

        if(damaged)
            ++damage_fps_counter;

        ++fps_counter;
        const double time_now = clock_get_monotonic_seconds();
        //const double frame_timer_elapsed = time_now - frame_timer_start;
        const double elapsed = time_now - fps_start_time;
        if (elapsed >= 1.0) {
            if(verbose) {
                fprintf(stderr, "update fps: %d, damage fps: %d\n", fps_counter, damage_fps_counter);
            }
            fps_start_time = time_now;
            fps_counter = 0;
            damage_fps_counter = 0;
        }

        const double this_video_frame_time = clock_get_monotonic_seconds() - paused_time_offset;
        const double time_since_last_frame_captured_seconds = this_video_frame_time - last_capture_seconds;
        double frame_time_overflow = time_since_last_frame_captured_seconds - target_fps;
        const bool frame_timeout = frame_time_overflow >= 0.0;

        bool force_frame_capture = wait_until_frame_time_elapsed && frame_timeout;
        bool allow_capture = !wait_until_frame_time_elapsed || force_frame_capture;
        if(framerate_mode == FramerateMode::CONTENT) {
            force_frame_capture = false;
            allow_capture = frame_timeout;
        }

        bool frame_captured = false;
        if((damaged || force_frame_capture) && allow_capture && !paused) {
            frame_captured = true;
            frame_time_overflow = std::min(std::max(0.0, frame_time_overflow), target_fps);
            last_capture_seconds = this_video_frame_time - frame_time_overflow;
            wait_until_frame_time_elapsed = false;

            gsr_damage_clear(&damage);
            if(capture->clear_damage)
                capture->clear_damage(capture);

            // TODO: Dont do this if no damage?
            egl.glClear(0);
            gsr_capture_capture(capture, video_frame, &color_conversion);
            gsr_egl_swap_buffers(&egl);
            gsr_video_encoder_copy_textures_to_frame(video_encoder, video_frame, &color_conversion);

            if(hdr && !hdr_metadata_set && replay_buffer_size_secs == -1 && add_hdr_metadata_to_video_stream(capture, video_stream))
                hdr_metadata_set = true;

            const int64_t expected_frames = std::round((this_video_frame_time - record_start_time) / target_fps);
            const int num_missed_frames = std::max((int64_t)1LL, expected_frames - video_pts_counter);

            // TODO: Check if duplicate frame can be saved just by writing it with a different pts instead of sending it again
            const int num_frames_to_encode = framerate_mode == FramerateMode::CONSTANT ? num_missed_frames : 1;
            for(int i = 0; i < num_frames_to_encode; ++i) {
                if(framerate_mode == FramerateMode::CONSTANT) {
                    video_frame->pts = video_pts_counter + i;
                } else {
                    video_frame->pts = (this_video_frame_time - record_start_time) * (double)AV_TIME_BASE;
                    const bool same_pts = video_frame->pts == video_prev_pts;
                    video_prev_pts = video_frame->pts;
                    if(same_pts)
                        continue;
                }

                int ret = avcodec_send_frame(video_codec_context, video_frame);
                if(ret == 0) {
                    // TODO: Move to separate thread because this could write to network (for example when livestreaming)
                    receive_frames(video_codec_context, VIDEO_STREAM_INDEX, video_stream, video_frame->pts, av_format_context,
                        record_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset);
                } else {
                    fprintf(stderr, "Error: avcodec_send_frame failed, error: %s\n", av_error_to_string(ret));
                }
            }

            video_pts_counter += num_frames_to_encode;
        }

        if(toggle_pause == 1) {
            const bool new_paused_state = !paused;
            if(new_paused_state) {
                paused_time_start = clock_get_monotonic_seconds();
                fprintf(stderr, "Paused\n");
            } else {
                paused_time_offset += (clock_get_monotonic_seconds() - paused_time_start);
                fprintf(stderr, "Unpaused\n");
            }

            toggle_pause = 0;
            paused = !paused;
        }

        if(save_replay_thread.valid() && save_replay_thread.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
            save_replay_thread.get();
            puts(save_replay_output_filepath.c_str());
            fflush(stdout);
            if(recording_saved_script)
                run_recording_saved_script_async(recording_saved_script, save_replay_output_filepath.c_str(), "replay");
            std::lock_guard<std::mutex> lock(write_output_mutex);
            save_replay_packets.clear();
        }

        if(save_replay == 1 && !save_replay_thread.valid() && replay_buffer_size_secs != -1) {
            save_replay = 0;
            save_replay_async(video_codec_context, VIDEO_STREAM_INDEX, audio_tracks, frame_data_queue, frames_erased, filename, container_format, file_extension, write_output_mutex, date_folders, hdr, capture);
        }

        const double frame_end = clock_get_monotonic_seconds();
        const double time_at_frame_end = frame_end - paused_time_offset;
        const double time_elapsed_total = time_at_frame_end - record_start_time;
        const int64_t frames_elapsed = (int64_t)(time_elapsed_total / target_fps);
        const double time_at_next_frame = (frames_elapsed + 1) * target_fps;
        double time_to_next_frame = time_at_next_frame - time_elapsed_total;
        if(time_to_next_frame > target_fps*1.1)
            time_to_next_frame = target_fps;

        const double frame_time = frame_end - frame_start;
        const bool frame_deadline_missed = frame_time > target_fps;
        if(time_to_next_frame >= 0.0 && !frame_deadline_missed && frame_captured)
            av_usleep(time_to_next_frame * 1000.0 * 1000.0);
        else {
            if(paused)
                av_usleep(20.0 * 1000.0); // 20 milliseconds
            else if(frame_deadline_missed)
            {}
            else if(framerate_mode == FramerateMode::CONTENT || !frame_captured)
                av_usleep(2.8 * 1000.0); // 2.8 milliseconds
            else if(!frame_captured)
                av_usleep(1.0 * 1000.0); // 1 milliseconds
            wait_until_frame_time_elapsed = true;
        }
    }

    running = 0;

    if(save_replay_thread.valid()) {
        save_replay_thread.get();
        puts(save_replay_output_filepath.c_str());
        fflush(stdout);
        if(recording_saved_script)
            run_recording_saved_script_async(recording_saved_script, save_replay_output_filepath.c_str(), "replay");
        std::lock_guard<std::mutex> lock(write_output_mutex);
        save_replay_packets.clear();
    }

    for(AudioTrack &audio_track : audio_tracks) {
        for(AudioDevice &audio_device : audio_track.audio_devices) {
            audio_device.thread.join();
            sound_device_close(&audio_device.sound_device);
        }
    }

    if(amix_thread.joinable())
        amix_thread.join();

    if (replay_buffer_size_secs == -1 && av_write_trailer(av_format_context) != 0) {
        fprintf(stderr, "Failed to write trailer\n");
    }

    if(replay_buffer_size_secs == -1 && !(output_format->flags & AVFMT_NOFILE))
        avio_close(av_format_context->pb);

    gsr_damage_deinit(&damage);
    gsr_color_conversion_deinit(&color_conversion);
    gsr_video_encoder_destroy(video_encoder, video_codec_context);
    gsr_capture_destroy(capture, video_codec_context);
#ifdef GSR_APP_AUDIO
    gsr_pipewire_audio_deinit(&pipewire_audio);
#endif

    if(replay_buffer_size_secs == -1 && recording_saved_script)
        run_recording_saved_script_async(recording_saved_script, filename, "regular");

    if(dpy) {
        // TODO: This causes a crash, why? maybe some other library dlclose xlib and that also happened to unload this???
        //XCloseDisplay(dpy);
    }

    //av_frame_free(&video_frame);
    free(empty_audio);
    // We do an _exit here because cuda uses at_exit to do _something_ that causes the program to freeze,
    // but only on some nvidia driver versions on some gpus (RTX?), and _exit exits the program without calling
    // the at_exit registered functions.
    // Cuda (cuvid library in this case) seems to be waiting for a thread that never finishes execution.
    // Maybe this happens because we dont clean up all ffmpeg resources?
    // TODO: Investigate this.
    _exit(should_stop_error ? 3 : 0);
}