CommandProcessingTimeEstimatorVersion3.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. //
  2. // Copyright (c) 2019-2021 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using Ryujinx.Audio.Common;
  18. using Ryujinx.Audio.Renderer.Dsp.Command;
  19. using Ryujinx.Audio.Renderer.Parameter.Effect;
  20. using System;
  21. using System.Diagnostics;
  22. using static Ryujinx.Audio.Renderer.Parameter.VoiceInParameter;
  23. namespace Ryujinx.Audio.Renderer.Server
  24. {
  25. /// <summary>
  26. /// <see cref="ICommandProcessingTimeEstimator"/> version 3. (added with REV8)
  27. /// </summary>
  28. public class CommandProcessingTimeEstimatorVersion3 : ICommandProcessingTimeEstimator
  29. {
  30. protected uint _sampleCount;
  31. protected uint _bufferCount;
  32. public CommandProcessingTimeEstimatorVersion3(uint sampleCount, uint bufferCount)
  33. {
  34. _sampleCount = sampleCount;
  35. _bufferCount = bufferCount;
  36. }
  37. public uint Estimate(PerformanceCommand command)
  38. {
  39. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  40. if (_sampleCount == 160)
  41. {
  42. return (uint)498.17f;
  43. }
  44. return (uint)489.42f;
  45. }
  46. public uint Estimate(ClearMixBufferCommand command)
  47. {
  48. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  49. float costPerBuffer = 440.68f;
  50. float baseCost = 0;
  51. if (_sampleCount == 160)
  52. {
  53. costPerBuffer = 266.65f;
  54. }
  55. return (uint)(baseCost + costPerBuffer * _bufferCount);
  56. }
  57. public uint Estimate(BiquadFilterCommand command)
  58. {
  59. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  60. if (_sampleCount == 160)
  61. {
  62. return (uint)4173.2f;
  63. }
  64. return (uint)5585.1f;
  65. }
  66. public uint Estimate(MixRampGroupedCommand command)
  67. {
  68. float costPerSample = 6.4434f;
  69. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  70. if (_sampleCount == 160)
  71. {
  72. costPerSample = 6.708f;
  73. }
  74. int volumeCount = 0;
  75. for (int i = 0; i < command.MixBufferCount; i++)
  76. {
  77. if (command.Volume0[i] != 0.0f || command.Volume1[i] != 0.0f)
  78. {
  79. volumeCount++;
  80. }
  81. }
  82. return (uint)(_sampleCount * costPerSample * volumeCount);
  83. }
  84. public uint Estimate(MixRampCommand command)
  85. {
  86. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  87. if (_sampleCount == 160)
  88. {
  89. return (uint)1968.7f;
  90. }
  91. return (uint)2459.4f;
  92. }
  93. public uint Estimate(DepopPrepareCommand command)
  94. {
  95. return 0;
  96. }
  97. public uint Estimate(VolumeRampCommand command)
  98. {
  99. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  100. if (_sampleCount == 160)
  101. {
  102. return (uint)1425.3f;
  103. }
  104. return (uint)1700.0f;
  105. }
  106. public uint Estimate(PcmInt16DataSourceCommandVersion1 command)
  107. {
  108. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  109. float costPerSample = 710.143f;
  110. float baseCost = 7853.286f;
  111. if (_sampleCount == 160)
  112. {
  113. costPerSample = 427.52f;
  114. baseCost = 6329.442f;
  115. }
  116. return (uint)(baseCost + (costPerSample * (((command.SampleRate / 200.0f) / _sampleCount) * (command.Pitch * 0.000030518f))));
  117. }
  118. public uint Estimate(AdpcmDataSourceCommandVersion1 command)
  119. {
  120. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  121. float costPerSample = 3564.1f;
  122. float baseCost = 9736.702f;
  123. if (_sampleCount == 160)
  124. {
  125. costPerSample = 2125.6f;
  126. baseCost = 7913.808f;
  127. }
  128. return (uint)(baseCost + (costPerSample * (((command.SampleRate / 200.0f) / _sampleCount) * (command.Pitch * 0.000030518f))));
  129. }
  130. public uint Estimate(DepopForMixBuffersCommand command)
  131. {
  132. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  133. if (_sampleCount == 160)
  134. {
  135. return (uint)739.64f;
  136. }
  137. return (uint)910.97f;
  138. }
  139. public uint Estimate(CopyMixBufferCommand command)
  140. {
  141. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  142. if (_sampleCount == 160)
  143. {
  144. return (uint)842.59f;
  145. }
  146. return (uint)986.72f;
  147. }
  148. public uint Estimate(MixCommand command)
  149. {
  150. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  151. if (_sampleCount == 160)
  152. {
  153. return (uint)1402.8f;
  154. }
  155. return (uint)1853.2f;
  156. }
  157. public virtual uint Estimate(DelayCommand command)
  158. {
  159. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  160. if (_sampleCount == 160)
  161. {
  162. if (command.Enabled)
  163. {
  164. switch (command.Parameter.ChannelCount)
  165. {
  166. case 1:
  167. return (uint)8929.04f;
  168. case 2:
  169. return (uint)25500.75f;
  170. case 4:
  171. return (uint)47759.62f;
  172. case 6:
  173. return (uint)82203.07f;
  174. default:
  175. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  176. }
  177. }
  178. else
  179. {
  180. switch (command.Parameter.ChannelCount)
  181. {
  182. case 1:
  183. return (uint)1295.20f;
  184. case 2:
  185. return (uint)1213.60f;
  186. case 4:
  187. return (uint)942.03f;
  188. case 6:
  189. return (uint)1001.55f;
  190. default:
  191. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  192. }
  193. }
  194. }
  195. if (command.Enabled)
  196. {
  197. switch (command.Parameter.ChannelCount)
  198. {
  199. case 1:
  200. return (uint)11941.05f;
  201. case 2:
  202. return (uint)37197.37f;
  203. case 4:
  204. return (uint)69749.84f;
  205. case 6:
  206. return (uint)120042.40f;
  207. default:
  208. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  209. }
  210. }
  211. else
  212. {
  213. switch (command.Parameter.ChannelCount)
  214. {
  215. case 1:
  216. return (uint)997.67f;
  217. case 2:
  218. return (uint)977.63f;
  219. case 4:
  220. return (uint)792.30f;
  221. case 6:
  222. return (uint)875.43f;
  223. default:
  224. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  225. }
  226. }
  227. }
  228. public virtual uint Estimate(ReverbCommand command)
  229. {
  230. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  231. if (_sampleCount == 160)
  232. {
  233. if (command.Enabled)
  234. {
  235. switch (command.Parameter.ChannelCount)
  236. {
  237. case 1:
  238. return (uint)81475.05f;
  239. case 2:
  240. return (uint)84975.0f;
  241. case 4:
  242. return (uint)91625.15f;
  243. case 6:
  244. return (uint)95332.27f;
  245. default:
  246. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  247. }
  248. }
  249. else
  250. {
  251. switch (command.Parameter.ChannelCount)
  252. {
  253. case 1:
  254. return (uint)536.30f;
  255. case 2:
  256. return (uint)588.70f;
  257. case 4:
  258. return (uint)643.70f;
  259. case 6:
  260. return (uint)706.0f;
  261. default:
  262. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  263. }
  264. }
  265. }
  266. if (command.Enabled)
  267. {
  268. switch (command.Parameter.ChannelCount)
  269. {
  270. case 1:
  271. return (uint)120174.47f;
  272. case 2:
  273. return (uint)25262.22f;
  274. case 4:
  275. return (uint)135751.23f;
  276. case 6:
  277. return (uint)141129.23f;
  278. default:
  279. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  280. }
  281. }
  282. else
  283. {
  284. switch (command.Parameter.ChannelCount)
  285. {
  286. case 1:
  287. return (uint)617.64f;
  288. case 2:
  289. return (uint)659.54f;
  290. case 4:
  291. return (uint)711.43f;
  292. case 6:
  293. return (uint)778.07f;
  294. default:
  295. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  296. }
  297. }
  298. }
  299. public virtual uint Estimate(Reverb3dCommand command)
  300. {
  301. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  302. if (_sampleCount == 160)
  303. {
  304. if (command.Enabled)
  305. {
  306. switch (command.Parameter.ChannelCount)
  307. {
  308. case 1:
  309. return (uint)116754.0f;
  310. case 2:
  311. return (uint)125912.05f;
  312. case 4:
  313. return (uint)146336.03f;
  314. case 6:
  315. return (uint)165812.66f;
  316. default:
  317. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  318. }
  319. }
  320. else
  321. {
  322. switch (command.Parameter.ChannelCount)
  323. {
  324. case 1:
  325. return (uint)734.0f;
  326. case 2:
  327. return (uint)766.62f;
  328. case 4:
  329. return (uint)797.46f;
  330. case 6:
  331. return (uint)867.43f;
  332. default:
  333. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  334. }
  335. }
  336. }
  337. if (command.Enabled)
  338. {
  339. switch (command.Parameter.ChannelCount)
  340. {
  341. case 1:
  342. return (uint)170292.34f;
  343. case 2:
  344. return (uint)183875.63f;
  345. case 4:
  346. return (uint)214696.19f;
  347. case 6:
  348. return (uint)243846.77f;
  349. default:
  350. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  351. }
  352. }
  353. else
  354. {
  355. switch (command.Parameter.ChannelCount)
  356. {
  357. case 1:
  358. return (uint)508.47f;
  359. case 2:
  360. return (uint)582.45f;
  361. case 4:
  362. return (uint)626.42f;
  363. case 6:
  364. return (uint)682.47f;
  365. default:
  366. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  367. }
  368. }
  369. }
  370. public uint Estimate(AuxiliaryBufferCommand command)
  371. {
  372. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  373. if (_sampleCount == 160)
  374. {
  375. if (command.Enabled)
  376. {
  377. return (uint)7182.14f;
  378. }
  379. return (uint)472.11f;
  380. }
  381. if (command.Enabled)
  382. {
  383. return (uint)9435.96f;
  384. }
  385. return (uint)462.62f;
  386. }
  387. public uint Estimate(VolumeCommand command)
  388. {
  389. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  390. if (_sampleCount == 160)
  391. {
  392. return (uint)1311.1f;
  393. }
  394. return (uint)1713.6f;
  395. }
  396. public uint Estimate(CircularBufferSinkCommand command)
  397. {
  398. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  399. float costPerBuffer = 770.26f;
  400. float baseCost = 0f;
  401. if (_sampleCount == 160)
  402. {
  403. costPerBuffer = 531.07f;
  404. }
  405. return (uint)(baseCost + costPerBuffer * command.InputCount);
  406. }
  407. public uint Estimate(DownMixSurroundToStereoCommand command)
  408. {
  409. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  410. if (_sampleCount == 160)
  411. {
  412. return (uint)9949.7f;
  413. }
  414. return (uint)14679.0f;
  415. }
  416. public uint Estimate(UpsampleCommand command)
  417. {
  418. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  419. if (_sampleCount == 160)
  420. {
  421. return (uint)312990.0f;
  422. }
  423. return (uint)0.0f;
  424. }
  425. public uint Estimate(DeviceSinkCommand command)
  426. {
  427. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  428. Debug.Assert(command.InputCount == 2 || command.InputCount == 6);
  429. if (command.InputCount == 2)
  430. {
  431. if (_sampleCount == 160)
  432. {
  433. return (uint)8980.0f;
  434. }
  435. return (uint)9221.9f;
  436. }
  437. if (_sampleCount == 160)
  438. {
  439. return (uint)9177.9f;
  440. }
  441. return (uint)9725.9f;
  442. }
  443. public uint Estimate(PcmFloatDataSourceCommandVersion1 command)
  444. {
  445. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  446. float costPerSample = 3490.9f;
  447. float baseCost = 10090.9f;
  448. if (_sampleCount == 160)
  449. {
  450. costPerSample = 2310.4f;
  451. baseCost = 7845.25f;
  452. }
  453. return (uint)(baseCost + (costPerSample * (((command.SampleRate / 200.0f) / _sampleCount) * (command.Pitch * 0.000030518f))));
  454. }
  455. public uint Estimate(DataSourceVersion2Command command)
  456. {
  457. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  458. (float baseCost, float costPerSample) = GetCostByFormat(_sampleCount, command.SampleFormat, command.SrcQuality);
  459. return (uint)(baseCost + (costPerSample * (((command.SampleRate / 200.0f) / _sampleCount) * (command.Pitch * 0.000030518f) - 1.0f)));
  460. }
  461. private static (float, float) GetCostByFormat(uint sampleCount, SampleFormat format, SampleRateConversionQuality quality)
  462. {
  463. Debug.Assert(sampleCount == 160 || sampleCount == 240);
  464. switch (format)
  465. {
  466. case SampleFormat.PcmInt16:
  467. switch (quality)
  468. {
  469. case SampleRateConversionQuality.Default:
  470. if (sampleCount == 160)
  471. {
  472. return (6329.44f, 427.52f);
  473. }
  474. return (7853.28f, 710.14f);
  475. case SampleRateConversionQuality.High:
  476. if (sampleCount == 160)
  477. {
  478. return (8049.42f, 371.88f);
  479. }
  480. return (10138.84f, 610.49f);
  481. case SampleRateConversionQuality.Low:
  482. if (sampleCount == 160)
  483. {
  484. return (5062.66f, 423.43f);
  485. }
  486. return (5810.96f, 676.72f);
  487. default:
  488. throw new NotImplementedException($"{format} {quality}");
  489. }
  490. case SampleFormat.PcmFloat:
  491. switch (quality)
  492. {
  493. case SampleRateConversionQuality.Default:
  494. if (sampleCount == 160)
  495. {
  496. return (7845.25f, 2310.4f);
  497. }
  498. return (10090.9f, 3490.9f);
  499. case SampleRateConversionQuality.High:
  500. if (sampleCount == 160)
  501. {
  502. return (9446.36f, 2308.91f);
  503. }
  504. return (12520.85f, 3480.61f);
  505. case SampleRateConversionQuality.Low:
  506. if (sampleCount == 160)
  507. {
  508. return (9446.36f, 2308.91f);
  509. }
  510. return (12520.85f, 3480.61f);
  511. default:
  512. throw new NotImplementedException($"{format} {quality}");
  513. }
  514. case SampleFormat.Adpcm:
  515. switch (quality)
  516. {
  517. case SampleRateConversionQuality.Default:
  518. if (sampleCount == 160)
  519. {
  520. return (7913.81f, 1827.66f);
  521. }
  522. return (9736.70f, 2756.37f);
  523. case SampleRateConversionQuality.High:
  524. if (sampleCount == 160)
  525. {
  526. return (9607.81f, 1829.29f);
  527. }
  528. return (12154.38f, 2731.31f);
  529. case SampleRateConversionQuality.Low:
  530. if (sampleCount == 160)
  531. {
  532. return (6517.48f, 1824.61f);
  533. }
  534. return (7929.44f, 2732.15f);
  535. default:
  536. throw new NotImplementedException($"{format} {quality}");
  537. }
  538. default:
  539. throw new NotImplementedException($"{format}");
  540. }
  541. }
  542. private uint EstimateLimiterCommandCommon(LimiterParameter parameter, bool enabled)
  543. {
  544. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  545. if (_sampleCount == 160)
  546. {
  547. if (enabled)
  548. {
  549. switch (parameter.ChannelCount)
  550. {
  551. case 1:
  552. return (uint)21392.0f;
  553. case 2:
  554. return (uint)26829.0f;
  555. case 4:
  556. return (uint)32405.0f;
  557. case 6:
  558. return (uint)52219.0f;
  559. default:
  560. throw new NotImplementedException($"{parameter.ChannelCount}");
  561. }
  562. }
  563. else
  564. {
  565. switch (parameter.ChannelCount)
  566. {
  567. case 1:
  568. return (uint)897.0f;
  569. case 2:
  570. return (uint)931.55f;
  571. case 4:
  572. return (uint)975.39f;
  573. case 6:
  574. return (uint)1016.8f;
  575. default:
  576. throw new NotImplementedException($"{parameter.ChannelCount}");
  577. }
  578. }
  579. }
  580. if (enabled)
  581. {
  582. switch (parameter.ChannelCount)
  583. {
  584. case 1:
  585. return (uint)30556.0f;
  586. case 2:
  587. return (uint)39011.0f;
  588. case 4:
  589. return (uint)48270.0f;
  590. case 6:
  591. return (uint)76712.0f;
  592. default:
  593. throw new NotImplementedException($"{parameter.ChannelCount}");
  594. }
  595. }
  596. else
  597. {
  598. switch (parameter.ChannelCount)
  599. {
  600. case 1:
  601. return (uint)874.43f;
  602. case 2:
  603. return (uint)921.55f;
  604. case 4:
  605. return (uint)945.26f;
  606. case 6:
  607. return (uint)992.26f;
  608. default:
  609. throw new NotImplementedException($"{parameter.ChannelCount}");
  610. }
  611. }
  612. }
  613. public uint Estimate(LimiterCommandVersion1 command)
  614. {
  615. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  616. return EstimateLimiterCommandCommon(command.Parameter, command.IsEffectEnabled);
  617. }
  618. public uint Estimate(LimiterCommandVersion2 command)
  619. {
  620. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  621. if (!command.Parameter.StatisticsEnabled || !command.IsEffectEnabled)
  622. {
  623. return EstimateLimiterCommandCommon(command.Parameter, command.IsEffectEnabled);
  624. }
  625. if (_sampleCount == 160)
  626. {
  627. switch (command.Parameter.ChannelCount)
  628. {
  629. case 1:
  630. return (uint)23309.0f;
  631. case 2:
  632. return (uint)29954.0f;
  633. case 4:
  634. return (uint)35807.0f;
  635. case 6:
  636. return (uint)58340.0f;
  637. default:
  638. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  639. }
  640. }
  641. switch (command.Parameter.ChannelCount)
  642. {
  643. case 1:
  644. return (uint)33526.0f;
  645. case 2:
  646. return (uint)43549.0f;
  647. case 4:
  648. return (uint)52190.0f;
  649. case 6:
  650. return (uint)85527.0f;
  651. default:
  652. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  653. }
  654. }
  655. public virtual uint Estimate(GroupedBiquadFilterCommand command)
  656. {
  657. return 0;
  658. }
  659. public virtual uint Estimate(CaptureBufferCommand command)
  660. {
  661. return 0;
  662. }
  663. }
  664. }