unsafe-libopus 0.2.0

libopus transpiled to rust by c2rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
use crate::externs::{free, malloc};

pub mod arch_h {
    pub type opus_val16 = f32;
    pub type opus_val32 = f32;
    pub const CELT_SIG_SCALE: f32 = 32768.0f32;
    pub const Q15ONE: f32 = 1.0f32;
    pub const EPSILON: f32 = 1e-15f32;
    pub const VERY_SMALL: f32 = 1e-30f32;
}
pub mod stddef_h {
    pub type size_t = u64;
    pub const NULL: i32 = 0;
}
pub mod xmmintrin_h {
    #[cfg(target_arch = "x86")]
    pub use core::arch::x86::{__m128, _mm_cvt_ss2si, _mm_cvtss_si32, _mm_set_ss};
    #[cfg(target_arch = "x86_64")]
    pub use core::arch::x86_64::{__m128, _mm_cvt_ss2si, _mm_cvtss_si32, _mm_set_ss};
}
pub mod cpu_support_h {
    #[inline]
    pub unsafe fn opus_select_arch() -> i32 {
        return 0;
    }
}
use self::arch_h::{opus_val16, opus_val32, CELT_SIG_SCALE, EPSILON, Q15ONE, VERY_SMALL};
pub use self::cpu_support_h::opus_select_arch;
pub use self::stddef_h::{size_t, NULL};
use crate::celt::celt::{
    CELT_GET_MODE_REQUEST, CELT_SET_ANALYSIS_REQUEST, CELT_SET_CHANNELS_REQUEST,
    CELT_SET_END_BAND_REQUEST, CELT_SET_PREDICTION_REQUEST, CELT_SET_SIGNALLING_REQUEST,
    CELT_SET_SILK_INFO_REQUEST, CELT_SET_START_BAND_REQUEST, OPUS_SET_ENERGY_MASK_REQUEST,
    OPUS_SET_LFE_REQUEST,
};
use crate::celt::celt_encoder::{
    celt_encode_with_ec, celt_encoder_get_size, celt_encoder_init, OpusCustomEncoder, SILKInfo,
};
use crate::celt::entcode::ec_tell;
use crate::celt::entenc::ec_enc;
use crate::celt::entenc::{ec_enc_bit_logp, ec_enc_done, ec_enc_init, ec_enc_shrink, ec_enc_uint};
use crate::celt::float_cast::FLOAT2INT16;
use crate::celt::mathops::{celt_exp2, celt_maxabs16, celt_sqrt};
use crate::celt::modes::OpusCustomMode;
use crate::celt::pitch::celt_inner_prod_c;
use crate::externs::{memcpy, memmove, memset};
use crate::silk::define::{
    DTX_ACTIVITY_THRESHOLD, MAX_CONSECUTIVE_DTX, NB_SPEECH_FRAMES_BEFORE_DTX, VAD_NO_DECISION,
};
use crate::silk::enc_API::silk_EncControlStruct;
use crate::silk::enc_API::{silk_Encode, silk_Get_Encoder_Size, silk_InitEncoder};
use crate::silk::float::structs_FLP::silk_encoder;
use crate::silk::lin2log::silk_lin2log;
use crate::silk::log2lin::silk_log2lin;
use crate::src::analysis::{
    downmix_func, run_analysis, tonality_analysis_init, tonality_analysis_reset, AnalysisInfo,
    TonalityAnalysisState,
};
use crate::src::opus_defines::{
    OPUS_ALLOC_FAIL, OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_RESTRICTED_LOWDELAY,
    OPUS_APPLICATION_VOIP, OPUS_AUTO, OPUS_BAD_ARG, OPUS_BANDWIDTH_FULLBAND,
    OPUS_BANDWIDTH_MEDIUMBAND, OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_SUPERWIDEBAND,
    OPUS_BANDWIDTH_WIDEBAND, OPUS_BITRATE_MAX, OPUS_BUFFER_TOO_SMALL, OPUS_FRAMESIZE_100_MS,
    OPUS_FRAMESIZE_10_MS, OPUS_FRAMESIZE_120_MS, OPUS_FRAMESIZE_20_MS, OPUS_FRAMESIZE_2_5_MS,
    OPUS_FRAMESIZE_40_MS, OPUS_FRAMESIZE_5_MS, OPUS_FRAMESIZE_60_MS, OPUS_FRAMESIZE_80_MS,
    OPUS_FRAMESIZE_ARG, OPUS_GET_APPLICATION_REQUEST, OPUS_GET_BANDWIDTH_REQUEST,
    OPUS_GET_BITRATE_REQUEST, OPUS_GET_COMPLEXITY_REQUEST, OPUS_GET_DTX_REQUEST,
    OPUS_GET_EXPERT_FRAME_DURATION_REQUEST, OPUS_GET_FINAL_RANGE_REQUEST,
    OPUS_GET_FORCE_CHANNELS_REQUEST, OPUS_GET_INBAND_FEC_REQUEST, OPUS_GET_IN_DTX_REQUEST,
    OPUS_GET_LOOKAHEAD_REQUEST, OPUS_GET_LSB_DEPTH_REQUEST, OPUS_GET_MAX_BANDWIDTH_REQUEST,
    OPUS_GET_PACKET_LOSS_PERC_REQUEST, OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST,
    OPUS_GET_PREDICTION_DISABLED_REQUEST, OPUS_GET_SAMPLE_RATE_REQUEST, OPUS_GET_SIGNAL_REQUEST,
    OPUS_GET_VBR_CONSTRAINT_REQUEST, OPUS_GET_VBR_REQUEST, OPUS_INTERNAL_ERROR, OPUS_OK,
    OPUS_RESET_STATE, OPUS_SET_APPLICATION_REQUEST, OPUS_SET_BANDWIDTH_REQUEST,
    OPUS_SET_BITRATE_REQUEST, OPUS_SET_COMPLEXITY_REQUEST, OPUS_SET_DTX_REQUEST,
    OPUS_SET_EXPERT_FRAME_DURATION_REQUEST, OPUS_SET_FORCE_CHANNELS_REQUEST,
    OPUS_SET_INBAND_FEC_REQUEST, OPUS_SET_LSB_DEPTH_REQUEST, OPUS_SET_MAX_BANDWIDTH_REQUEST,
    OPUS_SET_PACKET_LOSS_PERC_REQUEST, OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST,
    OPUS_SET_PREDICTION_DISABLED_REQUEST, OPUS_SET_SIGNAL_REQUEST, OPUS_SET_VBR_CONSTRAINT_REQUEST,
    OPUS_SET_VBR_REQUEST, OPUS_SIGNAL_MUSIC, OPUS_SIGNAL_VOICE, OPUS_UNIMPLEMENTED,
};
use crate::src::opus_private::{
    align, MODE_CELT_ONLY, MODE_HYBRID, MODE_SILK_ONLY, OPUS_GET_VOICE_RATIO_REQUEST,
    OPUS_SET_FORCE_MODE_REQUEST, OPUS_SET_VOICE_RATIO_REQUEST,
};
use crate::varargs::VarArgs;
use crate::{
    opus_custom_encoder_ctl, opus_packet_pad, opus_repacketizer_cat, opus_repacketizer_init,
    opus_repacketizer_out_range_impl, OpusRepacketizer,
};

