BitStream.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "BitStream.h"
  7. namespace Video::VP9 {
  8. u8 BitStream::read_byte()
  9. {
  10. VERIFY(m_bytes_remaining >= 1);
  11. m_bytes_remaining--;
  12. return *(m_data_ptr++);
  13. }
  14. bool BitStream::read_bit()
  15. {
  16. if (!m_current_byte.has_value()) {
  17. m_current_byte = read_byte();
  18. m_current_bit_position = 7;
  19. }
  20. bool bit_value = m_current_byte.value() & (1u << m_current_bit_position);
  21. if (--m_current_bit_position < 0)
  22. m_current_byte.clear();
  23. return bit_value;
  24. }
  25. u8 BitStream::read_f(size_t n)
  26. {
  27. u8 result = 0;
  28. for (size_t i = 0; i < n; i++) {
  29. result = (2 * result) + read_bit();
  30. }
  31. return result;
  32. }
  33. u8 BitStream::read_f8()
  34. {
  35. if (!m_current_byte.has_value())
  36. return read_byte();
  37. auto high_bits = m_current_byte.value() & ((1u << m_current_bit_position) - 1);
  38. u8 remaining_bits = 7 - m_current_bit_position;
  39. m_current_byte = read_byte();
  40. m_current_bit_position = 7;
  41. auto low_bits = (m_current_byte.value() >> (8u - remaining_bits)) & ((1u << remaining_bits) - 1);
  42. m_current_bit_position -= remaining_bits;
  43. return (high_bits << remaining_bits) | low_bits;
  44. }
  45. u16 BitStream::read_f16()
  46. {
  47. return (read_f8() << 8u) | read_f8();
  48. }
  49. /* 9.2.1 */
  50. bool BitStream::init_bool(size_t bytes)
  51. {
  52. if (bytes < 1)
  53. return false;
  54. m_bool_value = read_f8();
  55. m_bool_range = 255;
  56. m_bool_max_bits = (8 * bytes) - 8;
  57. return !read_bool(128);
  58. }
  59. /* 9.2.2 */
  60. bool BitStream::read_bool(u8 probability)
  61. {
  62. auto split = 1u + (((m_bool_range - 1u) * probability) >> 8u);
  63. bool return_bool;
  64. if (m_bool_value < split) {
  65. m_bool_range = split;
  66. return_bool = false;
  67. } else {
  68. m_bool_range -= split;
  69. m_bool_value -= split;
  70. return_bool = true;
  71. }
  72. while (m_bool_range < 128) {
  73. bool new_bit;
  74. if (m_bool_max_bits) {
  75. new_bit = read_bit();
  76. m_bool_max_bits--;
  77. } else {
  78. new_bit = false;
  79. }
  80. m_bool_range *= 2;
  81. m_bool_value = (m_bool_value << 1u) + new_bit;
  82. }
  83. return return_bool;
  84. }
  85. /* 9.2.3 */
  86. bool BitStream::exit_bool()
  87. {
  88. // FIXME: I'm not sure if this call to min is spec compliant, or if there is an issue elsewhere earlier in the parser.
  89. auto padding_element = read_f(min(m_bool_max_bits, (u64)bits_remaining()));
  90. // FIXME: It is a requirement of bitstream conformance that enough padding bits are inserted to ensure that the final coded byte of a frame is not equal to a superframe marker.
  91. // A byte b is equal to a superframe marker if and only if (b & 0xe0)is equal to 0xc0, i.e. if the most significant 3 bits are equal to 0b110.
  92. return padding_element == 0;
  93. }
  94. u8 BitStream::read_literal(size_t n)
  95. {
  96. u8 return_value = 0;
  97. for (size_t i = 0; i < n; i++) {
  98. return_value = (2 * return_value) + read_bool(128);
  99. }
  100. return return_value;
  101. }
  102. i8 BitStream::read_s(size_t n)
  103. {
  104. auto value = read_f(n);
  105. auto sign = read_bit();
  106. return sign ? -value : value;
  107. }
  108. u64 BitStream::get_position()
  109. {
  110. return (m_bytes_read * 8) + (7 - m_current_bit_position);
  111. }
  112. size_t BitStream::bytes_remaining()
  113. {
  114. return m_bytes_remaining;
  115. }
  116. size_t BitStream::bits_remaining()
  117. {
  118. return (bytes_remaining() * 8) + m_current_bit_position + 1;
  119. }
  120. }