h264_bit_reader.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2014 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/video/h264_bit_reader.h"
  5. #include "base/check.h"
  6. namespace media {
  7. H264BitReader::H264BitReader()
  8. : data_(nullptr),
  9. bytes_left_(0),
  10. curr_byte_(0),
  11. num_remaining_bits_in_curr_byte_(0),
  12. prev_two_bytes_(0),
  13. emulation_prevention_bytes_(0) {}
  14. H264BitReader::~H264BitReader() = default;
  15. bool H264BitReader::Initialize(const uint8_t* data, off_t size) {
  16. DCHECK(data);
  17. if (size < 1)
  18. return false;
  19. data_ = data;
  20. bytes_left_ = size;
  21. num_remaining_bits_in_curr_byte_ = 0;
  22. // Initially set to 0xffff to accept all initial two-byte sequences.
  23. prev_two_bytes_ = 0xffff;
  24. emulation_prevention_bytes_ = 0;
  25. return true;
  26. }
  27. bool H264BitReader::UpdateCurrByte() {
  28. if (bytes_left_ < 1)
  29. return false;
  30. // Emulation prevention three-byte detection.
  31. // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03).
  32. if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) {
  33. // Detected 0x000003, skip last byte.
  34. ++data_;
  35. --bytes_left_;
  36. ++emulation_prevention_bytes_;
  37. // Need another full three bytes before we can detect the sequence again.
  38. prev_two_bytes_ = 0xffff;
  39. if (bytes_left_ < 1)
  40. return false;
  41. }
  42. // Load a new byte and advance pointers.
  43. curr_byte_ = *data_++ & 0xff;
  44. --bytes_left_;
  45. num_remaining_bits_in_curr_byte_ = 8;
  46. prev_two_bytes_ = ((prev_two_bytes_ & 0xff) << 8) | curr_byte_;
  47. return true;
  48. }
  49. // Read |num_bits| (1 to 31 inclusive) from the stream and return them
  50. // in |out|, with first bit in the stream as MSB in |out| at position
  51. // (|num_bits| - 1).
  52. bool H264BitReader::ReadBits(int num_bits, int* out) {
  53. int bits_left = num_bits;
  54. *out = 0;
  55. DCHECK(num_bits <= 31);
  56. while (num_remaining_bits_in_curr_byte_ < bits_left) {
  57. // Take all that's left in current byte, shift to make space for the rest.
  58. *out |= (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_));
  59. bits_left -= num_remaining_bits_in_curr_byte_;
  60. if (!UpdateCurrByte())
  61. return false;
  62. }
  63. *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left));
  64. *out &= ((1u << num_bits) - 1u);
  65. num_remaining_bits_in_curr_byte_ -= bits_left;
  66. return true;
  67. }
  68. off_t H264BitReader::NumBitsLeft() {
  69. return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
  70. }
  71. bool H264BitReader::HasMoreRBSPData() {
  72. // Make sure we have more bits, if we are at 0 bits in current byte and
  73. // updating current byte fails, we don't have more data anyway.
  74. if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
  75. return false;
  76. // If there is no more RBSP data, then |curr_byte_| contains the stop bit and
  77. // zero padding. Check to see if there is other data instead.
  78. // (We don't actually check for the stop bit itself, instead treating the
  79. // invalid case of all trailing zeros identically).
  80. if ((curr_byte_ & ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0)
  81. return true;
  82. // While the spec disallows it (7.4.1: "The last byte of the NAL unit shall
  83. // not be equal to 0x00"), some streams have trailing null bytes anyway. We
  84. // don't handle emulation prevention sequences because HasMoreRBSPData() is
  85. // not used when parsing slices (where cabac_zero_word elements are legal).
  86. for (off_t i = 0; i < bytes_left_; i++) {
  87. if (data_[i] != 0)
  88. return true;
  89. }
  90. bytes_left_ = 0;
  91. return false;
  92. }
  93. size_t H264BitReader::NumEmulationPreventionBytesRead() {
  94. return emulation_prevention_bytes_;
  95. }
  96. } // namespace media