#[derive(Copy, Clone)]
#[repr(C)]
pub struct OpusEncoder {
    pub(crate) celt_enc_offset: i32,
    pub(crate) silk_enc_offset: i32,
    pub(crate) silk_mode: silk_EncControlStruct,
    pub(crate) application: i32,
    pub(crate) channels: i32,
    pub(crate) delay_compensation: i32,
    pub(crate) force_channels: i32,
    pub(crate) signal_type: i32,
    pub(crate) user_bandwidth: i32,
    pub(crate) max_bandwidth: i32,
    pub(crate) user_forced_mode: i32,
    pub(crate) voice_ratio: i32,
    pub(crate) Fs: i32,
    pub(crate) use_vbr: i32,
    pub(crate) vbr_constraint: i32,
    pub(crate) variable_duration: i32,
    pub(crate) bitrate_bps: i32,
    pub(crate) user_bitrate_bps: i32,
    pub(crate) lsb_depth: i32,
    pub(crate) encoder_buffer: i32,
    pub(crate) lfe: i32,
    pub(crate) arch: i32,
    pub(crate) use_dtx: i32,
    pub(crate) analysis: TonalityAnalysisState,
    pub(crate) stream_channels: i32,
    pub(crate) hybrid_stereo_width_Q14: i16,
    pub(crate) variable_HP_smth2_Q15: i32,
    pub(crate) prev_HB_gain: opus_val16,
    pub(crate) hp_mem: [opus_val32; 4],
    pub(crate) mode: i32,
    pub(crate) prev_mode: i32,
    pub(crate) prev_channels: i32,
    pub(crate) prev_framesize: i32,
    pub(crate) bandwidth: i32,
    pub(crate) auto_bandwidth: i32,
    pub(crate) silk_bw_switch: i32,
    pub(crate) first: i32,
    pub(crate) energy_masking: *mut opus_val16,
    pub(crate) width_mem: StereoWidthState,
    pub(crate) delay_buffer: [opus_val16; 960],
    pub(crate) detected_bandwidth: i32,
    pub(crate) nb_no_activity_frames: i32,
    pub(crate) peak_signal_energy: opus_val32,
    pub(crate) nonfinal_frame: i32,
    pub(crate) rangeFinal: u32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct StereoWidthState {
    pub XX: opus_val32,
    pub XY: opus_val32,
    pub YY: opus_val32,
    pub smoothed_width: opus_val16,
    pub max_follower: opus_val16,
}
pub const PSEUDO_SNR_THRESHOLD: f32 = 316.23f32;
static mut mono_voice_bandwidth_thresholds: [i32; 8] =
    [9000, 700, 9000, 700, 13500, 1000, 14000, 2000];
static mut mono_music_bandwidth_thresholds: [i32; 8] =
    [9000, 700, 9000, 700, 11000, 1000, 12000, 2000];
static mut stereo_voice_bandwidth_thresholds: [i32; 8] =
    [9000, 700, 9000, 700, 13500, 1000, 14000, 2000];
static mut stereo_music_bandwidth_thresholds: [i32; 8] =
    [9000, 700, 9000, 700, 11000, 1000, 12000, 2000];
static mut stereo_voice_threshold: i32 = 19000;
static mut stereo_music_threshold: i32 = 17000;
static mut mode_thresholds: [[i32; 2]; 2] = [[64000, 10000], [44000, 10000]];
static mut fec_thresholds: [i32; 10] = [
    12000, 1000, 14000, 1000, 16000, 1000, 20000, 1000, 22000, 1000,
];
pub unsafe fn opus_encoder_get_size(channels: i32) -> i32 {
    let mut silkEncSizeBytes: i32 = 0;
    let mut celtEncSizeBytes: i32 = 0;
    let mut ret: i32 = 0;
    if channels < 1 || channels > 2 {
        return 0;
    }
    ret = silk_Get_Encoder_Size(&mut silkEncSizeBytes);
    if ret != 0 {
        return 0;
    }
    silkEncSizeBytes = align(silkEncSizeBytes);
    celtEncSizeBytes = celt_encoder_get_size(channels);
    return align(::core::mem::size_of::<OpusEncoder>() as u64 as i32)
        + silkEncSizeBytes
        + celtEncSizeBytes;
}
pub unsafe fn opus_encoder_init(
    st: *mut OpusEncoder,
    Fs: i32,
    channels: i32,
    application: i32,
) -> i32 {
    let mut silk_enc: *mut core::ffi::c_void = 0 as *mut core::ffi::c_void;
    let mut celt_enc: *mut OpusCustomEncoder = 0 as *mut OpusCustomEncoder;
    let mut err: i32 = 0;
    let mut ret: i32 = 0;
    let mut silkEncSizeBytes: i32 = 0;
    if Fs != 48000 && Fs != 24000 && Fs != 16000 && Fs != 12000 && Fs != 8000
        || channels != 1 && channels != 2
        || application != OPUS_APPLICATION_VOIP
            && application != OPUS_APPLICATION_AUDIO
            && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY
    {
        return OPUS_BAD_ARG;
    }
    memset(
        st as *mut i8 as *mut core::ffi::c_void,
        0,
        (opus_encoder_get_size(channels) as u64).wrapping_mul(::core::mem::size_of::<i8>() as u64),
    );
    ret = silk_Get_Encoder_Size(&mut silkEncSizeBytes);
    if ret != 0 {
        return OPUS_BAD_ARG;
    }
    silkEncSizeBytes = align(silkEncSizeBytes);
    (*st).silk_enc_offset = align(::core::mem::size_of::<OpusEncoder>() as u64 as i32);
    (*st).celt_enc_offset = (*st).silk_enc_offset + silkEncSizeBytes;
    silk_enc = (st as *mut i8).offset((*st).silk_enc_offset as isize) as *mut core::ffi::c_void;
    celt_enc = (st as *mut i8).offset((*st).celt_enc_offset as isize) as *mut OpusCustomEncoder;
    (*st).channels = channels;
    (*st).stream_channels = (*st).channels;
    (*st).Fs = Fs;
    (*st).arch = opus_select_arch();
    ret = silk_InitEncoder(silk_enc, (*st).arch, &mut (*st).silk_mode);
    if ret != 0 {
        return OPUS_INTERNAL_ERROR;
    }
    (*st).silk_mode.nChannelsAPI = channels;
    (*st).silk_mode.nChannelsInternal = channels;
    (*st).silk_mode.API_sampleRate = (*st).Fs;
    (*st).silk_mode.maxInternalSampleRate = 16000;
    (*st).silk_mode.minInternalSampleRate = 8000;
    (*st).silk_mode.desiredInternalSampleRate = 16000;
    (*st).silk_mode.payloadSize_ms = 20;
    (*st).silk_mode.bitRate = 25000;
    (*st).silk_mode.packetLossPercentage = 0;
    (*st).silk_mode.complexity = 9;
    (*st).silk_mode.useInBandFEC = 0;
    (*st).silk_mode.useDTX = 0;
    (*st).silk_mode.useCBR = 0;
    (*st).silk_mode.reducedDependency = 0;
    err = celt_encoder_init(celt_enc, Fs, channels, (*st).arch);
    if err != OPUS_OK {
        return OPUS_INTERNAL_ERROR;
    }
    opus_custom_encoder_ctl!(celt_enc, CELT_SET_SIGNALLING_REQUEST, 0);
    opus_custom_encoder_ctl!(
        celt_enc,
        OPUS_SET_COMPLEXITY_REQUEST,
        (*st).silk_mode.complexity,
    );
    (*st).use_vbr = 1;
    (*st).vbr_constraint = 1;
    (*st).user_bitrate_bps = OPUS_AUTO;
    (*st).bitrate_bps = 3000 + Fs * channels;
    (*st).application = application;
    (*st).signal_type = OPUS_AUTO;
    (*st).user_bandwidth = OPUS_AUTO;
    (*st).max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
    (*st).force_channels = OPUS_AUTO;
    (*st).user_forced_mode = OPUS_AUTO;
    (*st).voice_ratio = -1;
    (*st).encoder_buffer = (*st).Fs / 100;
    (*st).lsb_depth = 24;
    (*st).variable_duration = OPUS_FRAMESIZE_ARG;
    (*st).delay_compensation = (*st).Fs / 250;
    (*st).hybrid_stereo_width_Q14 = ((1) << 14) as i16;
    (*st).prev_HB_gain = Q15ONE;
    (*st).variable_HP_smth2_Q15 = ((silk_lin2log(60) as u32) << 8) as i32;
    (*st).first = 1;
    (*st).mode = MODE_HYBRID;
    (*st).bandwidth = OPUS_BANDWIDTH_FULLBAND;
    tonality_analysis_init(&mut (*st).analysis, (*st).Fs);
    (*st).analysis.application = (*st).application;
    return OPUS_OK;
}
unsafe fn gen_toc(mode: i32, mut framerate: i32, bandwidth: i32, channels: i32) -> u8 {
    let mut period: i32 = 0;
    let mut toc: u8 = 0;
    period = 0;
    while framerate < 400 {
        framerate <<= 1;
        period += 1;
    }
    if mode == MODE_SILK_ONLY {
        toc = (bandwidth - OPUS_BANDWIDTH_NARROWBAND << 5) as u8;
        toc = (toc as i32 | (period - 2) << 3) as u8;
    } else if mode == MODE_CELT_ONLY {
        let mut tmp: i32 = bandwidth - OPUS_BANDWIDTH_MEDIUMBAND;
        if tmp < 0 {
            tmp = 0;
        }
        toc = 0x80;
        toc = (toc as i32 | tmp << 5) as u8;
        toc = (toc as i32 | period << 3) as u8;
    } else {
        toc = 0x60;
        toc = (toc as i32 | bandwidth - OPUS_BANDWIDTH_SUPERWIDEBAND << 4) as u8;
        toc = (toc as i32 | (period - 2) << 3) as u8;
    }
    toc = (toc as i32 | ((channels == 2) as i32) << 2) as u8;
    return toc;
}
unsafe fn silk_biquad_float(
    in_0: *const opus_val16,
    B_Q28: *const i32,
    A_Q28: *const i32,
    S: *mut opus_val32,
    out: *mut opus_val16,
    len: i32,
    stride: i32,
) {
    let mut k: i32 = 0;
    let mut vout: opus_val32 = 0.;
    let mut inval: opus_val32 = 0.;
    let mut A: [opus_val32; 2] = [0.; 2];
    let mut B: [opus_val32; 3] = [0.; 3];
    A[0 as usize] = *A_Q28.offset(0 as isize) as f32 * (1.0f32 / ((1) << 28) as f32);
    A[1 as usize] = *A_Q28.offset(1 as isize) as f32 * (1.0f32 / ((1) << 28) as f32);
    B[0 as usize] = *B_Q28.offset(0 as isize) as f32 * (1.0f32 / ((1) << 28) as f32);
    B[1 as usize] = *B_Q28.offset(1 as isize) as f32 * (1.0f32 / ((1) << 28) as f32);
    B[2 as usize] = *B_Q28.offset(2 as isize) as f32 * (1.0f32 / ((1) << 28) as f32);
    k = 0;
    while k < len {
        inval = *in_0.offset((k * stride) as isize);
        vout = *S.offset(0 as isize) + B[0 as usize] * inval;
        *S.offset(0 as isize) =
            *S.offset(1 as isize) - vout * A[0 as usize] + B[1 as usize] * inval;
        *S.offset(1 as isize) = -vout * A[1 as usize] + B[2 as usize] * inval + VERY_SMALL;
        *out.offset((k * stride) as isize) = vout;
        k += 1;
    }
}
unsafe fn hp_cutoff(
    in_0: *const opus_val16,
    cutoff_Hz: i32,
    out: *mut opus_val16,
    hp_mem: *mut opus_val32,
    len: i32,
    channels: i32,
    Fs: i32,
    _arch: i32,
) {
    let mut B_Q28: [i32; 3] = [0; 3];
    let mut A_Q28: [i32; 2] = [0; 2];
    let mut Fc_Q19: i32 = 0;
    let mut r_Q28: i32 = 0;
    let mut r_Q22: i32 = 0;
    Fc_Q19 = (1.5f64 * 3.14159f64 / 1000 as f64 * ((1) << 19) as f64 + 0.5f64) as i32 as i16 as i32
        * cutoff_Hz as i16 as i32
        / (Fs / 1000);
    r_Q28 = (1.0f64 * ((1) << 28) as f64 + 0.5f64) as i32
        - (0.92f64 * ((1) << 9) as f64 + 0.5f64) as i32 * Fc_Q19;
    B_Q28[0 as usize] = r_Q28;
    B_Q28[1 as usize] = ((-r_Q28 as u32) << 1) as i32;
    B_Q28[2 as usize] = r_Q28;
    r_Q22 = r_Q28 >> 6;
    A_Q28[0 as usize] = (r_Q22 as i64
        * ((Fc_Q19 as i64 * Fc_Q19 as i64 >> 16) as i32
            - (2.0f64 * ((1) << 22) as f64 + 0.5f64) as i32) as i64
        >> 16) as i32;
    A_Q28[1 as usize] = (r_Q22 as i64 * r_Q22 as i64 >> 16) as i32;
    silk_biquad_float(
        in_0,
        B_Q28.as_mut_ptr(),
        A_Q28.as_mut_ptr(),
        hp_mem,
        out,
        len,
        channels,
    );
    if channels == 2 {
        silk_biquad_float(
            in_0.offset(1 as isize),
            B_Q28.as_mut_ptr(),
            A_Q28.as_mut_ptr(),
            hp_mem.offset(2 as isize),
            out.offset(1 as isize),
            len,
            channels,
        );
    }
}
unsafe fn dc_reject(
    in_0: *const opus_val16,
    cutoff_Hz: i32,
    out: *mut opus_val16,
    hp_mem: *mut opus_val32,
    len: i32,
    channels: i32,
    Fs: i32,
) {
    let mut i: i32 = 0;
    let mut coef: f32 = 0.;
    let mut coef2: f32 = 0.;
    coef = 6.3f32 * cutoff_Hz as f32 / Fs as f32;
    coef2 = 1 as f32 - coef;
    if channels == 2 {
        let mut m0: f32 = 0.;
        let mut m2: f32 = 0.;
        m0 = *hp_mem.offset(0 as isize);
        m2 = *hp_mem.offset(2 as isize);
        i = 0;
        while i < len {
            let mut x0: opus_val32 = 0.;
            let mut x1: opus_val32 = 0.;
            let mut out0: opus_val32 = 0.;
            let mut out1: opus_val32 = 0.;
            x0 = *in_0.offset((2 * i + 0) as isize);
            x1 = *in_0.offset((2 * i + 1) as isize);
            out0 = x0 - m0;
            out1 = x1 - m2;
            m0 = coef * x0 + VERY_SMALL + coef2 * m0;
            m2 = coef * x1 + VERY_SMALL + coef2 * m2;
            *out.offset((2 * i + 0) as isize) = out0;
            *out.offset((2 * i + 1) as isize) = out1;
            i += 1;
        }
        *hp_mem.offset(0 as isize) = m0;
        *hp_mem.offset(2 as isize) = m2;
    } else {
        let mut m0_0: f32 = 0.;
        m0_0 = *hp_mem.offset(0 as isize);
        i = 0;
        while i < len {
            let mut x: opus_val32 = 0.;
            let mut y: opus_val32 = 0.;
            x = *in_0.offset(i as isize);
            y = x - m0_0;
            m0_0 = coef * x + VERY_SMALL + coef2 * m0_0;
            *out.offset(i as isize) = y;
            i += 1;
        }
        *hp_mem.offset(0 as isize) = m0_0;
    };
}
unsafe fn stereo_fade(
    in_0: *const opus_val16,
    out: *mut opus_val16,
    mut g1: opus_val16,
    mut g2: opus_val16,
    overlap48: i32,
    frame_size: i32,
    channels: i32,
    window: *const opus_val16,
    Fs: i32,
) {
    let mut i: i32 = 0;
    let mut overlap: i32 = 0;
    let mut inc: i32 = 0;
    inc = 48000 / Fs;
    overlap = overlap48 / inc;
    g1 = Q15ONE - g1;
    g2 = Q15ONE - g2;
    i = 0;
    while i < overlap {
        let mut diff: opus_val32 = 0.;
        let mut g: opus_val16 = 0.;
        let mut w: opus_val16 = 0.;
        w = *window.offset((i * inc) as isize) * *window.offset((i * inc) as isize);
        g = w * g2 + (1.0f32 - w) * g1;
        diff = 0.5f32
            * (*in_0.offset((i * channels) as isize) - *in_0.offset((i * channels + 1) as isize));
        diff = g * diff;
        *out.offset((i * channels) as isize) = *out.offset((i * channels) as isize) - diff;
        *out.offset((i * channels + 1) as isize) = *out.offset((i * channels + 1) as isize) + diff;
        i += 1;
    }
    while i < frame_size {
        let mut diff_0: opus_val32 = 0.;
        diff_0 = 0.5f32
            * (*in_0.offset((i * channels) as isize) - *in_0.offset((i * channels + 1) as isize));
        diff_0 = g2 * diff_0;
        *out.offset((i * channels) as isize) = *out.offset((i * channels) as isize) - diff_0;
        *out.offset((i * channels + 1) as isize) =
            *out.offset((i * channels + 1) as isize) + diff_0;
        i += 1;
    }
}
unsafe fn gain_fade(
    in_0: *const opus_val16,
    out: *mut opus_val16,
    g1: opus_val16,
    g2: opus_val16,
    overlap48: i32,
    frame_size: i32,
    channels: i32,
    window: *const opus_val16,
    Fs: i32,
) {
    let mut i: i32 = 0;
    let mut inc: i32 = 0;
    let mut overlap: i32 = 0;
    let mut c: i32 = 0;
    inc = 48000 / Fs;
    overlap = overlap48 / inc;
    if channels == 1 {
        i = 0;
        while i < overlap {
            let mut g: opus_val16 = 0.;
            let mut w: opus_val16 = 0.;
            w = *window.offset((i * inc) as isize) * *window.offset((i * inc) as isize);
            g = w * g2 + (1.0f32 - w) * g1;
            *out.offset(i as isize) = g * *in_0.offset(i as isize);
            i += 1;
        }
    } else {
        i = 0;
        while i < overlap {
            let mut g_0: opus_val16 = 0.;
            let mut w_0: opus_val16 = 0.;
            w_0 = *window.offset((i * inc) as isize) * *window.offset((i * inc) as isize);
            g_0 = w_0 * g2 + (1.0f32 - w_0) * g1;
            *out.offset((i * 2) as isize) = g_0 * *in_0.offset((i * 2) as isize);
            *out.offset((i * 2 + 1) as isize) = g_0 * *in_0.offset((i * 2 + 1) as isize);
            i += 1;
        }
    }
    c = 0;
    loop {
        i = overlap;
        while i < frame_size {
            *out.offset((i * channels + c) as isize) =
                g2 * *in_0.offset((i * channels + c) as isize);
            i += 1;
        }
        c += 1;
        if !(c < channels) {
            break;
        }
    }
}
pub unsafe fn opus_encoder_create(
    Fs: i32,
    channels: i32,
    application: i32,
    error: *mut i32,
) -> *mut OpusEncoder {
    let mut ret: i32 = 0;
    let mut st: *mut OpusEncoder = 0 as *mut OpusEncoder;
    if Fs != 48000 && Fs != 24000 && Fs != 16000 && Fs != 12000 && Fs != 8000
        || channels != 1 && channels != 2
        || application != OPUS_APPLICATION_VOIP
            && application != OPUS_APPLICATION_AUDIO
            && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY
    {
        if !error.is_null() {
            *error = OPUS_BAD_ARG;
        }
        return NULL as *mut OpusEncoder;
    }
    st = malloc(opus_encoder_get_size(channels) as size_t) as *mut OpusEncoder;
    if st.is_null() {
        if !error.is_null() {
            *error = OPUS_ALLOC_FAIL;
        }
        return NULL as *mut OpusEncoder;
    }
    ret = opus_encoder_init(st, Fs, channels, application);
    if !error.is_null() {
        *error = ret;
    }
    if ret != OPUS_OK {
        free(st as *mut core::ffi::c_void);
        st = NULL as *mut OpusEncoder;
    }
    return st;
}
unsafe fn user_bitrate_to_bitrate(
    st: *mut OpusEncoder,
    mut frame_size: i32,
    max_data_bytes: i32,
) -> i32 {
    if frame_size == 0 {
        frame_size = (*st).Fs / 400;
    }
    if (*st).user_bitrate_bps == OPUS_AUTO {
        return 60 * (*st).Fs / frame_size + (*st).Fs * (*st).channels;
    } else if (*st).user_bitrate_bps == OPUS_BITRATE_MAX {
        return max_data_bytes * 8 * (*st).Fs / frame_size;
    } else {
        return (*st).user_bitrate_bps;
    };
}
pub unsafe fn downmix_float(
    mut _x: *const core::ffi::c_void,
    y: *mut opus_val32,
    subframe: i32,
    offset: i32,
    c1: i32,
    c2: i32,
    C: i32,
) {
    let mut x: *const f32 = 0 as *const f32;
    let mut j: i32 = 0;
    x = _x as *const f32;
    j = 0;
    while j < subframe {
        *y.offset(j as isize) = *x.offset(((j + offset) * C + c1) as isize) * CELT_SIG_SCALE;
        j += 1;
    }
    if c2 > -1 {
        j = 0;
        while j < subframe {
            let ref mut fresh0 = *y.offset(j as isize);
            *fresh0 += *x.offset(((j + offset) * C + c2) as isize) * CELT_SIG_SCALE;
            j += 1;
        }
    } else if c2 == -(2) {
        let mut c: i32 = 0;
        c = 1;
        while c < C {
            j = 0;
            while j < subframe {
                let ref mut fresh1 = *y.offset(j as isize);
                *fresh1 += *x.offset(((j + offset) * C + c) as isize) * CELT_SIG_SCALE;
                j += 1;
            }
            c += 1;
        }
    }
}
pub unsafe fn downmix_int(
    mut _x: *const core::ffi::c_void,
    y: *mut opus_val32,
    subframe: i32,
    offset: i32,
    c1: i32,
    c2: i32,
    C: i32,
) {
    let mut x: *const i16 = 0 as *const i16;
    let mut j: i32 = 0;
    x = _x as *const i16;
    j = 0;
    while j < subframe {
        *y.offset(j as isize) = *x.offset(((j + offset) * C + c1) as isize) as opus_val32;
        j += 1;
    }
    if c2 > -1 {
        j = 0;
        while j < subframe {
            let ref mut fresh2 = *y.offset(j as isize);
            *fresh2 += *x.offset(((j + offset) * C + c2) as isize) as i32 as f32;
            j += 1;
        }
    } else if c2 == -(2) {
        let mut c: i32 = 0;
        c = 1;
        while c < C {
            j = 0;
            while j < subframe {
                let ref mut fresh3 = *y.offset(j as isize);
                *fresh3 += *x.offset(((j + offset) * C + c) as isize) as i32 as f32;
                j += 1;
            }
            c += 1;
        }
    }
}
pub unsafe fn frame_size_select(frame_size: i32, variable_duration: i32, Fs: i32) -> i32 {
    let mut new_size: i32 = 0;
    if frame_size < Fs / 400 {
        return -1;
    }
    if variable_duration == OPUS_FRAMESIZE_ARG {
        new_size = frame_size;
    } else if variable_duration >= OPUS_FRAMESIZE_2_5_MS
        && variable_duration <= OPUS_FRAMESIZE_120_MS
    {
        if variable_duration <= OPUS_FRAMESIZE_40_MS {
            new_size = (Fs / 400) << variable_duration - OPUS_FRAMESIZE_2_5_MS;
        } else {
            new_size = (variable_duration - OPUS_FRAMESIZE_2_5_MS - 2) * Fs / 50;
        }
    } else {
        return -1;
    }
    if new_size > frame_size {
        return -1;
    }
    if 400 * new_size != Fs
        && 200 * new_size != Fs
        && 100 * new_size != Fs
        && 50 * new_size != Fs
        && 25 * new_size != Fs
        && 50 * new_size != 3 * Fs
        && 50 * new_size != 4 * Fs
        && 50 * new_size != 5 * Fs
        && 50 * new_size != 6 * Fs
    {
        return -1;
    }
    return new_size;
}
pub unsafe fn compute_stereo_width(
    pcm: *const opus_val16,
    frame_size: i32,
    Fs: i32,
    mem: *mut StereoWidthState,
) -> opus_val16 {
    let mut xx: opus_val32 = 0.;
    let mut xy: opus_val32 = 0.;
    let mut yy: opus_val32 = 0.;
    let mut sqrt_xx: opus_val16 = 0.;
    let mut sqrt_yy: opus_val16 = 0.;
    let mut qrrt_xx: opus_val16 = 0.;
    let mut qrrt_yy: opus_val16 = 0.;
    let mut frame_rate: i32 = 0;
    let mut i: i32 = 0;
    let mut short_alpha: opus_val16 = 0.;
    frame_rate = Fs / frame_size;
    short_alpha =
        Q15ONE - 25 as opus_val32 * 1.0f32 / (if 50 > frame_rate { 50 } else { frame_rate }) as f32;
    yy = 0 as opus_val32;
    xy = yy;
    xx = xy;
    i = 0;
    while i < frame_size - 3 {
        let mut pxx: opus_val32 = 0 as opus_val32;
        let mut pxy: opus_val32 = 0 as opus_val32;
        let mut pyy: opus_val32 = 0 as opus_val32;
        let mut x: opus_val16 = 0.;
        let mut y: opus_val16 = 0.;
        x = *pcm.offset((2 * i) as isize);
        y = *pcm.offset((2 * i + 1) as isize);
        pxx = x * x;
        pxy = x * y;
        pyy = y * y;
        x = *pcm.offset((2 * i + 2) as isize);
        y = *pcm.offset((2 * i + 3) as isize);
        pxx += x * x;
        pxy += x * y;
        pyy += y * y;
        x = *pcm.offset((2 * i + 4) as isize);
        y = *pcm.offset((2 * i + 5) as isize);
        pxx += x * x;
        pxy += x * y;
        pyy += y * y;
        x = *pcm.offset((2 * i + 6) as isize);
        y = *pcm.offset((2 * i + 7) as isize);
        pxx += x * x;
        pxy += x * y;
        pyy += y * y;
        xx += pxx;
        xy += pxy;
        yy += pyy;
        i += 4;
    }
    if !(xx < 1e9f32) || xx != xx || !(yy < 1e9f32) || yy != yy {
        yy = 0 as opus_val32;
        xx = yy;
        xy = xx;
    }
    (*mem).XX += short_alpha * (xx - (*mem).XX);
    (*mem).XY += short_alpha * (xy - (*mem).XY);
    (*mem).YY += short_alpha * (yy - (*mem).YY);
    (*mem).XX = if 0 as f32 > (*mem).XX {
        0 as f32
    } else {
        (*mem).XX
    };
    (*mem).XY = if 0 as f32 > (*mem).XY {
        0 as f32
    } else {
        (*mem).XY
    };
    (*mem).YY = if 0 as f32 > (*mem).YY {
        0 as f32
    } else {
        (*mem).YY
    };
    if (if (*mem).XX > (*mem).YY {
        (*mem).XX
    } else {
        (*mem).YY
    }) > 8e-4f32
    {
        let mut corr: opus_val16 = 0.;
        let mut ldiff: opus_val16 = 0.;
        let mut width: opus_val16 = 0.;
        sqrt_xx = celt_sqrt((*mem).XX);
        sqrt_yy = celt_sqrt((*mem).YY);
        qrrt_xx = celt_sqrt(sqrt_xx);
        qrrt_yy = celt_sqrt(sqrt_yy);
        (*mem).XY = if (*mem).XY < sqrt_xx * sqrt_yy {
            (*mem).XY
        } else {
            sqrt_xx * sqrt_yy
        };
        corr = (*mem).XY / (1e-15f32 + sqrt_xx * sqrt_yy);
        ldiff = 1.0f32 * (qrrt_xx - qrrt_yy).abs() / (EPSILON + qrrt_xx + qrrt_yy);
        width = celt_sqrt(1.0f32 - corr * corr) * ldiff;
        (*mem).smoothed_width += (width - (*mem).smoothed_width) / frame_rate as f32;
        (*mem).max_follower =
            if (*mem).max_follower - 0.02f32 / frame_rate as f32 > (*mem).smoothed_width {
                (*mem).max_follower - 0.02f32 / frame_rate as f32
            } else {
                (*mem).smoothed_width
            };
    }
    return if 1.0f32 < 20 as opus_val32 * (*mem).max_follower {
        1.0f32
    } else {
        20 as opus_val32 * (*mem).max_follower
    };
}
unsafe fn decide_fec(
    useInBandFEC: i32,
    PacketLoss_perc: i32,
    last_fec: i32,
    mode: i32,
    bandwidth: *mut i32,
    rate: i32,
) -> i32 {
    let mut orig_bandwidth: i32 = 0;
    if useInBandFEC == 0 || PacketLoss_perc == 0 || mode == MODE_CELT_ONLY {
        return 0;
    }
    orig_bandwidth = *bandwidth;
    loop {
        let mut hysteresis: i32 = 0;
        let mut LBRR_rate_thres_bps: i32 = 0;
        LBRR_rate_thres_bps =
            fec_thresholds[(2 * (*bandwidth - OPUS_BANDWIDTH_NARROWBAND)) as usize];
        hysteresis = fec_thresholds[(2 * (*bandwidth - OPUS_BANDWIDTH_NARROWBAND) + 1) as usize];
        if last_fec == 1 {
            LBRR_rate_thres_bps -= hysteresis;
        }
        if last_fec == 0 {
            LBRR_rate_thres_bps += hysteresis;
        }
        LBRR_rate_thres_bps = ((LBRR_rate_thres_bps
            * (125
                - (if PacketLoss_perc < 25 {
                    PacketLoss_perc
                } else {
                    25
                }))) as i64
            * (0.01f64 * ((1) << 16) as f64 + 0.5f64) as i32 as i16 as i64
            >> 16) as i32;
        if rate > LBRR_rate_thres_bps {
            return 1;
        } else if PacketLoss_perc <= 5 {
            return 0;
        } else {
            if !(*bandwidth > OPUS_BANDWIDTH_NARROWBAND) {
                break;
            }
            *bandwidth -= 1;
        }
    }
    *bandwidth = orig_bandwidth;
    return 0;
}
unsafe fn compute_silk_rate_for_hybrid(
    mut rate: i32,
    bandwidth: i32,
    frame20ms: i32,
    vbr: i32,
    fec: i32,
    channels: i32,
) -> i32 {
    let mut entry: i32 = 0;
    let mut i: i32 = 0;
    let mut N: i32 = 0;
    let mut silk_rate: i32 = 0;
    static mut rate_table: [[i32; 5]; 7] = [
        [0, 0, 0, 0, 0],
        [12000, 10000, 10000, 11000, 11000],
        [16000, 13500, 13500, 15000, 15000],
        [20000, 16000, 16000, 18000, 18000],
        [24000, 18000, 18000, 21000, 21000],
        [32000, 22000, 22000, 28000, 28000],
        [64000, 38000, 38000, 50000, 50000],
    ];
    rate /= channels;
    entry = 1 + frame20ms + 2 * fec;
    N = (::core::mem::size_of::<[[i32; 5]; 7]>() as u64)
        .wrapping_div(::core::mem::size_of::<[i32; 5]>() as u64) as i32;
    i = 1;
    while i < N {
        if rate_table[i as usize][0 as usize] > rate {
            break;
        }
        i += 1;
    }
    if i == N {
        silk_rate = rate_table[(i - 1) as usize][entry as usize];
        silk_rate += (rate - rate_table[(i - 1) as usize][0 as usize]) / 2;
    } else {
        let mut lo: i32 = 0;
        let mut hi: i32 = 0;
        let mut x0: i32 = 0;
        let mut x1: i32 = 0;
        lo = rate_table[(i - 1) as usize][entry as usize];
        hi = rate_table[i as usize][entry as usize];
        x0 = rate_table[(i - 1) as usize][0 as usize];
        x1 = rate_table[i as usize][0 as usize];
        silk_rate = (lo * (x1 - rate) + hi * (rate - x0)) / (x1 - x0);
    }
    if vbr == 0 {
        silk_rate += 100;
    }
    if bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND {
        silk_rate += 300;
    }
    silk_rate *= channels;
    if channels == 2 && rate >= 12000 {
        silk_rate -= 1000;
    }
    return silk_rate;
}
unsafe fn compute_equiv_rate(
    bitrate: i32,
    channels: i32,
    frame_rate: i32,
    vbr: i32,
    mode: i32,
    complexity: i32,
    loss: i32,
) -> i32 {
    let mut equiv: i32 = 0;
    equiv = bitrate;
    if frame_rate > 50 {
        equiv -= (40 * channels + 20) * (frame_rate - 50);
    }
    if vbr == 0 {
        equiv -= equiv / 12;
    }
    equiv = equiv * (90 + complexity) / 100;
    if mode == MODE_SILK_ONLY || mode == MODE_HYBRID {
        if complexity < 2 {
            equiv = equiv * 4 / 5;
        }
        equiv -= equiv * loss / (6 * loss + 10);
    } else if mode == MODE_CELT_ONLY {
        if complexity < 5 {
            equiv = equiv * 9 / 10;
        }
    } else {
        equiv -= equiv * loss / (12 * loss + 20);
    }
    return equiv;
}
pub unsafe fn is_digital_silence(
    pcm: *const opus_val16,
    frame_size: i32,
    channels: i32,
    lsb_depth: i32,
) -> i32 {
    let mut silence: i32 = 0;
    let mut sample_max: opus_val32 = 0 as opus_val32;
    sample_max = celt_maxabs16(pcm, frame_size * channels);
    silence = (sample_max <= 1 as opus_val16 / ((1) << lsb_depth) as f32) as i32;
    return silence;
}
unsafe fn compute_frame_energy(
    pcm: *const opus_val16,
    frame_size: i32,
    channels: i32,
    _arch: i32,
) -> opus_val32 {
    let len: i32 = frame_size * channels;
    return celt_inner_prod_c(pcm, pcm, len) / len as f32;
}
unsafe fn decide_dtx_mode(
    activity_probability: f32,
    nb_no_activity_frames: *mut i32,
    peak_signal_energy: opus_val32,
    pcm: *const opus_val16,
    frame_size: i32,
    channels: i32,
    mut is_silence: i32,
    arch: i32,
) -> i32 {
    let mut noise_energy: opus_val32 = 0.;
    if is_silence == 0 {
        if activity_probability < DTX_ACTIVITY_THRESHOLD {
            noise_energy = compute_frame_energy(pcm, frame_size, channels, arch);
            is_silence = (peak_signal_energy >= PSEUDO_SNR_THRESHOLD * noise_energy) as i32;
        }
    }
    if is_silence != 0 {
        *nb_no_activity_frames += 1;
        if *nb_no_activity_frames > NB_SPEECH_FRAMES_BEFORE_DTX {
            if *nb_no_activity_frames <= NB_SPEECH_FRAMES_BEFORE_DTX + MAX_CONSECUTIVE_DTX {
                return 1;
            } else {
                *nb_no_activity_frames = NB_SPEECH_FRAMES_BEFORE_DTX;
            }
        }
    } else {
        *nb_no_activity_frames = 0;
    }
    return 0;
}
unsafe fn encode_multiframe_packet(
    st: *mut OpusEncoder,
    pcm: *const opus_val16,
    nb_frames: i32,
    frame_size: i32,
    data: *mut u8,
    out_data_bytes: i32,
    to_celt: i32,
    lsb_depth: i32,
    float_api: i32,
) -> i32 {
    let mut i: i32 = 0;
    let mut ret: i32 = 0;
    let mut bak_mode: i32 = 0;
    let mut bak_bandwidth: i32 = 0;
    let mut bak_channels: i32 = 0;
    let mut bak_to_mono: i32 = 0;
    let mut max_header_bytes: i32 = 0;
    let mut bytes_per_frame: i32 = 0;
    let mut cbr_bytes: i32 = 0;
    let mut repacketize_len: i32 = 0;
    let mut tmp_len: i32 = 0;
    max_header_bytes = if nb_frames == 2 {
        3
    } else {
        2 + (nb_frames - 1) * 2
    };
    if (*st).use_vbr != 0 || (*st).user_bitrate_bps == OPUS_BITRATE_MAX {
        repacketize_len = out_data_bytes;
    } else {
        cbr_bytes = 3 * (*st).bitrate_bps / (3 * 8 * (*st).Fs / (frame_size * nb_frames));
        repacketize_len = if cbr_bytes < out_data_bytes {
            cbr_bytes
        } else {
            out_data_bytes
        };
    }
    bytes_per_frame = if (1276) < 1 + (repacketize_len - max_header_bytes) / nb_frames {
        1276
    } else {
        1 + (repacketize_len - max_header_bytes) / nb_frames
    };
    let vla = (nb_frames * bytes_per_frame) as usize;
    let mut tmp_data: Vec<u8> = ::std::vec::from_elem(0, vla);
    let mut rp: [OpusRepacketizer; 1] = [OpusRepacketizer {
        toc: 0,
        nb_frames: 0,
        frames: [0 as *const u8; 48],
        len: [0; 48],
        framesize: 0,
    }; 1];
    opus_repacketizer_init(rp.as_mut_ptr());
    bak_mode = (*st).user_forced_mode;
    bak_bandwidth = (*st).user_bandwidth;
    bak_channels = (*st).force_channels;
    (*st).user_forced_mode = (*st).mode;
    (*st).user_bandwidth = (*st).bandwidth;
    (*st).force_channels = (*st).stream_channels;
    bak_to_mono = (*st).silk_mode.toMono;
    if bak_to_mono != 0 {
        (*st).force_channels = 1;
    } else {
        (*st).prev_channels = (*st).stream_channels;
    }
    i = 0;
    while i < nb_frames {
        (*st).silk_mode.toMono = 0;
        (*st).nonfinal_frame = (i < nb_frames - 1) as i32;
        if to_celt != 0 && i == nb_frames - 1 {
            (*st).user_forced_mode = MODE_CELT_ONLY;
        }
        tmp_len = opus_encode_native(
            st,
            pcm.offset((i * ((*st).channels * frame_size)) as isize),
            frame_size,
            tmp_data.as_mut_ptr().offset((i * bytes_per_frame) as isize),
            bytes_per_frame,
            lsb_depth,
            NULL as *const core::ffi::c_void,
            0,
            0,
            0,
            0,
            None,
            float_api,
        );
        if tmp_len < 0 {
            return OPUS_INTERNAL_ERROR;
        }
        ret = opus_repacketizer_cat(
            rp.as_mut_ptr(),
            tmp_data.as_mut_ptr().offset((i * bytes_per_frame) as isize),
            tmp_len,
        );
        if ret < 0 {
            return OPUS_INTERNAL_ERROR;
        }
        i += 1;
    }
    ret = opus_repacketizer_out_range_impl(
        rp.as_mut_ptr(),
        0,
        nb_frames,
        data,
        repacketize_len,
        0,
        ((*st).use_vbr == 0) as i32,
    );
    if ret < 0 {
        return OPUS_INTERNAL_ERROR;
    }
    (*st).user_forced_mode = bak_mode;
    (*st).user_bandwidth = bak_bandwidth;
    (*st).force_channels = bak_channels;
    (*st).silk_mode.toMono = bak_to_mono;
    return ret;
}
unsafe fn compute_redundancy_bytes(
    max_data_bytes: i32,
    bitrate_bps: i32,
    frame_rate: i32,
    channels: i32,
) -> i32 {
    let mut redundancy_bytes_cap: i32 = 0;
    let mut redundancy_bytes: i32 = 0;
    let mut redundancy_rate: i32 = 0;
    let mut base_bits: i32 = 0;
    let mut available_bits: i32 = 0;
    base_bits = 40 * channels + 20;
    redundancy_rate = bitrate_bps + base_bits * (200 - frame_rate);
    redundancy_rate = 3 * redundancy_rate / 2;
    redundancy_bytes = redundancy_rate / 1600;
    available_bits = max_data_bytes * 8 - 2 * base_bits;
    redundancy_bytes_cap = (available_bits * 240 / (240 + 48000 / frame_rate) + base_bits) / 8;
    redundancy_bytes = if redundancy_bytes < redundancy_bytes_cap {
        redundancy_bytes
    } else {
        redundancy_bytes_cap
    };
    if redundancy_bytes > 4 + 8 * channels {
        redundancy_bytes = if (257) < redundancy_bytes {
            257
        } else {
            redundancy_bytes
        };
    } else {
        redundancy_bytes = 0;
    }
    return redundancy_bytes;
}
pub unsafe fn opus_encode_native(
    st: *mut OpusEncoder,
    pcm: *const opus_val16,
    frame_size: i32,
    mut data: *mut u8,
    out_data_bytes: i32,
    mut lsb_depth: i32,
    analysis_pcm: *const core::ffi::c_void,
    analysis_size: i32,
    c1: i32,
    c2: i32,
    analysis_channels: i32,
    downmix: downmix_func,
    float_api: i32,
) -> i32 {
    let mut silk_enc: *mut core::ffi::c_void = 0 as *mut core::ffi::c_void;
    let mut celt_enc: *mut OpusCustomEncoder = 0 as *mut OpusCustomEncoder;
    let mut i: i32 = 0;
    let mut ret: i32 = 0;
    let mut nBytes: i32 = 0;
    let mut enc: ec_enc = ec_enc {
        buf: &mut [],
        storage: 0,
        end_offs: 0,
        end_window: 0,
        nend_bits: 0,
        nbits_total: 0,
        offs: 0,
        rng: 0,
        val: 0,
        ext: 0,
        rem: 0,
        error: 0,
    };
    let mut bytes_target: i32 = 0;
    let mut prefill: i32 = 0;
    let mut start_band: i32 = 0;
    let mut redundancy: i32 = 0;
    let mut redundancy_bytes: i32 = 0;
    let mut celt_to_silk: i32 = 0;
    let mut nb_compr_bytes: i32 = 0;
    let mut to_celt: i32 = 0;
    let mut redundant_rng: u32 = 0;
    let mut cutoff_Hz: i32 = 0;
    let mut hp_freq_smth1: i32 = 0;
    let mut voice_est: i32 = 0;
    let mut equiv_rate: i32 = 0;
    let mut delay_compensation: i32 = 0;
    let mut frame_rate: i32 = 0;
    let mut max_rate: i32 = 0;
    let mut curr_bandwidth: i32 = 0;
    let mut HB_gain: opus_val16 = 0.;
    let mut max_data_bytes: i32 = 0;
    let mut total_buffer: i32 = 0;
    let mut stereo_width: opus_val16 = 0.;
    let mut celt_mode: *const OpusCustomMode = 0 as *const OpusCustomMode;
    let mut analysis_info: AnalysisInfo = AnalysisInfo {
        valid: 0,
        tonality: 0.,
        tonality_slope: 0.,
        noisiness: 0.,
        activity: 0.,
        music_prob: 0.,
        music_prob_min: 0.,
        music_prob_max: 0.,
        bandwidth: 0,
        activity_probability: 0.,
        max_pitch_ratio: 0.,
        leak_boost: [0; 19],
    };
    let mut analysis_read_pos_bak: i32 = -1;
    let mut analysis_read_subframe_bak: i32 = -1;
    let mut is_silence: i32 = 0;
    max_data_bytes = if (1276) < out_data_bytes {
        1276
    } else {
        out_data_bytes
    };
    (*st).rangeFinal = 0;
    if frame_size <= 0 || max_data_bytes <= 0 {
        return OPUS_BAD_ARG;
    }
    if max_data_bytes == 1 && (*st).Fs == frame_size * 10 {
        return OPUS_BUFFER_TOO_SMALL;
    }
    silk_enc = (st as *mut i8).offset((*st).silk_enc_offset as isize) as *mut core::ffi::c_void;
    celt_enc = (st as *mut i8).offset((*st).celt_enc_offset as isize) as *mut OpusCustomEncoder;
    if (*st).application == OPUS_APPLICATION_RESTRICTED_LOWDELAY {
        delay_compensation = 0;
    } else {
        delay_compensation = (*st).delay_compensation;
    }
    lsb_depth = if lsb_depth < (*st).lsb_depth {
        lsb_depth
    } else {
        (*st).lsb_depth
    };
    opus_custom_encoder_ctl!(celt_enc, CELT_GET_MODE_REQUEST, &mut celt_mode);
    analysis_info.valid = 0;
    if (*st).silk_mode.complexity >= 7 && (*st).Fs >= 16000 {
        is_silence = is_digital_silence(pcm, frame_size, (*st).channels, lsb_depth);
        analysis_read_pos_bak = (*st).analysis.read_pos;
        analysis_read_subframe_bak = (*st).analysis.read_subframe;
        run_analysis(
            &mut (*st).analysis,
            celt_mode,
            analysis_pcm,
            analysis_size,
            frame_size,
            c1,
            c2,
            analysis_channels,
            (*st).Fs,
            lsb_depth,
            downmix,
            &mut analysis_info,
        );
        if is_silence == 0 && analysis_info.activity_probability > DTX_ACTIVITY_THRESHOLD {
            (*st).peak_signal_energy = if 0.999f32 * (*st).peak_signal_energy
                > compute_frame_energy(pcm, frame_size, (*st).channels, (*st).arch)
            {
                0.999f32 * (*st).peak_signal_energy
            } else {
                compute_frame_energy(pcm, frame_size, (*st).channels, (*st).arch)
            };
        }
    } else if (*st).analysis.initialized != 0 {
        tonality_analysis_reset(&mut (*st).analysis);
    }
    if is_silence == 0 {
        (*st).voice_ratio = -1;
    }
    (*st).detected_bandwidth = 0;
    if analysis_info.valid != 0 {
        let mut analysis_bandwidth: i32 = 0;
        if (*st).signal_type == OPUS_AUTO {
            let mut prob: f32 = 0.;
            if (*st).prev_mode == 0 {
                prob = analysis_info.music_prob;
            } else if (*st).prev_mode == MODE_CELT_ONLY {
                prob = analysis_info.music_prob_max;
            } else {
                prob = analysis_info.music_prob_min;
            }
            (*st).voice_ratio = (0.5 + (100.0 * (1.0 - prob))).floor() as i32;
        }
        analysis_bandwidth = analysis_info.bandwidth;
        if analysis_bandwidth <= 12 {
            (*st).detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
        } else if analysis_bandwidth <= 14 {
            (*st).detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
        } else if analysis_bandwidth <= 16 {
            (*st).detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
        } else if analysis_bandwidth <= 18 {
            (*st).detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
        } else {
            (*st).detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
        }
    }
    if (*st).channels == 2 && (*st).force_channels != 1 {
        stereo_width = compute_stereo_width(pcm, frame_size, (*st).Fs, &mut (*st).width_mem);
    } else {
        stereo_width = 0 as opus_val16;
    }
    total_buffer = delay_compensation;
    (*st).bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
    frame_rate = (*st).Fs / frame_size;
    if (*st).use_vbr == 0 {
        let mut cbrBytes: i32 = 0;
        let frame_rate12: i32 = 12 * (*st).Fs / frame_size;
        cbrBytes =
            if (12 * (*st).bitrate_bps / 8 + frame_rate12 / 2) / frame_rate12 < max_data_bytes {
                (12 * (*st).bitrate_bps / 8 + frame_rate12 / 2) / frame_rate12
            } else {
                max_data_bytes
            };
        (*st).bitrate_bps = cbrBytes * frame_rate12 * 8 / 12;
        max_data_bytes = if 1 > cbrBytes { 1 } else { cbrBytes };
    }
    if max_data_bytes < 3
        || (*st).bitrate_bps < 3 * frame_rate * 8
        || frame_rate < 50 && (max_data_bytes * frame_rate < 300 || (*st).bitrate_bps < 2400)
    {
        let mut tocmode: i32 = (*st).mode;
        let mut bw: i32 = if (*st).bandwidth == 0 {
            OPUS_BANDWIDTH_NARROWBAND
        } else {
            (*st).bandwidth
        };
        let mut packet_code: i32 = 0;
        let mut num_multiframes: i32 = 0;
        if tocmode == 0 {
            tocmode = MODE_SILK_ONLY;
        }
        if frame_rate > 100 {
            tocmode = MODE_CELT_ONLY;
        }
        if frame_rate == 25 && tocmode != MODE_SILK_ONLY {
            frame_rate = 50;
            packet_code = 1;
        }
        if frame_rate <= 16 {
            if out_data_bytes == 1 || tocmode == MODE_SILK_ONLY && frame_rate != 10 {
                tocmode = MODE_SILK_ONLY;
                packet_code = (frame_rate <= 12) as i32;
                frame_rate = if frame_rate == 12 { 25 } else { 16 };
            } else {
                num_multiframes = 50 / frame_rate;
                frame_rate = 50;
                packet_code = 3;
            }
        }
        if tocmode == MODE_SILK_ONLY && bw > OPUS_BANDWIDTH_WIDEBAND {
            bw = OPUS_BANDWIDTH_WIDEBAND;
        } else if tocmode == MODE_CELT_ONLY && bw == OPUS_BANDWIDTH_MEDIUMBAND {
            bw = OPUS_BANDWIDTH_NARROWBAND;
        } else if tocmode == MODE_HYBRID && bw <= OPUS_BANDWIDTH_SUPERWIDEBAND {
            bw = OPUS_BANDWIDTH_SUPERWIDEBAND;
        }
        *data.offset(0 as isize) = gen_toc(tocmode, frame_rate, bw, (*st).stream_channels);
        let ref mut fresh4 = *data.offset(0 as isize);
        *fresh4 = (*fresh4 as i32 | packet_code) as u8;
        ret = if packet_code <= 1 { 1 } else { 2 };
        max_data_bytes = if max_data_bytes > ret {
            max_data_bytes
        } else {
            ret
        };
        if packet_code == 3 {
            *data.offset(1 as isize) = num_multiframes as u8;
        }
        if (*st).use_vbr == 0 {
            ret = opus_packet_pad(data, ret, max_data_bytes);
            if ret == OPUS_OK {
                ret = max_data_bytes;
            } else {
                ret = OPUS_INTERNAL_ERROR;
            }
        }
        return ret;
    }
    max_rate = frame_rate * max_data_bytes * 8;
    equiv_rate = compute_equiv_rate(
        (*st).bitrate_bps,
        (*st).channels,
        (*st).Fs / frame_size,
        (*st).use_vbr,
        0,
        (*st).silk_mode.complexity,
        (*st).silk_mode.packetLossPercentage,
    );
    if (*st).signal_type == OPUS_SIGNAL_VOICE {
        voice_est = 127;
    } else if (*st).signal_type == OPUS_SIGNAL_MUSIC {
        voice_est = 0;
    } else if (*st).voice_ratio >= 0 {
        voice_est = (*st).voice_ratio * 327 >> 8;
        if (*st).application == OPUS_APPLICATION_AUDIO {
            voice_est = if voice_est < 115 { voice_est } else { 115 };
        }
    } else if (*st).application == OPUS_APPLICATION_VOIP {
        voice_est = 115;
    } else {
        voice_est = 48;
    }
    if (*st).force_channels != OPUS_AUTO && (*st).channels == 2 {
        (*st).stream_channels = (*st).force_channels;
    } else if (*st).channels == 2 {
        let mut stereo_threshold: i32 = 0;
        stereo_threshold = stereo_music_threshold
            + (voice_est * voice_est * (stereo_voice_threshold - stereo_music_threshold) >> 14);
        if (*st).stream_channels == 2 {
            stereo_threshold -= 1000;
        } else {
            stereo_threshold += 1000;
        }
        (*st).stream_channels = if equiv_rate > stereo_threshold { 2 } else { 1 };
    } else {
        (*st).stream_channels = (*st).channels;
    }
    equiv_rate = compute_equiv_rate(
        (*st).bitrate_bps,
        (*st).stream_channels,
        (*st).Fs / frame_size,
        (*st).use_vbr,
        0,
        (*st).silk_mode.complexity,
        (*st).silk_mode.packetLossPercentage,
    );
    (*st).silk_mode.useDTX =
        ((*st).use_dtx != 0 && !(analysis_info.valid != 0 || is_silence != 0)) as i32;
    if (*st).application == OPUS_APPLICATION_RESTRICTED_LOWDELAY {
        (*st).mode = MODE_CELT_ONLY;
    } else if (*st).user_forced_mode == OPUS_AUTO {
        let mut mode_voice: i32 = 0;
        let mut mode_music: i32 = 0;
        let mut threshold: i32 = 0;
        mode_voice = ((1.0f32 - stereo_width) * mode_thresholds[0 as usize][0 as usize] as f32
            + stereo_width * mode_thresholds[1 as usize][0 as usize] as f32)
            as i32;
        mode_music = ((1.0f32 - stereo_width) * mode_thresholds[1 as usize][1 as usize] as f32
            + stereo_width * mode_thresholds[1 as usize][1 as usize] as f32)
            as i32;
        threshold = mode_music + (voice_est * voice_est * (mode_voice - mode_music) >> 14);
        if (*st).application == OPUS_APPLICATION_VOIP {
            threshold += 8000;
        }
        if (*st).prev_mode == MODE_CELT_ONLY {
            threshold -= 4000;
        } else if (*st).prev_mode > 0 {
            threshold += 4000;
        }
        (*st).mode = if equiv_rate >= threshold {
            MODE_CELT_ONLY
        } else {
            MODE_SILK_ONLY
        };
        if (*st).silk_mode.useInBandFEC != 0
            && (*st).silk_mode.packetLossPercentage > 128 - voice_est >> 4
        {
            (*st).mode = MODE_SILK_ONLY;
        }
        if (*st).silk_mode.useDTX != 0 && voice_est > 100 {
            (*st).mode = MODE_SILK_ONLY;
        }
        if max_data_bytes
            < (if frame_rate > 50 { 9000 } else { 6000 }) * frame_size / ((*st).Fs * 8)
        {
            (*st).mode = MODE_CELT_ONLY;
        }
    } else {
        (*st).mode = (*st).user_forced_mode;
    }
    if (*st).mode != MODE_CELT_ONLY && frame_size < (*st).Fs / 100 {
        (*st).mode = MODE_CELT_ONLY;
    }
    if (*st).lfe != 0 {
        (*st).mode = MODE_CELT_ONLY;
    }
    if (*st).prev_mode > 0
        && ((*st).mode != MODE_CELT_ONLY && (*st).prev_mode == MODE_CELT_ONLY
            || (*st).mode == MODE_CELT_ONLY && (*st).prev_mode != MODE_CELT_ONLY)
    {
        redundancy = 1;
        celt_to_silk = ((*st).mode != MODE_CELT_ONLY) as i32;
        if celt_to_silk == 0 {
            if frame_size >= (*st).Fs / 100 {
                (*st).mode = (*st).prev_mode;
                to_celt = 1;
            } else {
                redundancy = 0;
            }
        }
    }
    if (*st).stream_channels == 1
        && (*st).prev_channels == 2
        && (*st).silk_mode.toMono == 0
        && (*st).mode != MODE_CELT_ONLY
        && (*st).prev_mode != MODE_CELT_ONLY
    {
        (*st).silk_mode.toMono = 1;
        (*st).stream_channels = 2;
    } else {
        (*st).silk_mode.toMono = 0;
    }
    equiv_rate = compute_equiv_rate(
        (*st).bitrate_bps,
        (*st).stream_channels,
        (*st).Fs / frame_size,
        (*st).use_vbr,
        (*st).mode,
        (*st).silk_mode.complexity,
        (*st).silk_mode.packetLossPercentage,
    );
    if (*st).mode != MODE_CELT_ONLY && (*st).prev_mode == MODE_CELT_ONLY {
        let mut dummy: silk_EncControlStruct = silk_EncControlStruct {
            nChannelsAPI: 0,
            nChannelsInternal: 0,
            API_sampleRate: 0,
            maxInternalSampleRate: 0,
            minInternalSampleRate: 0,
            desiredInternalSampleRate: 0,
            payloadSize_ms: 0,
            bitRate: 0,
            packetLossPercentage: 0,
            complexity: 0,
            useInBandFEC: 0,
            LBRR_coded: 0,
            useDTX: 0,
            useCBR: 0,
            maxBits: 0,
            toMono: 0,
            opusCanSwitch: 0,
            reducedDependency: 0,
            internalSampleRate: 0,
            allowBandwidthSwitch: 0,
            inWBmodeWithoutVariableLP: 0,
            stereoWidth_Q14: 0,
            switchReady: 0,
            signalType: 0,
            offset: 0,
        };
        silk_InitEncoder(silk_enc, (*st).arch, &mut dummy);
        prefill = 1;
    }
    if (*st).mode == MODE_CELT_ONLY || (*st).first != 0 || (*st).silk_mode.allowBandwidthSwitch != 0
    {
        let mut voice_bandwidth_thresholds: *const i32 = 0 as *const i32;
        let mut music_bandwidth_thresholds: *const i32 = 0 as *const i32;
        let mut bandwidth_thresholds: [i32; 8] = [0; 8];
        let mut bandwidth: i32 = OPUS_BANDWIDTH_FULLBAND;
        if (*st).channels == 2 && (*st).force_channels != 1 {
            voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds.as_ptr();
            music_bandwidth_thresholds = stereo_music_bandwidth_thresholds.as_ptr();
        } else {
            voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds.as_ptr();
            music_bandwidth_thresholds = mono_music_bandwidth_thresholds.as_ptr();
        }
        i = 0;
        while i < 8 {
            bandwidth_thresholds[i as usize] = *music_bandwidth_thresholds.offset(i as isize)
                + (voice_est
                    * voice_est
                    * (*voice_bandwidth_thresholds.offset(i as isize)
                        - *music_bandwidth_thresholds.offset(i as isize))
                    >> 14);
            i += 1;
        }
        loop {
            let mut threshold_0: i32 = 0;
            let mut hysteresis: i32 = 0;
            threshold_0 =
                bandwidth_thresholds[(2 * (bandwidth - OPUS_BANDWIDTH_MEDIUMBAND)) as usize];
            hysteresis =
                bandwidth_thresholds[(2 * (bandwidth - OPUS_BANDWIDTH_MEDIUMBAND) + 1) as usize];
            if (*st).first == 0 {
                if (*st).auto_bandwidth >= bandwidth {
                    threshold_0 -= hysteresis;
                } else {
                    threshold_0 += hysteresis;
                }
            }
            if equiv_rate >= threshold_0 {
                break;
            }
            bandwidth -= 1;
            if !(bandwidth > OPUS_BANDWIDTH_NARROWBAND) {
                break;
            }
        }
        if bandwidth == OPUS_BANDWIDTH_MEDIUMBAND {
            bandwidth = OPUS_BANDWIDTH_WIDEBAND;
        }
        (*st).auto_bandwidth = bandwidth;
        (*st).bandwidth = (*st).auto_bandwidth;
        if (*st).first == 0
            && (*st).mode != MODE_CELT_ONLY
            && (*st).silk_mode.inWBmodeWithoutVariableLP == 0
            && (*st).bandwidth > OPUS_BANDWIDTH_WIDEBAND
        {
            (*st).bandwidth = OPUS_BANDWIDTH_WIDEBAND;
        }
    }
    if (*st).bandwidth > (*st).max_bandwidth {
        (*st).bandwidth = (*st).max_bandwidth;
    }
    if (*st).user_bandwidth != OPUS_AUTO {
        (*st).bandwidth = (*st).user_bandwidth;
    }
    if (*st).mode != MODE_CELT_ONLY && max_rate < 15000 {
        (*st).bandwidth = if (*st).bandwidth < 1103 {
            (*st).bandwidth
        } else {
            1103
        };
    }
    if (*st).Fs <= 24000 && (*st).bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND {
        (*st).bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
    }
    if (*st).Fs <= 16000 && (*st).bandwidth > OPUS_BANDWIDTH_WIDEBAND {
        (*st).bandwidth = OPUS_BANDWIDTH_WIDEBAND;
    }
    if (*st).Fs <= 12000 && (*st).bandwidth > OPUS_BANDWIDTH_MEDIUMBAND {
        (*st).bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
    }
    if (*st).Fs <= 8000 && (*st).bandwidth > OPUS_BANDWIDTH_NARROWBAND {
        (*st).bandwidth = OPUS_BANDWIDTH_NARROWBAND;
    }
    if (*st).detected_bandwidth != 0 && (*st).user_bandwidth == OPUS_AUTO {
        let mut min_detected_bandwidth: i32 = 0;
        if equiv_rate <= 18000 * (*st).stream_channels && (*st).mode == MODE_CELT_ONLY {
            min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
        } else if equiv_rate <= 24000 * (*st).stream_channels && (*st).mode == MODE_CELT_ONLY {
            min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
        } else if equiv_rate <= 30000 * (*st).stream_channels {
            min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
        } else if equiv_rate <= 44000 * (*st).stream_channels {
            min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
        } else {
            min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
        }
        (*st).detected_bandwidth = if (*st).detected_bandwidth > min_detected_bandwidth {
            (*st).detected_bandwidth
        } else {
            min_detected_bandwidth
        };
        (*st).bandwidth = if (*st).bandwidth < (*st).detected_bandwidth {
            (*st).bandwidth
        } else {
            (*st).detected_bandwidth
        };
    }
    (*st).silk_mode.LBRR_coded = decide_fec(
        (*st).silk_mode.useInBandFEC,
        (*st).silk_mode.packetLossPercentage,
        (*st).silk_mode.LBRR_coded,
        (*st).mode,
        &mut (*st).bandwidth,
        equiv_rate,
    );
    opus_custom_encoder_ctl!(celt_enc, OPUS_SET_LSB_DEPTH_REQUEST, lsb_depth);
    if (*st).mode == MODE_CELT_ONLY && (*st).bandwidth == OPUS_BANDWIDTH_MEDIUMBAND {
        (*st).bandwidth = OPUS_BANDWIDTH_WIDEBAND;
    }
    if (*st).lfe != 0 {
        (*st).bandwidth = OPUS_BANDWIDTH_NARROWBAND;
    }
    curr_bandwidth = (*st).bandwidth;
    if (*st).mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND {
        (*st).mode = MODE_HYBRID;
    }
    if (*st).mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND {
        (*st).mode = MODE_SILK_ONLY;
    }
    if frame_size > (*st).Fs / 50 && (*st).mode != MODE_SILK_ONLY || frame_size > 3 * (*st).Fs / 50
    {
        let mut enc_frame_size: i32 = 0;
        let mut nb_frames: i32 = 0;
        if (*st).mode == MODE_SILK_ONLY {
            if frame_size == 2 * (*st).Fs / 25 {
                enc_frame_size = (*st).Fs / 25;
            } else if frame_size == 3 * (*st).Fs / 25 {
                enc_frame_size = 3 * (*st).Fs / 50;
            } else {
                enc_frame_size = (*st).Fs / 50;
            }
        } else {
            enc_frame_size = (*st).Fs / 50;
        }
        nb_frames = frame_size / enc_frame_size;
        if analysis_read_pos_bak != -1 {
            (*st).analysis.read_pos = analysis_read_pos_bak;
            (*st).analysis.read_subframe = analysis_read_subframe_bak;
        }
        ret = encode_multiframe_packet(
            st,
            pcm,
            nb_frames,
            enc_frame_size,
            data,
            out_data_bytes,
            to_celt,
            lsb_depth,
            float_api,
        );
        return ret;
    }
    if (*st).silk_bw_switch != 0 {
        redundancy = 1;
        celt_to_silk = 1;
        (*st).silk_bw_switch = 0;
        prefill = 2;
    }
    if (*st).mode == MODE_CELT_ONLY {
        redundancy = 0;
    }
    if redundancy != 0 {
        redundancy_bytes = compute_redundancy_bytes(
            max_data_bytes,
            (*st).bitrate_bps,
            frame_rate,
            (*st).stream_channels,
        );
        if redundancy_bytes == 0 {
            redundancy = 0;
        }
    }
    bytes_target =
        (if max_data_bytes - redundancy_bytes < (*st).bitrate_bps * frame_size / ((*st).Fs * 8) {
            max_data_bytes - redundancy_bytes
        } else {
            (*st).bitrate_bps * frame_size / ((*st).Fs * 8)
        }) - 1;
    data = data.offset(1 as isize);
    enc = ec_enc_init(std::slice::from_raw_parts_mut(
        data,
        (max_data_bytes - 1) as usize,
    ));
    let vla = ((total_buffer + frame_size) * (*st).channels) as usize;
    let mut pcm_buf: Vec<opus_val16> = ::std::vec::from_elem(0., vla);
    memcpy(
        pcm_buf.as_mut_ptr() as *mut core::ffi::c_void,
        &mut *((*st).delay_buffer)
            .as_mut_ptr()
            .offset((((*st).encoder_buffer - total_buffer) * (*st).channels) as isize)
            as *mut opus_val16 as *const core::ffi::c_void,
        ((total_buffer * (*st).channels) as u64)
            .wrapping_mul(::core::mem::size_of::<opus_val16>() as u64)
            .wrapping_add(
                (0 * pcm_buf.as_mut_ptr().offset_from(
                    &mut *((*st).delay_buffer)
                        .as_mut_ptr()
                        .offset((((*st).encoder_buffer - total_buffer) * (*st).channels) as isize),
                ) as i64) as u64,
            ),
    );
    if (*st).mode == MODE_CELT_ONLY {
        hp_freq_smth1 = ((silk_lin2log(60) as u32) << 8) as i32;
    } else {
        hp_freq_smth1 = (*(silk_enc as *mut silk_encoder)).state_Fxx[0 as usize]
            .sCmn
            .variable_HP_smth1_Q15;
    }
    (*st).variable_HP_smth2_Q15 = ((*st).variable_HP_smth2_Q15 as i64
        + ((hp_freq_smth1 - (*st).variable_HP_smth2_Q15) as i64
            * ((0.015f32 * ((1) << 16) as f32) as f64 + 0.5f64) as i32 as i16 as i64
            >> 16)) as i32;
    cutoff_Hz = silk_log2lin((*st).variable_HP_smth2_Q15 >> 8);
    if (*st).application == OPUS_APPLICATION_VOIP {
        hp_cutoff(
            pcm,
            cutoff_Hz,
            &mut *pcm_buf
                .as_mut_ptr()
                .offset((total_buffer * (*st).channels) as isize),
            ((*st).hp_mem).as_mut_ptr(),
            frame_size,
            (*st).channels,
            (*st).Fs,
            (*st).arch,
        );
    } else {
        dc_reject(
            pcm,
            3,
            &mut *pcm_buf
                .as_mut_ptr()
                .offset((total_buffer * (*st).channels) as isize),
            ((*st).hp_mem).as_mut_ptr(),
            frame_size,
            (*st).channels,
            (*st).Fs,
        );
    }
    if float_api != 0 {
        let mut sum: opus_val32 = 0.;
        sum = celt_inner_prod_c(
            &mut *pcm_buf
                .as_mut_ptr()
                .offset((total_buffer * (*st).channels) as isize),
            &mut *pcm_buf
                .as_mut_ptr()
                .offset((total_buffer * (*st).channels) as isize),
            frame_size * (*st).channels,
        );
        if !(sum < 1e9f32) || sum != sum {
            memset(
                &mut *pcm_buf
                    .as_mut_ptr()
                    .offset((total_buffer * (*st).channels) as isize)
                    as *mut opus_val16 as *mut core::ffi::c_void,
                0,
                ((frame_size * (*st).channels) as u64)
                    .wrapping_mul(::core::mem::size_of::<opus_val16>() as u64),
            );
            (*st).hp_mem[3 as usize] = 0 as opus_val32;
            (*st).hp_mem[2 as usize] = (*st).hp_mem[3 as usize];
            (*st).hp_mem[1 as usize] = (*st).hp_mem[2 as usize];
            (*st).hp_mem[0 as usize] = (*st).hp_mem[1 as usize];
        }
    }
    HB_gain = Q15ONE;
    if (*st).mode != MODE_CELT_ONLY {
        let mut total_bitRate: i32 = 0;
        let mut celt_rate: i32 = 0;
        let mut activity: i32 = 0;
        let vla_0 = ((*st).channels * frame_size) as usize;
        let mut pcm_silk: Vec<i16> = ::std::vec::from_elem(0, vla_0);
        activity = VAD_NO_DECISION;
        if analysis_info.valid != 0 {
            activity = (analysis_info.activity_probability >= DTX_ACTIVITY_THRESHOLD) as i32;
        }
        total_bitRate = 8 * bytes_target * frame_rate;
        if (*st).mode == MODE_HYBRID {
            (*st).silk_mode.bitRate = compute_silk_rate_for_hybrid(
                total_bitRate,
                curr_bandwidth,
                ((*st).Fs == 50 * frame_size) as i32,
                (*st).use_vbr,
                (*st).silk_mode.LBRR_coded,
                (*st).stream_channels,
            );
            if ((*st).energy_masking).is_null() {
                celt_rate = total_bitRate - (*st).silk_mode.bitRate;
                HB_gain = Q15ONE - celt_exp2(-celt_rate as f32 * (1.0f32 / 1024f32));
            }
        } else {
            (*st).silk_mode.bitRate = total_bitRate;
        }
        if !((*st).energy_masking).is_null() && (*st).use_vbr != 0 && (*st).lfe == 0 {
            let mut mask_sum: opus_val32 = 0 as opus_val32;
            let mut masking_depth: opus_val16 = 0.;
            let mut rate_offset: i32 = 0;
            let mut c: i32 = 0;
            let mut end: i32 = 17;
            let mut srate: i16 = 16000;
            if (*st).bandwidth == OPUS_BANDWIDTH_NARROWBAND {
                end = 13;
                srate = 8000;
            } else if (*st).bandwidth == OPUS_BANDWIDTH_MEDIUMBAND {
                end = 15;
                srate = 12000;
            }
            c = 0;
            while c < (*st).channels {
                i = 0;
                while i < end {
                    let mut mask: opus_val16 = 0.;
                    mask = if (if *((*st).energy_masking).offset((21 * c + i) as isize) < 0.5f32 {
                        *((*st).energy_masking).offset((21 * c + i) as isize)
                    } else {
                        0.5f32
                    }) > -2.0f32
                    {
                        if *((*st).energy_masking).offset((21 * c + i) as isize) < 0.5f32 {
                            *((*st).energy_masking).offset((21 * c + i) as isize)
                        } else {
                            0.5f32
                        }
                    } else {
                        -2.0f32
                    };
                    if mask > 0 as f32 {
                        mask = 0.5f32 * mask;
                    }
                    mask_sum += mask;
                    i += 1;
                }
                c += 1;
            }
            masking_depth = mask_sum / end as f32 * (*st).channels as f32;
            masking_depth += 0.2f32;
            rate_offset = (srate as opus_val32 * masking_depth) as i32;
            rate_offset = if rate_offset > -(2) * (*st).silk_mode.bitRate / 3 {
                rate_offset
            } else {
                -(2) * (*st).silk_mode.bitRate / 3
            };
            if (*st).bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND
                || (*st).bandwidth == OPUS_BANDWIDTH_FULLBAND
            {
                (*st).silk_mode.bitRate += 3 * rate_offset / 5;
            } else {
                (*st).silk_mode.bitRate += rate_offset;
            }
        }
        (*st).silk_mode.payloadSize_ms = 1000 * frame_size / (*st).Fs;
        (*st).silk_mode.nChannelsAPI = (*st).channels;
        (*st).silk_mode.nChannelsInternal = (*st).stream_channels;
        if curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND {
            (*st).silk_mode.desiredInternalSampleRate = 8000;
        } else if curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND {
            (*st).silk_mode.desiredInternalSampleRate = 12000;
        } else {
            assert!((*st).mode == 1001 || curr_bandwidth == 1103);
            (*st).silk_mode.desiredInternalSampleRate = 16000;
        }
        if (*st).mode == MODE_HYBRID {
            (*st).silk_mode.minInternalSampleRate = 16000;
        } else {
            (*st).silk_mode.minInternalSampleRate = 8000;
        }
        (*st).silk_mode.maxInternalSampleRate = 16000;
        if (*st).mode == MODE_SILK_ONLY {
            let mut effective_max_rate: i32 = max_rate;
            if frame_rate > 50 {
                effective_max_rate = effective_max_rate * 2 / 3;
            }
            if effective_max_rate < 8000 {
                (*st).silk_mode.maxInternalSampleRate = 12000;
                (*st).silk_mode.desiredInternalSampleRate =
                    if (12000) < (*st).silk_mode.desiredInternalSampleRate {
                        12000
                    } else {
                        (*st).silk_mode.desiredInternalSampleRate
                    };
            }
            if effective_max_rate < 7000 {
                (*st).silk_mode.maxInternalSampleRate = 8000;
                (*st).silk_mode.desiredInternalSampleRate =
                    if (8000) < (*st).silk_mode.desiredInternalSampleRate {
                        8000
                    } else {
                        (*st).silk_mode.desiredInternalSampleRate
                    };
            }
        }
        (*st).silk_mode.useCBR = ((*st).use_vbr == 0) as i32;
        (*st).silk_mode.maxBits = (max_data_bytes - 1) * 8;
        if redundancy != 0 && redundancy_bytes >= 2 {
            (*st).silk_mode.maxBits -= redundancy_bytes * 8 + 1;
            if (*st).mode == MODE_HYBRID {
                (*st).silk_mode.maxBits -= 20;
            }
        }
        if (*st).silk_mode.useCBR != 0 {
            if (*st).mode == MODE_HYBRID {
                (*st).silk_mode.maxBits =
                    if (*st).silk_mode.maxBits < (*st).silk_mode.bitRate * frame_size / (*st).Fs {
                        (*st).silk_mode.maxBits
                    } else {
                        (*st).silk_mode.bitRate * frame_size / (*st).Fs
                    };
            }
        } else if (*st).mode == MODE_HYBRID {
            let maxBitRate: i32 = compute_silk_rate_for_hybrid(
                (*st).silk_mode.maxBits * (*st).Fs / frame_size,
                curr_bandwidth,
                ((*st).Fs == 50 * frame_size) as i32,
                (*st).use_vbr,
                (*st).silk_mode.LBRR_coded,
                (*st).stream_channels,
            );
            (*st).silk_mode.maxBits = maxBitRate * frame_size / (*st).Fs;
        }
        if prefill != 0 {
            let mut zero: i32 = 0;
            let mut prefill_offset: i32 = 0;
            prefill_offset =
                (*st).channels * ((*st).encoder_buffer - (*st).delay_compensation - (*st).Fs / 400);
            gain_fade(
                ((*st).delay_buffer)
                    .as_mut_ptr()
                    .offset(prefill_offset as isize),
                ((*st).delay_buffer)
                    .as_mut_ptr()
                    .offset(prefill_offset as isize),
                0 as opus_val16,
                Q15ONE,
                (*celt_mode).overlap,
                (*st).Fs / 400,
                (*st).channels,
                (*celt_mode).window,
                (*st).Fs,
            );
            memset(
                ((*st).delay_buffer).as_mut_ptr() as *mut core::ffi::c_void,
                0,
                (prefill_offset as u64).wrapping_mul(::core::mem::size_of::<opus_val16>() as u64),
            );
            i = 0;
            while i < (*st).encoder_buffer * (*st).channels {
                *pcm_silk.as_mut_ptr().offset(i as isize) =
                    FLOAT2INT16((*st).delay_buffer[i as usize]);
                i += 1;
            }
            silk_Encode(
                silk_enc,
                &mut (*st).silk_mode,
                pcm_silk.as_mut_ptr(),
                (*st).encoder_buffer,
                None,
                &mut zero,
                prefill,
                activity,
            );
            (*st).silk_mode.opusCanSwitch = 0;
        }
        i = 0;
        while i < frame_size * (*st).channels {
            *pcm_silk.as_mut_ptr().offset(i as isize) = FLOAT2INT16(
                *pcm_buf
                    .as_mut_ptr()
                    .offset((total_buffer * (*st).channels + i) as isize),
            );
            i += 1;
        }
        ret = silk_Encode(
            silk_enc,
            &mut (*st).silk_mode,
            pcm_silk.as_mut_ptr(),
            frame_size,
            Some(&mut enc),
            &mut nBytes,
            0,
            activity,
        );
        if ret != 0 {
            return OPUS_INTERNAL_ERROR;
        }
        if (*st).mode == MODE_SILK_ONLY {
            if (*st).silk_mode.internalSampleRate == 8000 {
                curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
            } else if (*st).silk_mode.internalSampleRate == 12000 {
                curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
            } else if (*st).silk_mode.internalSampleRate == 16000 {
                curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
            }
        } else {
            assert!((*st).silk_mode.internalSampleRate == 16000)
        };
        (*st).silk_mode.opusCanSwitch =
            ((*st).silk_mode.switchReady != 0 && (*st).nonfinal_frame == 0) as i32;
        if nBytes == 0 {
            (*st).rangeFinal = 0;
            *data.offset(-1 as isize) = gen_toc(
                (*st).mode,
                (*st).Fs / frame_size,
                curr_bandwidth,
                (*st).stream_channels,
            );
            return 1;
        }
        if (*st).silk_mode.opusCanSwitch != 0 {
            redundancy_bytes = compute_redundancy_bytes(
                max_data_bytes,
                (*st).bitrate_bps,
                frame_rate,
                (*st).stream_channels,
            );
            redundancy = (redundancy_bytes != 0) as i32;
            celt_to_silk = 0;
            (*st).silk_bw_switch = 1;
        }
    }
    let mut endband: i32 = 21;
    match curr_bandwidth {
        OPUS_BANDWIDTH_NARROWBAND => {
            endband = 13;
        }
        OPUS_BANDWIDTH_MEDIUMBAND | OPUS_BANDWIDTH_WIDEBAND => {
            endband = 17;
        }
        OPUS_BANDWIDTH_SUPERWIDEBAND => {
            endband = 19;
        }
        OPUS_BANDWIDTH_FULLBAND => {
            endband = 21;
        }
        _ => {}
    }
    opus_custom_encoder_ctl!(celt_enc, CELT_SET_END_BAND_REQUEST, endband);
    opus_custom_encoder_ctl!(celt_enc, CELT_SET_CHANNELS_REQUEST, (*st).stream_channels);
    opus_custom_encoder_ctl!(celt_enc, OPUS_SET_BITRATE_REQUEST, -1);
    if (*st).mode != MODE_SILK_ONLY {
        let mut celt_pred: opus_val32 = 2 as opus_val32;
        opus_custom_encoder_ctl!(celt_enc, OPUS_SET_VBR_REQUEST, 0);
        if (*st).silk_mode.reducedDependency != 0 {
            celt_pred = 0 as opus_val32;
        }
        opus_custom_encoder_ctl!(celt_enc, CELT_SET_PREDICTION_REQUEST, celt_pred as i32);
        if (*st).mode == MODE_HYBRID {
            if (*st).use_vbr != 0 {
                opus_custom_encoder_ctl!(
                    celt_enc,
                    OPUS_SET_BITRATE_REQUEST,
                    (*st).bitrate_bps - (*st).silk_mode.bitRate,
                );
                opus_custom_encoder_ctl!(celt_enc, OPUS_SET_VBR_CONSTRAINT_REQUEST, 0,);
            }
        } else if (*st).use_vbr != 0 {
            opus_custom_encoder_ctl!(celt_enc, OPUS_SET_VBR_REQUEST, 1);
            opus_custom_encoder_ctl!(
                celt_enc,
                OPUS_SET_VBR_CONSTRAINT_REQUEST,
                (*st).vbr_constraint,
            );
            opus_custom_encoder_ctl!(celt_enc, OPUS_SET_BITRATE_REQUEST, (*st).bitrate_bps);
        }
    }
    let vla_1 = ((*st).channels * (*st).Fs / 400) as usize;
    let mut tmp_prefill: Vec<opus_val16> = ::std::vec::from_elem(0., vla_1);
    if (*st).mode != MODE_SILK_ONLY && (*st).mode != (*st).prev_mode && (*st).prev_mode > 0 {
        memcpy(
            tmp_prefill.as_mut_ptr() as *mut core::ffi::c_void,
            &mut *((*st).delay_buffer).as_mut_ptr().offset(
                (((*st).encoder_buffer - total_buffer - (*st).Fs / 400) * (*st).channels) as isize,
            ) as *mut opus_val16 as *const core::ffi::c_void,
            (((*st).channels * (*st).Fs / 400) as u64)
                .wrapping_mul(::core::mem::size_of::<opus_val16>() as u64)
                .wrapping_add(
                    (0 * tmp_prefill.as_mut_ptr().offset_from(
                        &mut *((*st).delay_buffer).as_mut_ptr().offset(
                            (((*st).encoder_buffer - total_buffer - (*st).Fs / 400)
                                * (*st).channels) as isize,
                        ),
                    ) as i64) as u64,
                ),
        );
    }
    if (*st).channels * ((*st).encoder_buffer - (frame_size + total_buffer)) > 0 {
        memmove(
            ((*st).delay_buffer).as_mut_ptr() as *mut core::ffi::c_void,
            &mut *((*st).delay_buffer)
                .as_mut_ptr()
                .offset(((*st).channels * frame_size) as isize) as *mut opus_val16
                as *const core::ffi::c_void,
            (((*st).channels * ((*st).encoder_buffer - frame_size - total_buffer)) as u64)
                .wrapping_mul(::core::mem::size_of::<opus_val16>() as u64)
                .wrapping_add(
                    (0 * ((*st).delay_buffer).as_mut_ptr().offset_from(
                        &mut *((*st).delay_buffer)
                            .as_mut_ptr()
                            .offset(((*st).channels * frame_size) as isize),
                    ) as i64) as u64,
                ),
        );
        memcpy(
            &mut *((*st).delay_buffer).as_mut_ptr().offset(
                ((*st).channels * ((*st).encoder_buffer - frame_size - total_buffer)) as isize,
            ) as *mut opus_val16 as *mut core::ffi::c_void,
            &mut *pcm_buf.as_mut_ptr().offset(0 as isize) as *mut opus_val16
                as *const core::ffi::c_void,
            (((frame_size + total_buffer) * (*st).channels) as u64)
                .wrapping_mul(::core::mem::size_of::<opus_val16>() as u64)
                .wrapping_add(
                    (0 * (&mut *((*st).delay_buffer).as_mut_ptr().offset(
                        ((*st).channels * ((*st).encoder_buffer - frame_size - total_buffer))
                            as isize,
                    ) as *mut opus_val16)
                        .offset_from(&mut *pcm_buf.as_mut_ptr().offset(0 as isize))
                        as i64) as u64,
                ),
        );
    } else {
        memcpy(
            ((*st).delay_buffer).as_mut_ptr() as *mut core::ffi::c_void,
            &mut *pcm_buf.as_mut_ptr().offset(
                ((frame_size + total_buffer - (*st).encoder_buffer) * (*st).channels) as isize,
            ) as *mut opus_val16 as *const core::ffi::c_void,
            (((*st).encoder_buffer * (*st).channels) as u64)
                .wrapping_mul(::core::mem::size_of::<opus_val16>() as u64)
                .wrapping_add(
                    (0 * ((*st).delay_buffer).as_mut_ptr().offset_from(
                        &mut *pcm_buf.as_mut_ptr().offset(
                            ((frame_size + total_buffer - (*st).encoder_buffer) * (*st).channels)
                                as isize,
                        ),
                    ) as i64) as u64,
                ),
        );
    }
    if (*st).prev_HB_gain < Q15ONE || HB_gain < Q15ONE {
        gain_fade(
            pcm_buf.as_mut_ptr(),
            pcm_buf.as_mut_ptr(),
            (*st).prev_HB_gain,
            HB_gain,
            (*celt_mode).overlap,
            frame_size,
            (*st).channels,
            (*celt_mode).window,
            (*st).Fs,
        );
    }
    (*st).prev_HB_gain = HB_gain;
    if (*st).mode != MODE_HYBRID || (*st).stream_channels == 1 {
        if equiv_rate > 32000 {
            (*st).silk_mode.stereoWidth_Q14 = 16384;
        } else if equiv_rate < 16000 {
            (*st).silk_mode.stereoWidth_Q14 = 0;
        } else {
            (*st).silk_mode.stereoWidth_Q14 =
                16384 - 2048 * (32000 - equiv_rate) / (equiv_rate - 14000);
        }
    }
    if ((*st).energy_masking).is_null() && (*st).channels == 2 {
        if ((*st).hybrid_stereo_width_Q14 as i32) < (1) << 14
            || (*st).silk_mode.stereoWidth_Q14 < (1) << 14
        {
            let mut g1: opus_val16 = 0.;
            let mut g2: opus_val16 = 0.;
            g1 = (*st).hybrid_stereo_width_Q14 as opus_val16;
            g2 = (*st).silk_mode.stereoWidth_Q14 as opus_val16;
            g1 *= 1.0f32 / 16384 as f32;
            g2 *= 1.0f32 / 16384 as f32;
            stereo_fade(
                pcm_buf.as_mut_ptr(),
                pcm_buf.as_mut_ptr(),
                g1,
                g2,
                (*celt_mode).overlap,
                frame_size,
                (*st).channels,
                (*celt_mode).window,
                (*st).Fs,
            );
            (*st).hybrid_stereo_width_Q14 = (*st).silk_mode.stereoWidth_Q14 as i16;
        }
    }
    if (*st).mode != MODE_CELT_ONLY
        && ec_tell(&mut enc) + 17 + 20 * ((*st).mode == MODE_HYBRID) as i32
            <= 8 * (max_data_bytes - 1)
    {
        if (*st).mode == MODE_HYBRID {
            ec_enc_bit_logp(&mut enc, redundancy, 12);
        }
        if redundancy != 0 {
            let mut max_redundancy: i32 = 0;
            ec_enc_bit_logp(&mut enc, celt_to_silk, 1);
            if (*st).mode == MODE_HYBRID {
                max_redundancy = max_data_bytes - 1 - (ec_tell(&mut enc) + 8 + 3 + 7 >> 3);
            } else {
                max_redundancy = max_data_bytes - 1 - (ec_tell(&mut enc) + 7 >> 3);
            }
            redundancy_bytes = if max_redundancy < redundancy_bytes {
                max_redundancy
            } else {
                redundancy_bytes
            };
            redundancy_bytes = if (257)
                < (if 2 > redundancy_bytes {
                    2
                } else {
                    redundancy_bytes
                }) {
                257
            } else if 2 > redundancy_bytes {
                2
            } else {
                redundancy_bytes
            };
            if (*st).mode == MODE_HYBRID {
                ec_enc_uint(&mut enc, (redundancy_bytes - 2) as u32, 256);
            }
        }
    } else {
        redundancy = 0;
    }
    if redundancy == 0 {
        (*st).silk_bw_switch = 0;
        redundancy_bytes = 0;
    }
    if (*st).mode != MODE_CELT_ONLY {
        start_band = 17;
    }
    if (*st).mode == MODE_SILK_ONLY {
        ret = ec_tell(&mut enc) + 7 >> 3;
        ec_enc_done(&mut enc);
        nb_compr_bytes = ret;
    } else {
        nb_compr_bytes = max_data_bytes - 1 - redundancy_bytes;
        ec_enc_shrink(&mut enc, nb_compr_bytes as u32);
    }
    if redundancy != 0 || (*st).mode != MODE_SILK_ONLY {
        opus_custom_encoder_ctl!(celt_enc, CELT_SET_ANALYSIS_REQUEST, &mut analysis_info);
    }
    if (*st).mode == MODE_HYBRID {
        let mut info: SILKInfo = SILKInfo {
            signalType: 0,
            offset: 0,
        };
        info.signalType = (*st).silk_mode.signalType;
        info.offset = (*st).silk_mode.offset;
        opus_custom_encoder_ctl!(celt_enc, CELT_SET_SILK_INFO_REQUEST, &mut info);
    }
    if redundancy != 0 && celt_to_silk != 0 {
        let mut err: i32 = 0;
        opus_custom_encoder_ctl!(celt_enc, CELT_SET_START_BAND_REQUEST, 0);
        opus_custom_encoder_ctl!(celt_enc, OPUS_SET_VBR_REQUEST, 0);
        opus_custom_encoder_ctl!(celt_enc, OPUS_SET_BITRATE_REQUEST, -1);
        err = celt_encode_with_ec(
            celt_enc,
            pcm_buf.as_mut_ptr(),
            (*st).Fs / 200,
            data.offset(nb_compr_bytes as isize),
            redundancy_bytes,
            None,
        );
        if err < 0 {
            return OPUS_INTERNAL_ERROR;
        }
        opus_custom_encoder_ctl!(celt_enc, OPUS_GET_FINAL_RANGE_REQUEST, &mut redundant_rng);
        opus_custom_encoder_ctl!(celt_enc, OPUS_RESET_STATE);
    }
    opus_custom_encoder_ctl!(celt_enc, CELT_SET_START_BAND_REQUEST, start_band);
    if (*st).mode != MODE_SILK_ONLY {
        if (*st).mode != (*st).prev_mode && (*st).prev_mode > 0 {
            let mut dummy_0: [u8; 2] = [0; 2];
            opus_custom_encoder_ctl!(celt_enc, OPUS_RESET_STATE);
            celt_encode_with_ec(
                celt_enc,
                tmp_prefill.as_mut_ptr(),
                (*st).Fs / 400,
                dummy_0.as_mut_ptr(),
                2,
                None,
            );
            opus_custom_encoder_ctl!(celt_enc, CELT_SET_PREDICTION_REQUEST, 0);
        }
        if ec_tell(&mut enc) <= 8 * nb_compr_bytes {
            if redundancy != 0
                && celt_to_silk != 0
                && (*st).mode == MODE_HYBRID
                && (*st).use_vbr != 0
            {
                opus_custom_encoder_ctl!(
                    celt_enc,
                    OPUS_SET_BITRATE_REQUEST,
                    (*st).bitrate_bps - (*st).silk_mode.bitRate,
                );
            }
            opus_custom_encoder_ctl!(celt_enc, OPUS_SET_VBR_REQUEST, (*st).use_vbr);
            ret = celt_encode_with_ec(
                celt_enc,
                pcm_buf.as_mut_ptr(),
                frame_size,
                NULL as *mut u8,
                nb_compr_bytes,
                Some(&mut enc),
            );
            if ret < 0 {
                return OPUS_INTERNAL_ERROR;
            }
            if redundancy != 0
                && celt_to_silk != 0
                && (*st).mode == MODE_HYBRID
                && (*st).use_vbr != 0
            {
                memmove(
                    data.offset(ret as isize) as *mut core::ffi::c_void,
                    data.offset(nb_compr_bytes as isize) as *const core::ffi::c_void,
                    (redundancy_bytes as u64)
                        .wrapping_mul(::core::mem::size_of::<u8>() as u64)
                        .wrapping_add(
                            (0 * data
                                .offset(ret as isize)
                                .offset_from(data.offset(nb_compr_bytes as isize))
                                as i64) as u64,
                        ),
                );
                nb_compr_bytes = nb_compr_bytes + redundancy_bytes;
            }
        }
    }
    if redundancy != 0 && celt_to_silk == 0 {
        let mut err_0: i32 = 0;
        let mut dummy_1: [u8; 2] = [0; 2];
        let mut N2: i32 = 0;
        let mut N4: i32 = 0;
        N2 = (*st).Fs / 200;
        N4 = (*st).Fs / 400;
        opus_custom_encoder_ctl!(celt_enc, OPUS_RESET_STATE);
        opus_custom_encoder_ctl!(celt_enc, CELT_SET_START_BAND_REQUEST, 0);
        opus_custom_encoder_ctl!(celt_enc, CELT_SET_PREDICTION_REQUEST, 0);
        opus_custom_encoder_ctl!(celt_enc, OPUS_SET_VBR_REQUEST, 0);
        opus_custom_encoder_ctl!(celt_enc, OPUS_SET_BITRATE_REQUEST, -1);
        if (*st).mode == MODE_HYBRID {
            nb_compr_bytes = ret;
            ec_enc_shrink(&mut enc, nb_compr_bytes as u32);
        }
        celt_encode_with_ec(
            celt_enc,
            pcm_buf
                .as_mut_ptr()
                .offset(((*st).channels * (frame_size - N2 - N4)) as isize),
            N4,
            dummy_1.as_mut_ptr(),
            2,
            None,
        );
        err_0 = celt_encode_with_ec(
            celt_enc,
            pcm_buf
                .as_mut_ptr()
                .offset(((*st).channels * (frame_size - N2)) as isize),
            N2,
            data.offset(nb_compr_bytes as isize),
            redundancy_bytes,
            None,
        );
        if err_0 < 0 {
            return OPUS_INTERNAL_ERROR;
        }
        opus_custom_encoder_ctl!(celt_enc, OPUS_GET_FINAL_RANGE_REQUEST, &mut redundant_rng);
    }
    data = data.offset(-1);
    *data.offset(0 as isize) = gen_toc(
        (*st).mode,
        (*st).Fs / frame_size,
        curr_bandwidth,
        (*st).stream_channels,
    );
    (*st).rangeFinal = enc.rng ^ redundant_rng;
    if to_celt != 0 {
        (*st).prev_mode = MODE_CELT_ONLY;
    } else {
        (*st).prev_mode = (*st).mode;
    }
    (*st).prev_channels = (*st).stream_channels;
    (*st).prev_framesize = frame_size;
    (*st).first = 0;
    if (*st).use_dtx != 0 && (analysis_info.valid != 0 || is_silence != 0) {
        if decide_dtx_mode(
            analysis_info.activity_probability,
            &mut (*st).nb_no_activity_frames,
            (*st).peak_signal_energy,
            pcm,
            frame_size,
            (*st).channels,
            is_silence,
            (*st).arch,
        ) != 0
        {
            (*st).rangeFinal = 0;
            *data.offset(0 as isize) = gen_toc(
                (*st).mode,
                (*st).Fs / frame_size,
                curr_bandwidth,
                (*st).stream_channels,
            );
            return 1;
        }
    } else {
        (*st).nb_no_activity_frames = 0;
    }
    if ec_tell(&mut enc) > (max_data_bytes - 1) * 8 {
        if max_data_bytes < 2 {
            return OPUS_BUFFER_TOO_SMALL;
        }
        *data.offset(1 as isize) = 0;
        ret = 1;
        (*st).rangeFinal = 0;
    } else if (*st).mode == MODE_SILK_ONLY && redundancy == 0 {
        while ret > 2 && *data.offset(ret as isize) as i32 == 0 {
            ret -= 1;
        }
    }
    ret += 1 + redundancy_bytes;
    if (*st).use_vbr == 0 {
        if opus_packet_pad(data, ret, max_data_bytes) != OPUS_OK {
            return OPUS_INTERNAL_ERROR;
        }
        ret = max_data_bytes;
    }
    return ret;
}
pub unsafe fn opus_encode(
    st: *mut OpusEncoder,
    pcm: *const i16,
    analysis_frame_size: i32,
    data: *mut u8,
    max_data_bytes: i32,
) -> i32 {
    let mut i: i32 = 0;
    let mut ret: i32 = 0;
    let mut frame_size: i32 = 0;
    frame_size = frame_size_select(analysis_frame_size, (*st).variable_duration, (*st).Fs);
    if frame_size <= 0 {
        return OPUS_BAD_ARG;
    }
    let vla = (frame_size * (*st).channels) as usize;
    let mut in_0: Vec<f32> = ::std::vec::from_elem(0., vla);
    i = 0;
    while i < frame_size * (*st).channels {
        *in_0.as_mut_ptr().offset(i as isize) =
            1.0f32 / 32768 as f32 * *pcm.offset(i as isize) as i32 as f32;
        i += 1;
    }
    ret = opus_encode_native(
        st,
        in_0.as_mut_ptr(),
        frame_size,
        data,
        max_data_bytes,
        16,
        pcm as *const core::ffi::c_void,
        analysis_frame_size,
        0,
        -(2),
        (*st).channels,
        Some(
            downmix_int
                as unsafe fn(
                    *const core::ffi::c_void,
                    *mut opus_val32,
                    i32,
                    i32,
                    i32,
                    i32,
                    i32,
                ) -> (),
        ),
        0,
    );
    return ret;
}
pub unsafe fn opus_encode_float(
    st: *mut OpusEncoder,
    pcm: *const f32,
    analysis_frame_size: i32,
    data: *mut u8,
    out_data_bytes: i32,
) -> i32 {
    let mut frame_size: i32 = 0;
    frame_size = frame_size_select(analysis_frame_size, (*st).variable_duration, (*st).Fs);
    return opus_encode_native(
        st,
        pcm,
        frame_size,
        data,
        out_data_bytes,
        24,
        pcm as *const core::ffi::c_void,
        analysis_frame_size,
        0,
        -(2),
        (*st).channels,
        Some(
            downmix_float
                as unsafe fn(
                    *const core::ffi::c_void,
                    *mut opus_val32,
                    i32,
                    i32,
                    i32,
                    i32,
                    i32,
                ) -> (),
        ),
        1,
    );
}
pub unsafe fn opus_encoder_ctl_impl(st: *mut OpusEncoder, request: i32, args: VarArgs) -> i32 {
    let mut current_block: u64;
    let mut ret: i32 = 0;
    let mut celt_enc: *mut OpusCustomEncoder = 0 as *mut OpusCustomEncoder;
    ret = OPUS_OK;

    let mut ap = args;

    celt_enc = (st as *mut i8).offset((*st).celt_enc_offset as isize) as *mut OpusCustomEncoder;
    match request {
        OPUS_SET_APPLICATION_REQUEST => {
            let value: i32 = ap.arg::<i32>();
            if value != OPUS_APPLICATION_VOIP
                && value != OPUS_APPLICATION_AUDIO
                && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY
                || (*st).first == 0 && (*st).application != value
            {
                ret = OPUS_BAD_ARG;
            } else {
                (*st).application = value;
                (*st).analysis.application = value;
            }
            current_block = 16167632229894708628;
        }
        OPUS_GET_APPLICATION_REQUEST => {
            let value_0 = ap.arg::<&mut i32>();
            *value_0 = (*st).application;
            current_block = 16167632229894708628;
        }
        OPUS_SET_BITRATE_REQUEST => {
            let mut value_1: i32 = ap.arg::<i32>();
            if value_1 != OPUS_AUTO && value_1 != OPUS_BITRATE_MAX {
                if value_1 <= 0 {
                    current_block = 12343738388509029619;
                } else {
                    if value_1 <= 500 {
                        value_1 = 500;
                    } else if value_1 > 300000 * (*st).channels {
                        value_1 = 300000 * (*st).channels;
                    }
                    current_block = 6057473163062296781;
                }
            } else {
                current_block = 6057473163062296781;
            }
            match current_block {
                12343738388509029619 => {}
                _ => {
                    (*st).user_bitrate_bps = value_1;
                    current_block = 16167632229894708628;
                }
            }
        }
        OPUS_GET_BITRATE_REQUEST => {
            let value_2 = ap.arg::<&mut i32>();
            *value_2 = user_bitrate_to_bitrate(st, (*st).prev_framesize, 1276);
            current_block = 16167632229894708628;
        }
        OPUS_SET_FORCE_CHANNELS_REQUEST => {
            let value_3: i32 = ap.arg::<i32>();
            if (value_3 < 1 || value_3 > (*st).channels) && value_3 != OPUS_AUTO {
                current_block = 12343738388509029619;
            } else {
                (*st).force_channels = value_3;
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_FORCE_CHANNELS_REQUEST => {
            let value_4 = ap.arg::<&mut i32>();
            *value_4 = (*st).force_channels;
            current_block = 16167632229894708628;
        }
        OPUS_SET_MAX_BANDWIDTH_REQUEST => {
            let value_5: i32 = ap.arg::<i32>();
            if value_5 < OPUS_BANDWIDTH_NARROWBAND || value_5 > OPUS_BANDWIDTH_FULLBAND {
                current_block = 12343738388509029619;
            } else {
                (*st).max_bandwidth = value_5;
                if (*st).max_bandwidth == OPUS_BANDWIDTH_NARROWBAND {
                    (*st).silk_mode.maxInternalSampleRate = 8000;
                } else if (*st).max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND {
                    (*st).silk_mode.maxInternalSampleRate = 12000;
                } else {
                    (*st).silk_mode.maxInternalSampleRate = 16000;
                }
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_MAX_BANDWIDTH_REQUEST => {
            let value_6 = ap.arg::<&mut i32>();
            *value_6 = (*st).max_bandwidth;
            current_block = 16167632229894708628;
        }
        OPUS_SET_BANDWIDTH_REQUEST => {
            let value_7: i32 = ap.arg::<i32>();
            if (value_7 < OPUS_BANDWIDTH_NARROWBAND || value_7 > OPUS_BANDWIDTH_FULLBAND)
                && value_7 != OPUS_AUTO
            {
                current_block = 12343738388509029619;
            } else {
                (*st).user_bandwidth = value_7;
                if (*st).user_bandwidth == OPUS_BANDWIDTH_NARROWBAND {
                    (*st).silk_mode.maxInternalSampleRate = 8000;
                } else if (*st).user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND {
                    (*st).silk_mode.maxInternalSampleRate = 12000;
                } else {
                    (*st).silk_mode.maxInternalSampleRate = 16000;
                }
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_BANDWIDTH_REQUEST => {
            let value_8 = ap.arg::<&mut i32>();
            *value_8 = (*st).bandwidth;
            current_block = 16167632229894708628;
        }
        OPUS_SET_DTX_REQUEST => {
            let value_9: i32 = ap.arg::<i32>();
            if value_9 < 0 || value_9 > 1 {
                current_block = 12343738388509029619;
            } else {
                (*st).use_dtx = value_9;
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_DTX_REQUEST => {
            let value_10 = ap.arg::<&mut i32>();
            *value_10 = (*st).use_dtx;
            current_block = 16167632229894708628;
        }
        OPUS_SET_COMPLEXITY_REQUEST => {
            let value_11: i32 = ap.arg::<i32>();
            if value_11 < 0 || value_11 > 10 {
                current_block = 12343738388509029619;
            } else {
                (*st).silk_mode.complexity = value_11;
                opus_custom_encoder_ctl!(celt_enc, OPUS_SET_COMPLEXITY_REQUEST, value_11);
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_COMPLEXITY_REQUEST => {
            let value_12 = ap.arg::<&mut i32>();
            *value_12 = (*st).silk_mode.complexity;
            current_block = 16167632229894708628;
        }
        OPUS_SET_INBAND_FEC_REQUEST => {
            let value_13: i32 = ap.arg::<i32>();
            if value_13 < 0 || value_13 > 1 {
                current_block = 12343738388509029619;
            } else {
                (*st).silk_mode.useInBandFEC = value_13;
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_INBAND_FEC_REQUEST => {
            let value_14 = ap.arg::<&mut i32>();
            *value_14 = (*st).silk_mode.useInBandFEC;
            current_block = 16167632229894708628;
        }
        OPUS_SET_PACKET_LOSS_PERC_REQUEST => {
            let value_15: i32 = ap.arg::<i32>();
            if value_15 < 0 || value_15 > 100 {
                current_block = 12343738388509029619;
            } else {
                (*st).silk_mode.packetLossPercentage = value_15;
                opus_custom_encoder_ctl!(celt_enc, OPUS_SET_PACKET_LOSS_PERC_REQUEST, value_15);
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_PACKET_LOSS_PERC_REQUEST => {
            let value_16 = ap.arg::<&mut i32>();
            *value_16 = (*st).silk_mode.packetLossPercentage;
            current_block = 16167632229894708628;
        }
        OPUS_SET_VBR_REQUEST => {
            let value_17: i32 = ap.arg::<i32>();
            if value_17 < 0 || value_17 > 1 {
                current_block = 12343738388509029619;
            } else {
                (*st).use_vbr = value_17;
                (*st).silk_mode.useCBR = 1 - value_17;
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_VBR_REQUEST => {
            let value_18 = ap.arg::<&mut i32>();
            *value_18 = (*st).use_vbr;
            current_block = 16167632229894708628;
        }
        OPUS_SET_VOICE_RATIO_REQUEST => {
            let value_19: i32 = ap.arg::<i32>();
            if value_19 < -1 || value_19 > 100 {
                current_block = 12343738388509029619;
            } else {
                (*st).voice_ratio = value_19;
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_VOICE_RATIO_REQUEST => {
            let value_20 = ap.arg::<&mut i32>();
            *value_20 = (*st).voice_ratio;
            current_block = 16167632229894708628;
        }
        OPUS_SET_VBR_CONSTRAINT_REQUEST => {
            let value_21: i32 = ap.arg::<i32>();
            if value_21 < 0 || value_21 > 1 {
                current_block = 12343738388509029619;
            } else {
                (*st).vbr_constraint = value_21;
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_VBR_CONSTRAINT_REQUEST => {
            let value_22 = ap.arg::<&mut i32>();
            *value_22 = (*st).vbr_constraint;
            current_block = 16167632229894708628;
        }
        OPUS_SET_SIGNAL_REQUEST => {
            let value_23: i32 = ap.arg::<i32>();
            if value_23 != OPUS_AUTO
                && value_23 != OPUS_SIGNAL_VOICE
                && value_23 != OPUS_SIGNAL_MUSIC
            {
                current_block = 12343738388509029619;
            } else {
                (*st).signal_type = value_23;
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_SIGNAL_REQUEST => {
            let value_24 = ap.arg::<&mut i32>();
            *value_24 = (*st).signal_type;
            current_block = 16167632229894708628;
        }
        OPUS_GET_LOOKAHEAD_REQUEST => {
            let value_25 = ap.arg::<&mut i32>();
            *value_25 = (*st).Fs / 400;
            if (*st).application != OPUS_APPLICATION_RESTRICTED_LOWDELAY {
                *value_25 += (*st).delay_compensation;
            }
            current_block = 16167632229894708628;
        }
        OPUS_GET_SAMPLE_RATE_REQUEST => {
            let value_26 = ap.arg::<&mut i32>();
            *value_26 = (*st).Fs;
            current_block = 16167632229894708628;
        }
        OPUS_GET_FINAL_RANGE_REQUEST => {
            let value_27 = ap.arg::<&mut u32>();
            *value_27 = (*st).rangeFinal;
            current_block = 16167632229894708628;
        }
        OPUS_SET_LSB_DEPTH_REQUEST => {
            let value_28: i32 = ap.arg::<i32>();
            if value_28 < 8 || value_28 > 24 {
                current_block = 12343738388509029619;
            } else {
                (*st).lsb_depth = value_28;
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_LSB_DEPTH_REQUEST => {
            let value_29 = ap.arg::<&mut i32>();
            *value_29 = (*st).lsb_depth;
            current_block = 16167632229894708628;
        }
        OPUS_SET_EXPERT_FRAME_DURATION_REQUEST => {
            let value_30: i32 = ap.arg::<i32>();
            if value_30 != OPUS_FRAMESIZE_ARG
                && value_30 != OPUS_FRAMESIZE_2_5_MS
                && value_30 != OPUS_FRAMESIZE_5_MS
                && value_30 != OPUS_FRAMESIZE_10_MS
                && value_30 != OPUS_FRAMESIZE_20_MS
                && value_30 != OPUS_FRAMESIZE_40_MS
                && value_30 != OPUS_FRAMESIZE_60_MS
                && value_30 != OPUS_FRAMESIZE_80_MS
                && value_30 != OPUS_FRAMESIZE_100_MS
                && value_30 != OPUS_FRAMESIZE_120_MS
            {
                current_block = 12343738388509029619;
            } else {
                (*st).variable_duration = value_30;
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_EXPERT_FRAME_DURATION_REQUEST => {
            let value_31 = ap.arg::<&mut i32>();
            *value_31 = (*st).variable_duration;
            current_block = 16167632229894708628;
        }
        OPUS_SET_PREDICTION_DISABLED_REQUEST => {
            let value_32: i32 = ap.arg::<i32>();
            if value_32 > 1 || value_32 < 0 {
                current_block = 12343738388509029619;
            } else {
                (*st).silk_mode.reducedDependency = value_32;
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_PREDICTION_DISABLED_REQUEST => {
            let value_33 = ap.arg::<&mut i32>();
            *value_33 = (*st).silk_mode.reducedDependency;
            current_block = 16167632229894708628;
        }
        OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST => {
            let value_34: i32 = ap.arg::<i32>();
            if value_34 < 0 || value_34 > 1 {
                current_block = 12343738388509029619;
            } else {
                opus_custom_encoder_ctl!(
                    celt_enc,
                    OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST,
                    value_34,
                );
                current_block = 16167632229894708628;
            }
        }
        OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST => {
            let value_35 = ap.arg::<&mut i32>();
            opus_custom_encoder_ctl!(
                celt_enc,
                OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST,
                value_35
            );
            current_block = 16167632229894708628;
        }
        OPUS_RESET_STATE => {
            let mut silk_enc: *mut core::ffi::c_void = 0 as *mut core::ffi::c_void;
            let mut dummy: silk_EncControlStruct = silk_EncControlStruct {
                nChannelsAPI: 0,
                nChannelsInternal: 0,
                API_sampleRate: 0,
                maxInternalSampleRate: 0,
                minInternalSampleRate: 0,
                desiredInternalSampleRate: 0,
                payloadSize_ms: 0,
                bitRate: 0,
                packetLossPercentage: 0,
                complexity: 0,
                useInBandFEC: 0,
                LBRR_coded: 0,
                useDTX: 0,
                useCBR: 0,
                maxBits: 0,
                toMono: 0,
                opusCanSwitch: 0,
                reducedDependency: 0,
                internalSampleRate: 0,
                allowBandwidthSwitch: 0,
                inWBmodeWithoutVariableLP: 0,
                stereoWidth_Q14: 0,
                switchReady: 0,
                signalType: 0,
                offset: 0,
            };
            let mut start: *mut i8 = 0 as *mut i8;
            silk_enc =
                (st as *mut i8).offset((*st).silk_enc_offset as isize) as *mut core::ffi::c_void;
            tonality_analysis_reset(&mut (*st).analysis);
            start = &mut (*st).stream_channels as *mut i32 as *mut i8;
            memset(
                start as *mut core::ffi::c_void,
                0,
                (::core::mem::size_of::<OpusEncoder>() as u64)
                    .wrapping_sub(start.offset_from(st as *mut i8) as i64 as u64)
                    .wrapping_mul(::core::mem::size_of::<i8>() as u64),
            );
            opus_custom_encoder_ctl!(celt_enc, OPUS_RESET_STATE);
            silk_InitEncoder(silk_enc, (*st).arch, &mut dummy);
            (*st).stream_channels = (*st).channels;
            (*st).hybrid_stereo_width_Q14 = ((1) << 14) as i16;
            (*st).prev_HB_gain = Q15ONE;
            (*st).first = 1;
            (*st).mode = MODE_HYBRID;
            (*st).bandwidth = OPUS_BANDWIDTH_FULLBAND;
            (*st).variable_HP_smth2_Q15 = ((silk_lin2log(60) as u32) << 8) as i32;
            current_block = 16167632229894708628;
        }
        OPUS_SET_FORCE_MODE_REQUEST => {
            let value_36: i32 = ap.arg::<i32>();
            if (value_36 < MODE_SILK_ONLY || value_36 > MODE_CELT_ONLY) && value_36 != OPUS_AUTO {
                current_block = 12343738388509029619;
            } else {
                (*st).user_forced_mode = value_36;
                current_block = 16167632229894708628;
            }
        }
        OPUS_SET_LFE_REQUEST => {
            let value_37: i32 = ap.arg::<i32>();
            (*st).lfe = value_37;
            ret = opus_custom_encoder_ctl!(celt_enc, OPUS_SET_LFE_REQUEST, value_37);
            current_block = 16167632229894708628;
        }
        OPUS_SET_ENERGY_MASK_REQUEST => {
            let value_38: *mut opus_val16 = ap.arg::<*mut opus_val16>();
            (*st).energy_masking = value_38;
            ret = opus_custom_encoder_ctl!(
                celt_enc,
                OPUS_SET_ENERGY_MASK_REQUEST,
                value_38.offset(value_38.offset_from(value_38) as i64 as isize),
            );
            current_block = 16167632229894708628;
        }
        OPUS_GET_IN_DTX_REQUEST => {
            let value_39: &mut i32 = ap.arg::<&mut i32>();
            if (*st).silk_mode.useDTX != 0
                && ((*st).prev_mode == MODE_SILK_ONLY || (*st).prev_mode == MODE_HYBRID)
            {
                let mut n: i32 = 0;
                let silk_enc_0: *mut core::ffi::c_void = (st as *mut i8)
                    .offset((*st).silk_enc_offset as isize)
                    as *mut core::ffi::c_void;
                *value_39 = 1;
                n = 0;
                while n < (*st).silk_mode.nChannelsInternal {
                    *value_39 = (*value_39 != 0
                        && (*(silk_enc_0 as *mut silk_encoder)).state_Fxx[n as usize]
                            .sCmn
                            .noSpeechCounter
                            >= NB_SPEECH_FRAMES_BEFORE_DTX) as i32;
                    n += 1;
                }
            } else if (*st).use_dtx != 0 {
                *value_39 = ((*st).nb_no_activity_frames >= NB_SPEECH_FRAMES_BEFORE_DTX) as i32;
            } else {
                *value_39 = 0;
            }
            current_block = 16167632229894708628;
        }
        CELT_GET_MODE_REQUEST => {
            let value_40: &mut *const OpusCustomMode = ap.arg::<&mut *const OpusCustomMode>();
            ret = opus_custom_encoder_ctl!(celt_enc, CELT_GET_MODE_REQUEST, value_40);
            current_block = 16167632229894708628;
        }
        _ => {
            ret = OPUS_UNIMPLEMENTED;
            current_block = 16167632229894708628;
        }
    }
    match current_block {
        12343738388509029619 => return OPUS_BAD_ARG,
        _ => return ret,
    };
}
#[macro_export]
macro_rules! opus_encoder_ctl {
    ($st:expr, $request:expr, $($args:expr),*) => {
        $crate::opus_encoder_ctl_impl($st, $request, $crate::varargs!($($args),*))
    };
    ($st:expr, $request:expr, $($args:expr),*,) => {
        opus_encoder_ctl!($st, $request, $($args),*)
    };
    ($st:expr, $request:expr) => {
        opus_encoder_ctl!($st, $request,)
    };
}
pub unsafe fn opus_encoder_destroy(st: *mut OpusEncoder) {
    free(st as *mut core::ffi::c_void);
}