BitRateWindow.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef BIT_RATE_WINDOW_H
  3. #define BIT_RATE_WINDOW_H
  4. #include "BitBufferCounter.h"
  5. #include "ml-private.h"
  6. namespace ml {
  7. class BitRateWindow {
  8. public:
  9. enum class State {
  10. NotFilled,
  11. BelowThreshold,
  12. AboveThreshold,
  13. Idle
  14. };
  15. using Edge = std::pair<State, State>;
  16. using Action = size_t (BitRateWindow::*)(State PrevState, bool NewBit);
  17. private:
  18. std::map<Edge, Action> EdgeActions = {
  19. // From == To
  20. {
  21. Edge(State::NotFilled, State::NotFilled),
  22. &BitRateWindow::onRoundtripNotFilled,
  23. },
  24. {
  25. Edge(State::BelowThreshold, State::BelowThreshold),
  26. &BitRateWindow::onRoundtripBelowThreshold,
  27. },
  28. {
  29. Edge(State::AboveThreshold, State::AboveThreshold),
  30. &BitRateWindow::onRoundtripAboveThreshold,
  31. },
  32. {
  33. Edge(State::Idle, State::Idle),
  34. &BitRateWindow::onRoundtripIdle,
  35. },
  36. // NotFilled => {BelowThreshold, AboveThreshold}
  37. {
  38. Edge(State::NotFilled, State::BelowThreshold),
  39. &BitRateWindow::onNotFilledToBelowThreshold
  40. },
  41. {
  42. Edge(State::NotFilled, State::AboveThreshold),
  43. &BitRateWindow::onNotFilledToAboveThreshold
  44. },
  45. // BelowThreshold => AboveThreshold
  46. {
  47. Edge(State::BelowThreshold, State::AboveThreshold),
  48. &BitRateWindow::onBelowToAboveThreshold
  49. },
  50. // AboveThreshold => Idle
  51. {
  52. Edge(State::AboveThreshold, State::Idle),
  53. &BitRateWindow::onAboveThresholdToIdle
  54. },
  55. // Idle => NotFilled
  56. {
  57. Edge(State::Idle, State::NotFilled),
  58. &BitRateWindow::onIdleToNotFilled
  59. },
  60. };
  61. public:
  62. BitRateWindow(size_t MinLength, size_t MaxLength, size_t IdleLength,
  63. size_t SetBitsThreshold) :
  64. MinLength(MinLength), MaxLength(MaxLength), IdleLength(IdleLength),
  65. SetBitsThreshold(SetBitsThreshold),
  66. CurrState(State::NotFilled), CurrLength(0), BBC(MinLength) {}
  67. std::pair<Edge, size_t> insert(bool Bit);
  68. void print(std::ostream &OS) const;
  69. private:
  70. size_t onRoundtripNotFilled(State PrevState, bool NewBit) {
  71. (void) PrevState, (void) NewBit;
  72. CurrLength += 1;
  73. return CurrLength;
  74. }
  75. size_t onRoundtripBelowThreshold(State PrevState, bool NewBit) {
  76. (void) PrevState, (void) NewBit;
  77. CurrLength = MinLength;
  78. return CurrLength;
  79. }
  80. size_t onRoundtripAboveThreshold(State PrevState, bool NewBit) {
  81. (void) PrevState, (void) NewBit;
  82. CurrLength += 1;
  83. return CurrLength;
  84. }
  85. size_t onRoundtripIdle(State PrevState, bool NewBit) {
  86. (void) PrevState, (void) NewBit;
  87. CurrLength += 1;
  88. return CurrLength;
  89. }
  90. size_t onNotFilledToBelowThreshold(State PrevState, bool NewBit) {
  91. (void) PrevState, (void) NewBit;
  92. CurrLength = MinLength;
  93. return CurrLength;
  94. }
  95. size_t onNotFilledToAboveThreshold(State PrevState, bool NewBit) {
  96. (void) PrevState, (void) NewBit;
  97. CurrLength += 1;
  98. return CurrLength;
  99. }
  100. size_t onBelowToAboveThreshold(State PrevState, bool NewBit) {
  101. (void) PrevState, (void) NewBit;
  102. CurrLength = MinLength;
  103. return CurrLength;
  104. }
  105. size_t onAboveThresholdToIdle(State PrevState, bool NewBit) {
  106. (void) PrevState, (void) NewBit;
  107. size_t PrevLength = CurrLength;
  108. CurrLength = 1;
  109. return PrevLength;
  110. }
  111. size_t onIdleToNotFilled(State PrevState, bool NewBit) {
  112. (void) PrevState, (void) NewBit;
  113. BBC = BitBufferCounter(MinLength);
  114. BBC.insert(NewBit);
  115. CurrLength = 1;
  116. return CurrLength;
  117. }
  118. private:
  119. size_t MinLength;
  120. size_t MaxLength;
  121. size_t IdleLength;
  122. size_t SetBitsThreshold;
  123. State CurrState;
  124. size_t CurrLength;
  125. BitBufferCounter BBC;
  126. };
  127. } // namespace ml
  128. inline std::ostream& operator<<(std::ostream &OS, const ml::BitRateWindow BRW) {
  129. BRW.print(OS);
  130. return OS;
  131. }
  132. #endif /* BIT_RATE_WINDOW_H */