CommandProcessingTimeEstimatorVersion3.cs 24 KB

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