BitStream.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
  3. * Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Error.h>
  8. #include "BitStream.h"
  9. namespace Video::VP9 {
  10. ErrorOr<u8> BitStream::read_byte()
  11. {
  12. if (m_bytes_remaining < 1)
  13. return Error::from_string_literal("read_byte: Out of data.");
  14. VERIFY(m_bytes_remaining >= 1);
  15. m_bytes_remaining--;
  16. return *(m_data_ptr++);
  17. }
  18. ErrorOr<bool> BitStream::read_bit()
  19. {
  20. if (!m_current_byte.has_value()) {
  21. m_current_byte = TRY(read_byte());
  22. m_current_bit_position = 7;
  23. }
  24. bool bit_value = m_current_byte.value() & (1u << m_current_bit_position);
  25. if (--m_current_bit_position < 0)
  26. m_current_byte.clear();
  27. return bit_value;
  28. }
  29. ErrorOr<u8> BitStream::read_f(size_t n)
  30. {
  31. u8 result = 0;
  32. for (size_t i = 0; i < n; i++) {
  33. result = (2 * result) + TRY(read_bit());
  34. }
  35. return result;
  36. }
  37. ErrorOr<u8> BitStream::read_f8()
  38. {
  39. if (!m_current_byte.has_value())
  40. return read_byte();
  41. auto high_bits = m_current_byte.value() & ((1u << m_current_bit_position) - 1);
  42. u8 remaining_bits = 7 - m_current_bit_position;
  43. m_current_byte = TRY(read_byte());
  44. m_current_bit_position = 7;
  45. auto low_bits = (m_current_byte.value() >> (8u - remaining_bits)) & ((1u << remaining_bits) - 1);
  46. m_current_bit_position -= remaining_bits;
  47. return (high_bits << remaining_bits) | low_bits;
  48. }
  49. ErrorOr<u16> BitStream::read_f16()
  50. {
  51. return (TRY(read_f8()) << 8u) | TRY(read_f8());
  52. }
  53. /* 9.2.1 */
  54. ErrorOr<void> BitStream::init_bool(size_t bytes)
  55. {
  56. if (bytes == 0)
  57. return Error::from_string_literal("Range coder size cannot be zero.");
  58. m_bool_value = TRY(read_f8());
  59. m_bool_range = 255;
  60. m_bool_max_bits = (8 * bytes) - 8;
  61. if (TRY(read_bool(128)))
  62. return Error::from_string_literal("Range coder's first bool was non-zero.");
  63. return {};
  64. }
  65. /* 9.2.2 */
  66. ErrorOr<bool> BitStream::read_bool(u8 probability)
  67. {
  68. auto split = 1u + (((m_bool_range - 1u) * probability) >> 8u);
  69. bool return_bool;
  70. if (m_bool_value < split) {
  71. m_bool_range = split;
  72. return_bool = false;
  73. } else {
  74. m_bool_range -= split;
  75. m_bool_value -= split;
  76. return_bool = true;
  77. }
  78. while (m_bool_range < 128) {
  79. bool new_bit;
  80. if (m_bool_max_bits) {
  81. new_bit = TRY(read_bit());
  82. m_bool_max_bits--;
  83. } else {
  84. new_bit = false;
  85. }
  86. m_bool_range *= 2;
  87. m_bool_value = (m_bool_value << 1u) + new_bit;
  88. }
  89. return return_bool;
  90. }
  91. /* 9.2.3 */
  92. ErrorOr<void> BitStream::exit_bool()
  93. {
  94. // FIXME: I'm not sure if this call to min is spec compliant, or if there is an issue elsewhere earlier in the parser.
  95. auto padding_element = TRY(read_f(min(m_bool_max_bits, (u64)bits_remaining())));
  96. // 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.
  97. // 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.
  98. if (padding_element != 0)
  99. return Error::from_string_literal("Range coder padding was non-zero.");
  100. return {};
  101. }
  102. ErrorOr<u8> BitStream::read_literal(size_t n)
  103. {
  104. u8 return_value = 0;
  105. for (size_t i = 0; i < n; i++) {
  106. return_value = (2 * return_value) + TRY(read_bool(128));
  107. }
  108. return return_value;
  109. }
  110. ErrorOr<i8> BitStream::read_s(size_t n)
  111. {
  112. auto value = TRY(read_f(n));
  113. auto sign = TRY(read_bit());
  114. return sign ? -value : value;
  115. }
  116. u64 BitStream::get_position()
  117. {
  118. return (m_bytes_read * 8) + (7 - m_current_bit_position);
  119. }
  120. size_t BitStream::bytes_remaining()
  121. {
  122. return m_bytes_remaining;
  123. }
  124. size_t BitStream::bits_remaining()
  125. {
  126. return (bytes_remaining() * 8) + m_current_bit_position + 1;
  127. }
  128. }