vp9_bool_decoder.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "media/filters/vp9_bool_decoder.h"
  5. #include <algorithm>
  6. #include <memory>
  7. #include "base/logging.h"
  8. #include "media/base/bit_reader.h"
  9. namespace media {
  10. namespace {
  11. // This is an optimization lookup table for the loop in spec 9.2.2.
  12. // while BoolRange <= 128:
  13. // read 1 bit
  14. // BoolRange *= 2
  15. // This table indicates how many iterations to run for a given BoolRange. So
  16. // the loop could be reduced to
  17. // read (kCountToShiftTo128[BoolRange]) bits
  18. const int kCountToShiftTo128[256] = {
  19. 0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3,
  20. 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  21. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1,
  22. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  23. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  24. 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  27. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  29. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  30. };
  31. } // namespace
  32. Vp9BoolDecoder::Vp9BoolDecoder() = default;
  33. Vp9BoolDecoder::~Vp9BoolDecoder() = default;
  34. // 9.2.1 Initialization process for Boolean decoder
  35. bool Vp9BoolDecoder::Initialize(const uint8_t* data, size_t size) {
  36. DCHECK(data);
  37. if (size < 1) {
  38. DVLOG(1) << "input size of bool decoder shall be at least 1";
  39. valid_ = false;
  40. return false;
  41. }
  42. reader_ = std::make_unique<BitReader>(data, size);
  43. valid_ = true;
  44. bool_value_ = 0;
  45. count_to_fill_ = 8;
  46. bool_range_ = 255;
  47. if (ReadLiteral(1) != 0) {
  48. DVLOG(1) << "marker bit should be 0";
  49. valid_ = false;
  50. return false;
  51. }
  52. return true;
  53. }
  54. // Fill at least |count_to_fill_| bits and prefill remain bits of |bool_value_|
  55. // if data is enough.
  56. bool Vp9BoolDecoder::Fill() {
  57. DCHECK_GE(count_to_fill_, 0);
  58. int bits_left = reader_->bits_available();
  59. if (bits_left < count_to_fill_) {
  60. valid_ = false;
  61. DVLOG(1) << "Vp9BoolDecoder reads beyond the end of stream";
  62. return false;
  63. }
  64. DCHECK_LE(count_to_fill_, kBoolSize);
  65. int max_bits_to_read = kBigBoolBitSize - kBoolSize + count_to_fill_;
  66. int bits_to_read = std::min(max_bits_to_read, bits_left);
  67. BigBool data;
  68. reader_->ReadBits(bits_to_read, &data);
  69. bool_value_ |= data << (max_bits_to_read - bits_to_read);
  70. count_to_fill_ -= bits_to_read;
  71. return true;
  72. }
  73. // 9.2.2 Boolean decoding process
  74. bool Vp9BoolDecoder::ReadBool(int prob) {
  75. DCHECK(reader_);
  76. if (count_to_fill_ > 0) {
  77. if (!Fill())
  78. return false;
  79. }
  80. unsigned int split = (bool_range_ * prob + (256 - prob)) >> kBoolSize;
  81. BigBool big_split = static_cast<BigBool>(split)
  82. << (kBigBoolBitSize - kBoolSize);
  83. bool bit;
  84. if (bool_value_ < big_split) {
  85. bool_range_ = split;
  86. bit = false;
  87. } else {
  88. bool_range_ -= split;
  89. bool_value_ -= big_split;
  90. bit = true;
  91. }
  92. // Need to fill |count| bits next time in order to make |bool_range_| >=
  93. // 128.
  94. DCHECK_LT(bool_range_, std::size(kCountToShiftTo128));
  95. DCHECK_GT(bool_range_, 0u);
  96. int count = kCountToShiftTo128[bool_range_];
  97. bool_range_ <<= count;
  98. bool_value_ <<= count;
  99. count_to_fill_ += count;
  100. return bit;
  101. }
  102. // 9.2.4 Parsing process for read_literal
  103. uint8_t Vp9BoolDecoder::ReadLiteral(int bits) {
  104. DCHECK_LT(static_cast<size_t>(bits), sizeof(uint8_t) * 8);
  105. DCHECK(reader_);
  106. uint8_t x = 0;
  107. for (int i = 0; i < bits; i++)
  108. x = 2 * x + ReadBool(128);
  109. return x;
  110. }
  111. bool Vp9BoolDecoder::ConsumePaddingBits() {
  112. DCHECK(reader_);
  113. if (count_to_fill_ > reader_->bits_available()) {
  114. // 9.2.2 Boolean decoding process
  115. // Although we actually don't used the value, spec says the bitstream
  116. // should have enough bits to fill bool range, this should never happen.
  117. DVLOG(2) << "not enough bits in bitstream to fill bool range";
  118. return false;
  119. }
  120. if (bool_value_ != 0) {
  121. DVLOG(1) << "prefilled padding bits are not zero";
  122. return false;
  123. }
  124. while (reader_->bits_available() > 0) {
  125. int data;
  126. int size_to_read =
  127. std::min(reader_->bits_available(), static_cast<int>(sizeof(data) * 8));
  128. reader_->ReadBits(size_to_read, &data);
  129. if (data != 0) {
  130. DVLOG(1) << "padding bits are not zero";
  131. return false;
  132. }
  133. }
  134. return true;
  135. }
  136. } // namespace media